From 9fd6496f5929cda949b92f2a630707c4f84ba315 Mon Sep 17 00:00:00 2001 From: Von Random Date: Thu, 3 Oct 2024 02:33:53 +0300 Subject: [PATCH] initial commit --- .gitignore | 1 + LICENSE | 9 ++++ README.md | 41 +++++++++++++++++++ ansible.cfg | 2 + deploy.yml | 8 ++++ inventory.ini | 4 ++ roles/docker/tasks/main.yml | 40 ++++++++++++++++++ roles/iconserver/tasks/main.yml | 39 ++++++++++++++++++ .../compose/iconserver/compose.yml.j2 | 27 ++++++++++++ .../data/nginx/conf.d/iconserver.conf.j2 | 11 +++++ 10 files changed, 182 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 ansible.cfg create mode 100644 deploy.yml create mode 100644 inventory.ini create mode 100644 roles/docker/tasks/main.yml create mode 100644 roles/iconserver/tasks/main.yml create mode 100644 roles/iconserver/templates/containers/compose/iconserver/compose.yml.j2 create mode 100644 roles/iconserver/templates/containers/data/nginx/conf.d/iconserver.conf.j2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7275bb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..561b907 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 von + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..aa3115b --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# iconserver-deploy + +A playbook to deploy and manage [iconserver](https://github.com/mat/besticon). + +Includes two roles: +* docker +* iconserver + +## Docker +Supported operating systems: +* CentOS Stream 9, RHEL 9 and clones +* Ubuntu 24 +* Debian 12 + +Earlier versions and other debian-based distributions may not be compatible. +(Please check [docker documentation](https://docs.docker.com/engine/install/).) + +## Iconserver +The service is deployed via docker-compose config. Two services are deployed: +* [iconserver](https://hub.docker.com/r/matthiasluedtke/iconserver) +* [nginx](https://hub.docker.com/_/nginx) + +The service is deployed to be available via https using a self-signed certificate. +Nginx configuration template can be edited in the role. + +## Deployment + +The project deployment has been developed and tested with `ansible-core 2.17.4`. +If you run an earlier version your mileage may vary, but it should be compatible. + +To deploy the configuration the target servers have to be able to reach docker +repos and docker hub via internet. + +Ansible config and inventory files are included. Update `inventory.yml` to match +your setup. + +After that deploying the service is as simple as running: +```bash +ansible-playbook deploy.yml +``` +(Add necessary options, like `-u` or `-K` etc.) diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..7debec5 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +inventory=inventory.ini diff --git a/deploy.yml b/deploy.yml new file mode 100644 index 0000000..0a102d7 --- /dev/null +++ b/deploy.yml @@ -0,0 +1,8 @@ +--- +- name: Deploy docker and iconserver + become: true + hosts: iconserver + gather_facts: true + roles: + - docker + - iconserver diff --git a/inventory.ini b/inventory.ini new file mode 100644 index 0000000..b634f43 --- /dev/null +++ b/inventory.ini @@ -0,0 +1,4 @@ +[iconserver] +redhat ansible_host=172.20.0.9 +debian ansible_host=172.20.0.12 +ubuntu ansible_host=172.20.0.24 diff --git a/roles/docker/tasks/main.yml b/roles/docker/tasks/main.yml new file mode 100644 index 0000000..2970320 --- /dev/null +++ b/roles/docker/tasks/main.yml @@ -0,0 +1,40 @@ +--- +# official docker site recommends using a yum-config-manager command, +# but in real environment we'd probably have a custom repo anyways, +# so I'm using the yum_repository module instead +- name: Set up repo for yum + when: ansible_os_family == "RedHat" + ansible.builtin.yum_repository: + name: docker-ce-stable + description: Docker CE Stable - $basearch + baseurl: https://download.docker.com/linux/centos/$releasever/$basearch/stable + gpgkey: https://download.docker.com/linux/centos/gpg + gpgcheck: true + +- name: Set up repo for apt + when: ansible_os_family == "Debian" + block: + - name: Set up repo for apt (key) + ansible.builtin.get_url: + url: https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg + dest: /etc/apt/keyrings/docker.asc + + - name: Set up repo for apt (repo itself) + ansible.builtin.apt_repository: + repo: deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release | lower }} stable + +- name: Install packages + ansible.builtin.package: + name: + - docker-ce + - docker-ce-cli + - containerd.io + - docker-buildx-plugin + - docker-compose-plugin + state: present + +- name: Enable docker service + ansible.builtin.systemd_service: + name: docker + state: started + enabled: true diff --git a/roles/iconserver/tasks/main.yml b/roles/iconserver/tasks/main.yml new file mode 100644 index 0000000..e93c27b --- /dev/null +++ b/roles/iconserver/tasks/main.yml @@ -0,0 +1,39 @@ +--- +- name: Install dependencies + ansible.builtin.package: + name: python3-cryptography + state: present + +- name: Create directories + loop: + - /containers/compose/iconserver + - /containers/data/nginx/conf.d + ansible.builtin.file: + path: "{{ item }}" + state: directory + recurse: true + +- name: Install files from templates + loop: + - containers/compose/iconserver/compose.yml + - containers/data/nginx/conf.d/iconserver.conf + ansible.builtin.template: + src: "{{ item }}.j2" + dest: "/{{ item }}" + +# I would assume it does not matter how we get the self signed cert to the host +# so it's nicer (and safer) to generate one for each of the hosts. +# Another option is to pre-generate it and store it in ansible-vault. +- name: Create openssl key + community.crypto.openssl_privatekey: + path: /containers/data/nginx/conf.d/iconserver.key + +- name: Create openssl self-signed cert + community.crypto.x509_certificate: + path: /containers/data/nginx/conf.d/iconserver.crt + privatekey_path: /containers/data/nginx/conf.d/iconserver.key + provider: selfsigned + +- name: Deploy software via docker-compose + community.docker.docker_compose_v2: + project_src: /containers/compose/iconserver diff --git a/roles/iconserver/templates/containers/compose/iconserver/compose.yml.j2 b/roles/iconserver/templates/containers/compose/iconserver/compose.yml.j2 new file mode 100644 index 0000000..20595cb --- /dev/null +++ b/roles/iconserver/templates/containers/compose/iconserver/compose.yml.j2 @@ -0,0 +1,27 @@ +networks: + default: + driver: bridge + ipam: + config: + - subnet: 172.31.0.0/24 + gateway: 172.31.0.1 + +services: + iconserver: + image: matthiasluedtke/iconserver:latest + container_name: iconserver + restart: unless-stopped + networks: + default: + ipv4_address: 172.31.0.2 + + nginx: + image: nginx:stable-alpine + container_name: nginx + volumes: + - /containers/data/nginx/conf.d:/etc/nginx/conf.d:ro + ports: + - "443:443" + networks: + default: + ipv4_address: 172.31.0.3 diff --git a/roles/iconserver/templates/containers/data/nginx/conf.d/iconserver.conf.j2 b/roles/iconserver/templates/containers/data/nginx/conf.d/iconserver.conf.j2 new file mode 100644 index 0000000..08adfe6 --- /dev/null +++ b/roles/iconserver/templates/containers/data/nginx/conf.d/iconserver.conf.j2 @@ -0,0 +1,11 @@ +server { + listen 443 ssl http2 default; + server_name _; + + ssl_certificate /etc/nginx/conf.d/iconserver.crt; + ssl_certificate_key /etc/nginx/conf.d/iconserver.key; + + location / { + proxy_pass http://172.31.0.2:8080; + } +}