From 013b026453171db80248b653cc38b35a64bf591c Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 4 Oct 2024 13:42:40 +0200 Subject: [PATCH] [roles/server/docker-compose] install docker-compose and start default-service --- .../docker-compose/files/default/daemon.json | 3 + .../files/default/docker-compose.target | 10 +++ .../files/default/docker-compose@.service | 39 +++++++++++ roles/server/docker-compose/handlers/main.yml | 21 ++++++ roles/server/docker-compose/tasks/main.yml | 67 +++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 roles/server/docker-compose/files/default/daemon.json create mode 100644 roles/server/docker-compose/files/default/docker-compose.target create mode 100644 roles/server/docker-compose/files/default/docker-compose@.service create mode 100644 roles/server/docker-compose/handlers/main.yml create mode 100644 roles/server/docker-compose/tasks/main.yml diff --git a/roles/server/docker-compose/files/default/daemon.json b/roles/server/docker-compose/files/default/daemon.json new file mode 100644 index 0000000..fe3a9bc --- /dev/null +++ b/roles/server/docker-compose/files/default/daemon.json @@ -0,0 +1,3 @@ +{ + "log-driver": "journald" +} diff --git a/roles/server/docker-compose/files/default/docker-compose.target b/roles/server/docker-compose/files/default/docker-compose.target new file mode 100644 index 0000000..cb14ad8 --- /dev/null +++ b/roles/server/docker-compose/files/default/docker-compose.target @@ -0,0 +1,10 @@ +# +################################################ +### Managed by someone's ansible provisioner ### +################################################ +# Part of: https://git.somenet.org/root/pub/somesible.git +# 2017-2024 by someone +# + +[Unit] +Description=Docker compose services diff --git a/roles/server/docker-compose/files/default/docker-compose@.service b/roles/server/docker-compose/files/default/docker-compose@.service new file mode 100644 index 0000000..aae440a --- /dev/null +++ b/roles/server/docker-compose/files/default/docker-compose@.service @@ -0,0 +1,39 @@ +# +################################################ +### Managed by someone's ansible provisioner ### +################################################ +# Part of: https://git.somenet.org/root/pub/somesible.git +# 2017-2024 by someone +# + +[Unit] +Description=Docker compose service %i +OnFailure=unit-status-mail@%n.service +Requires=docker.service +After=network.target +PartOf=docker-compose.target +ConditionPathIsDirectory=/etc/docker/compose-%i/ + + +[Service] +Type=simple +TimeoutSec=1800 +Restart=on-failure +RestartSec=15 +WorkingDirectory=/etc/docker/compose-%i/ +ExecStartPre=/bin/bash -c 'source pre-exec-sourced.sh' + +# Compose up +ExecStartPre=-/usr/bin/docker-compose pull +ExecStart=/usr/bin/docker-compose up --no-recreate --build +ExecStartPost=-/bin/bash -c '(sleep 600; docker system prune -af)&' + +# Compose down, remove containers and volumes +ExecStop=/usr/bin/docker-compose down -v + +# reloading cant be reliably used for checking for updates :( +#ExecReload=/usr/bin/docker-compose pull +#ExecReload=-/bin/bash -c '(sleep 600; docker system prune -af)&' + +[Install] +WantedBy=multi-user.target diff --git a/roles/server/docker-compose/handlers/main.yml b/roles/server/docker-compose/handlers/main.yml new file mode 100644 index 0000000..09281cb --- /dev/null +++ b/roles/server/docker-compose/handlers/main.yml @@ -0,0 +1,21 @@ +##################################### +### someone's ansible provisioner ### +##################################### +# Part of: https://git.somenet.org/root/pub/somesible.git +# 2017-2024 by someone +# +--- +- name: restart docker.service + systemd: + name: docker.service + daemon_reload: yes + state: restarted + ignore_errors: yes + + +- name: restart docker-compose.target + systemd: + name: docker-compose.target + daemon_reload: yes + state: restarted + ignore_errors: yes diff --git a/roles/server/docker-compose/tasks/main.yml b/roles/server/docker-compose/tasks/main.yml new file mode 100644 index 0000000..e2b0ffd --- /dev/null +++ b/roles/server/docker-compose/tasks/main.yml @@ -0,0 +1,67 @@ +##################################### +### someone's ansible provisioner ### +##################################### +# Part of: https://git.somenet.org/root/pub/somesible.git +# 2017-2024 by someone +# +--- +- name: install docker and docker-compose + apt: + pkg: + - docker.io + - docker-compose + state: present + policy_rc_d: 101 + tags: "online" + ignore_errors: "{{ignore_online_errors | bool}}" + + +- name: copy daemon.json to /etc/docker/daemon.json + copy: + src: "{{item}}" + dest: "/etc/docker/daemon.json" + mode: 0644 + owner: "root" + group: "root" + with_first_found: + - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/daemon.json" + - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/daemon.json" + - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/daemon.json" + - "default/daemon.json" + notify: restart docker.service + + +- name: copy docker-compose.target to /etc/systemd/system/ + copy: + src: "{{item}}" + dest: "/etc/systemd/system/docker-compose.target" + mode: 0644 + owner: "root" + group: "root" + with_first_found: + - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/docker-compose.target" + - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/docker-compose.target" + - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/docker-compose.target" + - "default/docker-compose.target" + notify: restart docker-compose.target + + +- name: copy docker-compose@.service to /etc/systemd/system/ + copy: + src: "{{item}}" + dest: "/etc/systemd/system/docker-compose@.service" + mode: 0644 + owner: "root" + group: "root" + with_first_found: + - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/docker-compose@.service" + - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/docker-compose@.service" + - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/docker-compose@.service" + - "default/docker-compose@.service" + notify: restart docker-compose.target + + +- name: enable and start docker.service + include_role: name="base/systemd/enable-and-start" + vars: + service_name: docker.service -- 2.43.0