From fe26d7a250af3982572b36ee5a2dc16c2fb10ba6 Mon Sep 17 00:00:00 2001 From: fejao <mail@fejao.de> Date: Wed, 12 Feb 2025 19:59:11 +0100 Subject: [PATCH 1/3] Added files --- .gitignore | 1 + .gitlab-ci.yml | 50 +++++++++ .yamllint | 50 +++++++++ README.md | 172 ++++++++++++++++++++----------- ci-cd/python_requirements.txt | 1 + defaults/main.yml | 6 ++ meta/main.yml | 19 ++++ molecule/README.md | 42 ++++++++ molecule/default/converge.yml | 61 +++++++++++ molecule/default/molecule.yml | 68 ++++++++++++ molecule/default/verify.yml | 188 ++++++++++++++++++++++++++++++++++ tasks/docker_image_app.yml | 28 +++++ tasks/docker_image_cups.yml | 33 ++++++ tasks/docker_image_proxy.yml | 28 +++++ tasks/main.yml | 11 ++ 15 files changed, 700 insertions(+), 58 deletions(-) create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100644 .yamllint create mode 100644 ci-cd/python_requirements.txt create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100644 molecule/README.md create mode 100644 molecule/default/converge.yml create mode 100644 molecule/default/molecule.yml create mode 100644 molecule/default/verify.yml create mode 100644 tasks/docker_image_app.yml create mode 100644 tasks/docker_image_cups.yml create mode 100644 tasks/docker_image_proxy.yml create mode 100644 tasks/main.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b3c5964 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*OLD* diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..8f6b8da --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,50 @@ +--- + +### +### VARIABLES +### +variables: + AUTHOR: fejao + IMAGE_NAME_LINT: python:3.10.15-bullseye + PATH_ENV_DIR: "/builds/fejao/c3infodesk-deployment-role-docker-images" + PATH_PYTHON_REQ_FILE: "/builds/fejao/c3infodesk-deployment-role-docker-images/ci-cd/python_requirements.txt" + PATH_PLAYBOOK: "/builds/fejao/c3infodesk-deployment-role-docker-images/ci-cd/playbook_role_test.yml" + +### +### STAGES +### +stages: + - yamllint + - upstream-trigger + +### +### SCRIPTS +### +.install_dependencies: &install_dependencies + # Install python and it's requirements + - apt-get update && apt-get install -y python3-pip && apt-get clean && rm -rf /var/lib/apt/lists/* + - pip3 install -r $PYTHON_REQ_FILE_PATH + ### Correct the permissions on the build environment directory. + ### See https://github.com/ansible/ansible/pull/42142/files + - chmod 700 $PATH_ENV_DIR + +yamllint-test: + stage: yamllint + image: $IMAGE_NAME_LINT + variables: + PYTHON_REQ_FILE_PATH: $PATH_PYTHON_REQ_FILE + PLAYBOOK_PATH: $PATH_PLAYBOOK + before_script: + - *install_dependencies + script: + - echo "This is the 'yamllint' test from the 'test' role" + - yamllint . + +# trigger-pipeline: +# stage: upstream-trigger +# variables: +# RUN_UPSTREAM_FOR_ROLE_DOCKER_IMAGES: true +# trigger: +# project: fejao/c3infodesk-deployment +# branch: main +# strategy: depend diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..2382bed --- /dev/null +++ b/.yamllint @@ -0,0 +1,50 @@ +--- + +# https://ansible.readthedocs.io/projects/lint/rules/yaml/ + +yaml-files: + - '*.yaml' + - '*.yml' + - '.yamllint' + +rules: + anchors: enable + # braces: enable + # braces: disable + braces: + max-spaces-inside: 1 + brackets: enable + colons: enable + commas: enable + comments: + level: warning + min-spaces-from-content: 1 + comments-indentation: false + document-end: disable + document-start: + level: warning + empty-lines: enable + empty-values: disable + float-values: disable + hyphens: enable + # indentation: enable + indentation: disable + key-duplicates: enable + key-ordering: disable + # line-length: enable + line-length: + # max: 160 + ### roles/dependencies/defaults/main.yml:72 --> 232 characters + max: 240 + new-line-at-end-of-file: enable + new-lines: enable + # octal-values: disable + quoted-strings: disable + trailing-spaces: enable + truthy: + level: warning + #### + # var_naming_pattern: "^[a-z_][a-z0-9_]*$" + # var-naming: disable + # var-naming: + # no-role-prefix: disable diff --git a/README.md b/README.md index 54b540a..d6eb8d3 100644 --- a/README.md +++ b/README.md @@ -1,93 +1,149 @@ -# c3infodesk-deployment-role-docker-images +# docker_images role +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. -## Getting started +## Configuration +You need to set some configuration for this to work +- ### ansible.cfg + You have to edit the **ansible.cfg** file to your needs for accessing your target. + - #### remote_user + You need to set the **remote_user** configuration for accessing the target host. -To make it easy for you to get started with GitLab, here's a list of recommended next steps. + Example: [remote_user](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/ansible.cfg#L227) -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! +- ### HOST_VARS + In order to access your target host, you need to create/edit your **inventories/host_vars/<YOUR_HOST_NAME>.yml** + - #### ansible_become_pass + You need to set the **sudo** password from the user setted on **remote_user** for installing packages and etc. at the target. -## Add your files + Example: [ansible_become_pass](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/host_vars/example_server.yml#L5) -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: +- ### HOSTS + You need to provide a **group** in which your target belongs to with the same name setted for **inventories/host_vars/<YOUR_HOST_NAME>.yml**. -``` -cd existing_repo -git remote add origin https://git.cccv.de/fejao/c3infodesk-deployment-role-docker-images.git -git branch -M main -git push -uf origin main -``` + There you can add/edit the target address/IP. -## Integrate with your tools + Example: [hosts](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/hosts) -- [ ] [Set up project integrations](https://git.cccv.de/fejao/c3infodesk-deployment-role-docker-images/-/settings/integrations) -## Collaborate with your team +## Role Variables +Here are the variables setted: -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) +- ### 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 -## Test and Deploy +- ### 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 -Use the built-in continuous integration in GitLab. +- ### 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 -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) -*** +## Dependencies +The role dependencies are: + - [dependencies(role)](roles/dependencies/README.md) + - [clone_repositories](roles/clone_repositories/README.md) -# Editing this README -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template. +## Example Playbook -## Suggestions for a good README +- ### Using the provided example + For using the example playbook **[playbooks/examples/example_04_docker_images.yml](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/playbooks/examples/example_04_docker_images.yml)** file you only need to: + - Change the user at the **[ansible.cfg](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/ansible.cfg)** file at the field **[remote_user](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/ansible.cfg#L227)** for acessing your target. + - Update the file **[inventories/host_vars/example_server.yml](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/host_vars/example_server.yml)** and change the value for **[ansible_become_pass](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/host_vars/example_server.yml#L5)** from the **[remote_user](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/ansible.cfg#L227)** + - Update the **[inventories/hosts](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/hosts)** file and change the **[example_server](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/hosts#L2)** with the IP address from your target. -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. -## Name -Choose a self-explaining name for your project. + And run the command: + ```bash + ansible-playbook -i inventories/hosts playbooks/examples/example_04_docker_images.yml + ``` -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. +- ### 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. -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. + Here is an example using the **<HOST_NAME>** as **test_servers** -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. + ```yaml + - hosts: + - test_servers + roles: + - roles/docker_images + ``` -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. + And call the playbook as: -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. + ```bash + ansible-playbook -i <INVENTORY> <PLAYBOOK_NAME>.yml + ``` -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. +## Testing +You can test this locally using **DIND (docker in docker)** at your own computer using [ansible molecule](https://ansible.readthedocs.io/projects/molecule/installation/) -## Contributing -State if you are open to contributions and what your requirements are for accepting them. +- ### Testing requirements + You need docker installed at your system and the [ansible molecule](https://ansible.readthedocs.io/projects/molecule/installation/). -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. + You might have to set DIND at your system, for linux, you can create the file **/etc/docker/daemon.json** -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. + And add the content and restart docker. + + ```json + { + "exec-opts": ["native.cgroupdriver=systemd"], + "features": { + "buildkit": true + }, + "experimental": true, + "cgroup-parent": "docker.slice" + } + ``` + +- ### Using molecule + Just go to the root of the role **roles/dependencies** and run the command: + + ```bash + molecule test + ``` -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. ## License -For open source projects, say how it is licensed. -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +MIT / BSD + + +## Author Information +[https://chaos.social/@fejao](https://chaos.social/@fejao) diff --git a/ci-cd/python_requirements.txt b/ci-cd/python_requirements.txt new file mode 100644 index 0000000..b2c729c --- /dev/null +++ b/ci-cd/python_requirements.txt @@ -0,0 +1 @@ +yamllint diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..9bc853f --- /dev/null +++ b/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/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..55c61fa --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,19 @@ +--- + +galaxy_info: + role_name: docker_images + description: Role to clone the repositories used from c3InfoDesk sytems + author: fejao + company: cccv.de + license: "MIT / BSD" + 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: c3infodesk-deployment-role-dependencies + - role: c3infodesk-deployment-role-clone-repositories diff --git a/molecule/README.md b/molecule/README.md new file mode 100644 index 0000000..91e1d07 --- /dev/null +++ b/molecule/README.md @@ -0,0 +1,42 @@ +## ansible molecule tests for role: docker_images + + + +## Why to use it +It's allways a good idea to test your things. + +With **ansible molecule** you can test your role locally without the need of using a **real server** but a local **docker container**. + +## Dependencies + +- ### molecule + You need to have [ansible molecule](https://ansible.readthedocs.io/projects/molecule/installation/) installed. + +- ### DIND (docker in docker) + For using the tests, you will to have **docker** installed locally and and set it for using **DIND (docker in docker)** + + + - ### DIND linux + You have to create/edit the file **/etc/docker/daemon.json** with the content: + + ```json + { + "exec-opts": ["native.cgroupdriver=systemd"], + "features": { + "buildkit": true + }, + "experimental": true, + "cgroup-parent": "docker.slice" + } + ``` + + +## Configuration +There's not much for configuring for using the tests + +## Running the tests +For running the **molecule tests** you just need to run the command: + +```bash +molecule test +``` diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 0000000..7c6b1b7 --- /dev/null +++ b/molecule/default/converge.yml @@ -0,0 +1,61 @@ +--- + +- name: MOLECULE | Converge + hosts: all + pre_tasks: + - name: MOLECULE | COVERGE | PRE-TASKS | VARIABLES | From -> ../../defaults + ansible.builtin.include_vars: + dir: ../../defaults + depth: 1 + extensions: + - 'yml' + + - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../roles/c3infodesk-deployment-role-dependencies/defaults + ansible.builtin.include_vars: + dir: ../../../../roles/c3infodesk-deployment-role-dependencies/defaults + depth: 1 + extensions: + - 'yml' + + - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../roles/c3infodesk-deployment-role-clone-repositories/defaults + ansible.builtin.include_vars: + dir: ../../../../roles/c3infodesk-deployment-role-clone-repositories/defaults + depth: 1 + extensions: + - 'yml' + + # - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../roles/dependencies/defaults + # ansible.builtin.include_vars: + # dir: ../../../../roles/dependencies/defaults + # depth: 1 + # extensions: + # - 'yml' + + # - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../roles/clone_repositories/defaults + # ansible.builtin.include_vars: + # dir: ../../../../roles/clone_repositories/defaults + # depth: 1 + # extensions: + # - 'yml' + + - name: MOLECULE | COVERGE | PRE-TASKS | VARIABLES | From -> ../../../../inventories/group_vars + ansible.builtin.include_vars: + dir: ../../../../inventories/group_vars + extensions: + - 'yml' + + - name: MOLECULE | COVERGE | PRE-TASKS | VARIABLES | From -> ../../../../inventories/host_vars + ansible.builtin.include_vars: + dir: ../../../../inventories/host_vars + extensions: + - 'yml' + + - name: MOLECULE | COVERGE | PRE-TASKS | Install python 'requests' package + ansible.builtin.pip: + name: requests + + tasks: + - name: MOLECULE | CONVERGE | Call the 'docker_images' role. + ansible.builtin.include_role: + # name: docker_images + name: c3infodesk-deployment-role-docker-images diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..6d58bb2 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,68 @@ +--- + +dependency: + name: galaxy + +driver: + name: docker + +platforms: + - name: molecule_docker_images + ### At the moment I'm not running at the latest kernel, please use always with a older kernel than the host + # image: docker.io/geerlingguy/docker-ubuntu2204-ansible + image: geerlingguy/docker-ubuntu2204-ansible:latest + command: /usr/sbin/init + pre_build_image: true + cgroupns_mode: host + privileged: true + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + - /var/lib/containerd + +provisioner: + name: ansible + +lint: | + ansible-lint --exclude molecule/default/ + +scenario: + name: default + create_sequence: + - dependency + - create + - prepare + check_sequence: + - dependency + - cleanup + - destroy + - create + - prepare + - converge + - check + - destroy + converge_sequence: + - dependency + - create + - prepare + - converge + destroy_sequence: + - dependency + - cleanup + - destroy + test_sequence: + # - lint + # - dependency + # - cleanup + - destroy + - syntax + - create + # - prepare + - converge + - verify + - idempotence + # - side_effect + # - cleanup + - destroy + +verifier: + name: ansible diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml new file mode 100644 index 0000000..a771ab6 --- /dev/null +++ b/molecule/default/verify.yml @@ -0,0 +1,188 @@ +--- + +- name: VERIFY | Test role for debugging variables + hosts: all + vars: + package_name_docker: 'docker' + package_name_docker_ce: 'docker-ce' + package_name_docker_compose: 'docker-compose' + package_name_docker_compose_plugin: 'docker-compose-plugin' + + pre_tasks: + - name: MOLECULE | COVERGE | PRE-TASKS | VARIABLES | From -> ../../defaults + ansible.builtin.include_vars: + dir: ../../defaults + depth: 1 + extensions: + - 'yml' + + - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../roles/c3infodesk-deployment-role-dependencies/defaults + ansible.builtin.include_vars: + dir: ../../../../roles/c3infodesk-deployment-role-dependencies/defaults + depth: 1 + extensions: + - 'yml' + + - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../roles/c3infodesk-deployment-role-clone-repositories/defaults + ansible.builtin.include_vars: + dir: ../../../../roles/c3infodesk-deployment-role-clone-repositories/defaults + depth: 1 + extensions: + - 'yml' + + # - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../roles/dependencies/defaults + # ansible.builtin.include_vars: + # dir: ../../../../roles/dependencies/defaults + # depth: 1 + # extensions: + # - 'yml' + + # - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../roles/clone_repositories/defaults + # ansible.builtin.include_vars: + # dir: ../../../../roles/clone_repositories/defaults + # depth: 1 + # extensions: + # - 'yml' + + - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../inventories/group_vars + ansible.builtin.include_vars: + dir: ../../../../inventories/group_vars + extensions: + - 'yml' + + - name: MOLECULE | VERIFY | PRE-TASKS | VARIABLES | From -> ../../../../inventories/host_vars + ansible.builtin.include_vars: + dir: ../../../../inventories/host_vars + extensions: + - 'yml' + + tasks: + ### + ### POPULATE + ### + - name: MOLECULE | VERIFY | POPULATE | Gather the package facts + ansible.builtin.package_facts: + manager: auto + + # - name: MOLECULE | VERIFY | POPULATE | Gather service facts + # ansible.builtin.service_facts: + + ### + ### TESTS - PACKAGES + ### + - name: MOLECULE | VERIFY | TEST | Check that the packages from 'os_dependencies_packages' are installed. + ansible.builtin.debug: + msg: "Package: '{{ package_name }}' is installed" + loop: "{{ os_dependencies_packages }}" + loop_control: + loop_var: package_name + failed_when: package_name not in ansible_facts.packages + + - name: MOLECULE | VERIFY | TEST | Check that docker is installed. + ansible.builtin.debug: + msg: "Package: 'docker' is installed" + failed_when: + - package_name_docker not in ansible_facts.packages + - package_name_docker_ce not in ansible_facts.packages + + - name: MOLECULE | VERIFY | TEST | Check that docker-compose is installed. + ansible.builtin.debug: + msg: "Package: 'docker-cokmpose' is installed" + failed_when: + - package_name_docker_compose not in ansible_facts.packages + - package_name_docker_compose_plugin not in ansible_facts.packages + + ### + ### TESTS - DOCKER + ### + - name: MOLECULE | VERIFY | TEST | Get docker service state. + ansible.builtin.systemd: + name: "docker" + register: docker_service + + - name: MOLECULE | VERIFY | TEST | Check that docker is running. + ansible.builtin.debug: + msg: "Docker IS running..." + failed_when: "'active' != docker_service.status.ActiveState" + + ### + ### TESTS - ADDED USERS + ### + - name: MOLECULE | VERIFY | TEST | Get added users infos. + ansible.builtin.user: + name: "{{ item }}" + loop: "{{ docker_users }}" + check_mode: true + register: test_users + + - name: MOLECULE | VERIFY | TEST | Check if users exists + ansible.builtin.debug: + msg: "User {{ item.item }} {{ 'exists' if item.state | d('') == 'present' else 'does not exist' }}" + loop: "{{ test_users.results }}" + loop_control: + label: "{{ item.item }}" + + - name: MOLECULE | VERIFY | TEST | Get the groups that the users belongs to. + ansible.builtin.command: + cmd: "id -Gn {{ item.item }}" + loop: "{{ test_users.results }}" + loop_control: + label: "{{ item.item }}" + register: user_groups + changed_when: false + + - name: MOLECULE | VERIFY | TEST | Check that the users are at the 'docker' group. + ansible.builtin.debug: + msg: "User '{{ item.item.item }}' belongs to the docker group" + with_items: "{{ user_groups.results }}" + failed_when: "'docker' not in item.stdout" + + ### + ### TESTS - CLONE REPOSITORY - PRINTER + ### + - name: MOLECULE | VERIFY | TEST | Get if the repository was cloned. + ansible.builtin.git: + repo: "{{ c3infodesk_printer_repo_url }}" + dest: "{{ c3infodesk_printer_folder }}" + version: main + clone: false + update: false + register: check_repository + + - name: MOLECULE | VERIFY | TEST | Checking if the repository was cloned. + ansible.builtin.debug: + msg: "Repository: '{{ c3infodesk_printer_repo_url }}' cloned..." + failed_when: check_repository.changed | bool + + ### + ### TESTS - DOCKER IMAGES + ### + - name: MOLECULE | VERIFY | TEST | Get if the image is created for 'cups'. + community.docker.docker_image_info: + name: "{{ c3infodesk_printer_docker_img_cups_name }}" + register: result_check_image_cups + + - name: MOLECULE | VERIFY | TEST | Check the image creation from 'cups'. + ansible.builtin.debug: + msg: "Image for 'cups' exists" + when: result_check_image_cups.images | length > 0 + + - name: MOLECULE | VERIFY | TEST | Get if the image is created for 'app'. + community.docker.docker_image_info: + name: "{{ c3infodesk_printer_docker_img_app_name }}" + register: result_check_image_app + + - name: MOLECULE | VERIFY | TEST | Check the image creation from 'app'. + ansible.builtin.debug: + msg: "Image for 'app' exists" + when: result_check_image_app.images | length > 0 + + - name: MOLECULE | VERIFY | TEST | Get if the image is created for 'proxy'. + community.docker.docker_image_info: + name: "{{ c3infodesk_printer_docker_img_proxy_name }}" + register: result_check_image_proxy + + - name: MOLECULE | VERIFY | TEST | Check the image creation from 'proxy'. + ansible.builtin.debug: + msg: "Image for 'proxy' exists" + when: result_check_image_proxy.images | length > 0 diff --git a/tasks/docker_image_app.yml b/tasks/docker_image_app.yml new file mode 100644 index 0000000..662606d --- /dev/null +++ b/tasks/docker_image_app.yml @@ -0,0 +1,28 @@ +--- +# 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 + changed_when: build_img_result.rc != 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 diff --git a/tasks/docker_image_cups.yml b/tasks/docker_image_cups.yml new file mode 100644 index 0000000..fdc5ac7 --- /dev/null +++ b/tasks/docker_image_cups.yml @@ -0,0 +1,33 @@ +--- +# 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 + changed_when: build_img_result.rc != 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 diff --git a/tasks/docker_image_proxy.yml b/tasks/docker_image_proxy.yml new file mode 100644 index 0000000..8cb215c --- /dev/null +++ b/tasks/docker_image_proxy.yml @@ -0,0 +1,28 @@ +--- +# 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 + changed_when: build_img_result.rc != 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 diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..ba59881 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,11 @@ +--- +# tasks file for docker-images + +- name: Create the docker image for cups + ansible.builtin.import_tasks: docker_image_cups.yml + +- name: Create the docker image for app + ansible.builtin.import_tasks: docker_image_app.yml + +- name: Create the docker image for proxy + ansible.builtin.import_tasks: docker_image_proxy.yml -- GitLab From 1b434d5512ae2f523e41d7d6db590e8a565fdd3c Mon Sep 17 00:00:00 2001 From: fejao <mail@fejao.de> Date: Wed, 12 Feb 2025 20:02:35 +0100 Subject: [PATCH 2/3] setting ci-cd --- .gitlab-ci.yml | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8f6b8da..c5051d0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -40,11 +40,12 @@ yamllint-test: - echo "This is the 'yamllint' test from the 'test' role" - yamllint . -# trigger-pipeline: -# stage: upstream-trigger -# variables: -# RUN_UPSTREAM_FOR_ROLE_DOCKER_IMAGES: true -# trigger: -# project: fejao/c3infodesk-deployment -# branch: main -# strategy: depend +trigger-pipeline: + stage: upstream-trigger + variables: + RUN_UPSTREAM_FOR_ROLE_DOCKER_IMAGES: true + trigger: + project: fejao/c3infodesk-deployment + # branch: main + branch: Adding_role_docker_images_submodule + strategy: depend -- GitLab From 4abadfb5e53c31034e2aae072c6c420c4227bc4b Mon Sep 17 00:00:00 2001 From: fejao <mail@fejao.de> Date: Wed, 12 Feb 2025 20:08:38 +0100 Subject: [PATCH 3/3] setting ci-cd --- .gitlab-ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c5051d0..f7c46b6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -46,6 +46,5 @@ trigger-pipeline: RUN_UPSTREAM_FOR_ROLE_DOCKER_IMAGES: true trigger: project: fejao/c3infodesk-deployment - # branch: main - branch: Adding_role_docker_images_submodule + branch: main strategy: depend -- GitLab