From d1ecdcda9e11bb3458017ef53b4bb6816dbcb44a Mon Sep 17 00:00:00 2001
From: fejao <mail@fejao.de>
Date: Sat, 25 Jan 2025 13:57:37 +0000
Subject: [PATCH] Adding role docker images

---
 README.md                                     |  3 +-
 .../group_vars/example_servers_group.yml      | 14 +++
 inventories/host_vars/example_server.yml      |  8 ++
 playbook_example_03_clone_repositories.yml    |  1 -
 playbook_example_04_docker_images.yml         |  7 ++
 roles/clone_repositories/meta/main.yml        | 32 +------
 roles/dependencies/meta/main.yml              |  2 +-
 roles/docker_images/README.md                 | 96 +++++++++++++++++++
 roles/docker_images/defaults/main.yml         |  6 ++
 roles/docker_images/meta/main.yml             | 15 +++
 .../docker_images/tasks/docker_image_app.yml  | 27 ++++++
 .../docker_images/tasks/docker_image_cups.yml | 32 +++++++
 .../tasks/docker_image_proxy.yml              | 27 ++++++
 roles/docker_images/tasks/main.yml            |  6 ++
 14 files changed, 246 insertions(+), 30 deletions(-)
 create mode 100644 playbook_example_04_docker_images.yml
 create mode 100644 roles/docker_images/README.md
 create mode 100644 roles/docker_images/defaults/main.yml
 create mode 100644 roles/docker_images/meta/main.yml
 create mode 100644 roles/docker_images/tasks/docker_image_app.yml
 create mode 100644 roles/docker_images/tasks/docker_image_cups.yml
 create mode 100644 roles/docker_images/tasks/docker_image_proxy.yml
 create mode 100644 roles/docker_images/tasks/main.yml

diff --git a/README.md b/README.md
index 02bb93b..67c41dd 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@
 - [ ] finish the roles for deploying the **c3InfoDesk Printer**
     - [x] dependencies
     - [x] clone_repositories
-    - [ ] docker_images
+    - [x] docker_images
     - [ ] docker_containers
 - [ ] set ansible-lint
 
@@ -22,6 +22,7 @@ Here are the used roles:
 
 - [dependencies](roles/dependencies/README.md)
 - [clone_repositories](roles/clone_repositories/README.md)
+- [docker_images](roles/docker_images/README.md)
 
 
 ## License
diff --git a/inventories/group_vars/example_servers_group.yml b/inventories/group_vars/example_servers_group.yml
index e8051e2..1dc5843 100644
--- a/inventories/group_vars/example_servers_group.yml
+++ b/inventories/group_vars/example_servers_group.yml
@@ -11,3 +11,17 @@ var_setted_only_group_vars: "MESSAGE FROM GROUP_VARS ONLY"
 ### roles/clone_repositories
 ###
 c3infodesk_printer_folder: "Coding/c3infodesk-printer"
+
+###
+### roles/docker_images
+###
+c3infodesk_printer_folder_docker: "Coding/c3infodesk-printer/docker"
+c3infodesk_printer_folder_docker_cups: "Coding/c3infodesk-printer/docker/cups"
+c3infodesk_printer_folder_docker_app: "Coding/c3infodesk-printer/docker/app"
+c3infodesk_printer_folder_docker_proxy: "Coding/c3infodesk-printer/docker/proxy"
+c3infodesk_printer_docker_img_cups_name: "c3printing-cups"
+c3infodesk_printer_docker_img_cups_tag: "latest"
+c3infodesk_printer_docker_img_app_name: "c3printing-app"
+c3infodesk_printer_docker_img_app_tag: "latest"
+c3infodesk_printer_docker_img_proxy_name: "c3printing-proxy"
+c3infodesk_printer_docker_img_proxy_tag: "latest"
diff --git a/inventories/host_vars/example_server.yml b/inventories/host_vars/example_server.yml
index 23035ee..0e1e96d 100644
--- a/inventories/host_vars/example_server.yml
+++ b/inventories/host_vars/example_server.yml
@@ -20,3 +20,11 @@ docker_users: [test-user]
 ###
 # c3infodesk_printer_repo_clone: true
 c3infodesk_printer_repo_new_branch_name: "test_branch"
+
+###
+### roles/docker_images
+###
+# Set this variables, only if you need to debug creating the docker images
+# c3infodesk_printer_debug_create_image_cups: true
+# c3infodesk_printer_debug_create_image_app: true
+# c3infodesk_printer_debug_create_image_proxy: true
diff --git a/playbook_example_03_clone_repositories.yml b/playbook_example_03_clone_repositories.yml
index d9aff5b..b6b3636 100644
--- a/playbook_example_03_clone_repositories.yml
+++ b/playbook_example_03_clone_repositories.yml
@@ -4,5 +4,4 @@
 - hosts:
   - example_servers_group
   roles:
