From 43a63c703ee84cb6f18b116880bdea672e4bdda6 Mon Sep 17 00:00:00 2001
From: fejao <mail@fejao.de>
Date: Mon, 27 Jan 2025 15:53:06 +0000
Subject: [PATCH] Adding role docker containers

---
 README.md                                     |  24 ++-
 inventories/host_vars/example_server.yml      |  14 ++
 playbook_example_05_docker_containers.yml     |  11 ++
 roles/README.md                               |  13 ++
 roles/docker_containers/README.md             | 159 ++++++++++++++++++
 roles/docker_containers/defaults/main.yml     |  26 +++
 roles/docker_containers/meta/main.yml         |  16 ++
 roles/docker_containers/tasks/main.yml        |  22 +++
 .../tasks/printer_compose_down.yml            |  14 ++
 .../tasks/printer_compose_restart.yml         |  13 ++
 .../tasks/printer_compose_stop.yml            |  14 ++
 .../tasks/printer_compose_up.yml              |  14 ++
 .../tasks/printer_host_cups_disable.yml       |  41 +++++
 roles/docker_images/README.md                 |   2 +-
 14 files changed, 375 insertions(+), 8 deletions(-)
 create mode 100644 playbook_example_05_docker_containers.yml
 create mode 100644 roles/README.md
 create mode 100644 roles/docker_containers/README.md
 create mode 100644 roles/docker_containers/defaults/main.yml
 create mode 100644 roles/docker_containers/meta/main.yml
 create mode 100644 roles/docker_containers/tasks/main.yml
 create mode 100644 roles/docker_containers/tasks/printer_compose_down.yml
 create mode 100644 roles/docker_containers/tasks/printer_compose_restart.yml
 create mode 100644 roles/docker_containers/tasks/printer_compose_stop.yml
 create mode 100644 roles/docker_containers/tasks/printer_compose_up.yml
 create mode 100644 roles/docker_containers/tasks/printer_host_cups_disable.yml

diff --git a/README.md b/README.md
index 67c41dd..7e13159 100644
--- a/README.md
+++ b/README.md
@@ -3,19 +3,17 @@
 
 
 ## WIP
-- [ ] finish the roles for deploying the **c3InfoDesk Printer**
+- [x] Finish the roles for deploying the **c3InfoDesk Printer**.
     - [x] dependencies
     - [x] clone_repositories
     - [x] docker_images
-    - [ ] docker_containers
-- [ ] set ansible-lint
+    - [x] docker_containers
+- [ ] Set ansible-lint for the roles.
 
----
 
 ## What is this?
-This is a repo for deploying the the printer system used at the c3infodesk for printing EXTREMILY NECESSARY documents
+This is a repo for deploying the the printer system used at the c3infodesk for printing EXTREMILY NECESSARY documents.
 
----
 
 ## Roles
 Here are the used roles:
@@ -23,7 +21,19 @@ Here are the used roles:
 - [dependencies](roles/dependencies/README.md)
 - [clone_repositories](roles/clone_repositories/README.md)
 - [docker_images](roles/docker_images/README.md)
