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

Commit

Permalink
feat: container hooks with executable files
Browse files Browse the repository at this point in the history
  • Loading branch information
saitho committed Dec 19, 2020
1 parent f1abed2 commit d61c10e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
36 changes: 29 additions & 7 deletions tasks/steps/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
---
- name: "StackHead::Container || Build src folder list || Project: {{ project_name }}"
block:
- name: "Container hooks location"
set_fact:
managedContainerFolders: ["{{ stackhead__containerhooks_location }}"]
- name: "Collect local volumes"
set_fact:
managedContainerVolumePaths: "{{ managedContainerVolumePaths|default([]) + [ ''~ stackhead__containerdata_location_services|format(item.0.name, item.1.src|default()) ~'' ] }}"
managedContainerFolders: "{{ managedContainerFolders|default([]) + [ ''~ stackhead__containerdata_location_services|format(item.0.name, item.1.src|default()) ~'' ] }}"
when: item.1.type == 'local'
with_subelements:
- "{{ app_config.container.services }}"
Expand All @@ -13,7 +16,7 @@
skip_missing: True
- name: "Collect global volumes"
set_fact:
managedContainerVolumePaths: "{{ managedContainerVolumePaths|default([]) + [ '' ~ stackhead__containerdata_location_global|format(item.1.src|default()) ~ '' ] }}"
managedContainerFolders: "{{ managedContainerFolders|default([]) + [ '' ~ stackhead__containerdata_location_global|format(item.1.src|default()) ~ '' ] }}"
when: item.1.type == 'global'
with_subelements:
- "{{ app_config.container.services }}"
Expand All @@ -22,26 +25,45 @@
skip_missing: True
- name: "Collect custom volumes"
set_fact:
managedContainerVolumePaths: "{{ managedContainerVolumePaths|default([]) + [ '' ~ item.1.src ~ '' ] }}"
managedContainerFolders: "{{ managedContainerFolders|default([]) + [ '' ~ item.1.src ~ '' ] }}"
when: item.1.type == 'custom'
with_subelements:
- "{{ app_config.container.services }}"
- volumes
- flags:
skip_missing: True
- block:
- name: "StackHead::Container || Checking volume folders"
- name: "StackHead::Container || Checking project Docker folders"
stat:
path: "{{ item }}"
register: folder_stats
with_items: "{{ managedContainerVolumePaths }}"
- name: "StackHead::Container || Creating missing volume folders"
with_items: "{{ managedContainerFolders }}"
- name: "StackHead::Container || Creating missing project Docker folders"
file:
path: "{{ item.item }}"
state: directory
mode: 0755
when: item.stat.exists == false
with_items: "{{ folder_stats.results }}"
when: managedContainerVolumePaths is defined
when: managedContainerFolders is defined
- name: remove old hook files
raw: "rm -rf {{ stackhead__containerhooks_location }}/*"
- block:
- set_fact:
collected_hooks: "{{ collected_hooks|default([]) + [{ 'src': item.hooks.execute_after_setup, 'prefix': 'afterSetup' }] }}"
when: item.hooks.execute_after_setup is defined
with_items: "{{ app_config.container.services }}"
- set_fact:
collected_hooks: "{{ collected_hooks|default([]) + [{ 'src': item.hooks.execute_before_destroy, 'prefix': 'beforeDestroy' }] }}"
when: item.hooks.execute_before_destroy is defined
with_items: "{{ app_config.container.services }}"
- name: Copy hook files
synchronize:
src: "{{ stackhead__local_config_file|dirname }}/{{ item.src }}"
dest: "{{ stackhead__containerhooks_location }}/{{ item.prefix}}_{{ item.src|basename }}"
perms: false
rsync_opts:
- "--chmod=F755"
with_items: "{{ collected_hooks|default([]) }}"
- name: "StackHead::Container || Generate Terraform Docker configuration file | Project: {{ project_name }}"
include_tasks: "{{ module_role_path | default(role_path) }}/tasks/docker_steps/generate-docker-tf.yml"
8 changes: 7 additions & 1 deletion tasks/steps/sub/execute-destroy-hook.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
---
- raw: "docker exec stackhead-{{ project_name }}-{{ outer_item.name }} {{ item|getstackhead.stackhead.TFescapeDoubleQuotes }}"
with_items: "{{ outer_item.hooks.before_destroy }}"
with_items: "{{ outer_item.hooks.before_destroy|default([]) }}"
- find:
path: "{{ stackhead__containerhooks_location }}"
patterns: "beforeDestroy_*"
register: destroy_hook_files
- 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 }}"
with_items: "{{ destroy_hook_files.files }}"
5 changes: 5 additions & 0 deletions templates/terraform/docker.tf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ resource "docker_container" "stackhead-{{ project_name }}-{{ service.name }}" {
restart = "always"

{% if service.hooks is defined %}
{% if service.hooks.execute_after_setup is defined %}
provisioner "local-exec" {
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 }}"
}
{% endif %}
{% if service.hooks.after_setup is defined %}
{% for hook in service.hooks.after_setup %}
provisioner "local-exec" {
Expand Down
1 change: 1 addition & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
stackhead__containerdata_location: "{{ stackhead__project_folder }}/container_data"
stackhead__containerhooks_location: "{{ stackhead__project_folder }}/container_hooks"
stackhead__containerdata_location_global: "{{ stackhead__containerdata_location }}/global/%s" # %s = src
stackhead__containerdata_location_services: "{{ stackhead__containerdata_location }}/services/%s/%s" # %s = service name, %s = src

0 comments on commit d61c10e

Please sign in to comment.