/
main.yml
391 lines (387 loc) · 15.9 KB
/
main.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# "cb" = shortened for codebase
# "qs" = shortened for quick starts
---
- hosts: "{{ variable_host | default('control') }}"
vars:
project_ini: "project section=Settings file={{ settings_dir }}/{{ settings_file }}"
saved_project: "{{ lookup('ini', project_ini) }}"
operation: "{{ (op is defined) | ternary(op, 'none') }}"
use_project: "{{ (cb is defined) | ternary(cb, saved_project) }}"
template: "{{ (tpl is defined) | ternary(tpl, 'vanilla') }}"
qs_branch: "{{ (template == 'elib-blog') | ternary('bootstrap4', 'master') }}"
arch_ini: "arch section=Settings file={{ settings_dir }}/{{ settings_file }}"
saved_arch: "{{ lookup('ini', arch_ini) }}"
use_arch: "{{ (saved_arch == 'pi') | ternary('pi', '') }}"
host_ini: "host section=Settings file={{ settings_dir }}/{{ settings_file }}"
saved_host: "{{ lookup('ini', host_ini) }}"
use_host: "{{ (saved_host != '') | ternary(saved_host, 'www.dev.org') }}"
gather_facts: no
tasks:
- name: default
block:
- name: "show error"
debug:
msg: "No operation specified."
when: operation == "none"
- name: boot
block:
- name: install python docker deps
pip:
virtualenv: "{{ virtualenv_dir }}"
name:
- cython<3.0.0
- docker==6.1.3
- name: install python docker
pip:
virtualenv: "{{ virtualenv_dir }}"
name:
- docker-compose
extra_args: --no-build-isolation
- name: populate docker-compose
template:
src: "{{ playbook_dir }}/env/docker-compose.j2"
dest: "{{ playbook_dir }}/docker-compose.yml"
- name: stop any existing containers
docker_compose:
project_src: "{{ playbook_dir }}"
state: absent
- name: boot containers
docker_compose:
project_src: "{{ playbook_dir }}"
state: present
# is below possible when
# potentially an open docker port is
# being detected?
# (setting approximate delay)
- name: wait for db
ansible.builtin.wait_for:
port: 3306
delay: 20
host: "{{ use_host }}"
- name: detect build
stat:
path: "{{ code_dir }}/{{ use_project }}/vendor"
register: st
- name: install
include_tasks: "{{ playbook_dir }}/main_install.yml"
when: st.stat.exists == false
- name: configure
include_tasks: "{{ playbook_dir }}/main_configure.yml"
- name: detect db setup file
stat:
path: "{{ code_dir }}/{{ use_project }}/setup.sql"
register: stsetup
- name: detect db dump file
stat:
path: "{{ code_dir }}/{{ use_project }}/dump.sql"
register: stdump
- name: populate db (dump)
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --mysql populate"
when: stsetup.stat.exists and stdump.stat.exists
- name: populate db (setup)
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --mysql setup"
when: stsetup.stat.exists and not stdump.stat.exists
when: operation == "boot"
- name: switch
block:
- name: save settings
include_tasks: "{{ playbook_dir }}/main_save_settings.yml"
when: operation == "switch"
- name: qs
block:
- name: detect dir name
stat:
path: "{{ code_dir }}/{{ use_project }}"
register: st
- name: exit if dir exists
ansible.builtin.fail:
msg: Directory exists.
when: st.stat.exists == true
- name: create directory
file:
path: "{{ code_dir }}/{{ use_project }}"
state: directory
- name: populate composer config
template:
src: "{{ playbook_dir }}/env/templates/vanilla/composer.j2"
dest: "{{ code_dir }}/{{ use_project }}/composer.json"
- name: install python docker deps
pip:
virtualenv: "{{ virtualenv_dir }}"
name:
- cython<3.0.0
- docker==6.1.3
- name: install python docker
pip:
virtualenv: "{{ virtualenv_dir }}"
name:
- docker-compose
- pexpect
- sqlfluff
extra_args: --no-build-isolation
- name: populate docker-compose
template:
src: "{{ playbook_dir }}/env/docker-compose.j2"
dest: "{{ playbook_dir }}/docker-compose.yml"
- name: stop any existing containers
docker_compose:
project_src: "{{ playbook_dir }}"
state: absent
- name: boot containers
docker_compose:
project_src: "{{ playbook_dir }}"
state: present
- name: install
include_tasks: "{{ playbook_dir }}/main_install.yml"
- name: configure
include_tasks: "{{ playbook_dir }}/main_configure.yml"
- name: save settings
include_tasks: "{{ playbook_dir }}/main_save_settings.yml"
when: operation == "qs"
- name: qs elib-base template
block:
- name: populate elib.yml
template:
src: "{{ playbook_dir }}/env/templates/elib-base/elib.j2"
dest: "{{ code_dir }}/{{ use_project }}/elib.yml"
- name: enable elib
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --set_elib"
- name: get installed elibs
command: docker exec -i -u www-data app php ./vendor/bin/empathy --get_elibs
register: installed_elibs
- name: set installed libs list
ansible.builtin.set_fact:
installed_elibs_list: "{{ installed_elibs.stdout | from_json }}"
- name: 'set file name'
ansible.builtin.set_fact:
input_file: 'dd.sql'
- name: initialise data array
ansible.builtin.set_fact:
data_found: [ ]
- name: read dd
include_tasks: "{{ playbook_dir }}/main_read_files.yml"
loop: "{{ installed_elibs_list }}"
- name: get dd tables
command: docker exec -i -u www-data app php ./vendor/bin/empathy --get_elib_tables
register: tables
- name: set table list
ansible.builtin.set_fact:
table_list: "{{ tables.stdout | from_json }}"
- name: populate setup.sql
template:
src: "{{ playbook_dir }}/env/templates/elib-base/setup.j2"
dest: "{{ code_dir }}/{{ use_project }}/setup.sql"
- name: 'set file name'
ansible.builtin.set_fact:
input_file: 'dm.sql'
- name: initialise data array
ansible.builtin.set_fact:
data_found: [ ]
- name: read dm
include_tasks: "{{ playbook_dir }}/main_read_files.yml"
loop: "{{ installed_elibs_list }}"
- name: populate inserts.sql
template:
src: "{{ playbook_dir }}/env/templates/elib-base/inserts.j2"
dest: "{{ code_dir }}/{{ use_project }}/inserts.sql"
- name: 'set file name'
ansible.builtin.set_fact:
input_file: 'inserts.sql'
- name: lint inserts
include_tasks: "{{ playbook_dir }}/main_lint_sql.yml"
- name: 'set file name'
ansible.builtin.set_fact:
input_file: 'setup.sql'
- name: lint setup
include_tasks: "{{ playbook_dir }}/main_lint_sql.yml"
- name: populate db setup
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --mysql setup"
- name: admin module
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --inst_mod admin"
- name: admin module
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --inst_mod user"
- name: clear cache
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --clear_cache"
when: operation == "qs" and template != "vanilla"
- name: qs elib-acl template
block:
# overwrite dm.sql
- name: read dm.sql
slurp:
src: "{{ code_dir }}/{{ use_project }}/vendor/mikejw/elib-acl/dm.sql"
register: data
- name: initialise data array
ansible.builtin.set_fact:
data_found: "{{ [ data['content'] | b64decode ] }}"
- name: populate inserts.sql
template:
src: "{{ playbook_dir }}/env/templates/elib-base/inserts.j2"
dest: "{{ code_dir }}/{{ use_project }}/inserts.sql"
- name: 'set file name'
ansible.builtin.set_fact:
input_file: 'inserts.sql'
- name: lint
include_tasks: "{{ playbook_dir }}/main_lint_sql.yml"
# setup plugins
- name: set acl plugin config
ansible.builtin.set_fact:
json:
name: Empathy\ELib\MVC\Plugin\Acl
version: "1.0"
- name: assert acl plugin
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --assert_plugin '{{ json | to_json }}'"
- name: set json view plugin config
ansible.builtin.set_fact:
json:
name: JSONView
version: "1.0"
config: '[{ "api": {} }, { "api2": {} }, { "api3": {} }]'
- name: assert json view plugin
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --assert_plugin '{{ json | to_json }}'"
# modify elib
- name: open elib config
slurp:
path: "{{ code_dir }}/{{ use_project }}/elib.yml"
register: r_elibconfig
- name: read yaml
set_fact:
elibconfigdata: "{{ r_elibconfig['content'] | b64decode | from_yaml }}"
- name: patch yaml
set_fact:
elibconfigdata: "{{ elibconfigdata | combine(newdata, recursive=True) }}"
vars:
newdata:
jwt_secret: my_super_secret_key
- name: write yaml
copy:
content: '{{ elibconfigdata | to_nice_yaml }}'
dest: "{{ code_dir }}/{{ use_project }}/elib.yml"
# templates
- name: populate services.php
template:
src: "{{ playbook_dir }}/env/templates/elib-acl/services.j2"
dest: "{{ code_dir }}/{{ use_project }}/services.php"
- name: populate admin.php
template:
src: "{{ playbook_dir }}/env/templates/elib-acl/application/admin.j2"
dest: "{{ code_dir }}/{{ use_project }}/application/admin/admin.php"
- name: generate api module
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --inst_mod api"
- name: clear cache
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --clear_cache"
- name: populate db
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --mysql setup"
- name: create module dir api2
file:
path: "{{ code_dir }}/{{ use_project }}/application/api2"
state: directory
- name: create module dir api3
file:
path: "{{ code_dir }}/{{ use_project }}/application/api3"
state: directory
- name: populate api2.php
template:
src: "{{ playbook_dir }}/env/templates/elib-acl/application/api2.j2"
dest: "{{ code_dir }}/{{ use_project }}/application/api2/api2.php"
- name: populate api3.php
template:
src: "{{ playbook_dir }}/env/templates/elib-acl/application/api3.j2"
dest: "{{ code_dir }}/{{ use_project }}/application/api3/api3.php"
when: operation == "qs" and template == "elib-acl"
- name: qs elib-blog template
block:
- name: debug branch
debug:
var: qs_branch
- name: generate front blog module
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --inst_mod blog"
- name: generate front blog feed module
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --inst_mod feed"
- name: populate db
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --mysql setup"
# modify elib
- name: open elib config
slurp:
path: "{{ code_dir }}/{{ use_project }}/elib.yml"
register: r_elibconfig
- name: read yaml
set_fact:
elibconfigdata: "{{ r_elibconfig['content'] | b64decode | from_yaml }}"
- name: patch yaml
set_fact:
elibconfigdata: "{{ elibconfigdata | combine(newdata, recursive=True) }}"
vars:
newdata:
blog_title: An Empathy Blog
blog_description: Some words from me
truncate_blog_items: true
fetch_blog_images: false
def_blog_category: 0
def_blog_per_page: 3
blog_social_links: '{ "X": "https://twitter.com/mikejw" }'
- name: write yaml
copy:
content: '{{ elibconfigdata | to_nice_yaml }}'
dest: "{{ code_dir }}/{{ use_project }}/elib.yml"
- name: copy application files
copy:
src: "{{ playbook_dir }}/env/templates/elib-blog/application/"
dest: "{{ code_dir }}/{{ use_project }}/application"
- name: copy tpl files
copy:
src: "{{ playbook_dir }}/env/templates/elib-blog/tpl/"
dest: "{{ code_dir }}/{{ use_project }}/presentation"
- name: copy htaccess files
copy:
src: "{{ playbook_dir }}/env/templates/elib-blog/public_html/htaccess"
dest: "{{ code_dir }}/{{ use_project }}/public_html/.htaccess"
- name: copy public files
copy:
src: "{{ playbook_dir }}/env/templates/elib-blog/public_html/"
dest: "{{ code_dir }}/{{ use_project }}/public_html"
- name: set redbean plugin
ansible.builtin.set_fact:
json:
name: Redbean
version: "1.0"
- name: assert redbean plugin
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --assert_plugin '{{ json | to_json }}'"
- name: set site info plugin
ansible.builtin.set_fact:
json:
name: Empathy\ELib\MVC\Plugin\SiteInfo
version: "1.0"
- name: assert site info plugin
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --assert_plugin '{{ json | to_json }}'"
- name: disable error handling
ansible.builtin.lineinfile:
path: "{{ code_dir }}/{{ use_project }}/config.yml"
regexp: 'handle_errors'
line: " handle_errors: false"
- name: enable apcu debug
ansible.builtin.lineinfile:
path: "{{ code_dir }}/{{ use_project }}/services.php"
regexp: 'ApcuDebug'
line: " 'ApcuDebug' => true,"
- name: set default module
ansible.builtin.lineinfile:
path: "{{ code_dir }}/{{ use_project }}/config.yml"
regexp: 'default_module'
line: " default_module: blog"
- name: populate services.php
template:
src: "{{ playbook_dir }}/env/templates/elib-blog/services.j2"
dest: "{{ code_dir }}/{{ use_project }}/services.php"
- name: create uploads dir
file:
path: "{{ code_dir }}/{{ use_project }}/public_html/uploads"
state: directory
- name: clear cache
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --clear_cache"
- name: copy dump file
copy:
src: "{{ playbook_dir }}/env/templates/elib-blog/dump.sql"
dest: "{{ code_dir }}/{{ use_project }}"
- name: populate db
command: "docker exec -i -u www-data app php ./vendor/bin/empathy --mysql populate"
when: operation == "qs" and template == "elib-blog"