#####################################
### someone's ansible provisioner ###
#####################################
# Part of: https://git.somenet.org/root/pub/somesible.git
# 2017-2025 by someone <someone@somenet.org>
#
---
- name: create config dir /etc/docker/compose-{{dco_svc_name}}
  file:
    path: "/etc/docker/compose-{{dco_svc_name}}"
    state: directory
    mode: 0750
    owner: "root"
    group: "root"


# fail here, if wrong dco_svc_name given
- name: copy config dirtree for service {{dco_svc_name}}
  file:
    path: "/etc/docker/compose-{{dco_svc_name}}/{{item.path}}"
    state: directory
    owner: "root"
    group: "root"
  with_filetree:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/svc-{{dco_svc_name}}/config/"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/svc-{{dco_svc_name}}/config/"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/svc-{{dco_svc_name}}/config/"
    - "default/svc-{{dco_svc_name}}/config/"
  when: item.state == 'directory'
  notify: restart docker-compose@{{dco_svc_name}}.service


- name: copy config filetree for service {{dco_svc_name}}
  copy:
    src:  "{{item.src}}"
    dest: "/etc/docker/compose-{{dco_svc_name}}/{{item.path}}"
    owner: "root"
    group: "root"
  with_filetree:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/svc-{{dco_svc_name}}/config/"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/svc-{{dco_svc_name}}/config/"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/svc-{{dco_svc_name}}/config/"
    - "default/svc-default/config/"
  when: item.state == 'file'
  notify: restart docker-compose@{{dco_svc_name}}.service


- name: create volume dir for service {{dco_svc_name}}
  file:
    path: "/srv/docker-compose/svc-{{dco_svc_name}}"
    state: directory
    mode: 0750
    owner: "root"
    group: "root"


- name: copy volume dirtree for service {{dco_svc_name}}
  file:
    path: "/srv/docker-compose/svc-{{dco_svc_name}}/{{item.path}}"
    state: directory
    owner: "root"
    group: "root"
  with_filetree:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/svc-{{dco_svc_name}}/volumes/"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/svc-{{dco_svc_name}}/volumes/"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/svc-{{dco_svc_name}}/volumes/"
    - "default/svc-default/volumes/"
  when: item.state == 'directory'
  notify: restart docker-compose@{{dco_svc_name}}.service


- name: copy volume filetree for service {{dco_svc_name}}
  copy:
    src:  "{{item.src}}"
    dest: "/srv/docker-compose/svc-{{dco_svc_name}}/{{item.path}}"
    owner: "root"
    group: "root"
  with_filetree:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/svc-{{dco_svc_name}}/volumes/"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/svc-{{dco_svc_name}}/volumes/"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/svc-{{dco_svc_name}}/volumes/"
    - "default/svc-default/volumes/"
  when: item.state == 'file'
  notify: restart docker-compose@{{dco_svc_name}}.service


- name: enable and start docker-compose@{{dco_svc_name}}.service
  systemd:
    name: docker-compose@{{dco_svc_name}}.service
    daemon_reload: yes
    enabled: yes
    state: started
  when: dco_svc_cron == ''


- name: generate docker-compose-{{dco_svc_name}}.timer
  template:
    src: "{{item}}"
    dest: "/etc/systemd/system/docker-compose-{{dco_svc_name}}.timer"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/docker-compose.timer.j2"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/docker-compose.timer.j2"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/docker-compose.timer.j2"
    - "default/docker-compose.timer.j2"
  when: dco_svc_cron != ''


- name: enable and start docker-compose-{{dco_svc_name}}.timer
  systemd:
    name: docker-compose-{{dco_svc_name}}.timer
    daemon_reload: yes
    enabled: yes
    state: started
  when: dco_svc_cron != ''
