mirror of
https://github.com/luzifer-ansible/docker-compose.git
synced 2024-11-08 13:39:59 +00:00
Knut Ahlers
1b54459c12
This enables users of this role to execute `docker-compose stop` instead of `docker-compose down` on shutdown and speed up the startup. For full discussion leading to this see: https://git.io/vpIOy Thanks @EugenMayer for the suggestion! Signed-off-by: Knut Ahlers <knut@ahlers.me>
137 lines
3.6 KiB
YAML
137 lines
3.6 KiB
YAML
---
|
|
|
|
# Install docker-compose
|
|
|
|
- name: Ensure pip to be available
|
|
apt:
|
|
name: python-pip
|
|
cache_valid_time: 86400
|
|
|
|
- name: Install or update docker-compose
|
|
pip:
|
|
name: docker-compose
|
|
executable: pip
|
|
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
|
|
when: compose_file.changed
|
|
|
|
# Install and start services
|
|
|
|
- name: '[Migration] Check multi-user.target dependency of service'
|
|
command: grep -Fxq 'WantedBy=multi-user.target' /etc/systemd/system/docker-compose.service
|
|
register: docker_compose_olddep
|
|
check_mode: no
|
|
ignore_errors: yes
|
|
changed_when: no
|
|
|
|
- name: '[Migration] Disable service still WantedBy=multi-user.target'
|
|
systemd:
|
|
name: docker-compose.service
|
|
enabled: no
|
|
when: docker_compose_olddep.rc == 0
|
|
|
|
- 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
|
|
|
|
ExecStartPre=/usr/local/bin/docker-compose pull --quiet --ignore-pull-failures
|
|
ExecStart=/usr/local/bin/docker-compose up -d --remove-orphans
|
|
|
|
ExecStop=/usr/local/bin/docker-compose {{ docker_compose_stop_action }}
|
|
|
|
ExecReload=/usr/local/bin/docker-compose pull --quiet --ignore-pull-failures
|
|
ExecReload=/usr/local/bin/docker-compose up -d --remove-orphans
|
|
|
|
[Install]
|
|
WantedBy=docker.service
|
|
register: docker_compose_service
|
|
|
|
- name: Enable docker-compose service
|
|
systemd:
|
|
name: docker-compose.service
|
|
daemon_reload: yes
|
|
enabled: yes
|
|
|
|
- name: Restart service if service configuration changed
|
|
systemd:
|
|
name: docker-compose.service
|
|
daemon_reload: yes
|
|
state: restarted
|
|
when: docker_compose_service.changed and packer_build_name is not defined
|
|
|
|
- name: Reload and apply configuration
|
|
systemd:
|
|
name: docker-compose.service
|
|
state: reloaded
|
|
when: (docker_compose.changed or compose_file.changed) and packer_build_name is not defined
|
|
|
|
- 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
|
|
|
|
ExecStart=/bin/systemctl reload docker-compose.service
|
|
|
|
- name: Install docker-compose-reload.timer
|
|
copy:
|
|
dest: /etc/systemd/system/docker-compose-reload.timer
|
|
content: |
|
|
[Unit]
|
|
Description=Refresh images and update containers
|
|
Requires=docker-compose.service
|
|
After=docker-compose.service
|
|
|
|
[Timer]
|
|
OnCalendar=*:0/{{ docker_compose_reload_minutes }}
|
|
|
|
[Install]
|
|
WantedBy=timers.target
|
|
register: docker_compose_reload_timer
|
|
|
|
- name: Enable docker-compose-reload.timer
|
|
systemd:
|
|
name: docker-compose-reload.timer
|
|
daemon_reload: yes
|
|
enabled: yes
|
|
|
|
- name: Restart docker-compose-reload.timer if it changed
|
|
systemd:
|
|
name: docker-compose-reload.timer
|
|
daemon_reload: yes
|
|
state: restarted
|
|
when: docker_compose_reload_timer.changed and packer_build_name is not defined
|