- hosts: containers
  name: Stop mailu
  ignore_unreachable: false
  vars:
      arch_name: docker_mailu2_data
  tasks:
    - name: Start mailu rasp
      uri:
        url: "http://192.168.77.106:9000/api/stacks/{{ item }}/start?endpointId=13"
        method: POST
        body_format: form-urlencoded
        return_content: yes
        headers:
          Content-Type: "application/json"
          X-API-Key: "ptr_DfS2M6Fj2P3fVvYpkhE0KJh2UGCzY47ePaFaLqadsjg="
        timeout: 60
      ignore_errors: yes
      when: inventory_hostname in groups['nas']
      loop:
        - 130
        - 149
        #- 140
        - 180

    - name: Get ruleset
      command: nvram get vts_rulelist
      when: inventory_hostname in groups['router']
      register: ruleset
    
    - name: Pause for 60 seconds
      ansible.builtin.pause:
        seconds: 60
        
    - name: Set new ruleset
      command: nvram set vts_rulelist="{{ ruleset.stdout  | replace('192.168.77.106', '192.168.77.246')  }}"
      when: inventory_hostname in groups['router']

    - name: Test
      command: nvram commit
      when: inventory_hostname in groups['router']

    - name: Restart firewall
      command: service restart_firewall
      when: inventory_hostname in groups['router']
      args:
        warn: false

    - name: Stop mailu main
      uri:
        url: "http://192.168.77.106:9000/api/stacks/108/stop?endpointId=2"
        method: POST
        body_format: form-urlencoded
        return_content: yes
        headers:
          Content-Type: "application/json"
          X-API-Key: "ptr_DfS2M6Fj2P3fVvYpkhE0KJh2UGCzY47ePaFaLqadsjg="
        timeout: 60
      register: result
      ignore_errors: yes
      when: inventory_hostname in groups['nas']

    - name: Create archive
      #ansible.builtin.shell: 'tar -czf /share/Data/dockers.tar.gz /share/docker_data/{mailu2,webhub,ha} --exclude="/ha/backups/*" --exclude="home-assistant.log*"'
      ansible.builtin.shell: 'tar -czf /share/Data/{{ arch_name }}-{{ ansible_date_time.date }}.tar.gz /share/docker_data/mailu2 --exclude="/ha/backups/*" --exclude="home-assistant.log*"'
      ignore_errors: yes
      args:
        warn: false
      when: inventory_hostname in groups['nas']
      
    - name: Start mailu main
      uri:
        url: "http://192.168.77.106:9000/api/stacks/108/start?endpointId=2"
        method: POST
        body_format: form-urlencoded
        return_content: yes
        headers:
          Content-Type: "application/json"
          X-API-Key: "ptr_DfS2M6Fj2P3fVvYpkhE0KJh2UGCzY47ePaFaLqadsjg="
        timeout: 60
      ignore_errors: yes
      when: inventory_hostname in groups['nas']

    - name: Pause for 60 seconds
      ansible.builtin.pause:
        seconds: 60
    #- debug:
    #   msg: "{{ ruleset.stdout  }}"

    - name: Set new ruleset
      command: nvram set vts_rulelist="{{ ruleset.stdout | replace('192.168.77.246', '192.168.77.106') }}"
      when: inventory_hostname in groups['router']

    - name: Test
      command: nvram commit
      when: inventory_hostname in groups['router']

    - name: Restart firewall
      command: service restart_firewall
      args:
        warn: false
      when: inventory_hostname in groups['router']
      
    - name: Stop mailu rhasp
      uri:
        url: "http://192.168.77.106:9000/api/stacks/{{ item }}/stop?endpointId=13"
        method: POST
        body_format: form-urlencoded
        return_content: yes
        headers:
          Content-Type: "application/json"
          X-API-Key: "ptr_DfS2M6Fj2P3fVvYpkhE0KJh2UGCzY47ePaFaLqadsjg="
        timeout: 60
      register: result
      when: inventory_hostname in groups['nas']
      loop:
        - 130
        - 149
        #- 140
        - 180

    - name: Copy archive
      ansible.builtin.shell: 'scp /share/Data/{{ arch_name }}-{{ ansible_date_time.date }}.tar.gz admin@192.168.77.246:/tmp/'
      when: inventory_hostname in groups['nas']

    #- name: Unpack archive
    #  ansible.builtin.shell: 'tar -xzvf /tmp/dockers-{{ ansible_date_time.date }}.tar.gz -C /'
    #  become: true
    #  when: inventory_hostname in groups['raspberry']
    - name: Extract 
      become: true
      ansible.builtin.unarchive:
        src: /tmp/{{ arch_name }}-{{ ansible_date_time.date }}.tar.gz
        dest: /
        remote_src: yes
        owner: admin
        group: administrators
      when: inventory_hostname in groups['raspberry']
    - name: fix permissions
      file:
        path: /share/docker_data/mailu2/
        owner: admin
        group: administrators
        recurse: yes
      become: true
      when: inventory_hostname in groups['raspberry']