move molecule tests to hcloud
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Robert Kaussow 2020-10-02 09:32:14 +02:00
parent 4b9b40ab14
commit f40c6162e7
No known key found for this signature in database
GPG Key ID: 65362AE74AF98B61
10 changed files with 132 additions and 178 deletions

View File

@ -38,7 +38,7 @@ local PipelineDeployment(scenario='centos7') = {
name: 'ansible-molecule', name: 'ansible-molecule',
image: 'thegeeklab/molecule:3', image: 'thegeeklab/molecule:3',
environment: { environment: {
DO_API_KEY: { from_secret: 'do_api_key' }, HCLOUD_TOKEN: { from_secret: 'hcloud_token' },
}, },
commands: [ commands: [
'molecule test -s ' + scenario, 'molecule test -s ' + scenario,

View File

@ -39,8 +39,8 @@ steps:
commands: commands:
- molecule test -s centos7 - molecule test -s centos7
environment: environment:
DO_API_KEY: HCLOUD_TOKEN:
from_secret: do_api_key from_secret: hcloud_token
trigger: trigger:
ref: ref:
@ -71,8 +71,8 @@ steps:
commands: commands:
- molecule test -s centos8 - molecule test -s centos8
environment: environment:
DO_API_KEY: HCLOUD_TOKEN:
from_secret: do_api_key from_secret: hcloud_token
trigger: trigger:
ref: ref:
@ -164,6 +164,6 @@ depends_on:
--- ---
kind: signature kind: signature
hmac: 3c69667a28873cb28184446425699fe135f3d40cc0ee98a1ca4e9d41759cf1a8 hmac: 30f711abbdc57b2ecb5d9ad286e6c665bd9f13fc6ed2671793ce82849a206156
... ...

View File

@ -5,83 +5,66 @@
gather_facts: false gather_facts: false
no_log: "{{ molecule_no_log }}" no_log: "{{ molecule_no_log }}"
vars: vars:
ssh_user: root
ssh_port: 22 ssh_port: 22
ssh_user: root
keypair_name: molecule_key ssh_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
tasks: tasks:
- name: Create local keypair - name: Create SSH key
user: user:
name: "{{ lookup('env', 'USER') }}" name: "{{ lookup('env', 'USER') }}"
generate_ssh_key: true generate_ssh_key: true
ssh_key_file: "{{ keypair_path }}" ssh_key_file: "{{ ssh_path }}"
register: local_keypair force: true
register: generated_ssh_key
- name: Create remote keypair - name: Register the SSH key name
digital_ocean_sshkey: set_fact:
name: "{{ keypair_name }}" ssh_key_name: "molecule-generated-{{ 12345 | random | to_uuid }}"
ssh_pub_key: "{{ local_keypair.ssh_public_key }}"
- name: Register SSH key for test instance(s)
hcloud_ssh_key:
name: "{{ ssh_key_name }}"
public_key: "{{ generated_ssh_key.ssh_public_key }}"
state: present state: present
register: remote_keypair
- name: Create molecule instance(s) - name: Create molecule instance(s)
digital_ocean_droplet: hcloud_server:
name: "{{ item.name }}" name: "{{ item.name }}"
unique_name: true server_type: "{{ item.server_type }}"
region: "{{ item.region_id }}" ssh_keys:
image: "{{ item.image_id }}" - "{{ ssh_key_name }}"
size: "{{ item.size_id }}" volumes: "{{ item.volumes | default(omit) }}"
ssh_keys: "{{ remote_keypair.data.ssh_key.id }}" image: "{{ item.image }}"
wait: true location: "{{ item.location | default(omit) }}"
wait_timeout: 300 datacenter: "{{ item.datacenter | default(omit) }}"
user_data: "{{ item.user_data | default(omit) }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: present state: present
register: server register: server
loop: "{{ molecule_yml.platforms }}" with_items: "{{ molecule_yml.platforms }}"
async: 7200 async: 7200
poll: 0 poll: 0
- name: Wait for instance(s) creation to complete - name: Wait for instance(s) creation to complete
async_status: async_status:
jid: "{{ item.ansible_job_id }}" jid: "{{ item.ansible_job_id }}"
register: digitalocean_jobs register: hetzner_jobs
until: digitalocean_jobs.finished until: hetzner_jobs.finished
retries: 300 retries: 300
loop: "{{ server.results }}" with_items: "{{ server.results }}"
- name: Create new block storage
digital_ocean_block_storage:
state: present
command: create
region: "{{ item.data.droplet.region.slug }}"
block_size: 2
volume_name: "{{ item.data.droplet.name }}-blk"
loop: "{{ digitalocean_jobs.results }}"
- name: Attach block storage to instance
digital_ocean_block_storage:
state: present
command: attach
volume_name: "{{ item.data.droplet.name }}-blk"
region: "{{ item.data.droplet.region.slug }}"
droplet_id: "{{ item.data.droplet.id }}"
loop: "{{ digitalocean_jobs.results }}"
# Mandatory configuration for Molecule to function. # Mandatory configuration for Molecule to function.
- name: Populate instance config dict - name: Populate instance config dict
set_fact: set_fact:
instance_conf_dict: { instance_conf_dict: {
'instance': "{{ item.data.droplet.name }}", 'instance': "{{ item.hcloud_server.name }}",
'address': "{{ item.data.ip_address }}", 'ssh_key_name': "{{ ssh_key_name }}",
'address': "{{ item.hcloud_server.ipv4_address }}",
'user': "{{ ssh_user }}", 'user': "{{ ssh_user }}",
'port': "{{ ssh_port }}", 'port': "{{ ssh_port }}",
'identity_file': "{{ keypair_path }}", 'identity_file': "{{ ssh_path }}", }
'droplet_id': "{{ item.data.droplet.id }}", with_items: "{{ hetzner_jobs.results }}"
'droplet_region': "{{ item.data.droplet.region.slug }}",
'ssh_key_id': "{{ remote_keypair.data.ssh_key.id }}",
}
loop: "{{ digitalocean_jobs.results }}"
register: instance_config_dict register: instance_config_dict
when: server.changed | bool when: server.changed | bool
@ -102,5 +85,8 @@
host: "{{ item.address }}" host: "{{ item.address }}"
search_regex: SSH search_regex: SSH
delay: 10 delay: 10
timeout: 320 with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
- name: Wait for VM to settle down
pause:
seconds: 30

View File

@ -5,8 +5,9 @@
gather_facts: false gather_facts: false
no_log: "{{ molecule_no_log }}" no_log: "{{ molecule_no_log }}"
tasks: tasks:
- block: - name: Populate the instance config
- name: Populate instance config block:
- name: Populate instance config from file
set_fact: set_fact:
instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
skip_instances: false skip_instances: false
@ -16,22 +17,13 @@
instance_conf: {} instance_conf: {}
skip_instances: true skip_instances: true
- name: Detach block storage from instance
digital_ocean_block_storage:
state: absent
command: attach
volume_name: "{{ item.instance }}-blk"
region: "{{ item.droplet_region }}"
droplet_id: "{{ item.droplet_id }}"
loop: "{{ instance_conf | flatten(levels=1) }}"
- name: Destroy molecule instance(s) - name: Destroy molecule instance(s)
digital_ocean_droplet: hcloud_server:
name: "{{ item.instance }}" name: "{{ item.instance }}"
id: "{{ item.droplet_id }}" api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: absent state: absent
register: server register: server
loop: "{{ instance_conf | flatten(levels=1) }}" with_items: "{{ instance_conf }}"
when: not skip_instances when: not skip_instances
async: 7200 async: 7200
poll: 0 poll: 0
@ -39,24 +31,18 @@
- name: Wait for instance(s) deletion to complete - name: Wait for instance(s) deletion to complete
async_status: async_status:
jid: "{{ item.ansible_job_id }}" jid: "{{ item.ansible_job_id }}"
register: digitalocean_jobs register: hetzner_jobs
until: digitalocean_jobs.finished until: hetzner_jobs.finished
retries: 300 retries: 300
loop: "{{ server.results }}" with_items: "{{ server.results }}"
- name: Delete block storage - name: Remove registered SSH key
digital_ocean_block_storage: hcloud_ssh_key:
name: "{{ instance_conf[0].ssh_key_name }}"
state: absent state: absent
command: create when:
region: "{{ item.droplet_region }}" - not skip_instances
volume_name: "{{ item.instance }}-blk" - (instance_conf | default([])) | length > 0 # must contain at least one instance
loop: "{{ instance_conf | flatten(levels=1) }}"
- name: Delete remote keypair
digital_ocean_sshkey:
fingerprint: "{{ item.ssh_key_id }}"
state: absent
loop: "{{ instance_conf | flatten(levels=1) }}"
# Mandatory configuration for Molecule to function. # Mandatory configuration for Molecule to function.

View File

@ -8,9 +8,8 @@ driver:
name: delegated name: delegated
platforms: platforms:
- name: centos7-lvm - name: centos7-lvm
region_id: fra1 image: centos-7
image_id: centos-7-x64 server_type: cx11
size_id: s-1vcpu-1gb
lint: | lint: |
/usr/local/bin/flake8 /usr/local/bin/flake8
provisioner: provisioner:

View File

@ -3,7 +3,13 @@
hosts: all hosts: all
gather_facts: false gather_facts: false
tasks: tasks:
- name: Install python for Ansible - name: Bootstrap python for Ansible
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal) raw: |
command -v python3 python || (
(test -e /usr/bin/dnf && sudo dnf install -y python3) ||
(test -e /usr/bin/apt && (apt -y update && apt install -y python-minimal)) ||
(test -e /usr/bin/yum && sudo yum -y -qq install python3) ||
echo "Warning: Python not boostrapped due to unknown platform."
)
become: true become: true
changed_when: false changed_when: false

View File

@ -5,83 +5,66 @@
gather_facts: false gather_facts: false
no_log: "{{ molecule_no_log }}" no_log: "{{ molecule_no_log }}"
vars: vars:
ssh_user: root
ssh_port: 22 ssh_port: 22
ssh_user: root
keypair_name: molecule_key ssh_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
tasks: tasks:
- name: Create local keypair - name: Create SSH key
user: user:
name: "{{ lookup('env', 'USER') }}" name: "{{ lookup('env', 'USER') }}"
generate_ssh_key: true generate_ssh_key: true
ssh_key_file: "{{ keypair_path }}" ssh_key_file: "{{ ssh_path }}"
register: local_keypair force: true
register: generated_ssh_key
- name: Create remote keypair - name: Register the SSH key name
digital_ocean_sshkey: set_fact:
name: "{{ keypair_name }}" ssh_key_name: "molecule-generated-{{ 12345 | random | to_uuid }}"
ssh_pub_key: "{{ local_keypair.ssh_public_key }}"
- name: Register SSH key for test instance(s)
hcloud_ssh_key:
name: "{{ ssh_key_name }}"
public_key: "{{ generated_ssh_key.ssh_public_key }}"
state: present state: present
register: remote_keypair
- name: Create molecule instance(s) - name: Create molecule instance(s)
digital_ocean_droplet: hcloud_server:
name: "{{ item.name }}" name: "{{ item.name }}"
unique_name: true server_type: "{{ item.server_type }}"
region: "{{ item.region_id }}" ssh_keys:
image: "{{ item.image_id }}" - "{{ ssh_key_name }}"
size: "{{ item.size_id }}" volumes: "{{ item.volumes | default(omit) }}"
ssh_keys: "{{ remote_keypair.data.ssh_key.id }}" image: "{{ item.image }}"
wait: true location: "{{ item.location | default(omit) }}"
wait_timeout: 300 datacenter: "{{ item.datacenter | default(omit) }}"
user_data: "{{ item.user_data | default(omit) }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: present state: present
register: server register: server
loop: "{{ molecule_yml.platforms }}" with_items: "{{ molecule_yml.platforms }}"
async: 7200 async: 7200
poll: 0 poll: 0
- name: Wait for instance(s) creation to complete - name: Wait for instance(s) creation to complete
async_status: async_status:
jid: "{{ item.ansible_job_id }}" jid: "{{ item.ansible_job_id }}"
register: digitalocean_jobs register: hetzner_jobs
until: digitalocean_jobs.finished until: hetzner_jobs.finished
retries: 300 retries: 300
loop: "{{ server.results }}" with_items: "{{ server.results }}"
- name: Create new block storage
digital_ocean_block_storage:
state: present
command: create
region: "{{ item.data.droplet.region.slug }}"
block_size: 2
volume_name: "{{ item.data.droplet.name }}-blk"
loop: "{{ digitalocean_jobs.results }}"
- name: Attach block storage to instance
digital_ocean_block_storage:
state: present
command: attach
volume_name: "{{ item.data.droplet.name }}-blk"
region: "{{ item.data.droplet.region.slug }}"
droplet_id: "{{ item.data.droplet.id }}"
loop: "{{ digitalocean_jobs.results }}"
# Mandatory configuration for Molecule to function. # Mandatory configuration for Molecule to function.
- name: Populate instance config dict - name: Populate instance config dict
set_fact: set_fact:
instance_conf_dict: { instance_conf_dict: {
'instance': "{{ item.data.droplet.name }}", 'instance': "{{ item.hcloud_server.name }}",
'address': "{{ item.data.ip_address }}", 'ssh_key_name': "{{ ssh_key_name }}",
'address': "{{ item.hcloud_server.ipv4_address }}",
'user': "{{ ssh_user }}", 'user': "{{ ssh_user }}",
'port': "{{ ssh_port }}", 'port': "{{ ssh_port }}",
'identity_file': "{{ keypair_path }}", 'identity_file': "{{ ssh_path }}", }
'droplet_id': "{{ item.data.droplet.id }}", with_items: "{{ hetzner_jobs.results }}"
'droplet_region': "{{ item.data.droplet.region.slug }}",
'ssh_key_id': "{{ remote_keypair.data.ssh_key.id }}",
}
loop: "{{ digitalocean_jobs.results }}"
register: instance_config_dict register: instance_config_dict
when: server.changed | bool when: server.changed | bool
@ -102,5 +85,8 @@
host: "{{ item.address }}" host: "{{ item.address }}"
search_regex: SSH search_regex: SSH
delay: 10 delay: 10
timeout: 320 with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
- name: Wait for VM to settle down
pause:
seconds: 30

View File

@ -5,8 +5,9 @@
gather_facts: false gather_facts: false
no_log: "{{ molecule_no_log }}" no_log: "{{ molecule_no_log }}"
tasks: tasks:
- block: - name: Populate the instance config
- name: Populate instance config block:
- name: Populate instance config from file
set_fact: set_fact:
instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
skip_instances: false skip_instances: false
@ -16,22 +17,13 @@
instance_conf: {} instance_conf: {}
skip_instances: true skip_instances: true
- name: Detach block storage from instance
digital_ocean_block_storage:
state: absent
command: attach
volume_name: "{{ item.instance }}-blk"
region: "{{ item.droplet_region }}"
droplet_id: "{{ item.droplet_id }}"
loop: "{{ instance_conf | flatten(levels=1) }}"
- name: Destroy molecule instance(s) - name: Destroy molecule instance(s)
digital_ocean_droplet: hcloud_server:
name: "{{ item.instance }}" name: "{{ item.instance }}"
id: "{{ item.droplet_id }}" api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: absent state: absent
register: server register: server
loop: "{{ instance_conf | flatten(levels=1) }}" with_items: "{{ instance_conf }}"
when: not skip_instances when: not skip_instances
async: 7200 async: 7200
poll: 0 poll: 0
@ -39,24 +31,18 @@
- name: Wait for instance(s) deletion to complete - name: Wait for instance(s) deletion to complete
async_status: async_status:
jid: "{{ item.ansible_job_id }}" jid: "{{ item.ansible_job_id }}"
register: digitalocean_jobs register: hetzner_jobs
until: digitalocean_jobs.finished until: hetzner_jobs.finished
retries: 300 retries: 300
loop: "{{ server.results }}" with_items: "{{ server.results }}"
- name: Delete block storage - name: Remove registered SSH key
digital_ocean_block_storage: hcloud_ssh_key:
name: "{{ instance_conf[0].ssh_key_name }}"
state: absent state: absent
command: create when:
region: "{{ item.droplet_region }}" - not skip_instances
volume_name: "{{ item.instance }}-blk" - (instance_conf | default([])) | length > 0 # must contain at least one instance
loop: "{{ instance_conf | flatten(levels=1) }}"
- name: Delete remote keypair
digital_ocean_sshkey:
fingerprint: "{{ item.ssh_key_id }}"
state: absent
loop: "{{ instance_conf | flatten(levels=1) }}"
# Mandatory configuration for Molecule to function. # Mandatory configuration for Molecule to function.

View File

@ -8,9 +8,8 @@ driver:
name: delegated name: delegated
platforms: platforms:
- name: centos8-lvm - name: centos8-lvm
region_id: fra1 image: centos-8
image_id: centos-8-x64 server_type: cx11
size_id: s-1vcpu-1gb
lint: | lint: |
/usr/local/bin/flake8 /usr/local/bin/flake8
provisioner: provisioner:

View File

@ -3,7 +3,13 @@
hosts: all hosts: all
gather_facts: false gather_facts: false
tasks: tasks:
- name: Install python for Ansible - name: Bootstrap python for Ansible
raw: test -e /usr/bin/python || (dnf -y install python3 && alternatives --set python /usr/bin/python3) raw: |
command -v python3 python || (
(test -e /usr/bin/dnf && sudo dnf install -y python3) ||
(test -e /usr/bin/apt && (apt -y update && apt install -y python-minimal)) ||
(test -e /usr/bin/yum && sudo yum -y -qq install python3) ||
echo "Warning: Python not boostrapped due to unknown platform."
)
become: true become: true
changed_when: false changed_when: false