dokku-sync/playbook2.yml

195 lines
4.9 KiB
YAML
Raw Normal View History

---
- name: Dokku migration playbook
hosts: localhost
gather_facts: no
vars_files:
- config.yml
tasks:
- name: Create temporary directory for SSH keypair
tempfile:
state: directory
suffix: dokku_sync
register: temp_dir
- name: Generate SSH keypair
community.crypto.openssh_keypair:
path: "{{ temp_dir.path }}/dokku_sync_migration"
type: ed25519
owner: "{{ lookup('env', 'USER') }}"
group: "{{ lookup('env', 'USER') }}"
mode: '0600'
- name: Handle Source Server
hosts: source
gather_facts: yes
vars_files:
- config.yml
tasks:
- name: Get list of Dokku MariaDB services
shell: dokku mariadb:list | tail -n+2
register: mariadb_services
- name: Get list of Dokku PostgreSQL services
shell: dokku postgres:list | tail -n+2
register: postgres_services
- name: Get list of Dokku Redis services
shell: dokku redis:list | tail -n+2
register: redis_services
- name: Get list of Dokku MongoDB services
shell: dokku mongo:list | tail -n+2
register: mongo_services
- name: Save Dokku service lists to disk
copy:
content: |
mariadb_services: {{ mariadb_services.stdout_lines }}
postgres_services: {{ postgres_services.stdout_lines }}
redis_services: {{ redis_services.stdout_lines }}
mongo_services: {{ mongo_services.stdout_lines }}
dest: /tmp/dokku_services.yml
- name: Stop all Dokku apps
command: dokku ps:stop --all
- name: Stop all MariaDB services
command: dokku mariadb:stop {{ item }}
loop: "{{ mariadb_services.stdout_lines }}"
- name: Stop all PostgreSQL services
command: dokku postgres:stop {{ item }}
loop: "{{ postgres_services.stdout_lines }}"
- name: Stop all Redis services
command: dokku redis:stop {{ item }}
loop: "{{ redis_services.stdout_lines }}"
- name: Stop all MongoDB services
command: dokku mongo:stop {{ item }}
loop: "{{ mongo_services.stdout_lines }}"
- name: Stop Docker service
service:
name: docker
state: stopped
- name: Stop Docker socket service
service:
name: docker.socket
state: stopped
- name: Fetch Dokku service lists to local
fetch:
src: /tmp/dokku_services.yml
dest: /tmp/dokku_services.yml
flat: yes
- name: Handle Destination Server
hosts: destination
gather_facts: yes
vars_files:
- config.yml
tasks:
- name: Copy Dokku service lists to destination
copy:
src: /tmp/dokku_services.yml
dest: /tmp/dokku_services.yml
- name: Load Dokku service lists
include_vars:
file: /tmp/dokku_services.yml
- name: Stop all Dokku apps
command: dokku ps:stop --all
- name: Stop Docker service
service:
name: docker
state: stopped
- name: Stop Docker socket service
service:
name: docker.socket
state: stopped
- name: Create necessary directories
file:
path: "{{ item }}"
state: directory
owner: dokku
group: dokku
mode: '0755'
loop:
- /home/dokku/
- /home/data/
- /var/lib/dokku/
- name: Synchronize /home/dokku
synchronize:
src: "{{ source_host }}:/home/dokku/"
dest: /home/dokku/
archive: yes
delete: yes
- name: Synchronize /var/lib/dokku
synchronize:
src: "{{ source_host }}:/var/lib/dokku/"
dest: /var/lib/dokku/
archive: yes
delete: yes
- name: Synchronize /home/data
synchronize:
src: "{{ source_host }}:/home/data/"
dest: /home/data/
archive: yes
delete: yes
- name: Synchronize /home/git
synchronize:
src: "{{ source_host }}:/home/git/"
dest: /home/git/
archive: yes
delete: yes
- name: Start all MariaDB services
command: dokku mariadb:start {{ item }}
loop: "{{ mariadb_services }}"
- name: Start all PostgreSQL services
command: dokku postgres:start {{ item }}
loop: "{{ postgres_services }}"
- name: Start all Redis services
command: dokku redis:start {{ item }}
loop: "{{ redis_services }}"
- name: Start all MongoDB services
command: dokku mongo:start {{ item }}
loop: "{{ mongo_services }}"
- name: List Dokku apps
command: dokku apps:list
- name: Patch URLs in Dokku configuration files
find:
paths:
- /home/dokku/
patterns: ['VHOST', 'URLS']
recurse: yes
register: dokku_config_files
- name: Replace source host with destination host in configuration files
lineinfile:
path: "{{ item.path }}"
regexp: "{{ source_host }}"
line: "{{ destination_host }}"
loop: "{{ dokku_config_files.files }}"
- name: Display success message
debug:
msg: "SUCCESS"