-    # - dependencies
     - clone_repositories
diff --git a/playbook_example_04_docker_images.yml b/playbook_example_04_docker_images.yml
new file mode 100644
index 0000000..0a2c733
--- /dev/null
+++ b/playbook_example_04_docker_images.yml
@@ -0,0 +1,7 @@
+---
+# Runs example for
+
+- hosts:
+  - example_servers_group
+  roles:
+    - docker_images
diff --git a/roles/clone_repositories/meta/main.yml b/roles/clone_repositories/meta/main.yml
index 74aa718..6de0edd 100644
--- a/roles/clone_repositories/meta/main.yml
+++ b/roles/clone_repositories/meta/main.yml
@@ -1,33 +1,11 @@
 galaxy_info:
-  author: your name
-  description: your role description
-  company: your company (optional)
-
-  # If the issue tracker for your role is not on github, uncomment the
-  # next line and provide a value
-  # issue_tracker_url: http://example.com/issue/tracker
-
-  # Choose a valid license ID from https://spdx.org - some suggested licenses:
-  # - BSD-3-Clause (default)
-  # - MIT
-  # - GPL-2.0-or-later
-  # - GPL-3.0-only
-  # - Apache-2.0
-  # - CC-BY-4.0
+  role_name: clone_repositories
+  description: Role to clone the repositories used from c3InfoDesk sytems
+  author: fejao
+  company: cccv.de
   license: license (GPL-2.0-or-later, MIT, etc)
-
-  min_ansible_version: 2.1
-
-  # If this a Container Enabled role, provide the minimum Ansible Container version.
-  # min_ansible_container_version:
-
+  min_ansible_version: 2.10
   galaxy_tags: []
-    # List tags for your role here, one per line. A tag is a keyword that describes
-    # and categorizes the role. Users find roles by searching for tags. Be sure to
-    # remove the '[]' above, if you add tags to this list.
-    #
-    # NOTE: A tag is limited to a single word comprised of alphanumeric characters.
-    #       Maximum 20 tags per role.
 
 # dependencies: []
   # List your role dependencies here, one per line. Be sure to remove the '[]' above,
diff --git a/roles/dependencies/meta/main.yml b/roles/dependencies/meta/main.yml
index 1860186..1fbb987 100644
--- a/roles/dependencies/meta/main.yml
+++ b/roles/dependencies/meta/main.yml
@@ -4,7 +4,7 @@ galaxy_info:
   author: fejao
   company: cccv.de
   license: license (GPL-2.0-or-later, MIT, etc)
-  min_ansible_version: 2.1
+  min_ansible_version: 2.10
   galaxy_tags: []
 
 dependencies: []
