---
- name: Create registry data directories
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    owner: root
    group: root
    mode: "0755"
  loop:
    - "{{ registry_data_dir }}"
    - "{{ registry_data_dir }}/data"
    - "{{ registry_data_dir }}/auth"
  tags: registry

- name: Deploy htpasswd file
  ansible.builtin.template:
    src: htpasswd.j2
    dest: "{{ registry_data_dir }}/auth/htpasswd"
    owner: root
    group: root
    mode: "0600"
  notify: Restart registry
  tags: registry

- name: Deploy Docker Compose file
  ansible.builtin.template:
    src: docker-compose.yml.j2
    dest: "{{ registry_data_dir }}/docker-compose.yml"
    owner: root
    group: root
    mode: "0644"
  notify: Restart registry
  tags: registry

- name: Start registry
  community.docker.docker_compose_v2:
    project_src: "{{ registry_data_dir }}"
    state: present
    pull: missing
  tags: registry

- name: Deploy registry GC script
  ansible.builtin.template:
    src: registry-gc.sh.j2
    dest: /usr/local/bin/registry-gc
    owner: root
    group: root
    mode: "0750"
  tags: registry

- name: Schedule weekly registry GC cron job
  ansible.builtin.cron:
    name: registry-gc
    user: root
    weekday: "0"
    hour: "3"
    minute: "0"
    job: /usr/local/bin/registry-gc >> /var/log/registry-gc.log 2>&1
    state: present
  tags: registry
