---
- name: Create temporary directory
  file:
    path: /tmp/nomad_install
    state: directory

- name: Download Nomad package checksum file
  get_url:
    url: "{{ nomad_checksum_file_url }}"
    dest: "/tmp/nomad_install/nomad_{{ nomad_version }}_SHA256SUMS"
    validate_certs: false
    timeout: 42
  register: nomad_checksum_result
  until: nomad_checksum_result.failed == false
  retries: 3
  delay: 1

- name: Read Nomad package checksum
  shell: "grep {{ nomad_pkg }} /tmp/nomad_install/nomad_{{ nomad_version }}_SHA256SUMS"
  register: nomad_sha256
  changed_when: false

- name: Download Nomad
  get_url:
    url: "{{ nomad_src }}"
    dest: "/tmp/nomad_install/{{ nomad_pkg }}"
    checksum: "sha256:{{ nomad_sha256.stdout.split(' ')|first }}"
    timeout: 42
  register: nomad_download_result
  until: nomad_download_result.failed == false
  retries: 3
  delay: 1

- name: Unarchive Nomad and install binary
  unarchive:
    remote_src: true
    src: "/tmp/nomad_install/{{ nomad_pkg }}"
    dest: "{{ local_bin_path }}"
    owner: "{{ nomad_user }}"
    group: "{{ hashistack_group }}"
    mode: 0755

- name: Fix permissions
  file:
    path: "{{ local_bin_path }}/{{ item }}"
    mode: 0755
    state: file
  with_items:
    - nomad

- name: Create CNI PLugins directory
  file:
    dest: "{{ nomad_driver_plugins_path }}"
    state: directory
    owner: "{{ nomad_user }}"
    group: "{{ hashistack_group }}"
    mode: 0755

- name: Install CNI PLugins
  unarchive:
    src: "{{ nomad_driver_plugins_src }}"
    dest: "{{ nomad_driver_plugins_path }}"
    remote_src: yes
  register: cni_download_result
  until: cni_download_result.failed == false
  retries: 3
  delay: 1

- name: Cleanup
  file:
    path: "/tmp/nomad_install"
    state: "absent"
  tags: installation

- name: Create systemd service UNIT
  template:
    src: nomad/systemd.service
    dest: /etc/systemd/system/nomad.service
    owner: root
    group: root
    mode: 0644

- name: Create systemd clean UNIT
  template:
    src: nomad/clean-nomad.service
    dest: /etc/systemd/system/clean-nomad.service
    owner: root
    group: root
    mode: 0644
  when: layerops_instance_type == "client"

# Config template files
- name: create client.hcl template config file
  template:
    src: "nomad/config/client.hcl.{{ layerops_instance_type }}"
    dest: "{{ nomad_config_template_path }}/client.hcl"
    variable_start_string: "€#"
    variable_end_string: "#€"
    block_start_string: "€%"
    block_end_string: "%€"

- name: create nomad template config files
  template:
    src: "nomad/config/{{ item }}"
    dest: "{{ nomad_config_template_path }}/{{ item }}"
    variable_start_string: "€#"
    variable_end_string: "#€"
    block_start_string: "€%"
    block_end_string: "%€"
  with_items:
    - nomad.hcl
    - acl.hcl
    - telemetry.hcl
    - consul.hcl
    - vault.hcl
    - tls.hcl

- name: create nomad server template config files
  template:
    src: "nomad/config/{{ item }}"
    dest: "{{ nomad_config_template_path }}/{{ item }}"
    variable_start_string: "€#"
    variable_end_string: "#€"
    block_start_string: "€%"
    block_end_string: "%€"
  with_items:
    - server.hcl
  when: layerops_instance_type == "orchestrator"

- name: Create nomad garbage collector script
  template:
    src: "nomad/garbage-collector.sh"
    dest: "{{ nomad_gb_script }}"
    owner: root
    group: root
    mode: 0755
  when: layerops_instance_type == "orchestrator"

- name: Setup nomad garbage collector cron
  cron:
    name: "force nomad gargage collector"
    job: "{{ nomad_gb_script }}"
    minute: "0"
    hour: "0"
  when: layerops_instance_type == "orchestrator"
