---
- name: Create state directory
  ansible.builtin.file:
    path: "{{ hermes_auto_improve_state_dir }}"
    state: directory
    mode: '0755'

- name: Deploy auto-improve script
  ansible.builtin.copy:
    src: "{{ playbook_dir }}/../../tools/hermes-auto-improve/auto-improve.py"
    dest: /usr/local/bin/hermes-auto-improve
    mode: '0755'
  notify: Restart hermes-auto-improve-server

- name: Deploy trigger server
  ansible.builtin.copy:
    src: "{{ playbook_dir }}/../../tools/hermes-auto-improve/trigger-server.py"
    dest: /usr/local/bin/hermes-auto-improve-server
    mode: '0755'
  notify: Restart hermes-auto-improve-server

- name: Deploy systemd service
  ansible.builtin.template:
    src: hermes-auto-improve.service.j2
    dest: /etc/systemd/system/hermes-auto-improve.service
    mode: '0644'
  notify: Restart hermes-auto-improve-server

- name: Enable and start service
  ansible.builtin.systemd:
    name: hermes-auto-improve
    enabled: true
    state: started
    daemon_reload: true

- name: Open port in nftables
  ansible.builtin.lineinfile:
    path: /etc/nftables.conf
    insertafter: '# prometheus'
    line: "    tcp dport {{ hermes_auto_improve_port }} ip saddr { 10.42.0.0/16, 192.168.10.0/24 } accept comment \"hermes-auto-improve\""
    state: present
  notify: Reload nftables
  ignore_errors: true
