Skip to content
This repository was archived by the owner on Aug 21, 2022. It is now read-only.

Commit d61c10e

Browse files
committed
feat: container hooks with executable files
1 parent f1abed2 commit d61c10e

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

tasks/steps/deploy.yml

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
---
33
- name: "StackHead::Container || Build src folder list || Project: {{ project_name }}"
44
block:
5+
- name: "Container hooks location"
6+
set_fact:
7+
managedContainerFolders: ["{{ stackhead__containerhooks_location }}"]
58
- name: "Collect local volumes"
69
set_fact:
7-
managedContainerVolumePaths: "{{ managedContainerVolumePaths|default([]) + [ ''~ stackhead__containerdata_location_services|format(item.0.name, item.1.src|default()) ~'' ] }}"
10+
managedContainerFolders: "{{ managedContainerFolders|default([]) + [ ''~ stackhead__containerdata_location_services|format(item.0.name, item.1.src|default()) ~'' ] }}"
811
when: item.1.type == 'local'
912
with_subelements:
1013
- "{{ app_config.container.services }}"
@@ -13,7 +16,7 @@
1316
skip_missing: True
1417
- name: "Collect global volumes"
1518
set_fact:
16-
managedContainerVolumePaths: "{{ managedContainerVolumePaths|default([]) + [ '' ~ stackhead__containerdata_location_global|format(item.1.src|default()) ~ '' ] }}"
19+
managedContainerFolders: "{{ managedContainerFolders|default([]) + [ '' ~ stackhead__containerdata_location_global|format(item.1.src|default()) ~ '' ] }}"
1720
when: item.1.type == 'global'
1821
with_subelements:
1922
- "{{ app_config.container.services }}"
@@ -22,26 +25,45 @@
2225
skip_missing: True
2326
- name: "Collect custom volumes"
2427
set_fact:
25-
managedContainerVolumePaths: "{{ managedContainerVolumePaths|default([]) + [ '' ~ item.1.src ~ '' ] }}"
28+
managedContainerFolders: "{{ managedContainerFolders|default([]) + [ '' ~ item.1.src ~ '' ] }}"
2629
when: item.1.type == 'custom'
2730
with_subelements:
2831
- "{{ app_config.container.services }}"
2932
- volumes
3033
- flags:
3134
skip_missing: True
3235
- block:
33-
- name: "StackHead::Container || Checking volume folders"
36+
- name: "StackHead::Container || Checking project Docker folders"
3437
stat:
3538
path: "{{ item }}"
3639
register: folder_stats
37-
with_items: "{{ managedContainerVolumePaths }}"
38-
- name: "StackHead::Container || Creating missing volume folders"
40+
with_items: "{{ managedContainerFolders }}"
41+
- name: "StackHead::Container || Creating missing project Docker folders"
3942
file:
4043
path: "{{ item.item }}"
4144
state: directory
4245
mode: 0755
4346
when: item.stat.exists == false
4447
with_items: "{{ folder_stats.results }}"
45-
when: managedContainerVolumePaths is defined
48+
when: managedContainerFolders is defined
49+
- name: remove old hook files
50+
raw: "rm -rf {{ stackhead__containerhooks_location }}/*"
51+
- block:
52+
- set_fact:
53+
collected_hooks: "{{ collected_hooks|default([]) + [{ 'src': item.hooks.execute_after_setup, 'prefix': 'afterSetup' }] }}"
54+
when: item.hooks.execute_after_setup is defined
55+
with_items: "{{ app_config.container.services }}"
56+
- set_fact:
57+
collected_hooks: "{{ collected_hooks|default([]) + [{ 'src': item.hooks.execute_before_destroy, 'prefix': 'beforeDestroy' }] }}"
58+
when: item.hooks.execute_before_destroy is defined
59+
with_items: "{{ app_config.container.services }}"
60+
- name: Copy hook files
61+
synchronize:
62+
src: "{{ stackhead__local_config_file|dirname }}/{{ item.src }}"
63+
dest: "{{ stackhead__containerhooks_location }}/{{ item.prefix}}_{{ item.src|basename }}"
64+
perms: false
65+
rsync_opts:
66+
- "--chmod=F755"
67+
with_items: "{{ collected_hooks|default([]) }}"
4668
- name: "StackHead::Container || Generate Terraform Docker configuration file | Project: {{ project_name }}"
4769
include_tasks: "{{ module_role_path | default(role_path) }}/tasks/docker_steps/generate-docker-tf.yml"
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
---
22
- raw: "docker exec stackhead-{{ project_name }}-{{ outer_item.name }} {{ item|getstackhead.stackhead.TFescapeDoubleQuotes }}"
3-
with_items: "{{ outer_item.hooks.before_destroy }}"
3+
with_items: "{{ outer_item.hooks.before_destroy|default([]) }}"
4+
- find:
5+
path: "{{ stackhead__containerhooks_location }}"
6+
patterns: "beforeDestroy_*"
7+
register: destroy_hook_files
8+
- raw: "docker cp -a {{ item.path }} stackhead-{{ project_name }}-{{ service.name }}:/{{ item.path|basename }} && docker exec stackhead-{{ project_name }}-{{ service.name }} sh /{{ item.path|basename }}"
9+
with_items: "{{ destroy_hook_files.files }}"

templates/terraform/docker.tf.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ resource "docker_container" "stackhead-{{ project_name }}-{{ service.name }}" {
1717
restart = "always"
1818

1919
{% if service.hooks is defined %}
20+
{% if service.hooks.execute_after_setup is defined %}
21+
provisioner "local-exec" {
22+
command = "docker cp -a {{ stackhead__containerhooks_location }}/afterSetup_{{ service.hooks.execute_after_setup|basename }} stackhead-{{ project_name }}-{{ service.name }}:/afterSetup_{{ service.hooks.execute_after_setup|basename }} && docker exec stackhead-{{ project_name }}-{{ service.name }} sh /afterSetup_{{ service.hooks.execute_after_setup|basename }}"
23+
}
24+
{% endif %}
2025
{% if service.hooks.after_setup is defined %}
2126
{% for hook in service.hooks.after_setup %}
2227
provisioner "local-exec" {

vars/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
22
stackhead__containerdata_location: "{{ stackhead__project_folder }}/container_data"
3+
stackhead__containerhooks_location: "{{ stackhead__project_folder }}/container_hooks"
34
stackhead__containerdata_location_global: "{{ stackhead__containerdata_location }}/global/%s" # %s = src
45
stackhead__containerdata_location_services: "{{ stackhead__containerdata_location }}/services/%s/%s" # %s = service name, %s = src

0 commit comments

Comments
 (0)