From b0250075767399292320474a7cffdcd85f548e1d Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Wed, 21 Oct 2020 17:17:50 +0200 Subject: [PATCH 01/15] First version --- org/ystia/docker/images/playbooks/create.yaml | 18 +++++ .../docker/images/playbooks/load_archive.yaml | 23 +++++++ .../docker/images/playbooks/remove_image.yaml | 4 ++ org/ystia/docker/images/types.yml | 69 +++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 org/ystia/docker/images/playbooks/create.yaml create mode 100644 org/ystia/docker/images/playbooks/load_archive.yaml create mode 100644 org/ystia/docker/images/playbooks/remove_image.yaml create mode 100644 org/ystia/docker/images/types.yml diff --git a/org/ystia/docker/images/playbooks/create.yaml b/org/ystia/docker/images/playbooks/create.yaml new file mode 100644 index 00000000..f25e5709 --- /dev/null +++ b/org/ystia/docker/images/playbooks/create.yaml @@ -0,0 +1,18 @@ +# +# Ystia Forge +# Copyright (C) 2018 Bull S. A. S. - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France. +# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. +# + + +- name: Install ansible docker_image requirements + hosts: all + become: true + tasks: + - name: Install pip + easy_install: + name: pip + state: latest + - name: Install 'docker' python package + pip: + name: docker diff --git a/org/ystia/docker/images/playbooks/load_archive.yaml b/org/ystia/docker/images/playbooks/load_archive.yaml new file mode 100644 index 00000000..1e67e5ed --- /dev/null +++ b/org/ystia/docker/images/playbooks/load_archive.yaml @@ -0,0 +1,23 @@ +# +# Ystia Forge +# Copyright (C) 2020 Bull S. A. S. - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France. +# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. +# +- name: Load Docker image from tar archive + hosts: all + become: true + tasks: + - name: Load tar archive + docker_image: + name: localhost:5000/myimages/sinatra + tag: v1 + push: yes + load_path: my_sinatra.tar + source: load + register: image + - name: Inspect image loaded + debug: + msg: "Image loaded: {{image}}" + - name: Set repo tags output + set_fact: + REPO_TAGS: "{{image.RepoTags}}" diff --git a/org/ystia/docker/images/playbooks/remove_image.yaml b/org/ystia/docker/images/playbooks/remove_image.yaml new file mode 100644 index 00000000..6ba4eba1 --- /dev/null +++ b/org/ystia/docker/images/playbooks/remove_image.yaml @@ -0,0 +1,4 @@ +- name: noop playbook + hosts: localhost + tasks: + - debug: msg="Nothing to do" diff --git a/org/ystia/docker/images/types.yml b/org/ystia/docker/images/types.yml new file mode 100644 index 00000000..5ce45cd9 --- /dev/null +++ b/org/ystia/docker/images/types.yml @@ -0,0 +1,69 @@ +tosca_definitions_version: alien_dsl_2_0_0 + +# +# Ystia Forge +# Copyright (C) 2018 Bull S. A. S. - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France. +# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. +# + +metadata: + template_name: org.ystia.docker.images + template_version: 3.0.0-SNAPSHOT + template_author: Ystia + + + +imports: + - tosca-normative-types:1.0.0-ALIEN20 + - yorc-types:1.1.0 + +node_types: + org.ystia.docker.images.Loader: + derived_from: tosca.nodes.SoftwareComponent + description: Loads a docker image from a tar archive + properties: + path: + type: string + description: Path to the tar archive (can be compressed with gzip, bzip2, or xz)) + required: true + name: + type: string + description: > + Image name. Name format will be one of: name, repository/name, registry_server:port/name. + When pushing an image the name can optionally include the tag by appending ':tag_name'. + required: false + tag: + type: string + description: > + Tag added to the image when pushing. + If name property format is name:tag, then tag value from name will take precedence. + required: false + repository: + type: string + description: > + Full path to a repository. Expects format repository:tag. + If no tag is provided, will use the value of the tag parameter or latest. + required: false + push: + type: boolean + description: Push the image to the registry. Specify the registry as part of the name or repository parameter. + default: false + required: false + attributes: + repo_tags: { get_operation_output: [SELF, Standard, start, REPO_TAGS] } + interfaces: + Standard: + inputs: + PATH: { get_property: [SELF, path] } + NAME: { get_property: [SELF, name] } + TAG: { get_property: [SELF, tag] } + REPOSITORY: { get_property: [SELF, repository] } + PUSH: { get_property: [SELF, push] } + create: + implementation: playbooks/create.yaml + start: + implementation: playbooks/load_archive.yaml + delete: + inputs: + repo_tags: { get_attribute: [SELF, repo_tags] } + implementation: playbooks/remove_image.yaml From 973cf4d8f0038196b2e654904035c515785ddea7 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Thu, 22 Oct 2020 21:59:56 +0000 Subject: [PATCH 02/15] Updates to load and removal --- org/ystia/docker/images/playbooks/create.yaml | 28 ++++++++++++++---- .../docker/images/playbooks/load_archive.yaml | 29 +++++++++++++------ .../docker/images/playbooks/remove_image.yaml | 28 ++++++++++++++++-- org/ystia/docker/images/types.yml | 15 ++++++++-- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/org/ystia/docker/images/playbooks/create.yaml b/org/ystia/docker/images/playbooks/create.yaml index f25e5709..8c2193be 100644 --- a/org/ystia/docker/images/playbooks/create.yaml +++ b/org/ystia/docker/images/playbooks/create.yaml @@ -3,16 +3,34 @@ # Copyright (C) 2018 Bull S. A. S. - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France. # Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. # - - - name: Install ansible docker_image requirements hosts: all become: true tasks: + - name: check if pip is installed + command: "pip --version" + ignore_errors: true + changed_when: false + check_mode: no + register: pip_found + # No need to go further if pip is already installed + - meta: end_play + when: pip_found.rc == 0 + + - name: Get python version + python_requirements_info: + register: pyinfo + failed_when: "pyinfo == None or pyinfo.python_version == None or pyinfo.python_version == ''" + - name: Download pip + get_url: + url: https://bootstrap.pypa.io/get-pip.py + dest: /tmp/get-pip.py - name: Install pip - easy_install: - name: pip - state: latest + command: "{{ pyinfo.python }} /tmp/get-pip.py" + - name: cleanup downloaded file + file: + state: absent + path: /tmp/get-pip.py - name: Install 'docker' python package pip: name: docker diff --git a/org/ystia/docker/images/playbooks/load_archive.yaml b/org/ystia/docker/images/playbooks/load_archive.yaml index 1e67e5ed..99ff8392 100644 --- a/org/ystia/docker/images/playbooks/load_archive.yaml +++ b/org/ystia/docker/images/playbooks/load_archive.yaml @@ -9,15 +9,26 @@ tasks: - name: Load tar archive docker_image: - name: localhost:5000/myimages/sinatra - tag: v1 - push: yes - load_path: my_sinatra.tar + name: "{{NAME}}" + tag: "{{TAG}}" + push: "{{PUSH}}" + repository: "{{REPOSITORY}}" + load_path: "{{PATH}}" source: load - register: image - - name: Inspect image loaded + force_source: "{{FORCE_LOAD}}" + register: result + - name: Fail if the image specified does not correpond to the archive specified debug: - msg: "Image loaded: {{image}}" - - name: Set repo tags output + msg: "Checking if image {{NAME}} {{TAG}} is in tar archive {{PATH}}" + failed_when: result.image == None + - name: Check if an image was loaded + debug: + msg: "Image {{NAME}} {{TAG}} already loaded" + when: result.image == {} + - name: Intialize fact used in opration output + set_fact: + REPO_TAGS: [] + - name: Set repo tags output if returned by docker_image set_fact: - REPO_TAGS: "{{image.RepoTags}}" + REPO_TAGS: "{{result.image.RepoTags | to_json}}" + when: result.image != None and result.image != {} diff --git a/org/ystia/docker/images/playbooks/remove_image.yaml b/org/ystia/docker/images/playbooks/remove_image.yaml index 6ba4eba1..d9ee31ad 100644 --- a/org/ystia/docker/images/playbooks/remove_image.yaml +++ b/org/ystia/docker/images/playbooks/remove_image.yaml @@ -1,4 +1,26 @@ -- name: noop playbook - hosts: localhost +# +# Ystia Forge +# Copyright (C) 2020 Bull S. A. S. - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France. +# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. +# +- name: Remove Docker image + hosts: all + become: true tasks: - - debug: msg="Nothing to do" + # Trusting first REPO_TAGS returned by docker_image + # if there is just one such image in the registry + - name: "Get image {{NAME}} in regsitry" + set_fact: + images: "{{REPO_TAGS | from_json}}" + - name: "Remove image {{NAME}} {{TAG}}" + docker_image: + name: "{{images[0]}}" + state: absent + when: images | length == 1 + - name: "Remove image {{NAME}} {{TAG}}" + docker_image: + name: "{{NAME}}" + tag: "{{TAG}}" + repository: "{{REPOSITORY}}" + state: absent + when: images | length != 1 diff --git a/org/ystia/docker/images/types.yml b/org/ystia/docker/images/types.yml index 5ce45cd9..3f12fb2b 100644 --- a/org/ystia/docker/images/types.yml +++ b/org/ystia/docker/images/types.yml @@ -31,12 +31,13 @@ node_types: description: > Image name. Name format will be one of: name, repository/name, registry_server:port/name. When pushing an image the name can optionally include the tag by appending ':tag_name'. - required: false + required: true tag: type: string description: > Tag added to the image when pushing. If name property format is name:tag, then tag value from name will take precedence. + If not defined, docker considers the tag value is 'latest'. required: false repository: type: string @@ -49,7 +50,15 @@ node_types: description: Push the image to the registry. Specify the registry as part of the name or repository parameter. default: false required: false + force_load: + type: boolean + description: Load the archive even when the image already exists + default: false + required: false attributes: + # Array of repository name:tag in registry for the specified name. + # Empty if no name was specified or if the image was already loaded + # and force_load is set to false repo_tags: { get_operation_output: [SELF, Standard, start, REPO_TAGS] } interfaces: Standard: @@ -62,8 +71,10 @@ node_types: create: implementation: playbooks/create.yaml start: + inputs: + FORCE_LOAD: { get_property: [SELF, force_load] } implementation: playbooks/load_archive.yaml delete: inputs: - repo_tags: { get_attribute: [SELF, repo_tags] } + REPO_TAGS: { get_attribute: [SELF, repo_tags] } implementation: playbooks/remove_image.yaml From 9ab4d3458f727d236cf9bde30b3310e49a72e568 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Tue, 27 Oct 2020 20:13:05 +0000 Subject: [PATCH 03/15] Added an optional requirement on archive provider --- .../docker/images/playbooks/load_archive.yaml | 11 +++++- org/ystia/docker/images/types.yml | 35 +++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/org/ystia/docker/images/playbooks/load_archive.yaml b/org/ystia/docker/images/playbooks/load_archive.yaml index 99ff8392..acd7dd5c 100644 --- a/org/ystia/docker/images/playbooks/load_archive.yaml +++ b/org/ystia/docker/images/playbooks/load_archive.yaml @@ -7,13 +7,22 @@ hosts: all become: true tasks: + - name: Get archive PATH defined at runtime + set_fact: + archive_path: "{{RUNTIME_PATH}}" + when: RUNTIME_PATH != "" + - name: Get archive PATH property + set_fact: + archive_path: "{{PATH}}" + when: RUNTIME_PATH == "" + failed_when: RUNTIME_PATH == "" and PATH == "" - name: Load tar archive docker_image: name: "{{NAME}}" tag: "{{TAG}}" push: "{{PUSH}}" repository: "{{REPOSITORY}}" - load_path: "{{PATH}}" + load_path: "{{archive_path}}" source: load force_source: "{{FORCE_LOAD}}" register: result diff --git a/org/ystia/docker/images/types.yml b/org/ystia/docker/images/types.yml index 3f12fb2b..8236c936 100644 --- a/org/ystia/docker/images/types.yml +++ b/org/ystia/docker/images/types.yml @@ -24,8 +24,13 @@ node_types: properties: path: type: string - description: Path to the tar archive (can be compressed with gzip, bzip2, or xz)) - required: true + description: > + Path to the tar archive (can be compressed with gzip, bzip2, or xz)). + Either this property 'path' should be provided, or this Loader component + should be in relationship with an ArchiveProvider providing in its attribute + 'path', the path to an archive computed at runtime. + The ArchiveProvider 'path' attribute takes precedence over the Loader 'path' property. + required: false name: type: string description: > @@ -60,21 +65,45 @@ node_types: # Empty if no name was specified or if the image was already loaded # and force_load is set to false repo_tags: { get_operation_output: [SELF, Standard, start, REPO_TAGS] } + requirements: + - archive_provider: + capability: org.ystia.docker.images.capabilities.ArchiveProvider + relationship: org.ystia.docker.images.relationships.ArchiveProvider + occurrences: [0, 1] interfaces: Standard: inputs: PATH: { get_property: [SELF, path] } + RUNTIME_PATH: {get_attribute: [REQ_TARGET, archive_provider, path]} NAME: { get_property: [SELF, name] } TAG: { get_property: [SELF, tag] } REPOSITORY: { get_property: [SELF, repository] } PUSH: { get_property: [SELF, push] } + # Install dependencies create: implementation: playbooks/create.yaml + # load the image from the tar archive start: inputs: FORCE_LOAD: { get_property: [SELF, force_load] } implementation: playbooks/load_archive.yaml - delete: + # Remove the image from docker registry + stop: inputs: REPO_TAGS: { get_attribute: [SELF, repo_tags] } implementation: playbooks/remove_image.yaml +capability_types: + org.ystia.docker.images.capabilities.ArchiveProvider: + derived_from: tosca.capabilities.Root + description: > + A capability provided by a component providing a docker image tar archive. + attributes: + path: + type: string + description: Path to the docker image tar archive +relationship_types: + org.ystia.docker.images.relationships.ArchiveProvider: + derived_from: tosca.relationships.DependsOn + description: > + Relationship with a provider of docker image tar archive + valid_target_types: [ org.ystia.docker.images.capabilities.ArchiveProvider ] From 704a8f5973aa5e179e65c0714a4ff10af8e53104 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Mon, 1 Feb 2021 06:13:50 +0000 Subject: [PATCH 04/15] Minor updates --- .../containers/generic/playbooks/create.yaml | 14 ++++- org/ystia/docker/images/playbooks/create.yaml | 56 ++++++++++++------- .../docker/images/playbooks/load_archive.yaml | 21 +++++-- .../docker/images/playbooks/remove_image.yaml | 13 +++++ org/ystia/docker/images/types.yml | 10 ++++ 5 files changed, 87 insertions(+), 27 deletions(-) diff --git a/org/ystia/docker/containers/generic/playbooks/create.yaml b/org/ystia/docker/containers/generic/playbooks/create.yaml index 3ae7cf46..8cb31842 100644 --- a/org/ystia/docker/containers/generic/playbooks/create.yaml +++ b/org/ystia/docker/containers/generic/playbooks/create.yaml @@ -9,11 +9,23 @@ hosts: all become: true tasks: + - name: Get python version + python_requirements_info: + register: pri + failed_when: pri == None or pri.python_version == None or pri.python_version == '' + - name: Get python major version + set_fact: + python_major_version: "{{pri.python_version | replace('\n', '') | regex_replace('^(\\d+).*', '\\1') }}" + - name: Install pip version compatible with python 2 + easy_install: + name: pip<21.0 + state: latest + when: python_major_version == "2" - name: Install pip easy_install: name: pip state: latest - + when: python_major_version != "2" - name: Install 'virtualenv' package pip: name: virtualenv diff --git a/org/ystia/docker/images/playbooks/create.yaml b/org/ystia/docker/images/playbooks/create.yaml index 8c2193be..2af7291a 100644 --- a/org/ystia/docker/images/playbooks/create.yaml +++ b/org/ystia/docker/images/playbooks/create.yaml @@ -7,30 +7,44 @@ hosts: all become: true tasks: - - name: check if pip is installed - command: "pip --version" - ignore_errors: true - changed_when: false - check_mode: no - register: pip_found - # No need to go further if pip is already installed - - meta: end_play - when: pip_found.rc == 0 - - name: Get python version python_requirements_info: - register: pyinfo - failed_when: "pyinfo == None or pyinfo.python_version == None or pyinfo.python_version == ''" - - name: Download pip - get_url: - url: https://bootstrap.pypa.io/get-pip.py - dest: /tmp/get-pip.py + register: pri + failed_when: pri == None or pri.python_version == None or pri.python_version == '' + - name: Get python major version + set_fact: + python_major_version: "{{pri.python_version | replace('\n', '') | regex_replace('^(\\d+).*', '\\1') }}" + - name: Install pip version compatible with python 2 + easy_install: + name: pip<21.0 + state: latest + when: python_major_version == "2" - name: Install pip - command: "{{ pyinfo.python }} /tmp/get-pip.py" - - name: cleanup downloaded file - file: - state: absent - path: /tmp/get-pip.py + easy_install: + name: pip + state: latest + when: python_major_version != "2" + - name: Get user defined at runtime + set_fact: + user_loading: "{{RUNTIME_USER}}" + when: RUNTIME_USER != "" + - name: Get user property + set_fact: + user_loading: "{{USER}}" + when: RUNTIME_USER == "" + failed_when: RUNTIME_USER == "" and USER == "" + - name: Add to docker group + user: + name: "{{user_loading}}" + groups: docker + append: yes + when: user_loading != "root" - name: Install 'docker' python package pip: name: docker + # In user directory to avoid potential issues with distutils + # see issue https://github.com/pypa/pip/issues/5247 + extra_args: --user + become_user: "{{user_loading}}" + become_method: sudo + diff --git a/org/ystia/docker/images/playbooks/load_archive.yaml b/org/ystia/docker/images/playbooks/load_archive.yaml index acd7dd5c..9c0d0ed4 100644 --- a/org/ystia/docker/images/playbooks/load_archive.yaml +++ b/org/ystia/docker/images/playbooks/load_archive.yaml @@ -5,17 +5,25 @@ # - name: Load Docker image from tar archive hosts: all - become: true tasks: - name: Get archive PATH defined at runtime set_fact: archive_path: "{{RUNTIME_PATH}}" when: RUNTIME_PATH != "" - name: Get archive PATH property - set_fact: - archive_path: "{{PATH}}" - when: RUNTIME_PATH == "" - failed_when: RUNTIME_PATH == "" and PATH == "" + set_fact: + archive_path: "{{PATH}}" + when: RUNTIME_PATH == "" + failed_when: RUNTIME_PATH == "" and PATH == "" + - name: Get user defined at runtime + set_fact: + user_loading: "{{RUNTIME_USER}}" + when: RUNTIME_USER != "" + - name: Get user property + set_fact: + user_loading: "{{USER}}" + when: RUNTIME_USER == "" + failed_when: RUNTIME_USER == "" and USER == "" - name: Load tar archive docker_image: name: "{{NAME}}" @@ -25,6 +33,9 @@ load_path: "{{archive_path}}" source: load force_source: "{{FORCE_LOAD}}" + become: true + become_user: "{{user_loading}}" + become_method: sudo register: result - name: Fail if the image specified does not correpond to the archive specified debug: diff --git a/org/ystia/docker/images/playbooks/remove_image.yaml b/org/ystia/docker/images/playbooks/remove_image.yaml index d9ee31ad..26475bdc 100644 --- a/org/ystia/docker/images/playbooks/remove_image.yaml +++ b/org/ystia/docker/images/playbooks/remove_image.yaml @@ -12,11 +12,22 @@ - name: "Get image {{NAME}} in regsitry" set_fact: images: "{{REPO_TAGS | from_json}}" + - name: Get user defined at runtime + set_fact: + user_loading: "{{RUNTIME_USER}}" + when: RUNTIME_USER != "" + - name: Get user property + set_fact: + user_loading: "{{USER}}" + when: RUNTIME_USER == "" + failed_when: RUNTIME_USER == "" and USER == "" - name: "Remove image {{NAME}} {{TAG}}" docker_image: name: "{{images[0]}}" state: absent when: images | length == 1 + become_user: "{{user_loading}}" + become_method: sudo - name: "Remove image {{NAME}} {{TAG}}" docker_image: name: "{{NAME}}" @@ -24,3 +35,5 @@ repository: "{{REPOSITORY}}" state: absent when: images | length != 1 + become_user: "{{user_loading}}" + become_method: sudo diff --git a/org/ystia/docker/images/types.yml b/org/ystia/docker/images/types.yml index 8236c936..9b4a4f2f 100644 --- a/org/ystia/docker/images/types.yml +++ b/org/ystia/docker/images/types.yml @@ -60,6 +60,11 @@ node_types: description: Load the archive even when the image already exists default: false required: false + user: + type: string + description: Perform the operation as this existing user + default: root + required: false attributes: # Array of repository name:tag in registry for the specified name. # Empty if no name was specified or if the image was already loaded @@ -75,6 +80,8 @@ node_types: inputs: PATH: { get_property: [SELF, path] } RUNTIME_PATH: {get_attribute: [REQ_TARGET, archive_provider, path]} + USER: { get_property: [SELF, user] } + RUNTIME_USER: {get_attribute: [REQ_TARGET, archive_provider, user]} NAME: { get_property: [SELF, name] } TAG: { get_property: [SELF, tag] } REPOSITORY: { get_property: [SELF, repository] } @@ -101,6 +108,9 @@ capability_types: path: type: string description: Path to the docker image tar archive + user: + type: string + description: User having access to this archive relationship_types: org.ystia.docker.images.relationships.ArchiveProvider: derived_from: tosca.relationships.DependsOn From d53ff0ad1785d3e6c68169777c9ec71f7f7f668a Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Mon, 1 Feb 2021 08:52:19 +0000 Subject: [PATCH 05/15] Minor fix --- org/ystia/docker/containers/generic/playbooks/create.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/org/ystia/docker/containers/generic/playbooks/create.yaml b/org/ystia/docker/containers/generic/playbooks/create.yaml index 8cb31842..b68199df 100644 --- a/org/ystia/docker/containers/generic/playbooks/create.yaml +++ b/org/ystia/docker/containers/generic/playbooks/create.yaml @@ -19,7 +19,6 @@ - name: Install pip version compatible with python 2 easy_install: name: pip<21.0 - state: latest when: python_major_version == "2" - name: Install pip easy_install: From 7c212a883d22fdf005a9b0e66b4cd5f7ea8bf4d7 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Fri, 26 Feb 2021 12:59:44 +0000 Subject: [PATCH 06/15] Fixed issues depending on python version --- .../containers/generic/playbooks/create.yaml | 42 +++++++++++++++--- org/ystia/docker/images/playbooks/create.yaml | 43 +++++++++++++++---- 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/org/ystia/docker/containers/generic/playbooks/create.yaml b/org/ystia/docker/containers/generic/playbooks/create.yaml index 9b5ebe5c..c502d4b2 100644 --- a/org/ystia/docker/containers/generic/playbooks/create.yaml +++ b/org/ystia/docker/containers/generic/playbooks/create.yaml @@ -16,20 +16,48 @@ - name: Get python major version set_fact: python_major_version: "{{pri.python_version | replace('\n', '') | regex_replace('^(\\d+).*', '\\1') }}" - - name: Install pip version compatible with python 2 - easy_install: - name: pip<21.0 + - name: Set python version for pip + set_fact: + python_pip_pkg: "python3-pip" + pip_cmd: "pip3" + when: python_major_version != "2" + - name: Set python 2 version for pip + set_fact: + python_pip_pkg: "python-pip" + pip_cmd: "pip" when: python_major_version == "2" - - name: Install pip - easy_install: - name: pip - state: latest + - name: RedHat - install prerequisites + yum: + name: + - "{{ python_pip_pkg }}" + state: present + update_cache: yes + when: ansible_os_family == 'RedHat' + - name: Debian - install prerequisites + apt: + name: + - "{{ python_pip_pkg }}" + state: present + update_cache: yes + when: ansible_os_family == 'Debian' + - name: Install latest Pip version + pip: + name: "pip" + state: latest + executable: "{{pip_cmd}}" when: python_major_version != "2" + - name: Install latest Pip version compatible with python 2 + pip: + name: "pip<21.0" + state: latest + executable: "{{pip_cmd}}" + when: python_major_version == "2" - name: Install 'virtualenv' package pip: name: virtualenv state: latest + executable: "{{pip_cmd}}" - name: Check docker python dependencies shell: > diff --git a/org/ystia/docker/images/playbooks/create.yaml b/org/ystia/docker/images/playbooks/create.yaml index 2af7291a..a0cb038e 100644 --- a/org/ystia/docker/images/playbooks/create.yaml +++ b/org/ystia/docker/images/playbooks/create.yaml @@ -14,16 +14,42 @@ - name: Get python major version set_fact: python_major_version: "{{pri.python_version | replace('\n', '') | regex_replace('^(\\d+).*', '\\1') }}" - - name: Install pip version compatible with python 2 - easy_install: - name: pip<21.0 - state: latest + - name: Set python version for pip + set_fact: + python_pip_pkg: "python3-pip" + pip_cmd: "pip3" + when: python_major_version != "2" + - name: Set python 2 version for pip + set_fact: + python_pip_pkg: "python-pip" + pip_cmd: "pip" when: python_major_version == "2" - - name: Install pip - easy_install: - name: pip - state: latest + - name: RedHat - install prerequisites + yum: + name: + - "{{ python_pip_pkg }}" + state: present + update_cache: yes + when: ansible_os_family == 'RedHat' + - name: Debian - install prerequisites + apt: + name: + - "{{ python_pip_pkg }}" + state: present + update_cache: yes + when: ansible_os_family == 'Debian' + - name: Install latest Pip version + pip: + name: "pip" + state: latest + executable: "{{pip_cmd}}" when: python_major_version != "2" + - name: Install latest Pip version compatible with python 2 + pip: + name: "pip<21.0" + state: latest + executable: "{{pip_cmd}}" + when: python_major_version == "2" - name: Get user defined at runtime set_fact: user_loading: "{{RUNTIME_USER}}" @@ -45,6 +71,7 @@ # In user directory to avoid potential issues with distutils # see issue https://github.com/pypa/pip/issues/5247 extra_args: --user + executable: "{{pip_cmd}}" become_user: "{{user_loading}}" become_method: sudo From 3273480063d2ceea3066d76e1dcc668ef38d46a1 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Tue, 9 Mar 2021 16:54:05 +0000 Subject: [PATCH 07/15] Updated Loader components names --- .../docker/images/playbooks/load_archive.yaml | 17 +++----- org/ystia/docker/images/types.yml | 43 ++++++++++++++----- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/org/ystia/docker/images/playbooks/load_archive.yaml b/org/ystia/docker/images/playbooks/load_archive.yaml index 9c0d0ed4..88a1f4b2 100644 --- a/org/ystia/docker/images/playbooks/load_archive.yaml +++ b/org/ystia/docker/images/playbooks/load_archive.yaml @@ -6,24 +6,17 @@ - name: Load Docker image from tar archive hosts: all tasks: - - name: Get archive PATH defined at runtime - set_fact: - archive_path: "{{RUNTIME_PATH}}" - when: RUNTIME_PATH != "" - name: Get archive PATH property set_fact: archive_path: "{{PATH}}" - when: RUNTIME_PATH == "" - failed_when: RUNTIME_PATH == "" and PATH == "" - - name: Get user defined at runtime - set_fact: - user_loading: "{{RUNTIME_USER}}" - when: RUNTIME_USER != "" + failed_when: PATH == "" - name: Get user property set_fact: user_loading: "{{USER}}" - when: RUNTIME_USER == "" - failed_when: RUNTIME_USER == "" and USER == "" + - name: Default user is root + set_fact: + user_loading: "root" + when: USER == "" - name: Load tar archive docker_image: name: "{{NAME}}" diff --git a/org/ystia/docker/images/types.yml b/org/ystia/docker/images/types.yml index 9b4a4f2f..f63db350 100644 --- a/org/ystia/docker/images/types.yml +++ b/org/ystia/docker/images/types.yml @@ -18,7 +18,7 @@ imports: - yorc-types:1.1.0 node_types: - org.ystia.docker.images.Loader: + org.ystia.docker.images.ArchiveLoader: derived_from: tosca.nodes.SoftwareComponent description: Loads a docker image from a tar archive properties: @@ -26,11 +26,7 @@ node_types: type: string description: > Path to the tar archive (can be compressed with gzip, bzip2, or xz)). - Either this property 'path' should be provided, or this Loader component - should be in relationship with an ArchiveProvider providing in its attribute - 'path', the path to an archive computed at runtime. - The ArchiveProvider 'path' attribute takes precedence over the Loader 'path' property. - required: false + required: true name: type: string description: > @@ -70,18 +66,43 @@ node_types: # Empty if no name was specified or if the image was already loaded # and force_load is set to false repo_tags: { get_operation_output: [SELF, Standard, start, REPO_TAGS] } + interfaces: + Standard: + inputs: + PATH: { get_property: [SELF, path] } + USER: { get_property: [SELF, user] } + NAME: { get_property: [SELF, name] } + TAG: { get_property: [SELF, tag] } + REPOSITORY: { get_property: [SELF, repository] } + PUSH: { get_property: [SELF, push] } + # Install dependencies + create: + implementation: playbooks/create.yaml + # load the image from the tar archive + start: + inputs: + FORCE_LOAD: { get_property: [SELF, force_load] } + implementation: playbooks/load_archive.yaml + # Remove the image from docker registry + stop: + inputs: + REPO_TAGS: { get_attribute: [SELF, repo_tags] } + implementation: playbooks/remove_image.yaml + org.ystia.docker.images.RuntimePathArchiveLoader: + derived_from: org.ystia.docker.images.ArchiveLoader + description: > + Loads a docker image from a tar archive whose path is provided at runtime + by a component implementing capability org.ystia.docker.images.relationships.ArchiveProvider requirements: - archive_provider: capability: org.ystia.docker.images.capabilities.ArchiveProvider relationship: org.ystia.docker.images.relationships.ArchiveProvider - occurrences: [0, 1] + occurrences: [1, 1] interfaces: Standard: inputs: - PATH: { get_property: [SELF, path] } - RUNTIME_PATH: {get_attribute: [REQ_TARGET, archive_provider, path]} - USER: { get_property: [SELF, user] } - RUNTIME_USER: {get_attribute: [REQ_TARGET, archive_provider, user]} + PATH: {get_attribute: [REQ_TARGET, archive_provider, path]} + USER: {get_attribute: [REQ_TARGET, archive_provider, user]} NAME: { get_property: [SELF, name] } TAG: { get_property: [SELF, tag] } REPOSITORY: { get_property: [SELF, repository] } From dd91ce6dd7042986ade7aeead9df0f8d216f9fbc Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Tue, 9 Mar 2021 16:54:56 +0000 Subject: [PATCH 08/15] Fixed an install issue on CentOS 7 --- .../ansible/playbooks/consul_install.yaml | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/org/ystia/experimental/consul/linux/ansible/playbooks/consul_install.yaml b/org/ystia/experimental/consul/linux/ansible/playbooks/consul_install.yaml index ef773ccd..5523697a 100644 --- a/org/ystia/experimental/consul/linux/ansible/playbooks/consul_install.yaml +++ b/org/ystia/experimental/consul/linux/ansible/playbooks/consul_install.yaml @@ -65,16 +65,28 @@ register: pri failed_when: "pri == None or pri.python_version == None or pri.python_version == ''" + - name: Get python major version + set_fact: + python_major_version: "{{pri.python_version | replace('\n', '') | regex_replace('^(\\d+).*', '\\1') }}" + - name: Set python version for pip set_fact: python_pip_pkg: "python3-pip" pip_cmd: "pip3" - + when: python_major_version != "2" - name: Set python 2 version for pip set_fact: python_pip_pkg: "python-pip" pip_cmd: "pip" - when: pri.python_version is version('3', '<') + when: python_major_version == "2" + + - name: RedHat - install epel-release + yum: + name: + - epel-release + state: present + update_cache: yes + when: ansible_os_family == 'RedHat' - name: RedHat - install prerequisites yum: @@ -101,6 +113,13 @@ name: "pip" state: latest executable: "{{pip_cmd}}" + when: python_major_version != "2" + - name: Install latest Pip version compatible with python 2 + pip: + name: "pip<21.0" + state: latest + executable: "{{pip_cmd}}" + when: python_major_version == "2" - name: Copy python requirements copy: From f58a32240f7755ee0947d87522ce02fe09faa436 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Wed, 10 Mar 2021 13:16:23 +0000 Subject: [PATCH 09/15] Added an abstract component for common properties --- org/ystia/docker/images/types.yml | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/org/ystia/docker/images/types.yml b/org/ystia/docker/images/types.yml index f63db350..ca865226 100644 --- a/org/ystia/docker/images/types.yml +++ b/org/ystia/docker/images/types.yml @@ -18,15 +18,11 @@ imports: - yorc-types:1.1.0 node_types: - org.ystia.docker.images.ArchiveLoader: + org.ystia.docker.images.pub.ArchiveLoader: derived_from: tosca.nodes.SoftwareComponent - description: Loads a docker image from a tar archive + abstract: true + description: Loads a docker image from a tar archive (abstract) properties: - path: - type: string - description: > - Path to the tar archive (can be compressed with gzip, bzip2, or xz)). - required: true name: type: string description: > @@ -66,6 +62,15 @@ node_types: # Empty if no name was specified or if the image was already loaded # and force_load is set to false repo_tags: { get_operation_output: [SELF, Standard, start, REPO_TAGS] } + org.ystia.docker.images.ArchiveLoader: + derived_from: org.ystia.docker.images.pub.ArchiveLoader + description: Loads a docker image from a tar archive + properties: + path: + type: string + description: > + Path to the tar archive (can be compressed with gzip, bzip2, or xz)). + required: true interfaces: Standard: inputs: @@ -89,7 +94,7 @@ node_types: REPO_TAGS: { get_attribute: [SELF, repo_tags] } implementation: playbooks/remove_image.yaml org.ystia.docker.images.RuntimePathArchiveLoader: - derived_from: org.ystia.docker.images.ArchiveLoader + derived_from: org.ystia.docker.images.pub.ArchiveLoader description: > Loads a docker image from a tar archive whose path is provided at runtime by a component implementing capability org.ystia.docker.images.relationships.ArchiveProvider From 83458c0696a7b17801cb303573292d3c965ba412 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Thu, 12 Aug 2021 08:56:31 +0000 Subject: [PATCH 10/15] Fixed issues with docker repositories --- org/ystia/docker/ansible/playbooks/create.yaml | 9 +++++++-- org/ystia/docker/ansible/types.yml | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/org/ystia/docker/ansible/playbooks/create.yaml b/org/ystia/docker/ansible/playbooks/create.yaml index 10878caf..b010cb89 100644 --- a/org/ystia/docker/ansible/playbooks/create.yaml +++ b/org/ystia/docker/ansible/playbooks/create.yaml @@ -12,13 +12,18 @@ - set_fact: # The baseurl of the RedHat/CentOS docker repository. - redhat_docker_repository: "{{ REPOSITORY_URL if (REPOSITORY_URL != '') else ('https://download.docker.com/linux/centos/7/$basearch/stable') }}" + redhat_docker_repository: "{{ REPOSITORY_URL }}" redhat_docker_gpgkey: "{{ DOCKER_GPGKEY if (DOCKER_GPGKEY != '') else ('https://download.docker.com/linux/centos/gpg') }}" # The baseurl of the Debian/Ubuntu docker repository. - debian_docker_repository: "{{ REPOSITORY_URL if (REPOSITORY_URL != '') else ('deb https://download.docker.com/linux/ubuntu xenial stable') }}" + debian_docker_repository: "{{ REPOSITORY_URL }}" debian_docker_gpgkey: "{{ DOCKER_GPGKEY if (DOCKER_GPGKEY != '') else ('https://download.docker.com/linux/ubuntu/gpg') }}" docker_package_name: "docker-ce{{ '-' if (DOCKER_VERSION != '') else ('') }}{{ DOCKER_VERSION if (DOCKER_VERSION != '') else ('') }}" + - set_fact: + redhat_docker_repository: "https://download.docker.com/linux/centos/{{ ansible_distribution_major_version }}/$basearch/stable" + debian_docker_repository: "deb https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + when: REPOSITORY_URL == "" + - name: Add Docker Yum repository yum_repository: name: docker diff --git a/org/ystia/docker/ansible/types.yml b/org/ystia/docker/ansible/types.yml index a7809af0..43a407df 100644 --- a/org/ystia/docker/ansible/types.yml +++ b/org/ystia/docker/ansible/types.yml @@ -11,6 +11,7 @@ metadata: template_version: 3.0.0-SNAPSHOT template_author: Ystia +description: Docker types imports: - tosca-normative-types:1.0.0-ALIEN20 - yorc-types:1.1.0 From 50d65de3d71ac6aaeac4653cd86f74eb5be8fced Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Thu, 12 Aug 2021 08:58:54 +0000 Subject: [PATCH 11/15] Fixed issues with container stop/deletion --- .../containers/generic/playbooks/create.yaml | 7 +++++- .../containers/generic/playbooks/delete.yaml | 8 +++++- .../playbooks/docker_container_tasks.yaml | 1 + .../containers/generic/playbooks/start.yaml | 25 ++++++++++++++++--- .../containers/generic/playbooks/stop.yaml | 6 +++++ org/ystia/docker/containers/generic/types.yml | 5 +++- 6 files changed, 45 insertions(+), 7 deletions(-) diff --git a/org/ystia/docker/containers/generic/playbooks/create.yaml b/org/ystia/docker/containers/generic/playbooks/create.yaml index c502d4b2..eb259fc8 100644 --- a/org/ystia/docker/containers/generic/playbooks/create.yaml +++ b/org/ystia/docker/containers/generic/playbooks/create.yaml @@ -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: @@ -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 diff --git a/org/ystia/docker/containers/generic/playbooks/delete.yaml b/org/ystia/docker/containers/generic/playbooks/delete.yaml index 01d192b4..24dc40a5 100644 --- a/org/ystia/docker/containers/generic/playbooks/delete.yaml +++ b/org/ystia/docker/containers/generic/playbooks/delete.yaml @@ -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 != "" diff --git a/org/ystia/docker/containers/generic/playbooks/docker_container_tasks.yaml b/org/ystia/docker/containers/generic/playbooks/docker_container_tasks.yaml index 1aaf7162..601f5b16 100644 --- a/org/ystia/docker/containers/generic/playbooks/docker_container_tasks.yaml +++ b/org/ystia/docker/containers/generic/playbooks/docker_container_tasks.yaml @@ -30,6 +30,7 @@ name: "{{NODE}}-{{INSTANCE}}" auto_remove: "{{AUTO_REMOVE}}" cleanup: "{{CLEANUP}}" + command: "{{COMMAND}}" # cpus: "{{CPU_SHARE}}" detach: "{{DETACH}}" env: "{{DOCKER_ENV}}" diff --git a/org/ystia/docker/containers/generic/playbooks/start.yaml b/org/ystia/docker/containers/generic/playbooks/start.yaml index 12ec1332..eaa9dfd0 100644 --- a/org/ystia/docker/containers/generic/playbooks/start.yaml +++ b/org/ystia/docker/containers/generic/playbooks/start.yaml @@ -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: {} @@ -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 + diff --git a/org/ystia/docker/containers/generic/playbooks/stop.yaml b/org/ystia/docker/containers/generic/playbooks/stop.yaml index f8d6460a..fa3143e6 100644 --- a/org/ystia/docker/containers/generic/playbooks/stop.yaml +++ b/org/ystia/docker/containers/generic/playbooks/stop.yaml @@ -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 != "" diff --git a/org/ystia/docker/containers/generic/types.yml b/org/ystia/docker/containers/generic/types.yml index 02a05039..ee4fabda 100644 --- a/org/ystia/docker/containers/generic/types.yml +++ b/org/ystia/docker/containers/generic/types.yml @@ -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 @@ -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] } From 913a1ce6d93ffdd562f360c3650877d371afa19b Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Thu, 12 Aug 2021 09:00:11 +0000 Subject: [PATCH 12/15] Removed unused options --- org/ystia/docker/images/playbooks/create.yaml | 17 ++++------------- .../docker/images/playbooks/load_archive.yaml | 9 +-------- .../docker/images/playbooks/remove_image.yaml | 13 ++----------- org/ystia/docker/images/types.yml | 18 ++++++++++++------ 4 files changed, 19 insertions(+), 38 deletions(-) diff --git a/org/ystia/docker/images/playbooks/create.yaml b/org/ystia/docker/images/playbooks/create.yaml index a0cb038e..8898206e 100644 --- a/org/ystia/docker/images/playbooks/create.yaml +++ b/org/ystia/docker/images/playbooks/create.yaml @@ -50,21 +50,12 @@ state: latest executable: "{{pip_cmd}}" when: python_major_version == "2" - - name: Get user defined at runtime - set_fact: - user_loading: "{{RUNTIME_USER}}" - when: RUNTIME_USER != "" - - name: Get user property - set_fact: - user_loading: "{{USER}}" - when: RUNTIME_USER == "" - failed_when: RUNTIME_USER == "" and USER == "" - - name: Add to docker group + - name: Add user to docker group user: - name: "{{user_loading}}" + name: "{{USER}}" groups: docker append: yes - when: user_loading != "root" + when: USER != "root" - name: Install 'docker' python package pip: name: docker @@ -72,6 +63,6 @@ # see issue https://github.com/pypa/pip/issues/5247 extra_args: --user executable: "{{pip_cmd}}" - become_user: "{{user_loading}}" + become_user: "{{USER}}" become_method: sudo diff --git a/org/ystia/docker/images/playbooks/load_archive.yaml b/org/ystia/docker/images/playbooks/load_archive.yaml index 88a1f4b2..590d0737 100644 --- a/org/ystia/docker/images/playbooks/load_archive.yaml +++ b/org/ystia/docker/images/playbooks/load_archive.yaml @@ -10,13 +10,6 @@ set_fact: archive_path: "{{PATH}}" failed_when: PATH == "" - - name: Get user property - set_fact: - user_loading: "{{USER}}" - - name: Default user is root - set_fact: - user_loading: "root" - when: USER == "" - name: Load tar archive docker_image: name: "{{NAME}}" @@ -27,7 +20,7 @@ source: load force_source: "{{FORCE_LOAD}}" become: true - become_user: "{{user_loading}}" + become_user: "{{USER}}" become_method: sudo register: result - name: Fail if the image specified does not correpond to the archive specified diff --git a/org/ystia/docker/images/playbooks/remove_image.yaml b/org/ystia/docker/images/playbooks/remove_image.yaml index 26475bdc..976cf03e 100644 --- a/org/ystia/docker/images/playbooks/remove_image.yaml +++ b/org/ystia/docker/images/playbooks/remove_image.yaml @@ -12,21 +12,12 @@ - name: "Get image {{NAME}} in regsitry" set_fact: images: "{{REPO_TAGS | from_json}}" - - name: Get user defined at runtime - set_fact: - user_loading: "{{RUNTIME_USER}}" - when: RUNTIME_USER != "" - - name: Get user property - set_fact: - user_loading: "{{USER}}" - when: RUNTIME_USER == "" - failed_when: RUNTIME_USER == "" and USER == "" - name: "Remove image {{NAME}} {{TAG}}" docker_image: name: "{{images[0]}}" state: absent when: images | length == 1 - become_user: "{{user_loading}}" + become_user: "{{USER}}" become_method: sudo - name: "Remove image {{NAME}} {{TAG}}" docker_image: @@ -35,5 +26,5 @@ repository: "{{REPOSITORY}}" state: absent when: images | length != 1 - become_user: "{{user_loading}}" + become_user: "{{USER}}" become_method: sudo diff --git a/org/ystia/docker/images/types.yml b/org/ystia/docker/images/types.yml index ca865226..168ae017 100644 --- a/org/ystia/docker/images/types.yml +++ b/org/ystia/docker/images/types.yml @@ -11,7 +11,7 @@ metadata: template_version: 3.0.0-SNAPSHOT template_author: Ystia - +description: Docker image types imports: - tosca-normative-types:1.0.0-ALIEN20 @@ -57,11 +57,6 @@ node_types: description: Perform the operation as this existing user default: root required: false - attributes: - # Array of repository name:tag in registry for the specified name. - # Empty if no name was specified or if the image was already loaded - # and force_load is set to false - repo_tags: { get_operation_output: [SELF, Standard, start, REPO_TAGS] } org.ystia.docker.images.ArchiveLoader: derived_from: org.ystia.docker.images.pub.ArchiveLoader description: Loads a docker image from a tar archive @@ -71,6 +66,11 @@ node_types: description: > Path to the tar archive (can be compressed with gzip, bzip2, or xz)). required: true + attributes: + # Array of repository name:tag in registry for the specified name. + # Empty if no name was specified or if the image was already loaded + # and force_load is set to false + repo_tags: { get_operation_output: [SELF, Standard, start, REPO_TAGS] } interfaces: Standard: inputs: @@ -103,6 +103,11 @@ node_types: capability: org.ystia.docker.images.capabilities.ArchiveProvider relationship: org.ystia.docker.images.relationships.ArchiveProvider occurrences: [1, 1] + attributes: + # Array of repository name:tag in registry for the specified name. + # Empty if no name was specified or if the image was already loaded + # and force_load is set to false + repo_tags: { get_operation_output: [SELF, Standard, start, REPO_TAGS] } interfaces: Standard: inputs: @@ -137,6 +142,7 @@ capability_types: user: type: string description: User having access to this archive + default: root relationship_types: org.ystia.docker.images.relationships.ArchiveProvider: derived_from: tosca.relationships.DependsOn From a15243f551ba91b6fa7d7d4b49167ff3502e0b03 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Thu, 12 Aug 2021 09:09:34 +0000 Subject: [PATCH 13/15] Updated changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25fd334e..1b8a0e8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## UNRELEASED +### ENHANCEMENTS + +* Add a TOSCA component to load a docker image from a tar archive ([GH-140](https://github.com/ystia/forge/issues/140)) + ### BUG FIXES * Docker installation requires apt-transport-https to work properly on Debian based systems ([GH-147](https://github.com/ystia/forge/issues/147)) From d68a0c9552abd56b82cc3a840c9e84d575c69f23 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Thu, 12 Aug 2021 16:49:42 +0000 Subject: [PATCH 14/15] Add retries to apt --- org/ystia/docker/containers/generic/playbooks/create.yaml | 4 ++++ org/ystia/docker/images/playbooks/create.yaml | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/org/ystia/docker/containers/generic/playbooks/create.yaml b/org/ystia/docker/containers/generic/playbooks/create.yaml index eb259fc8..325c9a84 100644 --- a/org/ystia/docker/containers/generic/playbooks/create.yaml +++ b/org/ystia/docker/containers/generic/playbooks/create.yaml @@ -42,6 +42,10 @@ - "{{ python_pip_pkg }}" state: present update_cache: yes + register: apt_res + retries: 5 + delay: 15 + until: apt_res is success when: ansible_os_family == 'Debian' - name: Install latest Pip version pip: diff --git a/org/ystia/docker/images/playbooks/create.yaml b/org/ystia/docker/images/playbooks/create.yaml index 8898206e..59c62aeb 100644 --- a/org/ystia/docker/images/playbooks/create.yaml +++ b/org/ystia/docker/images/playbooks/create.yaml @@ -37,6 +37,10 @@ - "{{ python_pip_pkg }}" state: present update_cache: yes + register: apt_res + retries: 5 + delay: 15 + until: apt_res is success when: ansible_os_family == 'Debian' - name: Install latest Pip version pip: From 57cd842593ce0a3d8ecc95f1e4a73903b962c4d8 Mon Sep 17 00:00:00 2001 From: Laurent Ganne Date: Tue, 14 Dec 2021 19:50:55 +0000 Subject: [PATCH 15/15] Added opional hostname and network_mode properties --- .../generic/playbooks/docker_container_tasks.yaml | 2 ++ org/ystia/docker/containers/generic/types.yml | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/org/ystia/docker/containers/generic/playbooks/docker_container_tasks.yaml b/org/ystia/docker/containers/generic/playbooks/docker_container_tasks.yaml index 601f5b16..a6c25365 100644 --- a/org/ystia/docker/containers/generic/playbooks/docker_container_tasks.yaml +++ b/org/ystia/docker/containers/generic/playbooks/docker_container_tasks.yaml @@ -35,10 +35,12 @@ detach: "{{DETACH}}" env: "{{DOCKER_ENV}}" exposed_ports: "{{DOCKER_EXP_PORTS}}" + hostname: "{{HOSTNAME}}" image: "{{IMAGE}}" keep_volumes: "{{KEEP_VOLUMES}}" memory: "{{MEM_SHARE_LIMIT}}" memory_reservation: "{{MEM_SHARE}}" + network_mode: "{{NETWORK_MODE}}" published_ports: "{{DOCKER_PUB_PORT}}" restart_policy: "{{RESTART_POLICY}}" shm_size: "{{SHM_SIZE}}" diff --git a/org/ystia/docker/containers/generic/types.yml b/org/ystia/docker/containers/generic/types.yml index ee4fabda..0b1b0ed1 100644 --- a/org/ystia/docker/containers/generic/types.yml +++ b/org/ystia/docker/containers/generic/types.yml @@ -47,6 +47,10 @@ node_types: List of additional container ports which informs Docker that the container listens on the specified network ports at runtime. If the port is already exposed using EXPOSE in a Dockerfile, it does not need to be exposed again. required: false + hostname: + type: string + description: Container hostname + required: false image: type: string description: > @@ -59,6 +63,13 @@ node_types: description: > Retain volumes associated with a removed container. default: true + network_mode: + type: string + description: > + Connect the container to a network. + Possible values: bridge, none, host, | + or container: to reuse another container's network stack + required: false published_ports: type: list entry_schema: @@ -116,10 +127,12 @@ node_types: DETACH: { get_property: [SELF, detach] } ENV_VARS: { get_property: [SELF, docker_env_vars] } EXPOSED_PORTS: { get_property: [SELF, exposed_ports] } + HOSTNAME: { get_property: [SELF, hostname] } IMAGE: { get_property: [SELF, image] } KEEP_VOLUMES: { get_property: [SELF, keep_volumes] } MEM_SHARE: { get_property: [SELF, mem_share] } MEM_SHARE_LIMIT: { get_property: [SELF, mem_share_limit] } + NETWORK_MODE: { get_property: [SELF, network_mode] } PUBLISHED_PORTS: { get_property: [SELF, published_ports] } RESTART_POLICY: { get_property: [SELF, restart_policy] } SHM_SIZE: { get_property: [SELF, shm_size] }