Skip to content

Commit

Permalink
Add override_command command to platforms definition (#1771)
Browse files Browse the repository at this point in the history
Closes #1763.

This adds a `override_command` key to the platforms definition. It
defaults to true. A user who wants to override the `CMD` directive from
their `Dockerfile.j2` can specify `override_command: False` to have it
honoured.

This allows 3 use cases to be covered:

  * Getting the zero configuration default of `bash -c ...`
  * Overriding the `CMD` directive from the `command` key
  * Overriding the `CMD` directive from the `Dockerfile.j2` and setting `override_command: false`

See also:

  * #1615
  * #1614
  * #1441

Signed-off-by: Luke Murphy <[email protected]>
  • Loading branch information
decentral1se authored Feb 25, 2019
1 parent d7e209f commit 414489e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y pyth
elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \
elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi
{%- endraw %}

CMD ["sh", "-c", "while true; do sleep 10000; done"]
8 changes: 8 additions & 0 deletions molecule/driver/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Docker(base.Base):
username: $USERNAME
password: $PASSWORD
email: [email protected]
override_command: True|False
command: sleep infinity
privileged: True|False
security_opts:
Expand Down Expand Up @@ -88,6 +89,12 @@ class Docker(base.Base):
buildargs:
http_proxy: http://proxy.example.com:8080/
If specifying the `CMD`_ directive in your ``Dockerfile.j2`` or consuming a
built image which declares a ``CMD`` directive, then you must set
``override_command: False``. Otherwise, Molecule takes care to honour the
value of the ``command`` key or uses the default of ``bash -c "while true;
do sleep 10000; done"`` to run the container until it is provisioned.
When attempting to utilize a container image with `systemd`_ as your init
system inside the container to simulate a real machine, make sure to set
the ``privileged``, ``volume_mounts``, ``command``, and ``environment``
Expand Down Expand Up @@ -133,6 +140,7 @@ class Docker(base.Base):
.. _`Docker`: https://www.docker.com
.. _`systemd`: https://www.freedesktop.org/wiki/Software/systemd/
.. _`CMD`: https://docs.docker.com/engine/reference/builder/#cmd
""" # noqa

def __init__(self, config):
Expand Down
4 changes: 4 additions & 0 deletions molecule/model/schema_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,10 @@ def pre_validate_base_schema(env, keep_string):
},
}
},
'override_command': {
'type': 'boolean',
'nullable': True,
},
'command': {
'type': 'string',
'nullable': True,
Expand Down
8 changes: 7 additions & 1 deletion molecule/provisioner/ansible/playbooks/docker/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
state: present
with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks }}"

- name: Determine the CMD directive
set_fact:
command_directive: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}"
with_items: "{{ molecule_yml.platforms }}"
when: item.override_command | default(true)

- name: Create molecule instance(s)
docker_container:
name: "{{ item.name }}"
Expand All @@ -64,7 +70,7 @@
state: started
recreate: false
log_driver: json-file
command: "{{ item.command | default(omit) }}"
command: "{{ command_directive | default(omit) }}"
privileged: "{{ item.privileged | default(omit) }}"
security_opts: "{{ item.security_opts | default(omit) }}"
volumes: "{{ item.volumes | default(omit) }}"
Expand Down
2 changes: 0 additions & 2 deletions test/resources/playbooks/docker/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y pyth
elif [ $(command -v zypper) ]; then zypper refresh && 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; \
elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi

CMD ["sh", "-c", "while true; do sleep 10000; done"]
8 changes: 7 additions & 1 deletion test/resources/playbooks/docker/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
state: present
with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks }}"

- name: Determine the CMD directive
set_fact:
command_directive: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}"
with_items: "{{ molecule_yml.platforms }}"
when: item.override_command | default(true)

- name: Create molecule instance(s)
docker_container:
name: "{{ item.name }}"
Expand All @@ -64,7 +70,7 @@
state: started
recreate: false
log_driver: json-file
command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}"
command: "{{ command_directive | default(omit) }}"
privileged: "{{ item.privileged | default(omit) }}"
security_opts: "{{ item.security_opts | default(omit) }}"
volumes: "{{ item.volumes | default(omit) }}"
Expand Down
4 changes: 4 additions & 0 deletions test/unit/model/v2/test_platforms_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def _model_platforms_docker_section_data():
'email': '[email protected]',
},
},
'override_command':
False,
'command':
'sleep infinity',
'privileged':
Expand Down Expand Up @@ -126,6 +128,7 @@ def _model_platforms_docker_errors_section_data():
'email': int(),
},
},
'override_command': int(),
'command': int(),
'privileged': str(),
'security_opts': [
Expand Down Expand Up @@ -189,6 +192,7 @@ def test_platforms_docker_has_errors(_config):
0: ['must be of string type']
}],
'privileged': ['must be of boolean type'],
'override_command': ['must be of boolean type'],
'command': ['must be of string type'],
'registry': [{
'url': ['must be of string type'],
Expand Down

0 comments on commit 414489e

Please sign in to comment.