Skip to content
This repository has been archived by the owner on Oct 15, 2018. It is now read-only.

Commit

Permalink
Merge branch 'molecule'
Browse files Browse the repository at this point in the history
  • Loading branch information
mylokin committed Feb 15, 2018
2 parents 64de4f0 + d8eae04 commit d5ed162
Show file tree
Hide file tree
Showing 18 changed files with 872 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
molecule/default/.molecule/
molecule/default/.cache/
*.pyc
__pycache__
11 changes: 11 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extends: default

rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
line-length: disable
truthy: disable
32 changes: 32 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"


[packages]

molecule = "*"
ansible = "==2.3.3.0"
enum = "*"
"flake8" = "*"
docker = "*"
pbr = "*"
funcsigs = "*"
"backports.functools-lru-cache" = "*"
configparser = "*"
"enum34" = "*"
ipaddress = "*"
docker-py = "*"
"backports.ssl-match-hostname" = "*"
python-vagrant = "*"


[dev-packages]



[requires]

python_version = "2.7"
634 changes: 634 additions & 0 deletions Pipfile.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ consul_conf_client_addr: 0.0.0.0
consul_conf_bind_addr: 0.0.0.0
consul_conf_bootstrap_expect: 3
consul_conf_retry_join: ["provider=aws tag_key=Group tag_value=consul"]
consul_conf_ui: yes
consul_conf_enable_syslog: yes
consul_conf_leave_on_terminate: no
consul_conf_skip_leave_on_interrupt: yes
Expand Down
7 changes: 7 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@
service:
name: dnsmasq
state: restarted
when: lookup('env', 'MOLECULE_DRIVER_NAME') != 'docker'

- name: restart consul
service:
name: consul
state: restarted
when: lookup('env', 'MOLECULE_DRIVER_NAME') != 'docker'
9 changes: 9 additions & 0 deletions molecule/default/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions molecule/default/INSTALL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*******
Install
*******

Requirements
============

* Docker Engine
* docker-py

Install
=======

.. code-block:: bash
$ sudo pip install docker-py
60 changes: 60 additions & 0 deletions molecule/default/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
- 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: json-file
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) }}"
ulimits: "{{ item.ulimits | 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 }}"
27 changes: 27 additions & 0 deletions molecule/default/destroy.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
20 changes: 20 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
platforms:
- name: instance
image: ubuntu:16.04
provisioner:
name: ansible
lint:
name: ansible-lint
scenario:
name: default
verifier:
name: testinfra
lint:
name: flake8
5 changes: 5 additions & 0 deletions molecule/default/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Converge
hosts: all
roles:
- role: ansible-role-consul
5 changes: 5 additions & 0 deletions molecule/default/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Prepare
hosts: all
gather_facts: false
tasks: []
18 changes: 18 additions & 0 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


def test_hosts_file(host):
f = host.file('/etc/hosts')

assert f.exists
assert f.user == 'root'
assert f.group == 'root'


def test_consul_configuration(host):
host.run_expect([0], 'consul validate /etc/consul.d')
2 changes: 0 additions & 2 deletions tasks/dnsmasq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
apt:
name: dnsmasq
state: present
update_cache: yes
cache_valid_time: 3600
tags:
- dnsmasq

Expand Down
21 changes: 16 additions & 5 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
---
- name: update apt-cache
apt:
update_cache: yes
update_cache: true
cache_valid_time: 3600
changed_when: no
tags:
- install

Expand All @@ -22,7 +21,7 @@
src: "{{ consul_src }}"
dest: "{{ consul_dest }}"
mode: 0755
remote_src: yes
remote_src: true
tags:
- install

Expand Down Expand Up @@ -56,7 +55,8 @@
tags:
- install

- assert:
- name: assert consul mode
assert:
that:
- 'consul_mode in ("client", "server")'

Expand All @@ -65,8 +65,17 @@
tags:
- configuration

- set_fact:
- name: set datacenter to current region
set_fact:
consul_datacenter: "{{ ansible_ec2_placement_region }}"
when: ansible_ec2_placement_region is defined
tags:
- configuration

- name: set datacenter to default value
set_fact:
consul_datacenter: "{{ consul_conf_datacenter }}"
when: ansible_ec2_placement_region is undefined
tags:
- configuration

Expand All @@ -77,6 +86,7 @@
owner: root
group: root
mode: 0644
notify: restart consul
tags:
- configuration

Expand All @@ -102,6 +112,7 @@
service:
name: consul
enabled: "{{ consul_service_enabled }}"
when: lookup('env', 'MOLECULE_DRIVER_NAME') != 'docker'
tags:
- service

Expand Down
9 changes: 5 additions & 4 deletions templates/client.json.j2
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"server": false,
"ui": false,

"data_dir": "{{ consul_data_dir }}",
"pid_file": "{{ consul_data_dir }}/consul.pid",
"datacenter": "{{ consul_datacenter }}",
"retry_join": {{ consul_conf_retry_join | to_json }},
"enable_syslog": {{ consul_conf_enable_syslog | to_json}},
"enable_syslog": {{ consul_conf_enable_syslog | to_json }},

"leave_on_terminate": {{ consul_conf_leave_on_terminate | to_json}},
"skip_leave_on_interrupt": {{ consul_conf_skip_leave_on_interrupt | to_json}},
"rejoin_after_leave": {{ consul_conf_rejoin_after_leave | to_json}}
"leave_on_terminate": {{ consul_conf_leave_on_terminate | to_json }},
"skip_leave_on_interrupt": {{ consul_conf_skip_leave_on_interrupt | to_json }},
"rejoin_after_leave": {{ consul_conf_rejoin_after_leave | to_json }}
}
3 changes: 2 additions & 1 deletion templates/server.json.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"server": true,
"ui": {{ consul_conf_ui | to_json }},

"data_dir": "{{ consul_data_dir }}",
"pid_file": "{{ consul_data_dir }}/consul.pid",
Expand All @@ -13,5 +14,5 @@

"leave_on_terminate": {{ consul_conf_leave_on_terminate | to_json}},
"skip_leave_on_interrupt": {{ consul_conf_skip_leave_on_interrupt | to_json}},
"rejoin_after_leave": {{ consul_conf_rejoin_after_leave | to_json}}
"rejoin_after_leave": {{ consul_conf_rejoin_after_leave | to_json }}
}

0 comments on commit d5ed162

Please sign in to comment.