diff --git a/.gitignore b/.gitignore index 3838ca34e2160133b6c996de27eaa0bd2aa40b62..853699e135c0ec9cc4df009fc90768c0a0feef5a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .molecule +.vagrant .cache *.swp .DS_Store diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000000000000000000000000000000000000..3a2255e4656c4b4cf94ff9d60c2c4548246a2db0 --- /dev/null +++ b/.yamllint @@ -0,0 +1,13 @@ +extends: default + +rules: + braces: + max-spaces-inside: 1 + level: error + brackets: + max-spaces-inside: 1 + level: error + line-length: disable + # NOTE(retr0h): Templates no longer fail this lint rule. + # Uncomment if running old Molecule templates. + # truthy: disable diff --git a/LICENSE b/LICENSE index 93c8944179af2db6c8d240a940616be2fbb0442d..3cc600c2f63375e50e387685d20ad337cfb4b9c4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ BSD 2-Clause License -Copyright (c) 2017, European Spallation Source ERIC +Copyright (c) 2018, European Spallation Source ERIC All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/README.md b/README.md index 5268afbdfbf3a26cf36469eeaa18c00a15a7347d..f07633a1873962b9e0ac859d838c292e9b769935 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ Ansible role to install conda on CentOS. Requirements ------------ -- ansible >= 2.2 -- molecule >= 1.20 +- ansible >= 2.4 +- molecule >= 2.6 Role Variables -------------- diff --git a/meta/main.yml b/meta/main.yml index 593e07c3207b8bac9b5d5a4329795187cfde6d39..0f7c8f814c791d9ca2b9b77a89956b70ddfd55ed 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -3,12 +3,12 @@ galaxy_info: company: European Spallation Source ERIC description: Role to install conda. license: BSD - min_ansible_version: 2.2 + min_ansible_version: 2.4 platforms: - name: CentOS versions: - 7 dependencies: [] - # List your role dependencies here, one per line. - # Be sure to remove the '[]' above if you add dependencies - # to this list. +# List your role dependencies here, one per line. +# Be sure to remove the '[]' above if you add dependencies +# to this list. diff --git a/molecule.yml b/molecule.yml deleted file mode 100644 index 7b07929d896c0ddbf375711bdd91462e833cd53f..0000000000000000000000000000000000000000 --- a/molecule.yml +++ /dev/null @@ -1,32 +0,0 @@ ---- -ansible: - playbook: playbook.yml - group_vars: - default_group: - set_uid: - conda_uid: 48 - create_env: - conda_env_files: - - "{{ playbook_dir }}/tests/python27_env.yml" - - "{{ playbook_dir }}/tests/python36_env.yml" -driver: - name: docker -docker: - containers: - - name: ics-ans-role-conda-default - image: centos - image_version: 7 - ansible_groups: - - default_group - - name: ics-ans-role-conda-set-uid - image: centos - image_version: 7 - ansible_groups: - - set_uid - - name: ics-ans-role-conda-create-env - image: centos - image_version: 7 - ansible_groups: - - create_env -verifier: - name: testinfra diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 new file mode 100644 index 0000000000000000000000000000000000000000..f8b4e7530f589c80bab5139494c92f5b7e2a0b50 --- /dev/null +++ b/molecule/default/Dockerfile.j2 @@ -0,0 +1,9 @@ +# Molecule managed + +FROM {{ item.image }} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python2-dnf bash && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum update -y && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper update -y && zypper install -y python sudo bash python-xml && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; fi diff --git a/molecule/default/create.yml b/molecule/default/create.yml new file mode 100644 index 0000000000000000000000000000000000000000..a73a0210b49b97de2134c9f34d626ce18d7c8139 --- /dev/null +++ b/molecule/default/create.yml @@ -0,0 +1,59 @@ +--- +- name: Create + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" + vars: + molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}" + molecule_ephemeral_directory: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}" + molecule_scenario_directory: "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}" + molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}" + tasks: + - name: Create Dockerfiles from image names + template: + src: "{{ molecule_scenario_directory }}/Dockerfile.j2" + dest: "{{ molecule_ephemeral_directory }}/Dockerfile_{{ item.image | regex_replace('[^a-zA-Z0-9_]', '_') }}" + with_items: "{{ molecule_yml.platforms }}" + register: platforms + + - name: Discover local Docker images + docker_image_facts: + name: "molecule_local/{{ item.item.name }}" + with_items: "{{ platforms.results }}" + register: docker_images + + - name: Build an Ansible compatible image + docker_image: + path: "{{ molecule_ephemeral_directory }}" + name: "molecule_local/{{ item.item.image }}" + dockerfile: "{{ item.item.dockerfile | default(item.invocation.module_args.dest) }}" + force: "{{ item.item.force | default(true) }}" + with_items: "{{ platforms.results }}" + when: platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0 + + - name: Create molecule instance(s) + docker_container: + name: "{{ item.name }}" + hostname: "{{ item.name }}" + image: "molecule_local/{{ item.image }}" + state: started + recreate: false + log_driver: none + command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}" + privileged: "{{ item.privileged | default(omit) }}" + volumes: "{{ item.volumes | default(omit) }}" + capabilities: "{{ item.capabilities | default(omit) }}" + ports: "{{ item.exposed_ports | default(omit) }}" + 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: docker_jobs + until: docker_jobs.finished + retries: 300 + with_items: "{{ server.results }}" diff --git a/molecule/default/destroy.yml b/molecule/default/destroy.yml new file mode 100644 index 0000000000000000000000000000000000000000..3ce74788e111fe970c91fe14ccfaf20c8950dc0c --- /dev/null +++ b/molecule/default/destroy.yml @@ -0,0 +1,27 @@ +--- +- name: Destroy + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" + vars: + molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}" + molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}" + tasks: + - name: Destroy molecule instance(s) + docker_container: + name: "{{ item.name }}" + state: absent + force_kill: "{{ item.force_kill | default(true) }}" + register: server + with_items: "{{ molecule_yml.platforms }}" + async: 7200 + poll: 0 + + - name: Wait for instance(s) deletion to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: docker_jobs + until: docker_jobs.finished + retries: 300 + with_items: "{{ server.results }}" diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000000000000000000000000000000000000..6be4477aa79d416eae15495ef74ce69b0d940a8f --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,39 @@ +--- +dependency: + name: galaxy +lint: + name: yamllint +provisioner: + name: ansible + lint: + name: ansible-lint + inventory: + group_vars: + default_group: + set_uid: + conda_uid: 48 + create_env: + conda_env_files: + - "{{ playbook_dir }}/tests/python27_env.yml" + - "{{ playbook_dir }}/tests/python36_env.yml" +scenario: + name: default +verifier: + name: testinfra + lint: + name: flake8 +driver: + name: docker +platforms: + - name: ics-ans-role-conda-default + image: centos:7 + groups: + - default_group + - name: ics-ans-role-conda-set-uid + image: centos:7 + groups: + - set_uid + - name: ics-ans-role-conda-create-env + image: centos:7 + groups: + - create_env diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml new file mode 100644 index 0000000000000000000000000000000000000000..0eb2c7342fae2b84041491f396b73b32ebdd3e2f --- /dev/null +++ b/molecule/default/playbook.yml @@ -0,0 +1,6 @@ +--- +- name: Converge + hosts: all + become: yes + roles: + - role: ics-ans-role-conda diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml new file mode 100644 index 0000000000000000000000000000000000000000..5358b3bd8c20d3c0ab64e4affc81469426b9f92e --- /dev/null +++ b/molecule/default/prepare.yml @@ -0,0 +1,5 @@ +--- +- name: Prepare + hosts: all + gather_facts: false + tasks: [] diff --git a/tests/python27_env.yml b/molecule/default/tests/python27_env.yml similarity index 100% rename from tests/python27_env.yml rename to molecule/default/tests/python27_env.yml diff --git a/tests/python36_env.yml b/molecule/default/tests/python36_env.yml similarity index 100% rename from tests/python36_env.yml rename to molecule/default/tests/python36_env.yml diff --git a/tests/test_conda.py b/molecule/default/tests/test_conda.py similarity index 89% rename from tests/test_conda.py rename to molecule/default/tests/test_conda.py index e11a8017e514859e0abb3c0e6ae70c2cfcf041e7..67edc52a43e3c09b237eae245eaa7e36eaea40a2 100644 --- a/tests/test_conda.py +++ b/molecule/default/tests/test_conda.py @@ -1,7 +1,8 @@ +import os import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - '.molecule/ansible_inventory').get_hosts('all') + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') def test_conda_user(host): diff --git a/tests/test_conda_create_env.py b/molecule/default/tests/test_conda_create_env.py similarity index 88% rename from tests/test_conda_create_env.py rename to molecule/default/tests/test_conda_create_env.py index 4ef99843974f9ca5b54cc91c9a92a37ad90f5c20..b1f6260fc5e86ecdab043403e77fc6e7a2a175df 100644 --- a/tests/test_conda_create_env.py +++ b/molecule/default/tests/test_conda_create_env.py @@ -1,8 +1,9 @@ # Tests for the create_env group +import os import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - '.molecule/ansible_inventory').get_hosts('create_env') + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('create_env') def test_conda_env_python27(host): diff --git a/tests/test_conda_uid.py b/molecule/default/tests/test_conda_uid.py similarity index 82% rename from tests/test_conda_uid.py rename to molecule/default/tests/test_conda_uid.py index d15930999a439712d19bc0b037358219304536bd..f6c618509693161cb153a75d3b7b331b3fd85f79 100644 --- a/tests/test_conda_uid.py +++ b/molecule/default/tests/test_conda_uid.py @@ -1,8 +1,9 @@ # Tests for the set_uid group +import os import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - '.molecule/ansible_inventory').get_hosts('set_uid') + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('set_uid') def test_conda_uid(host):