---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Gathering facts on current container"
      community.docker.current_container_facts:
    
    -
      name: "Creating Installation Directories"
      ansible.builtin.file:
        mode: '0644'
        path: "{{ item }}"
        state: directory
        owner: "{{ service_user }}"
      with_items:
        - "{{ installer_dir }}/restapi/rest"
        - "{{ installer_dir }}/restapi/archives"
    
    -
      name: "Checking if the Rest Interface deployed"
      register: rest_interface_deployed
      ansible.builtin.stat:
        path: "{{ rest_app_home_dir }}/environment"
        checksum_algorithm: sha256
    
    -
      name: "Downloading Domino VagrantCRUD API {{ rest_vagrant_crud_version }}"
      register: vagrant_crud_api
      until: "vagrant_crud_api is not failed"
      retries: 3
      when: not rest_interface_deployed.stat.exists
      ansible.builtin.get_url:
        mode: '0644'
        url: "{{ rest_vagrant_crud_git_repo }}{{ rest_vagrant_crud_version }}/{{ rest_vagrant_crud_archive }}"
        dest: "{{ installer_dir }}/restapi/archives/{{ rest_vagrant_crud_archive }}"
    
    -
      name: "Extracting Domino VagrantCRUD API from {{ rest_vagrant_crud_archive }}"
      when: not rest_interface_deployed.stat.exists
      ansible.builtin.unarchive:
        src: "{{ installer_dir }}/restapi/archives/{{ rest_vagrant_crud_archive }}"
        dest: "{{ installer_dir }}/restapi/rest"
        remote_src: true
        creates: "{{ installer_dir }}/restapi/rest/*.jar"
    
    -
      name: "Making Rest Libraries and Binaries executable"
      when: not rest_interface_deployed.stat.exists
      ansible.builtin.file:
        path: "{{ item }}"
        owner: "{{ service_user }}"
        mode: "a+x"
      with_items:
        - "{{ installer_dir }}/restapi/rest/provision.sh"
        - "{{ installer_dir }}/restapi/rest/always.sh"
    
    -
      name: "Cleaning up first before installing Vagrant Rest interface"
      when: not rest_interface_deployed.stat.exists
      ansible.builtin.file:
        path: "{{ rest_app_home_dir }}"
        state: absent
        owner: "{{ service_user }}"
        mode: "0744"
    
    -
      name: "Installing Vagrant Rest interface"
      when: not rest_interface_deployed.stat.exists
      ansible.builtin.file:
        path: "{{ item.path }}"
        state: directory
        owner: "{{ service_user }}"
        mode: "0744"
      with_items:
        - { path: "/opt/domino/scripts" }
        - { path: "{{ rest_app_home_dir }}" }
        - { path: "{{ rest_app_home_dir }}/bin" }
        - { path: "{{ rest_app_home_dir }}/log" }
        - { path: "{{ rest_app_home_dir }}/config" }
    
    -
      name: "Copying default REST Interface JAR to working path"
      become: true
      when: not rest_interface_deployed.stat.exists and not use_custom_rest_interface_jar
      ansible.builtin.copy:
        src: "{{ installer_dir }}/restapi/rest/{{ rest_interface_jar }}"
        dest: "{{ rest_app_home_dir }}/bin/{{ rest_interface_jar }}"
        mode: "a+x"
        owner: "{{ service_user }}"
        remote_src: true

    -
      name: "Copying custom REST Interface JAR to working path"
      become: true
      when: not rest_interface_deployed.stat.exists and use_custom_rest_interface_jar
      ansible.builtin.copy:
        src: "{{ installer_dir }}/custom_restapi_jar/{{ rest_interface_jar }}"
        dest: "{{ rest_app_home_dir }}/bin/{{ rest_interface_jar }}"
        mode: "a+x"
        owner: "{{ service_user }}"
        remote_src: true
    
    -
      name: "Placing Domino Specific Configuration and scripts"
      become: true
      when: domino_home_dir is defined and domino_install_dir is defined and not rest_interface_deployed.stat.exists
      ansible.builtin.template:
        owner: "{{ item.owner }}"
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        mode: "a+x"
      loop:
        - { src: 'deploy_database.sh.j2', dest: '/opt/domino/scripts/deploy_database.sh', owner: '{{ service_user }}' }
        - { src: 'deploy_html.sh.j2', dest: '/opt/domino/scripts/deploy_html.sh', owner: '{{ service_user }}' }
        - { src: 'run_dxl_importer.sh.j2', dest: '/opt/domino/scripts/run_dxl_importer.sh', owner: '{{ service_user }}'}
        - { src: 'import_json_documents.sh.j2', dest: '/opt/domino/scripts/import_json_documents.sh', owner: '{{ service_user }}'}
    
    -
      name: "Placing Configuration and scripts"
      become: true
      when: not rest_interface_deployed.stat.exists
      ansible.builtin.template:
        owner: "{{ item.owner }}"
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        mode: "a+x"
      loop:
        - { src: 'rest_config.yml.j2', dest: '{{ rest_app_home_dir }}{{ rest_config_file }}', owner: '{{ service_user }}' }
        - { src: 'restapi.service.j2', dest: '/etc/systemd/system/restapi.service', owner: '{{ service_user }}' }
        - { src: 'environment.j2', dest: '{{ rest_app_home_dir }}/environment', owner: '{{ service_user }}' }
    
    -
      name: "Create additional supporting directories"
      ansible.builtin.file:
        mode: '0755'
        path: "{{ item }}"
        state: directory
        owner: "{{ service_user }}"
      loop: "{{ domino_vagrant_restapi_supporting_dirs|flatten(levels=1) }}"
    
    -
      name: "Copying restapi supervisord configuration file"
      when: not rest_interface_deployed.stat.exists and ansible_module_running_in_container
      ansible.builtin.template:
        src: supervisord.restapi.conf.j2
        dest: /etc/supervisor/conf.d/supervisord.restapi.conf
        owner: root
        group: root
        mode: '0644'
    
    -
      name: "Starting Vagrant CRUD Rest API"
      when: not rest_interface_deployed.stat.exists and not ansible_module_running_in_container
      ansible.builtin.service:
        name: restapi
        state: started
        enabled: true
    
    -
      name: "Checking Vagrant CRUD Rest API is listening on port {{ domino_vagrant_rest_api_port_forwards[0].guest }}"
      when: not rest_interface_deployed.stat.exists and not ansible_module_running_in_container
      ansible.builtin.wait_for:
        port: "{{ domino_vagrant_rest_api_port_forwards[0].guest }}"
        delay: 15
        timeout: 240
        msg: "Timeout waiting for {{ domino_vagrant_rest_api_port_forwards[0].guest }} to respond"
      register: port_check
