Skip to content

Commit 2fe7a21

Browse files
committed
Added the migration_tester role
1 parent b43472c commit 2fe7a21

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

roles/migration_tester/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Migration Tester
2+
3+
This role is for a VM to test out database migrations. If it breaks something, the database can be reset to a previous point by a docker recreate, without needing to sql import the database again.
4+
5+
## Configuration
6+
7+
To configure the role, you need to set the required variables in your Ansible playbook or inventory. The following variables are required:
8+
9+
- `artemis_database_password`: The Artemis Database password
10+
11+
When adding a new database image, additionally this will have to be added:
12+
- `dump_src`: The local path of the db backup, as .sql.gz file
13+
- `bake: true`
14+
15+
Running without bake, the database image will be recreated and therefore be reset to the last bake run.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
artemis_database_dbname: artemis
3+
artemis_database_username: artemis
4+
artemis_database_password: #FIXME
5+
artemis_database_temp_container_name: artemis-temp-db
6+
artemis_database_container_name: artemis-db
7+
8+
artemis_working_directory: "/opt/artemis"
9+
10+
db_port: 3306
11+
12+
mysql_base: mysql:8.0.45
13+
mysql_baked: artemis_db
14+
bake: false
15+
16+
dump_src: #FIXME
17+
dump_dest: "/tmp"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
- name: Copy db backup
3+
ansible.builtin.copy:
4+
src: "{{ dump_src }}"
5+
dest: "{{ dump_dest }}/artemis.sql.gz"
6+
7+
- name: Unzip db backup
8+
ansible.builtin.command: "gunzip -df {{ dump_dest }}/artemis.sql.gz"
9+
10+
- name: Make sure the baked image is not present
11+
community.docker.docker_image_remove:
12+
name: "{{ mysql_baked }}"
13+
force: true
14+
15+
- name: Make sure there is no old baking data
16+
become: true
17+
ansible.builtin.file:
18+
path: "/tmp/mysql"
19+
state: absent
20+
21+
- name: Create baking directory
22+
become: true
23+
become_user: artemis
24+
file:
25+
path: "/tmp/mysql"
26+
state: directory
27+
mode: "770"
28+
29+
- name: Start db for baking and import
30+
community.docker.docker_container:
31+
name: "{{ artemis_database_temp_container_name }}"
32+
image: "{{ mysql_base }}"
33+
state: healthy
34+
pull: missing
35+
recreate: true
36+
restart_policy: unless-stopped
37+
user: 1337:1337
38+
command: --lower_case_table_names=1 --tls-version='' --character_set_server=utf8mb4 --collation-server=utf8mb4_unicode_ci --explicit_defaults_for_timestamp --max_connections=100000
39+
env:
40+
MYSQL_ROOT_PASSWORD: "{{ artemis_database_password }}"
41+
MYSQL_USER: "{{ artemis_database_username }}"
42+
MYSQL_PASSWORD: "{{ artemis_database_password }}"
43+
MYSQL_DATABASE: "{{ artemis_database_dbname }}"
44+
published_ports:
45+
- "{{ db_port }}:3306"
46+
volumes:
47+
- "{{ dump_dest }}/artemis.sql:/docker-entrypoint-initdb.d/init.sql:ro"
48+
- "/tmp/mysql:/var/lib/mysql"
49+
healthcheck:
50+
test: mysqladmin ping -h 127.0.0.1 -u root --silent
51+
interval: 2s
52+
timeout: 3s
53+
retries: 60
54+
55+
- name: Stop baking db
56+
community.docker.docker_container:
57+
name: "{{ artemis_database_temp_container_name }}"
58+
state: stopped
59+
60+
- name: Create migration.cnf
61+
ansible.builtin.copy:
62+
dest: /tmp/migration.cnf
63+
content: |
64+
[mysqld]
65+
datadir=/var/lib/mysqlsafe
66+
67+
- name: Copy data
68+
ansible.builtin.command:
69+
cmd: "docker cp /tmp/mysql artemis-temp-db:/var/lib/mysqlsafe"
70+
71+
- name: Copy migration.cnf into container
72+
ansible.builtin.command:
73+
cmd: "docker cp /tmp/migration.cnf artemis-temp-db:/etc/mysql/conf.d/migration.cnf"
74+
75+
- name: Commit to new image
76+
ansible.builtin.command:
77+
cmd: "docker commit {{ artemis_database_temp_container_name }} {{ mysql_baked }}"
78+
changed_when: true
79+
80+
- name: Delete archive file
81+
ansible.builtin.file:
82+
state: absent
83+
path: "{{ dump_dest }}/artemis.sql.gz"
84+
85+
- name: Delete sql file
86+
ansible.builtin.file:
87+
state: absent
88+
path: "{{ dump_dest }}/artemis.sql"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
- name: Add database env variables
3+
become: true
4+
lineinfile:
5+
path: /etc/environment
6+
regexp: '^MYSQL_IMAGE='
7+
line: 'MYSQL_IMAGE="artemis_db"'
8+
9+
- name: Shut down Artemis
10+
community.docker.docker_compose_v2:
11+
project_src: "{{ artemis_working_directory }}/Artemis/docker"
12+
files:
13+
- "test-server-mysql.yml"
14+
env_files:
15+
- "{{ artemis_working_directory }}/docker.env"
16+
state: absent
17+
remove_volumes: true
18+
become: true
19+
20+
- name: Remove docker data
21+
become: true
22+
ansible.builtin.file:
23+
path: "{{ artemis_working_directory }}/data/database"
24+
state: absent
25+
26+
- name: Create artemis database directory
27+
become: true
28+
become_user: artemis
29+
file:
30+
path: "{{ artemis_working_directory }}/data/database"
31+
state: directory
32+
mode: "770"
33+
34+
- name: Run bake
35+
ansible.builtin.include_tasks: bake.yml
36+
when: bake | bool
37+
38+
- name: Start Artemis
39+
community.docker.docker_compose_v2:
40+
project_src: "{{ artemis_working_directory }}/Artemis/docker"
41+
files:
42+
- "test-server-mysql.yml"
43+
env_files:
44+
- "{{ artemis_working_directory }}/docker.env"
45+
pull: missing
46+
state: present
47+
recreate: always
48+
become: true

0 commit comments

Comments
 (0)