---
- name: Donne les bons droits sur le dossier Camino
  ansible.builtin.file:
    path: /srv/www/camino/
    state: directory
    owner: camino
    group: users
    mode: u=rwx,g=rwx,o=rx
  become: True
- name: Donne les bons droits sur le dossier backups
  ansible.builtin.file:
    path: /srv/backups
    state: directory
    owner: camino
    group: users
    mode: u=rwx,g=rwx,o=rx
  become: True
- name: Donne les bons droits sur le dossier dump
  ansible.builtin.file:
    path: /srv/backups/dump
    state: directory
    owner: camino
    group: users
    mode: u=rwx,g=rwx,o=rx
  become: True
- name: Donne les bons droits sur le dossier files
  ansible.builtin.file:
    path: /srv/backups/files
    state: directory
    owner: camino
    group: users
    mode: u=rwx,g=rwx,o=rx
  become: True
- name: Met en place le docker-compose
  ansible.builtin.copy:
    src: docker-compose.yml
    dest: /srv/www/camino/docker-compose.yml
    owner: camino
    group: users
    mode: u=rw,g=r,o=r
  become: True
- name: Installe le fichier d'environnement
  ansible.builtin.template:
    src: env
    dest: /srv/www/camino/.env
    mode: u=rw,g=r,o=r
    owner: camino
    group: users
  become: True
- name: Restauration des backups
  block:
    - name: Ajoute le script pour pouvoir mettre les données de production sur cet environnement
      template:
        src: restore-last-backup
        dest: /srv/scripts/
        mode: u=rwx,g=rx,o=r
        owner: camino
        group: users
      become: True
    - name: Ajoute le job de restauration du backup de la prod
      ansible.builtin.cron:
        name: "backup"
        minute: "0"
        hour: "6"
        weekday: "0"
        job: "/srv/scripts/restore-last-backup"
        user: camino
      become: True
      when: env == 'dev'
- name: Daily
  block:
    - name: Ajoute le script pour pouvoir lancer le daily
      ansible.builtin.template:
        src: daily
        dest: /srv/scripts/
        mode: u=rwx,g=rx,o=r
        owner: camino
        group: users
      become: True
    - name: Ajoute le cron de daily
      ansible.builtin.cron:
        name: "daily"
        minute: "0"
        hour: "4"
        job: "/srv/scripts/daily"
        user: camino
      become: True
- name: Monthly
  block:
    - name: Ajoute le script pour pouvoir lancer le monthly
      ansible.builtin.template:
        src: monthly
        dest: /srv/scripts/
        mode: u=rwx,g=rx,o=r
        owner: camino
        group: users
      become: True
    - name: Ajoute le cron de monthly
      ansible.builtin.cron:
        name: "monthly"
        minute: "0"
        hour: "6"
        day: "1"
        job: "/srv/scripts/monthly"
        user: camino
      become: True
- name: Ajoute le cron de nettoyage des téléchargements
  ansible.builtin.cron:
    name: "cleanup_downloads"
    minute: "0"
    hour: "5"
    job: "rm -rf /srv/www/camino/files/downloads/*"
    user: camino
  become: True
- name: Ajoute le cron de nettoyage des fichiers temporaires
  ansible.builtin.cron:
    name: "cleanup_tmps"
    minute: "30"
    hour: "5"
    job: "find /srv/www/camino/files/tmp/ -mtime +7 -type f -delete"
    user: camino
  become: True
- name: Continuous deployment
  block:
    - name: Ajoute le service de continuous deployment
      ansible.builtin.template:
        src: cd.service
        dest: /etc/systemd/system
      become: True
    - name: Dossier cible du CD
      ansible.builtin.file:
        path: /srv/www/cd
        state: directory
        owner: camino
        group: users
        mode: u=rwx,g=rwx,o=r
      become: True
    - name: Ajoute le binaire
      ansible.builtin.copy:
        src: ../../cd/target/x86_64-unknown-linux-gnu/release/cd
        dest: /srv/www/cd/cd
        owner: camino
        group: users
        mode: u=rwx,g=rwx,o=rx
      register: cd
      become: True
    - name: Lance le service
      ansible.builtin.systemd:
        state: started
        enabled: yes
        name: cd
      become: True
    - name: Redémarre le service
      ansible.builtin.systemd:
        state: restarted
        enabled: yes
        name: cd
      become: True
      when: cd.changed
