From 9f96f1fbcb85bf6869dc0088b39423086c2ad2ad Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Fri, 2 Oct 2020 09:32:17 +0200 Subject: [PATCH] move molecule tests to hcloud --- .drone.jsonnet | 2 +- .drone.yml | 6 +-- molecule/centos7/create.yml | 71 ++++++++++++++++++----------------- molecule/centos7/destroy.yml | 29 +++++++------- molecule/centos7/molecule.yml | 5 +-- molecule/centos7/prepare.yml | 10 ++++- 6 files changed, 66 insertions(+), 57 deletions(-) diff --git a/.drone.jsonnet b/.drone.jsonnet index 224caab..7eb7c0b 100644 --- a/.drone.jsonnet +++ b/.drone.jsonnet @@ -38,7 +38,7 @@ local PipelineDeployment(scenario='centos7') = { name: 'ansible-molecule', image: 'thegeeklab/molecule:3', environment: { - DO_API_KEY: { from_secret: 'do_api_key' }, + HCLOUD_TOKEN: { from_secret: 'hcloud_token' }, }, commands: [ 'molecule test -s ' + scenario, diff --git a/.drone.yml b/.drone.yml index 4402abd..a1d8303 100644 --- a/.drone.yml +++ b/.drone.yml @@ -39,8 +39,8 @@ steps: commands: - molecule test -s centos7 environment: - DO_API_KEY: - from_secret: do_api_key + HCLOUD_TOKEN: + from_secret: hcloud_token trigger: ref: @@ -131,6 +131,6 @@ depends_on: --- kind: signature -hmac: 50705a4284001b47422f777c8431bcfa1c2d56ef120457beaf832dd916ebae37 +hmac: ce80c12ddf6ff3fc5a617186134a18dfc8dc7d6e9ce50b19925246700e4f4b28 ... diff --git a/molecule/centos7/create.yml b/molecule/centos7/create.yml index 8874298..6c1d373 100644 --- a/molecule/centos7/create.yml +++ b/molecule/centos7/create.yml @@ -1,69 +1,70 @@ --- - - name: Create hosts: localhost connection: local gather_facts: false no_log: "{{ molecule_no_log }}" vars: - ssh_user: root ssh_port: 22 - - keypair_name: molecule_key - keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key" + ssh_user: root + ssh_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key" tasks: - - name: Create local keypair + - name: Create SSH key user: name: "{{ lookup('env', 'USER') }}" generate_ssh_key: true - ssh_key_file: "{{ keypair_path }}" - register: local_keypair + ssh_key_file: "{{ ssh_path }}" + force: true + register: generated_ssh_key - - name: Create remote keypair - digital_ocean_sshkey: - name: "{{ keypair_name }}" - ssh_pub_key: "{{ local_keypair.ssh_public_key }}" + - name: Register the SSH key name + set_fact: + ssh_key_name: "molecule-generated-{{ 12345 | random | to_uuid }}" + + - 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 - register: remote_keypair - name: Create molecule instance(s) - digital_ocean_droplet: + hcloud_server: name: "{{ item.name }}" - unique_name: true - region: "{{ item.region_id }}" - image: "{{ item.image_id }}" - size: "{{ item.size_id }}" - ssh_keys: "{{ remote_keypair.data.ssh_key.id }}" - wait: true - wait_timeout: 300 + server_type: "{{ item.server_type }}" + ssh_keys: + - "{{ ssh_key_name }}" + volumes: "{{ item.volumes | default(omit) }}" + image: "{{ item.image }}" + location: "{{ item.location | default(omit) }}" + datacenter: "{{ item.datacenter | default(omit) }}" + user_data: "{{ item.user_data | default(omit) }}" + api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" state: present register: server - loop: "{{ molecule_yml.platforms }}" + with_items: "{{ molecule_yml.platforms }}" async: 7200 poll: 0 - name: Wait for instance(s) creation to complete async_status: jid: "{{ item.ansible_job_id }}" - register: digitalocean_jobs - until: digitalocean_jobs.finished + register: hetzner_jobs + until: hetzner_jobs.finished retries: 300 - loop: "{{ server.results }}" + with_items: "{{ server.results }}" # Mandatory configuration for Molecule to function. - name: Populate instance config dict set_fact: instance_conf_dict: { - 'instance': "{{ item.data.droplet.name }}", - 'address': "{{ item.data.ip_address }}", + 'instance': "{{ item.hcloud_server.name }}", + 'ssh_key_name': "{{ ssh_key_name }}", + 'address': "{{ item.hcloud_server.ipv4_address }}", 'user': "{{ ssh_user }}", 'port': "{{ ssh_port }}", - 'identity_file': "{{ keypair_path }}", - 'droplet_id': "{{ item.data.droplet.id }}", - 'ssh_key_id': "{{ remote_keypair.data.ssh_key.id }}", - } - loop: "{{ digitalocean_jobs.results }}" + 'identity_file': "{{ ssh_path }}", } + with_items: "{{ hetzner_jobs.results }}" register: instance_config_dict when: server.changed | bool @@ -84,6 +85,8 @@ host: "{{ item.address }}" search_regex: SSH delay: 10 - timeout: 320 - loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" + with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" + - name: Wait for VM to settle down + pause: + seconds: 30 diff --git a/molecule/centos7/destroy.yml b/molecule/centos7/destroy.yml index 74de1c5..ecf2ed3 100644 --- a/molecule/centos7/destroy.yml +++ b/molecule/centos7/destroy.yml @@ -1,13 +1,13 @@ --- - - name: Destroy hosts: localhost connection: local gather_facts: false no_log: "{{ molecule_no_log }}" tasks: - - block: - - name: Populate instance config + - name: Populate the instance config + block: + - name: Populate instance config from file set_fact: instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" skip_instances: false @@ -18,12 +18,12 @@ skip_instances: true - name: Destroy molecule instance(s) - digital_ocean_droplet: + hcloud_server: name: "{{ item.instance }}" - id: "{{ item.droplet_id }}" + api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" state: absent register: server - loop: "{{ instance_conf | flatten(levels=1) }}" + with_items: "{{ instance_conf }}" when: not skip_instances async: 7200 poll: 0 @@ -31,16 +31,18 @@ - name: Wait for instance(s) deletion to complete async_status: jid: "{{ item.ansible_job_id }}" - register: digitalocean_jobs - until: digitalocean_jobs.finished + register: hetzner_jobs + until: hetzner_jobs.finished retries: 300 - loop: "{{ server.results }}" + with_items: "{{ server.results }}" - - name: Delete remote keypair - digital_ocean_sshkey: - fingerprint: "{{ item.ssh_key_id }}" + - name: Remove registered SSH key + hcloud_ssh_key: + name: "{{ instance_conf[0].ssh_key_name }}" state: absent - loop: "{{ instance_conf | flatten(levels=1) }}" + when: + - not skip_instances + - (instance_conf | default([])) | length > 0 # must contain at least one instance # Mandatory configuration for Molecule to function. @@ -53,4 +55,3 @@ content: "{{ instance_conf | molecule_to_yaml | molecule_header }}" dest: "{{ molecule_instance_config }}" when: server.changed | bool - diff --git a/molecule/centos7/molecule.yml b/molecule/centos7/molecule.yml index e18a824..175a43e 100644 --- a/molecule/centos7/molecule.yml +++ b/molecule/centos7/molecule.yml @@ -8,9 +8,8 @@ driver: name: delegated platforms: - name: centos7-openvpn-client - region_id: fra1 - image_id: centos-7-x64 - size_id: s-1vcpu-1gb + image: centos-7 + server_type: cx11 lint: | /usr/local/bin/flake8 provisioner: diff --git a/molecule/centos7/prepare.yml b/molecule/centos7/prepare.yml index 559de83..183f4d3 100644 --- a/molecule/centos7/prepare.yml +++ b/molecule/centos7/prepare.yml @@ -3,7 +3,13 @@ hosts: all gather_facts: false tasks: - - name: Install python for Ansible - raw: test -e /usr/bin/python || (dnf -y install python3 && alternatives --set python /usr/bin/python3) + - name: Bootstrap python for Ansible + 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 changed_when: false