From 2ab15d5004d46bc5dd1c8fcdd95eda0d9fe7998e Mon Sep 17 00:00:00 2001 From: Antonio Arenzana Pulido Date: Thu, 24 Feb 2022 14:16:10 +0100 Subject: [PATCH] #175 add error handling on deploy playbook --- CHANGELOG.md | 2 ++ dockerhub/Dockerfile.j2 | 7 +----- dockerhub/deploy_images.yml | 42 +++++++++++++++++-------------- dockerhub/execute_role.yml | 12 +++++++++ dockerhub/group_vars/all/main.yml | 1 + dockerhub/main.yml | 37 +++++++++++++++++++-------- 6 files changed, 65 insertions(+), 36 deletions(-) create mode 100644 dockerhub/execute_role.yml create mode 100644 dockerhub/group_vars/all/main.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index a56fd4a..29cbcb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a ch - *[#160](https://github.com/idealista/java_role/issues/160) Remove packer for playbook for the task of pushing images to Dockerhub* @aren-pulid0 - *[#165](https://github.com/idealista/java_role/issues/165) Add JAVA_HOME as ENV in docker image* @aren-pulid0 - *[#167](https://github.com/idealista/java_role/issues/167) Support for multiarch images* @aren-pulid0 +- *[#175](https://github.com/idealista/java_role/issues/175) Error handling on deploy playbook @aren-pulid0 + ## [7.0.2](https://github.com/idealista/java_role/tree/7.0.2) (2021-08-19) [Full Changelog](https://github.com/idealista/java_role/compare/7.0.1...7.0.2) diff --git a/dockerhub/Dockerfile.j2 b/dockerhub/Dockerfile.j2 index d009b0e..c116376 100644 --- a/dockerhub/Dockerfile.j2 +++ b/dockerhub/Dockerfile.j2 @@ -1,12 +1,7 @@ FROM {{ docker_image_base }} -RUN rm -rf /var/lib/apt/lists/* && apt-get update \ - && apt-get install -y sudo systemd init systemd-sysv \ - && apt-get autoremove -y && apt-get clean - -RUN mkdir -p /usr/share/man/man1 -RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && apt-get install -y python3 sudo bash ca-certificates && apt-get clean; \ +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && apt-get install -y python3 sudo bash ca-certificates systemd init systemd-sysv && apt-get clean; \ elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python36 sudo python3-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; \ diff --git a/dockerhub/deploy_images.yml b/dockerhub/deploy_images.yml index 4fac6b2..98d6177 100644 --- a/dockerhub/deploy_images.yml +++ b/dockerhub/deploy_images.yml @@ -1,26 +1,30 @@ --- -- name: Get JAVA_HOME variable - community.docker.docker_container_exec: - container: "{{ 'jdk' + '-' + item }}" - command: grep -i -o 'JAVA_HOME.*' /etc/profile.d/jdk.sh - register: java_home +- name: Commit environment variables into containers + block: + - name: Get JAVA_HOME variable + community.docker.docker_container_exec: + container: "{{ 'jdk' + '-' + item }}" + command: grep -i -o 'JAVA_HOME.*' /etc/profile.d/jdk.sh + register: java_home -- set_fact: java_home="{{ java_home.stdout }}" + - set_fact: java_home="{{ java_home.stdout }}" -- name: Commit container JAVA_HOME variable to image - command: docker commit --change "ENV {{ java_home }}" {{ 'jdk' + '-' + item }} idealista/jdk:{{ docker_tag + '-' + item}} + - name: Commit container JAVA_HOME variable to image + command: docker commit --change "ENV {{ java_home }}" {{ 'jdk' + '-' + item }} idealista/jdk:{{ docker_tag + '-' + item}} -- name: Log into Docker Hub - docker_login: - email: "{{ docker_hub_email }}" - username: "{{ docker_hub_username }}" - password: "{{ docker_hub_password }}" + - name: Log into Docker Hub + docker_login: + email: "{{ docker_hub_email }}" + username: "{{ docker_hub_username }}" + password: "{{ docker_hub_password }}" -- name: Tag and push to Docker Hub - command: docker push idealista/jdk:{{ docker_tag + '-' + item }} + - name: Tag and push to Docker Hub + command: docker push idealista/jdk:{{ docker_tag + '-' + item }} -- name: Remove the container - docker_container: - name: "{{ 'jdk' + '-' + item }}" - state: absent + - name: Remove the container + docker_container: + name: "{{ 'jdk' + '-' + item }}" + state: absent + + when: "failed_architectures is not defined or failed_architectures | join('|') | regex_search(item) != item" \ No newline at end of file diff --git a/dockerhub/execute_role.yml b/dockerhub/execute_role.yml new file mode 100644 index 0000000..cce555b --- /dev/null +++ b/dockerhub/execute_role.yml @@ -0,0 +1,12 @@ +--- + +- name: Execute role + block: + - include_role: + name: java_role + rescue: + - set_fact: + failed_execution: True + - debug: + msg: "The container {{ ansible_nodename }} couldn't execute roles correctly." + \ No newline at end of file diff --git a/dockerhub/group_vars/all/main.yml b/dockerhub/group_vars/all/main.yml new file mode 100644 index 0000000..a8febc9 --- /dev/null +++ b/dockerhub/group_vars/all/main.yml @@ -0,0 +1 @@ +dockerhub_image_architectures: ['amd64', 'arm64'] \ No newline at end of file diff --git a/dockerhub/main.yml b/dockerhub/main.yml index 7bc3ded..b550576 100644 --- a/dockerhub/main.yml +++ b/dockerhub/main.yml @@ -1,7 +1,6 @@ --- - name: Create Dockerfile, images and build containers hosts: localhost - connection: local gather_facts: false tasks: - name: Create Dockerfile @@ -24,29 +23,36 @@ - name: Build containers include_tasks: build_containers.yml - with_items: ['amd64', 'arm64'] + with_items: "{{ dockerhub_image_architectures }}" - name: Execute Java role in Docker containers hosts: jdk_containers - connection: local + connection: localhost + gather_facts: True vars: - java_jdk_vendor: "{{ jdk_vendor if jdk_vendor is defined and jdk_vendor is not sameas None and jdk_vendor != '' else 'openjdk' }}" - java_open_jdk_version: "{{ jdk_version if jdk_version is defined and jdk_version is not sameas None and jdk_version != '' }}" - java_open_jdk_version_major: "{{ jdk_major if jdk_major is defined and jdk_major is not sameas None and jdk_major != ''}}" - roles: - - java_role - + tasks: + - name: Execute role + include_tasks: execute_role.yml - name: Deploy images to DockerHub hosts: localhost connection: local - gather_facts: false + gather_facts: True tasks: + - name: Check failed containers + set_fact: + failed_architectures: "{{ failed_architectures + [ item ] }}" + with_items: "{{ groups['jdk_containers'] }}" + when: "{{ hostvars[item]['failed_execution'] is defined and hostvars[item]['failed_execution'] == True }}" + - name: Deploy images include_tasks: deploy_images.yml - with_items: ['amd64', 'arm64'] + with_items: "{{ dockerhub_image_architectures }}" - name: Deploy multiarch manifest to DockerHub and cleanup @@ -56,12 +62,21 @@ tasks: - name: Create manifest - command: docker manifest create idealista/jdk:{{ docker_tag }} - --amend idealista/jdk:{{ docker_tag + '-amd64' }} - --amend idealista/jdk:{{ docker_tag + '-arm64' }} + command: docker manifest create idealista/jdk:{{ docker_tag }} + {% for architecture in dockerhub_image_architectures %} + {% if failed_architectures is not defined or not failed_architectures | join('|') | regex_search(architecture) %} + --amend idealista/jdk:{{ docker_tag + '-' + architecture }} + {% endif %} + {% endfor %} - name: Push manifest command: docker manifest push idealista/jdk:{{ docker_tag }} - name: Delete builder command: docker buildx rm multiarch + + - name: Example using fail and when together + fail: + msg: Atleast one of the architectures could not be uploaded see logs for more information. + when: failed_architectures is defined and failed_architectures | length != 0 +