From 6f47fe5ef89dc9723914d5404955c2ff68af71d6 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Sun, 17 Jan 2021 17:33:23 +0100 Subject: [PATCH] feat: adjust volume folder owner if set --- tasks/steps/deploy.yml | 22 ++++++++++++++++------ templates/terraform/docker.tf.j2 | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/tasks/steps/deploy.yml b/tasks/steps/deploy.yml index 52a53d7..01beea9 100644 --- a/tasks/steps/deploy.yml +++ b/tasks/steps/deploy.yml @@ -4,10 +4,10 @@ block: - name: "Container hooks location" set_fact: - managedContainerFolders: ["{{ stackhead__containerhooks_location }}"] + managedContainerFolders: [{ 'path': "{{ stackhead__containerhooks_location }}" }] - name: "Collect local volumes" set_fact: - managedContainerFolders: "{{ managedContainerFolders|d([]) + [ ''~ stackhead__containerdata_location_services|format(item.0.name, item.1.src|d()) ~'' ] }}" + managedContainerFolders: "{{ managedContainerFolders|d([]) + [ { 'path': stackhead__containerdata_location_services|format(item.0.name, item.1.src|d()), 'user': item.1.user|d('') } ] }}" when: item.1.type == 'local' with_subelements: - "{{ app_config.container.services }}" @@ -16,7 +16,7 @@ skip_missing: True - name: "Collect global volumes" set_fact: - managedContainerFolders: "{{ managedContainerFolders|d([]) + [ '' ~ stackhead__containerdata_location_global|format(item.1.src|d()) ~ '' ] }}" + managedContainerFolders: "{{ managedContainerFolders|d([]) + [ { 'path': stackhead__containerdata_location_global|format(item.1.src|d()), 'user': item.1.user|d('') } ] }}" when: item.1.type == 'global' with_subelements: - "{{ app_config.container.services }}" @@ -25,7 +25,7 @@ skip_missing: True - name: "Collect custom volumes" set_fact: - managedContainerFolders: "{{ managedContainerFolders|d([]) + [ '' ~ item.1.src ~ '' ] }}" + managedContainerFolders: "{{ managedContainerFolders|d([]) + [ { 'path': item.1.src, 'user': item.1.user|d('') } ] }}" when: item.1.type == 'custom' with_subelements: - "{{ app_config.container.services }}" @@ -35,17 +35,27 @@ - block: - name: "StackHead::Container || Checking project Docker folders" stat: - path: "{{ item }}" + path: "{{ item.path }}" register: folder_stats with_items: "{{ managedContainerFolders }}" - name: "StackHead::Container || Creating missing project Docker folders" file: - path: "{{ item.item }}" + path: "{{ item.item.path }}" state: directory mode: 0755 + owner: stackhead + group: stackhead when: item.stat.exists == false with_items: "{{ folder_stats.results }}" + - name: Adjust Docker folder permissions + file: + path: "{{ item.path }}" + owner: "{{ (item.user|string).split(':')[0] }}" + group: stackhead + when: item.user is defined and item.user != '' + with_items: "{{ managedContainerFolders }}" when: managedContainerFolders|d([]) != [] + - name: remove old hook files raw: "rm -rf {{ stackhead__containerhooks_location }}/*" - block: diff --git a/templates/terraform/docker.tf.j2 b/templates/terraform/docker.tf.j2 index 358e83a..f5ebbd8 100644 --- a/templates/terraform/docker.tf.j2 +++ b/templates/terraform/docker.tf.j2 @@ -20,6 +20,24 @@ resource "docker_container" "stackhead-{{ project_name }}-{{ service.name }}" { name = "stackhead-{{ project_name }}-{{ service.name }}" restart = "always" +{# Ensure volume directories have correct user (there was an issue with mariadb) #} +{%- for volume in service.volumes|default([]) %} + +{%- if volume.type == "local" %} + {%- set path = stackhead__containerdata_location_services|format(service.name, volume.src) %} +{%- elif volume.type == "custom" %} + {%- set path = volume.src %} +{%- elif volume.type == "global" %} + {%- set path = stackhead__containerdata_location_global|format(volume.src) %} +{%- endif %} + +{%- if volume.user|d('') != '' and path|d('') != '' %} + provisioner "local-exec" { + command = "chown -R {{ volume.user }}:stackhead {{ path }}" + } +{%- endif %} +{%- endfor %} + {% if service.hooks is defined %} {% if service.hooks.execute_after_setup is defined %} provisioner "local-exec" {