diff --git a/roles/docker_images/README.md b/roles/docker_images/README.md
new file mode 100644
index 0000000..836b1ef
--- /dev/null
+++ b/roles/docker_images/README.md
@@ -0,0 +1,96 @@
+# docker_images
+
+This ansible-role for dealing with creating the docker images necessary for deploying the system used from **c3infodesk**.
+
+## Requirements
+- To have [ansible](https://docs.ansible.com/ansible/latest/installation_guide/installation_distros.html) installed at your system.
+- Set your **remote_user** configuration at the **ansible.cfg** file.
+- Set your **inventories/host_vars/<TARGET_NAME>** file.
+
+## Role Variables
+Here are the variables setted:
+
+- ### From: inventories/host_vars/<TARGET_NAME>
+  Here are the variables that you should be changing for your own deployment.
+  - **c3infodesk_printer_debug_create_image_cups**: DEFAULT=<NOT_SETTED>
+    - If you want to debug the output from creating the image for CUPS
+  - **c3infodesk_printer_debug_create_image_app**: DEFAULT=<NOT_SETTED>
+    - If you want to debug the output from creating the image for the APP
+  - **c3infodesk_printer_debug_create_image_proxy**: DEFAULT=<NOT_SETTED>
+    - If you want to debug the output from creating the image for the PROXY
+
+- ### From: inventories/group_vars/<TARGET_GROUP>
+  Please don't cahnge the default values at this file. If you need to update them, use the **inventories/host_vars/<TARGET_NAME>** file.
+  - **c3infodesk_printer_folder_docker**: DEFAULT="Coding/c3infodesk-printer/docker"
+    - The folder where the configuration from docker used for the system
+  - **c3infodesk_printer_folder_docker_cups**: DEFAULT="Coding/c3infodesk-printer/docker/cups"
+    - The folder where the configuration from docker CUPS used for the system
+  - **c3infodesk_printer_folder_docker_app**: DEFAULT="Coding/c3infodesk-printer/docker/app"
+    - The folder where the configuration from docker APP used for the system
+  - **c3infodesk_printer_folder_docker_proxy**: DEFAULT="Coding/c3infodesk-printer/docker/proxy"
+    - The folder where the configuration from docker PROXY used for the system
+  - **c3infodesk_printer_docker_img_cups_name**: DEFAULT="c3printing-cups"
+    - The name of the image to be set for the CUPS
+  - **c3infodesk_printer_docker_img_cups_tag**: DEFAULT="latest"
+    - The tag name/number to be setted for the CUPS image
+  - **c3infodesk_printer_docker_img_app_name**: DEFAULT="c3printing-app"
+    - The name of the image to be set for the APP
+  - **c3infodesk_printer_docker_img_app_tag**: DEFAULT="latest"
+    - The tag name/number to be setted for the APP image
+  - **c3infodesk_printer_docker_img_proxy_name**: DEFAULT="c3printing-proxy"
+    - The name of the image to be set for the PROXY
+  - **c3infodesk_printer_docker_img_proxy_tag**: DEFAULT="latest"
+    - The tag name/number to be setted for the PROXY image
+
+- ### From: roles/clone-repositories/defaults/main.yml
+  Don't change the values at this file, if you need to change them, change it at the **inventories/host_vars/<TARGET_NAME>** file
+  - **c3infodesk_printer_debug_create_image_cups**: DEFAULT=false
+  - **c3infodesk_printer_debug_create_image_app**: DEFAULT=false
+  - **c3infodesk_printer_debug_create_image_proxy**: DEFAULT=false
+
+
+## Dependencies
+The role dependencies are:
+  - [dependencies(role)](roles/dependencies/README.md)
+  - [clone_reposiries](roles/clone_repositories/README.md)
+
+
+## Example Playbook
+
+- ### Using the provided example
+  For using the playbook **playbook_example_04_docker_images.yml** file you only nned to:
+  - Change the user at the **ansible.cfg** file at the field **remote_user** for acessing your target.
+  - Update the file **inventories/host_vars/example_server.yml** and change the value for **ansible_become_pass** from the **remote_user**
+  - Update the **inventories/hosts** file and change the **example_server** with the IP address from your target.
+
+  And run the command:
+```bash
+ansible-playbook -i inventories/hosts playbook_example_04_docker_images.yml
+```
+
+- ### Setting your own example
+  After setting your variables at **inventories/host_vars/<HOST_NAME>**, you can add this role to your playbook as a regular role.
+
+  Here is an example using the **<HOST_NAME>** as **test_servers**
+
+```yaml
+- hosts:
+    - test_servers
+  roles:
+    - docker_images
+```
+
+  And call the playbook as:
+
+```bash
+ansible-playbook -i <INVENTORY> <PLAYBOOK_NAME>.yml
+```
+
+
+## License
+
+MIT / BSD
+
+
+## Author Information
+![fejao logo](../../files/from_authors/fejao_logo_circle.png "fejao") [https://chaos.social/@fejao](https://chaos.social/@fejao)
diff --git a/roles/docker_images/defaults/main.yml b/roles/docker_images/defaults/main.yml
new file mode 100644
index 0000000..9bc853f
--- /dev/null
+++ b/roles/docker_images/defaults/main.yml
@@ -0,0 +1,6 @@
+---
+# defaults file for docker-images
+
+c3infodesk_printer_debug_create_image_cups: false
+c3infodesk_printer_debug_create_image_app: false
+c3infodesk_printer_debug_create_image_proxy: false
diff --git a/roles/docker_images/meta/main.yml b/roles/docker_images/meta/main.yml
new file mode 100644
index 0000000..fcbd3be
--- /dev/null
+++ b/roles/docker_images/meta/main.yml
@@ -0,0 +1,15 @@
+galaxy_info:
+  role_name: docker_images
+  description: Role to clone the repositories used from c3InfoDesk sytems
+  author: fejao
+  company: cccv.de
+  license: license (GPL-2.0-or-later, MIT, etc)
+  min_ansible_version: 2.10
+  galaxy_tags: []
+
+# dependencies: []
+  # List your role dependencies here, one per line. Be sure to remove the '[]' above,
+  # if you add dependencies to this list.
+dependencies:
+  - role: dependencies
+  - role: clone_repositories
diff --git a/roles/docker_images/tasks/docker_image_app.yml b/roles/docker_images/tasks/docker_image_app.yml
new file mode 100644
index 0000000..983fa24
--- /dev/null
+++ b/roles/docker_images/tasks/docker_image_app.yml
@@ -0,0 +1,27 @@
+---
+# Creates the c3infodesk-app docker image
+
+- name: APP | Check if the image already exists on the system
+  community.docker.docker_image_info:
+    name: "{{ c3infodesk_printer_docker_img_app_name }}"
+  register: result_check_image
+
+- name: APP | Check image exists answer
+  ansible.builtin.debug:
+    msg: "Image already exists on target"
+  when: result_check_image.images | length > 0
+
+- name: APP | Build the docker image
+  ansible.builtin.command:
+    cmd: docker build . -t "{{ c3infodesk_printer_docker_img_app_name }}":"{{ c3infodesk_printer_docker_img_app_tag }}" -f docker/app/Dockerfile
+  args:
+    chdir: "{{ c3infodesk_printer_folder }}"
+  register: build_img_result
+  when: result_check_image.images | length <= 0
+
+- name: APP | DEBUG -> Output from creating the docker image
+  ansible.builtin.debug:
+    msg: "{{ build_img_result }}"
+  when:
+  - c3infodesk_printer_debug_create_image_app | bool
+  - build_img_result is defined
\ No newline at end of file
diff --git a/roles/docker_images/tasks/docker_image_cups.yml b/roles/docker_images/tasks/docker_image_cups.yml
new file mode 100644
index 0000000..a2810b2
--- /dev/null
+++ b/roles/docker_images/tasks/docker_image_cups.yml
@@ -0,0 +1,32 @@
+---
+# Creates the c3infodesk-cups docker image
+
+###
+### LINKS
+###
+# https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html
+
+- name: CUPS | Check if the image already exists on the system
+  community.docker.docker_image_info:
+    name: "{{ c3infodesk_printer_docker_img_cups_name }}"
+  register: result_check_image
+
+- name: CUPS | Check image exists answer
+  ansible.builtin.debug:
+    msg: "Image already exists on target"
+  when: result_check_image.images | length > 0
+
+- name: CUPS | Build the docker image
+  ansible.builtin.command:
+    cmd: docker build . -t "{{ c3infodesk_printer_docker_img_cups_name }}":"{{ c3infodesk_printer_docker_img_cups_tag }}" -f docker/cups/Dockerfile
+  args:
+    chdir: "{{ c3infodesk_printer_folder }}"
+  register: build_img_result
+  when: result_check_image.images | length <= 0
+
+- name: CUPS | DEBUG -> Output from creating the docker image
+  ansible.builtin.debug:
+    msg: "{{ build_img_result }}"
+  when:
+  - c3infodesk_printer_debug_create_image_cups | bool
+  - build_img_result is defined
\ No newline at end of file
diff --git a/roles/docker_images/tasks/docker_image_proxy.yml b/roles/docker_images/tasks/docker_image_proxy.yml
new file mode 100644
index 0000000..961a43d
--- /dev/null
+++ b/roles/docker_images/tasks/docker_image_proxy.yml
@@ -0,0 +1,27 @@
+---
+# Creates the c3infodesk-proxy docker image
+
+- name: PROXY | Check if the image already exists on the system
+  community.docker.docker_image_info:
+    name: "{{ c3infodesk_printer_docker_img_proxy_name }}"
+  register: result_check_image
+
+- name: PROXY | Check image exists answer
+  ansible.builtin.debug:
+    msg: "Image already exists on target"
+  when: result_check_image.images | length > 0
+
+- name: PROXY | Build the docker image
+  ansible.builtin.command:
+    cmd: docker build . -t "{{ c3infodesk_printer_docker_img_proxy_name }}":"{{ c3infodesk_printer_docker_img_proxy_tag }}" -f docker/proxy/Dockerfile
+  args:
+    chdir: "{{ c3infodesk_printer_folder }}"
+  register: build_img_result
+  when: result_check_image.images | length <= 0
+
+- name: PROXY | DEBUG -> Output from creating the docker image
+  ansible.builtin.debug:
+    msg: "{{ build_img_result }}"
+  when:
+  - c3infodesk_printer_debug_create_image_cups | bool
+  - build_img_result is defined
\ No newline at end of file
diff --git a/roles/docker_images/tasks/main.yml b/roles/docker_images/tasks/main.yml
new file mode 100644
index 0000000..c925fcb
--- /dev/null
+++ b/roles/docker_images/tasks/main.yml
@@ -0,0 +1,6 @@
+---
+# tasks file for docker-images
+
+- import_tasks: docker_image_cups.yml
+- import_tasks: docker_image_app.yml
+- import_tasks: docker_image_proxy.yml
-- 
GitLab