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

# Install Consul
- block:
  - name: Download Consul package checksum file
    get_url:
      url: "{{ consul_checksum_file_url }}"
      dest: "/tmp/consul_install/consul_{{ consul_version }}_SHA256SUMS"
      validate_certs: false
      timeout: 42
    register: consul_checksum_result
    until: consul_checksum_result.failed == false
    retries: 3
    delay: 1

  - name: Read Consul package checksum
    shell: "grep {{ consul_pkg }} /tmp/consul_install/consul_{{ consul_version }}_SHA256SUMS"
    register: consul_sha256
    changed_when: false

  - name: Download Consul
    get_url:
      url: "{{ consul_src }}"
      dest: "/tmp/consul_install/{{ consul_pkg }}"
      checksum: "sha256:{{ consul_sha256.stdout.split(' ')|first }}"
      timeout: 42
    register: consul_download_result
    until: consul_download_result.failed == false
    retries: 3
    delay: 1

  - name: Unarchive Consul and install binary
    unarchive:
      remote_src: true
      src: "/tmp/consul_install/{{ consul_pkg }}"
      dest: "{{ local_bin_path }}"
      owner: "{{ consul_user }}"
      group: "{{ hashistack_group }}"
      mode: 0755

  - name: Fix permissions
    file:
      path: "{{ local_bin_path }}/consul"
      mode: 0755
      state: file

# Install Consul Template
- block:
  - name: Download Consul Template package checksum file
    get_url:
      url: "{{ consul_template_checksum_file_url }}"
      dest: "/tmp/consul_install/consul_template_{{ consul_template_version }}_SHA256SUMS"
      validate_certs: false
      timeout: 42
    register: consul_template_checksum_result
    until: consul_template_checksum_result.failed == false
    retries: 3
    delay: 1

  - name: Read Consul package checksum
    shell: "grep {{ consul_template_pkg }} /tmp/consul_install/consul_template_{{ consul_template_version }}_SHA256SUMS"
    register: consul_template_sha256
    changed_when: false

  - name: Download Consul Template
    get_url:
      url: "{{ consul_template_src }}"
      dest: "/tmp/consul_install/{{ consul_template_pkg }}"
      checksum: "sha256:{{ consul_template_sha256.stdout.split(' ')|first }}"
      timeout: 42
    register: consul_template_download_result
    until: consul_template_download_result.failed == false
    retries: 3
    delay: 1

  - name: Unarchive Consul Template and install binary
    unarchive:
      remote_src: true
      src: "/tmp/consul_install/{{ consul_template_pkg }}"
      dest: "{{ local_bin_path }}"
      owner: root
      group: root
      mode: 0755

  - name: Fix permissions
    file:
      path: "{{ local_bin_path }}/consul-template"
      mode: 0755
      state: file

# Cleanup
- name: Cleanup
  file:
    path: "/tmp/consul_install"
    state: absent

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

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

- name: create consul template config files
  template:
    src: "consul/config/{{ item }}"
    dest: "{{ consul_config_template_path }}/{{ item }}"
    variable_start_string: "€#"
    variable_end_string: "#€"
    block_start_string: "€%"
    block_end_string: "%€"
  with_items:
    - connect-enabled.hcl
    - ports.hcl

# Create additionnal scritps and services
- name: Create consul error watcher script
  template:
    src: consul/consul-error-watch.sh
    dest: "{{ local_bin_path }}/consul-error-watch"
    mode: 0755
  when: layerops_instance_type == "orchestrator"

- name: copy consul error watcher systemd startup script
  template:
    src: consul/consul-error-watch.service
    dest: "/etc/systemd/system/consul-error-watch.service"
  when: layerops_instance_type == "orchestrator"
