From 7b145432c6b1390bd8e9e88984d3334d59f1cfea Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Mon, 23 Sep 2019 12:49:21 +0200 Subject: [PATCH] move molecule tests to digital ocean --- .drone.jsonnet | 8 +- .drone.yml | 13 +- .gitignore | 2 + .../tests/test_default.py | 0 molecule/default/INSTALL.rst | 22 ---- molecule/default/create.yml | 104 +++++---------- molecule/default/destroy.yml | 23 ++-- molecule/default/molecule.yml | 14 +- molecule/default/playbook.yml | 2 +- molecule/default/prepare.yml | 2 +- .../default/requirements.yml | 0 molecule/default/tests/test_default.pyc | Bin 1025 -> 0 bytes molecule/ec2-centos-7/INSTALL.rst | 22 ---- molecule/ec2-centos-7/create.yml | 124 ------------------ molecule/ec2-centos-7/destroy.yml | 47 ------- molecule/ec2-centos-7/molecule.yml | 22 ---- molecule/ec2-centos-7/playbook.yml | 10 -- molecule/ec2-centos-7/prepare.yml | 9 -- molecule/ec2-centos-7/tests/test_default.pyc | Bin 1025 -> 0 bytes 19 files changed, 68 insertions(+), 356 deletions(-) rename {molecule/ec2-centos-7 => defaults}/tests/test_default.py (100%) delete mode 100644 molecule/default/INSTALL.rst rename requirements.yml => molecule/default/requirements.yml (100%) delete mode 100644 molecule/default/tests/test_default.pyc delete mode 100644 molecule/ec2-centos-7/INSTALL.rst delete mode 100644 molecule/ec2-centos-7/create.yml delete mode 100644 molecule/ec2-centos-7/destroy.yml delete mode 100644 molecule/ec2-centos-7/molecule.yml delete mode 100644 molecule/ec2-centos-7/playbook.yml delete mode 100644 molecule/ec2-centos-7/prepare.yml delete mode 100644 molecule/ec2-centos-7/tests/test_default.pyc diff --git a/.drone.jsonnet b/.drone.jsonnet index 92508bf..501c4ac 100644 --- a/.drone.jsonnet +++ b/.drone.jsonnet @@ -45,13 +45,11 @@ local PipelineDeployment = { steps: [ { name: "molecule", - image: "xoxys/molecule:ec2-linux-amd64", + image: "xoxys/molecule:do-linux-amd64", pull: "always", environment: { - ANSIBLE_ROLES_PATH: "/drone/src", - AWS_ACCESS_KEY_ID: { "from_secret": "aws_access_key_id" }, - AWS_SECRET_ACCESS_KEY: { "from_secret": "aws_secret_access_key" }, - AWS_REGION: "eu-central-1", + DO_API_KEY: { "from_secret": "do_api_key" }, + USER: "root", MOLECULE_CUSTOM_MODULES_REPO: "https://gitea.rknet.org/ansible/custom_modules", MOLECULE_CUSTOM_FILTERS_REPO: "https://gitea.rknet.org/ansible/custom_filters", PY_COLORS: 1 diff --git a/.drone.yml b/.drone.yml index 4b4f494..d9b0973 100644 --- a/.drone.yml +++ b/.drone.yml @@ -52,20 +52,17 @@ workspace: steps: - name: molecule pull: always - image: xoxys/molecule:ec2-linux-amd64 + image: xoxys/molecule:do-linux-amd64 commands: - /bin/bash /docker-entrypoint.sh - molecule test --scenario-name ec2-centos-7 environment: - ANSIBLE_ROLES_PATH: /drone/src - AWS_ACCESS_KEY_ID: - from_secret: aws_access_key_id - AWS_REGION: eu-central-1 - AWS_SECRET_ACCESS_KEY: - from_secret: aws_secret_access_key + DO_API_KEY: + from_secret: do_api_key MOLECULE_CUSTOM_FILTERS_REPO: https://gitea.rknet.org/ansible/custom_filters MOLECULE_CUSTOM_MODULES_REPO: https://gitea.rknet.org/ansible/custom_modules PY_COLORS: 1 + USER: root trigger: ref: @@ -111,6 +108,6 @@ depends_on: --- kind: signature -hmac: 173cf4ff8ceaa9f6a3e74e305f091796275df73905000ccfe7d454127294e0da +hmac: 82cbe7cd6eb905d3e2b355022e38cb17364540df9910c8ef3239b7e1a3c3f185 ... diff --git a/.gitignore b/.gitignore index 5becda8..20fb979 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ __pycache__/ *.py[cod] *$py.class +# --> Molecule +.yamllint diff --git a/molecule/ec2-centos-7/tests/test_default.py b/defaults/tests/test_default.py similarity index 100% rename from molecule/ec2-centos-7/tests/test_default.py rename to defaults/tests/test_default.py diff --git a/molecule/default/INSTALL.rst b/molecule/default/INSTALL.rst deleted file mode 100644 index f305f0b..0000000 --- a/molecule/default/INSTALL.rst +++ /dev/null @@ -1,22 +0,0 @@ -******* -Amazon Web Services driver installation guide -******* - -Requirements -============ - -* An AWS credentials rc file - -Install -======= - -Please refer to the `Virtual environment`_ documentation for installation best -practices. If not using a virtual environment, please consider passing the -widely recommended `'--user' flag`_ when invoking ``pip``. - -.. _Virtual environment: https://virtualenv.pypa.io/en/latest/ -.. _'--user' flag: https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site - -.. code-block:: bash - - $ pip install 'molecule[ec2]' diff --git a/molecule/default/create.yml b/molecule/default/create.yml index 4ed1a0c..41f112d 100644 --- a/molecule/default/create.yml +++ b/molecule/default/create.yml @@ -3,100 +3,66 @@ hosts: localhost connection: local gather_facts: false - no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}" + no_log: "{{ molecule_no_log }}" vars: - ssh_user: ubuntu + ssh_user: root ssh_port: 22 - security_group_name: molecule - security_group_description: Security group for testing Molecule - security_group_rules: - - proto: tcp - from_port: "{{ ssh_port }}" - to_port: "{{ ssh_port }}" - cidr_ip: '0.0.0.0/0' - - proto: icmp - from_port: 8 - to_port: -1 - cidr_ip: '0.0.0.0/0' - security_group_rules_egress: - - proto: -1 - from_port: 0 - to_port: 0 - cidr_ip: '0.0.0.0/0' - keypair_name: molecule_key keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key" tasks: - - name: Create security group - ec2_group: - name: "{{ security_group_name }}" - description: "{{ security_group_name }}" - rules: "{{ security_group_rules }}" - rules_egress: "{{ security_group_rules_egress }}" + - name: Create local keypair + user: + name: "{{ lookup('env', 'USER') }}" + generate_ssh_key: true + ssh_key_file: "{{ keypair_path }}" + register: local_keypair - - name: Test for presence of local keypair - stat: - path: "{{ keypair_path }}" - register: keypair_local - - - name: Delete remote keypair - ec2_key: + - name: Create remote keypair + digital_ocean_sshkey: name: "{{ keypair_name }}" - state: absent - when: not keypair_local.stat.exists - - - name: Create keypair - ec2_key: - name: "{{ keypair_name }}" - register: keypair - - - name: Persist the keypair - copy: - dest: "{{ keypair_path }}" - content: "{{ keypair.key.private_key }}" - mode: 0600 - when: keypair.changed + ssh_pub_key: "{{ local_keypair.ssh_public_key }}" + state: present + register: remote_keypair - name: Create molecule instance(s) - ec2: - key_name: "{{ keypair_name }}" - image: "{{ item.image }}" - instance_type: "{{ item.instance_type }}" - vpc_subnet_id: "{{ item.vpc_subnet_id }}" - group: "{{ security_group_name }}" - instance_tags: - instance: "{{ item.name }}" + digital_ocean_droplet: + 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 - assign_public_ip: true - exact_count: 1 - count_tag: - instance: "{{ item.name }}" + wait_timeout: 300 + state: present register: server - with_items: "{{ molecule_yml.platforms }}" + loop: "{{ molecule_yml.platforms }}" async: 7200 poll: 0 - name: Wait for instance(s) creation to complete async_status: jid: "{{ item.ansible_job_id }}" - register: ec2_jobs - until: ec2_jobs.finished + register: digitalocean_jobs + until: digitalocean_jobs.finished retries: 300 - with_items: "{{ server.results }}" + loop: "{{ server.results }}" # Mandatory configuration for Molecule to function. - name: Populate instance config dict set_fact: instance_conf_dict: { - 'instance': "{{ item.instances[0].tags.instance }}", - 'address': "{{ item.instances[0].public_ip }}", + 'instance': "{{ item.data.droplet.name }}", + 'address': "{{ item.data.ip_address }}", 'user': "{{ ssh_user }}", 'port': "{{ ssh_port }}", 'identity_file': "{{ keypair_path }}", - 'instance_ids': "{{ item.instance_ids }}", } - with_items: "{{ ec2_jobs.results }}" + 'droplet_id': "{{ item.data.droplet.id }}", + 'ssh_key_id': "{{ remote_keypair.data.ssh_key.id }}", + } + loop: "{{ digitalocean_jobs.results }}" register: instance_config_dict when: server.changed | bool @@ -118,8 +84,4 @@ search_regex: SSH delay: 10 timeout: 320 - with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" - - - name: Wait for boot process to finish - pause: - minutes: 2 + loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" diff --git a/molecule/default/destroy.yml b/molecule/default/destroy.yml index b460c1e..19c8c93 100644 --- a/molecule/default/destroy.yml +++ b/molecule/default/destroy.yml @@ -3,7 +3,7 @@ hosts: localhost connection: local gather_facts: false - no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}" + no_log: "{{ molecule_no_log }}" tasks: - block: - name: Populate instance config @@ -17,11 +17,12 @@ skip_instances: true - name: Destroy molecule instance(s) - ec2: + digital_ocean_droplet: + name: "{{ item.instance }}" + id: "{{ item.droplet_id }}" state: absent - instance_ids: "{{ item.instance_ids }}" register: server - with_items: "{{ instance_conf }}" + loop: "{{ instance_conf | flatten(levels=1) }}" when: not skip_instances async: 7200 poll: 0 @@ -29,10 +30,16 @@ - name: Wait for instance(s) deletion to complete async_status: jid: "{{ item.ansible_job_id }}" - register: ec2_jobs - until: ec2_jobs.finished + register: digitalocean_jobs + until: digitalocean_jobs.finished retries: 300 - with_items: "{{ server.results }}" + loop: "{{ server.results }}" + + - 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. @@ -42,6 +49,6 @@ - name: Dump instance config copy: - content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + content: "{{ instance_conf | molecule_to_yaml | molecule_header }}" dest: "{{ molecule_instance_config }}" when: server.changed | bool diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 6f88100..464fae3 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -2,18 +2,20 @@ dependency: name: galaxy driver: - name: ec2 + name: digitalocean +platforms: + - name: centos7-homeassistant + region_id: fra1 + image_id: centos-7-x64 + size_id: s-1vcpu-1gb lint: name: yamllint -platforms: - - name: instance - image: ami-a5b196c0 - instance_type: t2.micro - vpc_subnet_id: subnet-6456fd1f + enabled: False provisioner: name: ansible lint: name: ansible-lint + enabled: False verifier: name: testinfra lint: diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml index dba2457..61256c9 100644 --- a/molecule/default/playbook.yml +++ b/molecule/default/playbook.yml @@ -1,5 +1,5 @@ ---- - name: Converge hosts: all roles: + - role: xoxys.python3 - role: xoxys.homeassistant diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index ddb01fb..4b18d48 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -4,6 +4,6 @@ gather_facts: false tasks: - name: Install python for Ansible - raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal python-zipstream) + raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal) become: true changed_when: false diff --git a/requirements.yml b/molecule/default/requirements.yml similarity index 100% rename from requirements.yml rename to molecule/default/requirements.yml diff --git a/molecule/default/tests/test_default.pyc b/molecule/default/tests/test_default.pyc deleted file mode 100644 index 04fa67aba20115b0eac460c4eb61c5cebcb74939..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1025 zcmdr~%}(4f5FRJXub_LX5Est5usKxR5Gq1JrQI&l@*{OsDM}NEI(Y5Ko=_<4rR~f0 z3Hl&?fHHPAKcU;x|96+n8W-EX?;zveM_dGhk*o`JopSI0q_8- z5UL(j3%~)YMJNK;{LY|Sf+7TRCd*Lt;0K*taTF)f0?~;-bd7L>-1o=B(mn2I(KS;>WWXEyle`$GbzF;jTPTav@%Fnf_*KhzswK}G$se6u>q|ix zA~&(kxyp>t@fA0s6oeqRC{$^(`1bZ`=%8at3t@PU+T6@$&tp2eRAp2C@8>SbUlcB3 zTJSP!Bo68u>{B-$^Ae^`uoZ&rG8tg~pwC{i$mvXD0w6uMoZnSFXi!Ky$dnbIBu`DF z6e;Nl9zwjXKFq8CEOebh_uJFs_V5xRTwMvHl_$F3n6~5XO7(iR*w|I}IGg`*KyDDb UQ(e$swda0CKbf_FZnhcT1Hw2ag#Z8m diff --git a/molecule/ec2-centos-7/INSTALL.rst b/molecule/ec2-centos-7/INSTALL.rst deleted file mode 100644 index f305f0b..0000000 --- a/molecule/ec2-centos-7/INSTALL.rst +++ /dev/null @@ -1,22 +0,0 @@ -******* -Amazon Web Services driver installation guide -******* - -Requirements -============ - -* An AWS credentials rc file - -Install -======= - -Please refer to the `Virtual environment`_ documentation for installation best -practices. If not using a virtual environment, please consider passing the -widely recommended `'--user' flag`_ when invoking ``pip``. - -.. _Virtual environment: https://virtualenv.pypa.io/en/latest/ -.. _'--user' flag: https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site - -.. code-block:: bash - - $ pip install 'molecule[ec2]' diff --git a/molecule/ec2-centos-7/create.yml b/molecule/ec2-centos-7/create.yml deleted file mode 100644 index bfdbf79..0000000 --- a/molecule/ec2-centos-7/create.yml +++ /dev/null @@ -1,124 +0,0 @@ ---- -- name: Create - hosts: localhost - connection: local - gather_facts: false - no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}" - vars: - ssh_user: centos - ssh_port: 22 - - security_group_name: molecule - security_group_description: Security group for testing Molecule - security_group_rules: - - proto: tcp - from_port: "{{ ssh_port }}" - to_port: "{{ ssh_port }}" - cidr_ip: '0.0.0.0/0' - - proto: icmp - from_port: 8 - to_port: -1 - cidr_ip: '0.0.0.0/0' - security_group_rules_egress: - - proto: -1 - from_port: 0 - to_port: 0 - cidr_ip: '0.0.0.0/0' - - keypair_name: molecule_key_hassio - keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key" - tasks: - - name: Create security group - ec2_group: - name: "{{ security_group_name }}" - description: "{{ security_group_name }}" - rules: "{{ security_group_rules }}" - rules_egress: "{{ security_group_rules_egress }}" - - - name: Delete remote keypair - ec2_key: - name: "{{ keypair_name }}" - state: absent - - - name: Create keypair - ec2_key: - name: "{{ keypair_name }}" - register: keypair - - - name: Persist the keypair - copy: - dest: "{{ keypair_path }}" - content: "{{ keypair.key.private_key }}" - mode: 0600 - when: keypair.changed - - - name: Create molecule instance(s) - ec2: - key_name: "{{ keypair_name }}" - image: "{{ item.image }}" - instance_type: "{{ item.instance_type }}" - vpc_subnet_id: "{{ item.vpc_subnet_id }}" - group: "{{ security_group_name }}" - instance_tags: - instance: "{{ item.name }}" - wait: true - assign_public_ip: true - exact_count: 1 - count_tag: - instance: "{{ item.name }}" - volumes: - - device_name: /dev/sda1 - volume_type: gp2 - volume_size: 8 - delete_on_termination: yes - register: server - 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: ec2_jobs - until: ec2_jobs.finished - retries: 300 - with_items: "{{ server.results }}" - - # Mandatory configuration for Molecule to function. - - - name: Populate instance config dict - set_fact: - instance_conf_dict: { - 'instance': "{{ item.instances[0].tags.instance }}", - 'address': "{{ item.instances[0].public_ip }}", - 'user': "{{ ssh_user }}", - 'port': "{{ ssh_port }}", - 'identity_file': "{{ keypair_path }}", - 'instance_ids': "{{ item.instance_ids }}", } - with_items: "{{ ec2_jobs.results }}" - register: instance_config_dict - when: server.changed | bool - - - name: Convert instance config dict to a list - set_fact: - instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" - when: server.changed | bool - - - name: Dump instance config - copy: - content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" - dest: "{{ molecule_instance_config }}" - when: server.changed | bool - - - name: Wait for SSH - wait_for: - port: "{{ ssh_port }}" - host: "{{ item.address }}" - search_regex: SSH - delay: 10 - timeout: 320 - with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" - - - name: Wait for boot process to finish - pause: - minutes: 2 diff --git a/molecule/ec2-centos-7/destroy.yml b/molecule/ec2-centos-7/destroy.yml deleted file mode 100644 index b460c1e..0000000 --- a/molecule/ec2-centos-7/destroy.yml +++ /dev/null @@ -1,47 +0,0 @@ ---- -- name: Destroy - hosts: localhost - connection: local - gather_facts: false - no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}" - tasks: - - block: - - name: Populate instance config - set_fact: - instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" - skip_instances: false - rescue: - - name: Populate instance config when file missing - set_fact: - instance_conf: {} - skip_instances: true - - - name: Destroy molecule instance(s) - ec2: - state: absent - instance_ids: "{{ item.instance_ids }}" - register: server - with_items: "{{ instance_conf }}" - when: not skip_instances - async: 7200 - poll: 0 - - - name: Wait for instance(s) deletion to complete - async_status: - jid: "{{ item.ansible_job_id }}" - register: ec2_jobs - until: ec2_jobs.finished - retries: 300 - with_items: "{{ server.results }}" - - # Mandatory configuration for Molecule to function. - - - name: Populate instance config - set_fact: - instance_conf: {} - - - name: Dump instance config - copy: - content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" - dest: "{{ molecule_instance_config }}" - when: server.changed | bool diff --git a/molecule/ec2-centos-7/molecule.yml b/molecule/ec2-centos-7/molecule.yml deleted file mode 100644 index a825859..0000000 --- a/molecule/ec2-centos-7/molecule.yml +++ /dev/null @@ -1,22 +0,0 @@ ---- -dependency: - name: galaxy -driver: - name: ec2 -platforms: - - name: centos-7-homeassistant - image: ami-04cf43aca3e6f3de3 - instance_type: t2.micro - vpc_subnet_id: subnet-9b6896f1 -lint: - name: yamllint - enabled: False -provisioner: - name: ansible - lint: - name: ansible-lint - enabled: False -verifier: - name: testinfra - lint: - name: flake8 diff --git a/molecule/ec2-centos-7/playbook.yml b/molecule/ec2-centos-7/playbook.yml deleted file mode 100644 index 7236213..0000000 --- a/molecule/ec2-centos-7/playbook.yml +++ /dev/null @@ -1,10 +0,0 @@ ---- -- name: Converge - hosts: all - vars: - python3_packages_extra: - - epel-release - - roles: - - role: xoxys.python3 - - role: xoxys.homeassistant diff --git a/molecule/ec2-centos-7/prepare.yml b/molecule/ec2-centos-7/prepare.yml deleted file mode 100644 index ddb01fb..0000000 --- a/molecule/ec2-centos-7/prepare.yml +++ /dev/null @@ -1,9 +0,0 @@ ---- -- name: Prepare - hosts: all - gather_facts: false - tasks: - - name: Install python for Ansible - raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal python-zipstream) - become: true - changed_when: false diff --git a/molecule/ec2-centos-7/tests/test_default.pyc b/molecule/ec2-centos-7/tests/test_default.pyc deleted file mode 100644 index 04fa67aba20115b0eac460c4eb61c5cebcb74939..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1025 zcmdr~%}(4f5FRJXub_LX5Est5usKxR5Gq1JrQI&l@*{OsDM}NEI(Y5Ko=_<4rR~f0 z3Hl&?fHHPAKcU;x|96+n8W-EX?;zveM_dGhk*o`JopSI0q_8- z5UL(j3%~)YMJNK;{LY|Sf+7TRCd*Lt;0K*taTF)f0?~;-bd7L>-1o=B(mn2I(KS;>WWXEyle`$GbzF;jTPTav@%Fnf_*KhzswK}G$se6u>q|ix zA~&(kxyp>t@fA0s6oeqRC{$^(`1bZ`=%8at3t@PU+T6@$&tp2eRAp2C@8>SbUlcB3 zTJSP!Bo68u>{B-$^Ae^`uoZ&rG8tg~pwC{i$mvXD0w6uMoZnSFXi!Ky$dnbIBu`DF z6e;Nl9zwjXKFq8CEOebh_uJFs_V5xRTwMvHl_$F3n6~5XO7(iR*w|I}IGg`*KyDDb UQ(e$swda0CKbf_FZnhcT1Hw2ag#Z8m