Skip to content

Commit

Permalink
Fixed issues with container stop/deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentganne committed Aug 12, 2021
1 parent 83458c0 commit 50d65de
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
7 changes: 6 additions & 1 deletion org/ystia/docker/containers/generic/playbooks/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- name: Install python requirements
hosts: all
become: true
environment:
# Adding /usr/local/bin needed by virtualenv on CentOS 8
PATH: "{{ ansible_env.PATH }}:/usr/local/bin"
tasks:
- name: Get python version
python_requirements_info:
Expand Down Expand Up @@ -66,7 +69,9 @@

- name: Install 'docker' python package
pip:
name: docker
name:
- docker
- six
virtualenv: /usr/local/docker-py-env

- name: Create Docker Container
Expand Down
8 changes: 7 additions & 1 deletion org/ystia/docker/containers/generic/playbooks/delete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
vars:
ansible_python_interpreter: "/usr/local/docker-py-env/bin/python"
tasks:
- name: delete docker container
- name: Delete docker container
docker_container:
name: "{{CONTAINER_ID}}"
state: absent
when: STARTED_CONTAINER_ID is not defined or STARTED_CONTAINER_ID == ""
- name: Delete started docker container
docker_container:
name: "{{STARTED_CONTAINER_ID}}"
state: absent
when: STARTED_CONTAINER_ID is defined and STARTED_CONTAINER_ID != ""

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
name: "{{NODE}}-{{INSTANCE}}"
auto_remove: "{{AUTO_REMOVE}}"
cleanup: "{{CLEANUP}}"
command: "{{COMMAND}}"
# cpus: "{{CPU_SHARE}}"
detach: "{{DETACH}}"
env: "{{DOCKER_ENV}}"
Expand Down
25 changes: 21 additions & 4 deletions org/ystia/docker/containers/generic/playbooks/start.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- name: Start Docker Container
hosts: all
become: true
# When the container fails, go on to retrieve outputs
vars:
ansible_python_interpreter: "/usr/local/docker-py-env/bin/python"
DOCKER_ENV: {}
Expand All @@ -15,8 +16,24 @@
DOCKER_VOLUMES: []
DOCKER_STATE: "started"
tasks:
- import_tasks: docker_container_tasks.yaml
- name: Get container output
- name: Start container
import_tasks: docker_container_tasks.yaml
no_log: true
ignore_errors: yes
- name: Store container id (needed if it was directly started or config changed between create and start)
set_fact:
STARTED_CONTAINER_ID: "{{docker_res.ansible_facts.docker_container['Id']}}"
when: docker_res.ansible_facts is defined
- name: Get container output (truncated)
debug:
msg: "{{ docker_res.ansible_facts.docker_container.Output }}"
when: not DETACH|bool
msg: "{{ docker_res.ansible_facts.docker_container.Output[-20000:] }}"
when: not DETACH|bool and docker_res.ansible_facts is defined
- name: Get error
debug:
msg: "{{ docker_res.msg[-20000:] }}"
when: docker_res.ansible_facts is not defined
- name: Check status
debug:
msg: "Check container exit code"
failed_when: docker_res.ansible_facts is not defined or docker_res.ansible_facts.docker_container.State.ExitCode != 0

6 changes: 6 additions & 0 deletions org/ystia/docker/containers/generic/playbooks/stop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@
docker_container:
name: "{{CONTAINER_ID}}"
state: stopped
when: STARTED_CONTAINER_ID is not defined or STARTED_CONTAINER_ID == ""
- name: Stop started docker container
docker_container:
name: "{{STARTED_CONTAINER_ID}}"
state: stopped
when: STARTED_CONTAINER_ID is defined and STARTED_CONTAINER_ID != ""

5 changes: 4 additions & 1 deletion org/ystia/docker/containers/generic/types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ metadata:
template_version: 3.0.0-SNAPSHOT
template_author: Ystia


description: Docker container types

imports:
- tosca-normative-types:1.0.0-ALIEN20
Expand Down Expand Up @@ -102,13 +102,16 @@ node_types:
required: false
attributes:
container_id: { get_operation_output: [SELF, Standard, create, CONTAINER_ID] }
started_container_id: { get_operation_output: [SELF, Standard, create, STARTED_CONTAINER_ID] }
interfaces:
Standard:
inputs:
# Will be empty for create operation but not a big deal
CONTAINER_ID: { get_attribute: [SELF, container_id] }
STARTED_CONTAINER_ID: { get_attribute: [SELF, started_container_id] }
AUTO_REMOVE: { get_property: [SELF, auto_remove] }
CLEANUP: { get_property: [SELF, cleanup] }
COMMAND: { get_property: [SELF, docker_run_cmd] }
CPU_SHARE: { get_property: [SELF, cpu_share] }
DETACH: { get_property: [SELF, detach] }
ENV_VARS: { get_property: [SELF, docker_env_vars] }
Expand Down

0 comments on commit 50d65de

Please sign in to comment.