2017-09-14 09:10:19 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# Install docker-compose
|
|
|
|
|
|
|
|
- name: Ensure pip to be available
|
|
|
|
apt:
|
|
|
|
name: python-pip
|
2018-03-19 15:09:58 +00:00
|
|
|
cache_valid_time: 86400
|
2017-09-14 09:10:19 +00:00
|
|
|
|
|
|
|
- name: Install or update docker-compose
|
|
|
|
pip:
|
|
|
|
name: docker-compose
|
2018-01-12 10:27:30 +00:00
|
|
|
executable: pip
|
2017-09-14 09:10:19 +00:00
|
|
|
state: latest
|
|
|
|
register: docker_compose
|
|
|
|
|
|
|
|
# Configure docker-compose
|
|
|
|
|
|
|
|
- name: Create configuration directory
|
|
|
|
file:
|
|
|
|
dest: /etc/docker-compose
|
|
|
|
state: directory
|
|
|
|
mode: 0700
|
|
|
|
|
|
|
|
- name: Write docker-compose.yml
|
|
|
|
template:
|
|
|
|
dest: /etc/docker-compose/docker-compose.yml
|
|
|
|
src: templates/docker-compose.yml
|
|
|
|
mode: 0600
|
|
|
|
register: compose_file
|
|
|
|
|
|
|
|
- name: Validate configuration
|
|
|
|
command: docker-compose config -q
|
|
|
|
args:
|
|
|
|
chdir: /etc/docker-compose
|
2018-04-24 14:04:17 +00:00
|
|
|
when: compose_file.changed and docker_compose_validate
|
2017-09-14 09:10:19 +00:00
|
|
|
|
|
|
|
# Install and start services
|
|
|
|
|
|
|
|
- name: Install docker-compose.service
|
|
|
|
copy:
|
|
|
|
dest: /etc/systemd/system/docker-compose.service
|
|
|
|
content: |
|
|
|
|
[Unit]
|
|
|
|
Description=Docker Compose container starter
|
|
|
|
After=docker.service network-online.target
|
|
|
|
Requires=docker.service network-online.target
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
WorkingDirectory=/etc/docker-compose
|
|
|
|
Type=oneshot
|
|
|
|
RemainAfterExit=yes
|
|
|
|
|
2018-08-16 04:35:44 +00:00
|
|
|
ExecStartPre=-/usr/local/bin/docker-compose pull --quiet --ignore-pull-failures
|
2017-09-18 21:19:35 +00:00
|
|
|
ExecStart=/usr/local/bin/docker-compose up -d --remove-orphans
|
2017-09-14 09:10:19 +00:00
|
|
|
|
2018-04-18 15:53:21 +00:00
|
|
|
ExecStop=/usr/local/bin/docker-compose {{ docker_compose_stop_action }}
|
2017-09-14 09:10:19 +00:00
|
|
|
|
2018-04-12 21:35:36 +00:00
|
|
|
ExecReload=/usr/local/bin/docker-compose pull --quiet --ignore-pull-failures
|
2017-09-18 21:19:35 +00:00
|
|
|
ExecReload=/usr/local/bin/docker-compose up -d --remove-orphans
|
2017-09-14 09:10:19 +00:00
|
|
|
|
|
|
|
[Install]
|
2018-01-07 12:02:32 +00:00
|
|
|
WantedBy=docker.service
|
2017-09-14 09:10:19 +00:00
|
|
|
register: docker_compose_service
|
|
|
|
|
2018-01-23 10:44:43 +00:00
|
|
|
- name: Enable docker-compose service
|
2017-09-14 09:10:19 +00:00
|
|
|
systemd:
|
|
|
|
name: docker-compose.service
|
2019-01-28 22:56:19 +00:00
|
|
|
daemon_reload: true
|
|
|
|
enabled: true
|
2017-09-14 09:10:19 +00:00
|
|
|
|
|
|
|
- name: Restart service if service configuration changed
|
|
|
|
systemd:
|
|
|
|
name: docker-compose.service
|
2019-01-28 22:56:19 +00:00
|
|
|
daemon_reload: true
|
2017-09-14 09:10:19 +00:00
|
|
|
state: restarted
|
2018-01-23 10:44:43 +00:00
|
|
|
when: docker_compose_service.changed and packer_build_name is not defined
|
2017-09-14 09:10:19 +00:00
|
|
|
|
|
|
|
- name: Reload and apply configuration
|
|
|
|
systemd:
|
|
|
|
name: docker-compose.service
|
|
|
|
state: reloaded
|
2018-01-23 13:41:45 +00:00
|
|
|
when: (docker_compose.changed or compose_file.changed) and packer_build_name is not defined
|
2017-09-14 09:10:19 +00:00
|
|
|
|
|
|
|
- name: Install docker-compose-reload.service
|
|
|
|
copy:
|
|
|
|
dest: /etc/systemd/system/docker-compose-reload.service
|
|
|
|
content: |
|
|
|
|
[Unit]
|
|
|
|
Description=Refresh images and update containers
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
Type=oneshot
|
|
|
|
|
2018-08-16 04:37:07 +00:00
|
|
|
ExecStart=/bin/systemctl reload-or-restart docker-compose.service
|
2017-09-14 09:10:19 +00:00
|
|
|
|
|
|
|
- name: Install docker-compose-reload.timer
|
|
|
|
copy:
|
|
|
|
dest: /etc/systemd/system/docker-compose-reload.timer
|
|
|
|
content: |
|
|
|
|
[Unit]
|
|
|
|
Description=Refresh images and update containers
|
|
|
|
After=docker-compose.service
|
|
|
|
|
|
|
|
[Timer]
|
2017-12-27 12:42:23 +00:00
|
|
|
OnCalendar=*:0/{{ docker_compose_reload_minutes }}
|
2018-04-25 08:32:33 +00:00
|
|
|
RandomizedDelaySec={{ docker_compose_reload_max_delay }}
|
2017-09-14 09:10:19 +00:00
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=timers.target
|
|
|
|
register: docker_compose_reload_timer
|
|
|
|
|
2018-01-23 10:44:43 +00:00
|
|
|
- name: Enable docker-compose-reload.timer
|
2017-09-14 09:10:19 +00:00
|
|
|
systemd:
|
|
|
|
name: docker-compose-reload.timer
|
2019-01-28 22:56:19 +00:00
|
|
|
daemon_reload: true
|
|
|
|
enabled: true
|
2017-09-14 09:10:19 +00:00
|
|
|
|
|
|
|
- name: Restart docker-compose-reload.timer if it changed
|
|
|
|
systemd:
|
|
|
|
name: docker-compose-reload.timer
|
2019-01-28 22:56:19 +00:00
|
|
|
daemon_reload: true
|
2017-09-14 09:10:19 +00:00
|
|
|
state: restarted
|
2018-01-23 10:44:43 +00:00
|
|
|
when: docker_compose_reload_timer.changed and packer_build_name is not defined
|
2019-01-28 22:50:19 +00:00
|
|
|
|
|
|
|
- name: Install docker-clean script
|
|
|
|
copy:
|
|
|
|
content: |
|
|
|
|
#!/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
function step() { echo $@; }
|
|
|
|
|
|
|
|
step "Removing containers created / exited >= ~1h ago..."
|
|
|
|
CONTAINERS=$(docker ps -a | gawk '/(hours?|days?|weeks?|months?) ago\s+(Created|Exited)/{ print $1 }' | xargs)
|
|
|
|
[ -n "${CONTAINERS}" ] && docker rm ${CONTAINERS}
|
|
|
|
|
|
|
|
step "Removing unused and dangling images..."
|
|
|
|
docker image prune -a -f
|
|
|
|
dest: /usr/local/bin/docker-clean
|
|
|
|
mode: '0755'
|
|
|
|
owner: 'root'
|
|
|
|
group: 'root'
|
|
|
|
when: docker_compose_add_cleanjob
|
|
|
|
|
|
|
|
- name: Install docker-clean.service
|
|
|
|
copy:
|
|
|
|
dest: /etc/systemd/system/docker-clean.service
|
|
|
|
content: |
|
|
|
|
[Unit]
|
|
|
|
Description=Clean out old containers and unused images
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
Type=oneshot
|
|
|
|
ExecStart=/usr/local/bin/docker-clean
|
|
|
|
when: docker_compose_add_cleanjob
|
|
|
|
|
|
|
|
- name: Install docker-clean.timer
|
|
|
|
copy:
|
|
|
|
dest: /etc/systemd/system/docker-clean.timer
|
|
|
|
content: |
|
|
|
|
[Unit]
|
|
|
|
Description=Clean out old containers and unused images
|
|
|
|
|
|
|
|
[Timer]
|
|
|
|
OnCalendar=*:0
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=timers.target
|
|
|
|
register: docker_compose_clean_timer
|
|
|
|
when: docker_compose_add_cleanjob
|
|
|
|
|
|
|
|
- name: Enable docker-clean timer
|
|
|
|
systemd:
|
|
|
|
name: docker-clean.timer
|
2019-01-28 22:56:19 +00:00
|
|
|
daemon_reload: true
|
|
|
|
enabled: true
|
2019-01-28 22:50:19 +00:00
|
|
|
when: docker_compose_add_cleanjob
|
|
|
|
|
|
|
|
- name: Restart docker-clean.timer if it changed
|
|
|
|
systemd:
|
|
|
|
name: docker-clean.timer
|
2019-01-28 22:56:19 +00:00
|
|
|
daemon_reload: true
|
2019-01-28 22:50:19 +00:00
|
|
|
state: restarted
|
|
|
|
when: docker_compose_clean_timer is defined and docker_compose_clean_timer.changed and packer_build_name is not defined and docker_compose_add_cleanjob
|
|
|
|
|
|
|
|
- name: Deactivate docker-clean timer
|
|
|
|
systemd:
|
|
|
|
name: docker-clean.timer
|
2019-01-28 22:56:19 +00:00
|
|
|
daemon_reload: true
|
|
|
|
enabled: false
|
|
|
|
state: stopped
|
2019-01-28 22:50:19 +00:00
|
|
|
when: not docker_compose_add_cleanjob
|
|
|
|
|
|
|
|
- name: Uninstall docker-clean components
|
|
|
|
file:
|
|
|
|
dest: '{{ item }}'
|
|
|
|
state: absent
|
|
|
|
with_items:
|
|
|
|
- /usr/local/bin/docker-clean
|
|
|
|
- /etc/systemd/system/docker-clean.service
|
|
|
|
- /etc/systemd/system/docker-clean.timer
|
|
|
|
when: not docker_compose_add_cleanjob
|