---
- name: Fetch core public key from infra-core-1
  delegate_to: infra-core-1
  slurp:
    src: /data/komodo/app/keys/core.pub
  register: core_pub_key_content
  run_once: true

- name: Create komodo periphery directory
  file:
    path: /opt/komodo-periphery
    state: directory
    mode: "0755"

- name: Create komodo periphery data directories
  file:
    path: "{{ item }}"
    state: directory
    mode: "0755"
  loop:
    - /data/komodo/app/keys
    - /etc/komodo

- name: Copy core public key to periphery hosts
  copy:
    content: "{{ core_pub_key_content['content'] | b64decode }}"
    dest: /data/komodo/app/keys/core.pub
    mode: "0644"

- name: Create docker-compose file for komodo periphery
  template:
    src: docker-compose.yml.j2
    dest: /opt/komodo-periphery/docker-compose.yml
    mode: "0644"

- name: Start komodo periphery
  shell: cd /opt/komodo-periphery && docker compose down && docker compose up -d
  args:
    chdir: /opt/komodo-periphery