-
+- [docker_containers](roles/docker_containers/README.md)
+
+## How to use this
+- 1. Clone this repo.
+- 2. Edit the files.
+    - Set your **remote_user** configuration at the **ansible.cfg** file.
+    - Set your **inventories/host_vars/<TARGET_NAME>** file.
+- 3. Run the playbooks
+    - Example for deploying the containers at the target:
+    ```bash
+    ansible-playbook -i inventories/hosts playbook_example_05_docker_containers.yml
+    ```
+- 4. Change the default environment variables setted from the [docker-compose.yml](https://git.cccv.de/fejao/c3infodesk-printer/-/blob/main/docker/docker-compose.yml) file to your needs and recreate the containers.
 
 ## License
 
diff --git a/inventories/host_vars/example_server.yml b/inventories/host_vars/example_server.yml
index 0e1e96d..18dbf74 100644
--- a/inventories/host_vars/example_server.yml
+++ b/inventories/host_vars/example_server.yml
@@ -28,3 +28,17 @@ c3infodesk_printer_repo_new_branch_name: "test_branch"
 # c3infodesk_printer_debug_create_image_cups: true
 # c3infodesk_printer_debug_create_image_app: true
 # c3infodesk_printer_debug_create_image_proxy: true
+
+###
+### roles/docker_containers
+###
+
+### CUPS
+# c3infodesk_printer_host_cups_disable: true
+# c3infodesk_printer_host_cups_mask: true
+
+### COMPOSE
+# c3infodesk_printer_docker_compose_up: true
+# c3infodesk_printer_docker_compose_stop: true
+# c3infodesk_printer_docker_compose_restart: true
+# c3infodesk_printer_docker_compose_down: true
diff --git a/playbook_example_05_docker_containers.yml b/playbook_example_05_docker_containers.yml
new file mode 100644
index 0000000..c6b865c
--- /dev/null
+++ b/playbook_example_05_docker_containers.yml
@@ -0,0 +1,11 @@
+---
+
+- name: Playbook for running the role 'docker_containers'
+  hosts:
+    - example_servers_group
+  roles:
+    - role: docker_containers
+      vars:
+        c3infodesk_printer_host_cups_disable: true
+        c3infodesk_printer_host_cups_mask: true
+        c3infodesk_printer_docker_compose_up: true
diff --git a/roles/README.md b/roles/README.md
new file mode 100644
index 0000000..88fa728
--- /dev/null
+++ b/roles/README.md
@@ -0,0 +1,13 @@
+# c3InfoDesk Deployment Roles
+
+
+
+## What is this?
+The roles used for deploying the [c3InfoDesk Printer](https://git.cccv.de/fejao/c3infodesk-printer)
+
+
+## Roles
+- [dependencies](roles/dependencies/README.md)
+- [clone_repositories](roles/clone_repositories/README.md)
+- [docker_images](roles/docker_images/README.md)
+- [docker_containers](roles/docker_containers/README.md)
diff --git a/roles/docker_containers/README.md b/roles/docker_containers/README.md
new file mode 100644
index 0000000..cf87bd9
--- /dev/null
+++ b/roles/docker_containers/README.md
@@ -0,0 +1,159 @@
+# docker-containers role
+
+This is a ansible-role for dealing with the containers 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.
+  - #### CUPS
+    Here you can set the variables if you want to **disable** and/or **mask** the CUPS service at the target.
+    - **c3infodesk_printer_host_cups_disable**: DEFAULT=<NOT_SETTED>
+    - **c3infodesk_printer_host_cups_mask**: DEFAULT=<NOT_SETTED>
+  - #### COMPOSE
+    Here you can set the variables if you want to set the **docker compose command** to up/stop/restart/down
+    - **c3infodesk_printer_docker_compose_up**: DEFAULT=<NOT_SETTED>
+    - **c3infodesk_printer_docker_compose_stop**: DEFAULT=<NOT_SETTED>
+    - **c3infodesk_printer_docker_compose_restart**: DEFAULT=<NOT_SETTED>
+    - **c3infodesk_printer_docker_compose_down**: DEFAULT=<NOT_SETTED>
+
+- ### 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.
+
+  At the moment, there's **NO VARIABLES** setted at the **group_vars** for this role.
+
+- ### 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
+  - #### CUPS
+    - **c3infodesk_printer_host_cups_disable**: DEFAULT=false
+      - If you want to disable the CUPS service at the target system
+    - **c3infodesk_printer_host_cups_disable_debug**: DEFAULT=false
+      - If you want to debug disabling CUPS at the target system
+    - **c3infodesk_printer_host_cups_mask**: DEFAULT=false
+      - If you want to mask the CUPS service at the target system
+  - #### COMPOSE UP
+    - **c3infodesk_printer_docker_compose_up**: DEFAULT=false
+      - If you want to run the command: "docker compose up" at the target host
+    - **c3infodesk_printer_docker_compose_up_debug**: DEFAULT=false
+      - If you want to debug running the command: "docker compose up" at the target host
+  - #### COMPOSE STOP
+    - **c3infodesk_printer_docker_compose_stop**: DEFAULT=false
+      - If you want to run the command: "docker compose stop" at the target host
+    - **c3infodesk_printer_docker_compose_stop_debug**: DEFAULT=false
+      - If you want to debug running the command: "docker compose stop" at the target host
+  - #### COMPOSE RESTART
+    - **c3infodesk_printer_docker_compose_restart**: DEFAULT=false
+      - If you want to run the command: "docker compose restart" at the target host
+    - **c3infodesk_printer_docker_compose_restart_debug**: DEFAULT=false
+      - If you want to debug running the command: "docker compose restart" at the target host
+  - #### COMPOSE DOWN
+    - **c3infodesk_printer_docker_compose_down**: DEFAULT=false
+      - If you want to run the command: "docker compose down" at the target host
+    - **c3infodesk_printer_docker_compose_down_debug**: DEFAULT=false
+      - If you want to debug running the command: "docker compose down" at the target host
+
+
+## Dependencies
+The role dependencies are:
+  - [dependencies(role)](roles/dependencies/README.md)
+  - [clone_repositories](roles/clone_repositories/README.md)
+  - [docker_images](roles/docker_images/README.md)
+
+
+## Example Playbook
+
+- ### What this example will deploy
+  This role will deploy all the containers setted on the [docker-compose.yml](https://git.cccv.de/fejao/c3infodesk-printer/-/blob/main/docker/docker-compose.yml) file.
+  **PLEASE CHANGE THE VALEUS** setted at this file on the target to your own.
+
+  The **docker-compose.yml** file can be founded at the location setted from the **c3infodesk_printer_folder** path variable. The default is: **"~/Coding/c3infodesk-printer"** folder
+
+- ### Using the provided example
+  For using the playbook **playbook_example_05_docker_containers.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_05_docker_containers.yml
+  ```
+
+- ### Setting your own example
+
+  - #### Deploying a new system 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**
+
+    You will need to set this variables fro a new deployment:
+      - **c3infodesk_printer_host_cups_disable**: true
+      - **c3infodesk_printer_host_cups_mask**: true
+      - **c3infodesk_printer_docker_compose_up**: true
+
+    ```yaml
+    - hosts:
+      - example_servers_group
+      roles:
+        - role: docker_containers
+          vars:
+            c3infodesk_printer_host_cups_disable: true
+            c3infodesk_printer_host_cups_mask: true
+            c3infodesk_printer_docker_compose_up: true
+    ```
+
+    And call the playbook as:
+    ```bash
+    ansible-playbook -i <INVENTORY> <PLAYBOOK_NAME>.yml
+    ```
+
+  - #### Restarting the containers example
+
+    Here is an example for adding this role for your playbook for **restarting** the **c3infodesk-printer** containers:
+
+    We are using the **<HOST_NAME>** as **test_servers** for example.
+
+    ```yaml
+    - hosts:
+        - test_servers
+      roles:
+        - role: docker-containers
+          vars:
+            printer_docker_compose_restart: true
+    ```
+
+    And call the playbook as:
+
+    ```bash
+    ansible-playbook -i <INVENTORY> <PLAYBOOK_NAME>.yml
+    ```
+
+      Or you can also pass it using the **role variables**. So your playbook file should looks like:
+
+    ```yaml
+    - hosts:
+        - test_servers
+      roles:
+        - docker-containers
+    ```
+
+      And call it passing the variable, for example, restarting the containers from **c3infodesk-printer**.
+    ```bash
+    ansible-playbook -i <INVENTORY> <PLAYBOOK_NAME>.yml --extra-vars "printer_docker_compose_restart=true"
+    ```
+
+
+
+## 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_containers/defaults/main.yml b/roles/docker_containers/defaults/main.yml
new file mode 100644
index 0000000..905d5d6
--- /dev/null
+++ b/roles/docker_containers/defaults/main.yml
@@ -0,0 +1,26 @@
+---
+# defaults file for docker-containers
+
+# c3infodesk_project_src: "PLEASE SET THIS VARIABLE"
+
+###
+### c3InfoDesk Printer
+###
+
+### CUPS
+c3infodesk_printer_host_cups_disable: false
+c3infodesk_printer_host_cups_disable_debug: false
+c3infodesk_printer_host_cups_mask: false
+
+### COMPOSE UP
+c3infodesk_printer_docker_compose_up: false
+c3infodesk_printer_docker_compose_up_debug: false
+### COMPOSE STOP
+c3infodesk_printer_docker_compose_stop: false
+c3infodesk_printer_docker_compose_stop_debug: false
+### COMPOSE RESTART
+c3infodesk_printer_docker_compose_restart: false
+c3infodesk_printer_docker_compose_restart_debug: false
+### COMPOSE DOWN
+c3infodesk_printer_docker_compose_down: false
+c3infodesk_printer_docker_compose_down_debug: false
diff --git a/roles/docker_containers/meta/main.yml b/roles/docker_containers/meta/main.yml
new file mode 100644
index 0000000..9831b4f
--- /dev/null
+++ b/roles/docker_containers/meta/main.yml
@@ -0,0 +1,16 @@
+galaxy_info:
+  role_name: docker-containers
+  description: Role to work with the containers for the 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
+  - role: docker_images
diff --git a/roles/docker_containers/tasks/main.yml b/roles/docker_containers/tasks/main.yml
new file mode 100644
index 0000000..b1ca66f
--- /dev/null
+++ b/roles/docker_containers/tasks/main.yml
@@ -0,0 +1,22 @@
+---
+# tasks file for docker-containers
+
+- name: Disabling CUPS on target host
+  ansible.builtin.import_tasks: printer_host_cups_disable.yml
+  when: c3infodesk_printer_host_cups_disable | bool
+
+- name: Running printer compose up
+  ansible.builtin.import_tasks: printer_compose_up.yml
+  when: c3infodesk_printer_docker_compose_up | bool
+
+- name: Running printer compose stop
+  ansible.builtin.import_tasks: printer_compose_stop.yml
+  when: c3infodesk_printer_docker_compose_stop | bool
+
+- name: Running printer compose restart
+  ansible.builtin.import_tasks: printer_compose_restart.yml
+  when: c3infodesk_printer_docker_compose_restart | bool
+
+- name: Running printer compose down
+  ansible.builtin.import_tasks: printer_compose_down.yml
+  when: c3infodesk_printer_docker_compose_down | bool
diff --git a/roles/docker_containers/tasks/printer_compose_down.yml b/roles/docker_containers/tasks/printer_compose_down.yml
new file mode 100644
index 0000000..61c92b5
--- /dev/null
+++ b/roles/docker_containers/tasks/printer_compose_down.yml
@@ -0,0 +1,14 @@
+---
+# tasks file for docker-compose down
+
+- name: COMPOSE DOWN | Run `docker compose down`
+  community.docker.docker_compose_v2:
+    project_src: "{{ c3infodesk_project_src }}"
+    state: absent
+  register: ret_docker_compose_down
+
+- name: COMPOSE DOWN | Output compose down
+  ansible.builtin.debug:
+    var: ret_docker_compose_down
+  when: c3infodesk_printer_docker_compose_down_debug | bool
+
diff --git a/roles/docker_containers/tasks/printer_compose_restart.yml b/roles/docker_containers/tasks/printer_compose_restart.yml
new file mode 100644
index 0000000..df26924
--- /dev/null
+++ b/roles/docker_containers/tasks/printer_compose_restart.yml
@@ -0,0 +1,13 @@
+---
+# tasks file for docker-compose restart
+
+- name: COMPOSE RESTART | Run `docker compose restart`
+  community.docker.docker_compose_v2:
+    project_src: "{{ c3infodesk_project_src }}"
+    state: restarted
+  register: ret_docker_compose_restart
+
+- name: COMPOSE RESTART | Output compose restart
+  ansible.builtin.debug:
+    var: ret_docker_compose_restart
+  when: c3infodesk_printer_docker_compose_restart_debug | bool
diff --git a/roles/docker_containers/tasks/printer_compose_stop.yml b/roles/docker_containers/tasks/printer_compose_stop.yml
new file mode 100644
index 0000000..30d8c35
--- /dev/null
+++ b/roles/docker_containers/tasks/printer_compose_stop.yml
@@ -0,0 +1,14 @@
+---
+# tasks file for docker-compose stop
+
+- name: COMPOSE STOP | Run `docker compose stop`
+  community.docker.docker_compose_v2:
+    project_src: "{{ c3infodesk_project_src }}"
+    state: stopped
+  register: ret_docker_compose_stop
+
+- name: COMPOSE STOP | Output compose stop
+  ansible.builtin.debug:
+    var: ret_docker_compose_stop
+  when: c3infodesk_printer_docker_compose_stop_debug | bool
+
diff --git a/roles/docker_containers/tasks/printer_compose_up.yml b/roles/docker_containers/tasks/printer_compose_up.yml
new file mode 100644
index 0000000..b621ae3
--- /dev/null
+++ b/roles/docker_containers/tasks/printer_compose_up.yml
@@ -0,0 +1,14 @@
+---
+# tasks file for docker-compose up
+
+- name: COMPOSE UP | Run `docker compose up`
+  community.docker.docker_compose_v2:
+    # project_src: "{{ c3infodesk_project_src }}"
+    project_src: "{{ c3infodesk_printer_folder_docker }}"
+    state: present
+  register: ret_docker_compose_up
+
+- name: COMPOSE UP | Output compose up
+  ansible.builtin.debug:
+    var: ret_docker_compose_up
+  when: c3infodesk_printer_docker_compose_up_debug | bool
diff --git a/roles/docker_containers/tasks/printer_host_cups_disable.yml b/roles/docker_containers/tasks/printer_host_cups_disable.yml
new file mode 100644
index 0000000..e5ecc25
--- /dev/null
+++ b/roles/docker_containers/tasks/printer_host_cups_disable.yml
@@ -0,0 +1,41 @@
+---
+# tasks file for disabling CUPS on target system
+
+- name: DISABLE CUPS | Populate service facts
+  ansible.builtin.service_facts:
+
+- name: DISABLE CUPS | Setting fact from cups status
+  ansible.builtin.set_fact:
+    cups_status: "{{ ansible_facts.services['cups.service'].status }}"
+  when: ansible_facts.services['cups.service'].status is defined
+
+- name: DISABLE CUPS | DEBUG -> Print cups_status
+  ansible.builtin.debug:
+    msg: "cups_status: {{ cups_status }}"
+  when: c3infodesk_printer_host_cups_disable_debug | bool
+
+- name: DISABLE CUPS | Stop the running CUPS on target
+  ansible.builtin.command:
+    cmd: systemctl stop cups
+  become: true
+  when:
+    - cups_status is defined
+    - cups_status != 'masked'
+
+- name: DISABLE CUPS | Disable the running CUPS on target
+  ansible.builtin.command:
+    cmd: systemctl disable cups
+  become: true
+  # when: cups_status != 'masked'
+  when:
+    - cups_status is defined
+    - cups_status != 'masked'
+
+- name: DISABLE CUPS | Mask the running CUPS on target
+  ansible.builtin.command:
+    cmd: systemctl mask cups
+  become: true
+  # when: cups_status != 'masked'
+  when:
+    - cups_status is defined
+    - cups_status != 'masked'
\ No newline at end of file
diff --git a/roles/docker_images/README.md b/roles/docker_images/README.md
index 836b1ef..39568d4 100644
--- a/roles/docker_images/README.md
+++ b/roles/docker_images/README.md
@@ -52,7 +52,7 @@ Here are the variables setted:
 ## Dependencies
 The role dependencies are:
   - [dependencies(role)](roles/dependencies/README.md)
-  - [clone_reposiries](roles/clone_repositories/README.md)
+  - [clone_repositories](roles/clone_repositories/README.md)
 
 
 ## Example Playbook
-- 
GitLab