Skip to content

Commit

Permalink
Adds pre_build_image option to Docker create playbook (#1447)
Browse files Browse the repository at this point in the history
* Adds pre_build_image option to Docker create playbook

* Corrects no_log and Dockerfile location
  • Loading branch information
wilmardo authored and retr0h committed Aug 17, 2018
1 parent 313a65a commit 9170961
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions molecule/driver/docker.py
Expand Up @@ -47,6 +47,7 @@ class Docker(base.Base):
hostname: instance
image: image_name:tag
pull: True|False
pre_build_image: True|False
registry:
url: registry.example.com
credentials:
Expand Down
3 changes: 3 additions & 0 deletions molecule/model/schema_v2.py
Expand Up @@ -611,6 +611,9 @@ def pre_validate_base_schema(env, keep_string):
'pull': {
'type': 'boolean',
},
'pre_build_image': {
'type': 'boolean',
},
'registry': {
'type': 'dict',
'schema': {
Expand Down
8 changes: 6 additions & 2 deletions molecule/provisioner/ansible/playbooks/docker/create.yml
Expand Up @@ -23,13 +23,15 @@
src: "{{ molecule_scenario_directory }}/Dockerfile.j2"
dest: "{{ molecule_ephemeral_directory }}/Dockerfile_{{ item.image | regex_replace('[^a-zA-Z0-9_]', '_') }}"
with_items: "{{ molecule_yml.platforms }}"
when: not item.pre_build_image | default(false)
register: platforms

- name: Discover local Docker images
docker_image_facts:
name: "molecule_local/{{ item.item.name }}"
docker_host: "{{ item.item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
with_items: "{{ platforms.results }}"
when: not item.pre_build_image | default(false)
register: docker_images

- name: Build an Ansible compatible image
Expand All @@ -41,7 +43,9 @@
force: "{{ item.item.force | default(true) }}"
pull: "{{ item.item.pull | default(omit) }}"
with_items: "{{ platforms.results }}"
when: platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0
when:
- platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0
- not item.item.pre_build_image | default(false)

- name: Create docker network(s)
docker_network:
Expand All @@ -55,7 +59,7 @@
name: "{{ item.name }}"
docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
hostname: "{{ item.hostname | default(item.name) }}"
image: "molecule_local/{{ item.image }}"
image: "{{ item.pre_build_image | default(false) | ternary('', 'molecule_local/') }}{{ item.image }}"
state: started
recreate: false
log_driver: json-file
Expand Down
8 changes: 6 additions & 2 deletions test/resources/playbooks/docker/create.yml
Expand Up @@ -23,13 +23,15 @@
src: Dockerfile.j2
dest: "{{ molecule_ephemeral_directory }}/Dockerfile_{{ item.image | regex_replace('[^a-zA-Z0-9_]', '_') }}"
with_items: "{{ molecule_yml.platforms }}"
when: not item.pre_build_image | default(false)
register: platforms

- name: Discover local Docker images
docker_image_facts:
name: "molecule_local/{{ item.item.name }}"
docker_host: "{{ item.item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
with_items: "{{ platforms.results }}"
when: not item.pre_build_image | default(false)
register: docker_images

- name: Build an Ansible compatible image
Expand All @@ -41,7 +43,9 @@
force: "{{ item.item.force | default(true) }}"
pull: "{{ item.item.pull | default(omit) }}"
with_items: "{{ platforms.results }}"
when: platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0
when:
- platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0
- not item.item.pre_build_image | default(false)

- name: Create docker network(s)
docker_network:
Expand All @@ -55,7 +59,7 @@
name: "{{ item.name }}"
docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}"
hostname: "{{ item.hostname | default(item.name) }}"
image: "molecule_local/{{ item.image }}"
image: "{{ item.pre_build_image | default(false) | ternary('', 'molecule_local/') }}{{ item.image }}"
state: started
recreate: false
log_driver: json-file
Expand Down
4 changes: 4 additions & 0 deletions test/unit/model/v2/test_platforms_section.py
Expand Up @@ -38,6 +38,8 @@ def _model_platforms_docker_section_data():
'image_name:tag',
'pull':
True,
'pre_build_image':
False,
'registry': {
'url': 'registry.example.com',
'credentials': {
Expand Down Expand Up @@ -111,6 +113,7 @@ def _model_platforms_docker_errors_section_data():
'hostname': int(),
'image': int(),
'pull': int(),
'pre_build_image': int(),
'registry': {
'url': int(),
'credentials': {
Expand Down Expand Up @@ -173,6 +176,7 @@ def test_platforms_docker_has_errors(_config):
}],
'image': ['must be of string type'],
'pull': ['must be of boolean type'],
'pre_build_image': ['must be of boolean type'],
'hostname': ['must be of string type'],
'security_opts': [{
0: ['must be of string type']
Expand Down

0 comments on commit 9170961

Please sign in to comment.