-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker_odoo_operations_playbook.yml
254 lines (229 loc) · 9.33 KB
/
docker_odoo_operations_playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
---
- hosts: "docker_odoo"
name: "Docker Odoo Operations Playbook"
gather_facts: false
# Define variables used in this playbook and templates.
vars:
instance: "odootest"
odoo_instance: "{{ {'key': instance, 'value': odoo_instances[instance]} }}"
odoo_instance_setup: "{{ odoo_setup_conf[odoo_instance.value.odoo_setup_version | default(odoo_setup_version)] }}"
pull: true
tags:
- "never"
tasks:
# Restart whitelist containers.
- name: "Restart whitelist containers"
block:
- name: "Remove whitelist containers"
community.docker.docker_compose_v2:
project_src: "/home/docker"
files: "whitelists.yaml"
project_name: "whitelists"
remove_orphans: true
state: absent
- name: "Start whitelist containers"
community.docker.docker_compose_v2:
project_src: "/home/docker"
files: "whitelists.yaml"
project_name: "whitelists"
recreate: always
remove_orphans: true
state: present
# Check if the selected Odoo instance support rebuild tasks. If not, exit playbook.
- name: "Checks"
tags:
- "rebuild"
block:
- name: "Failed cannot build"
ansible.builtin.fail:
msg: "{{ odoo_instance.key }} instance cannot be built"
- name: "Ends playbook"
ansible.builtin.meta: end_play
when: not (odoo_instance.value.image_instance | default(false) == odoo_instance.key)
# All tasks needed to rebuild an instance.
- name: "Rebuild instance {{ odoo_instance.key }}"
tags:
- "rebuild"
when: odoo_instance.value.image_instance | default(false) == odoo_instance.key
block:
- name: "Create templates directory"
ansible.builtin.file:
name: "templates"
state: directory
mode: "0755"
connection: local
become: false
delegate_to: localhost
check_mode: no
- name: Get odoo template files
ansible.builtin.get_url:
url: "https://sources.le-filament.com/lefilament/ansible-roles/docker_odoo/-/raw/master/templates/{{ file }}"
dest: "templates/{{ file }}"
mode: "0644"
loop:
- "Dockerfile.j2"
loop_control:
loop_var: file
connection: local
become: false
delegate_to: localhost
check_mode: no
# Only pull image if pull is true.
- name: "Pull latest image"
vars:
odoo_image_favor: "{{ '_ml' if (odoo_instance.value.odoo_multilingual | default(false)) else '_py3.6' if (odoo_instance.value.odoo_python36 | default(false)) else '' }}"
community.docker.docker_image:
name: "lefilament/odoo:{{ odoo_instance_setup.odoo_version }}{{ odoo_image_favor }}"
source: pull
force_source: true
when: pull is truthy(convert_bool=True)
- name: "Copy Dockerfile to retrieve private repos and extra OCA ones"
vars:
template_odoo_instance: "{{ odoo_instance }}"
template_odoo_instance_setup: "{{ odoo_instance_setup }}"
ansible.builtin.template:
src: "templates/Dockerfile.j2"
dest: "/home/docker/{{ odoo_instance.key }}/odoo/Dockerfile"
owner: "root"
group: "root"
mode: "0644"
- name: "Copy private GitLab ssh keys file"
ansible.builtin.copy:
content: "{{ lf_gitlab_ro_privkey }}"
dest: "/home/docker/{{ odoo_instance.key }}/odoo/id_ed25519.sources"
owner: "root"
group: "root"
mode: "0400"
- name: "Rebuild instance images"
ansible.builtin.command:
chdir: "/home/docker/{{ odoo_instance.key }}/"
cmd: "docker compose build --no-cache"
async: 600
poll: 10
- name: "Remove instances private keys"
ansible.builtin.file:
path: "/home/docker/{{ odoo_instance.key }}/odoo/id_ed25519.sources"
state: absent
when: inventory_hostname not in groups['maintenance_contract']
- name: "Remove intermediate image"
community.docker.docker_prune:
builder_cache: true
images: true
images_filters:
label: "stage=builder"
when: inventory_hostname not in groups['maintenance_contract']
# Check if the selected Odoo instance support copy FROM TEST instance. If not, exit playbook.
- name: "Copy test image to prod instance"
tags:
- "test_image_to_prod"
- "restart_prod_on_test_image"
when: odoo_instance.value.prod_instance | default(false) == odoo_instance.key
block:
- name: "Check test image"
when: odoo_instance.value.image_instance is undefined
block:
- name: "Failed cannot copy image to prod"
ansible.builtin.fail:
msg: "test image cannot be copied to prod instance"
- name: "End playbook"
ansible.builtin.meta: end_play
- name: "Copy image to prod instance (selected instance is prod)"
community.docker.docker_image:
force_tag: true
name: "filament/{{ odoo_instance.value.image_instance }}:{{ odoo_instance_setup.odoo_version }}"
repository: "filament/{{ odoo_instance.key }}:{{ odoo_instance_setup.odoo_version }}"
source: local
- name: "Backup instance {{ odoo_instance.key }}"
tags:
- "backup"
- "never"
block:
- name: "Backup instance"
ansible.builtin.command:
cmd: "docker compose --file /home/docker/backups/backup-{{ odoo_instance.key }}1.yaml run --rm backup_odoo"
async: 600
poll: 10
register: result
- name: "Display backup result"
ansible.builtin.debug:
var: result.stderr_lines
when: result.stderr_lines is defined
- name: "Restore last prod database backup on instance {{ odoo_instance.key }}"
tags:
- "restart_on_prod_db"
ansible.builtin.command:
cmd: "docker compose --file /home/docker/backups/restore-{{ odoo_instance.key }}1.yaml run --rm restore_test"
when: not ansible_check_mode
async: 600
poll: 10
- name: "Update modules for instance {{ odoo_instance.key }}"
tags:
- "rebuild"
- "restart"
- "restart_prod_on_test_image"
- "update"
when: not ansible_check_mode
block:
- name: "Install Odoo modules on instance {{ odoo_instance.key }}"
ansible.builtin.command:
chdir: "/home/docker/{{ odoo_instance.key }}/"
cmd: "docker compose run --rm -l traefik.enable=false odoo odoo -d {{ odoo_instance.value.db }} -i {{ modules_to_install }} --stop-after-init"
async: 600
poll: 10
when: >
modules_to_update is not defined
and modules_to_install is defined
register: result
- name: "Display install result"
ansible.builtin.debug:
var: result.stderr_lines
when: result.stderr_lines is defined
- name: "Update Odoo modules on instance {{ odoo_instance.key }}"
ansible.builtin.command:
chdir: "/home/docker/{{ odoo_instance.key }}/"
cmd: "docker compose run --rm -l traefik.enable=false odoo odoo -d {{ odoo_instance.value.db }} -u {{ modules_to_update }} --i18n-overwrite --stop-after-init"
register: result
when: >
modules_to_update is defined
and modules_to_install is not defined
async: 600
poll: 10
- name: "Display update result"
ansible.builtin.debug:
var: result.stderr_lines
when: result.stderr_lines is defined
- name: "Install and update Odoo modules on instance {{ odoo_instance.key }}"
ansible.builtin.command:
chdir: "/home/docker/{{ odoo_instance.key }}/"
cmd: "docker compose run --rm -l traefik.enable=false odoo odoo -d {{ odoo_instance.value.db }} -u {{ modules_to_update }} -i {{ modules_to_install }} --i18n-overwrite --stop-after-init"
async: 600
poll: 10
register: result
when: modules_to_update is defined and modules_to_install is defined
- name: "Display install and/or update result"
ansible.builtin.debug:
var: result.stderr_lines
when: result.stderr_lines is defined
- name: "Restart instance {{ odoo_instance.key }}"
tags:
- "rebuild"
- "restart"
- "restart_on_prod_db"
- "restart_prod_on_test_image"
when: not ansible_check_mode
block:
- name: "Restart instances: remove instance containers"
community.docker.docker_compose_v2:
project_src: "/home/docker/{{ odoo_instance.key }}/"
remove_orphans: true
state: absent
async: 600
poll: 10
- name: "Restart instances: start instance containers"
community.docker.docker_compose_v2:
project_src: /home/docker/{{ odoo_instance.key }}/
recreate: always
remove_orphans: true
state: present
async: 600
poll: 10