---
# file: roles/postgres/tasks/pg_backup_service.yml

- name: "install python_package dependencies"
  block:
    - name: "set fact python_package_install_from_role"
      set_fact:
        python_package_install_from_role:
          - package: pgcos
            script: pg_backup_service.py
    - name: "import role python_package"
      import_role:
        name: python_package
    - name: "unset fact python_package_install"
      set_fact:
        python_package_install_from_role: []
  tags:
    - scripts
    - postgres
    - patroni

- name: "pip upgrade pip in virtualenv"
  pip:
    name: pip
    extra_args: --upgrade
    virtualenv: /usr/local/venvs/pg_backup_service
    virtualenv_command: /usr/bin/python3 -m venv
  when: not ansible_check_mode
  tags:
    - scripts
    - postgres
    - patroni

- name: "pip install dependencies in virtualenv"
  pip:
    name: packaging
    extra_args: --upgrade
    virtualenv: /usr/local/venvs/pg_backup_service
    virtualenv_command: /usr/bin/python3 -m venv
  when: not ansible_check_mode
  tags:
    - scripts
    - postgres
    - patroni

- name: "/usr/local/venvs/pg_backup_service/bin/pg_backup_service.py"
  copy:
    src: pg_backup_service.py
    dest: /usr/local/venvs/pg_backup_service/bin/pg_backup_service.py
    mode: 0755
  tags:
    - scripts
    - postgres
    - patroni

- name: "ln -s /usr/local/venvs/pg_backup_service/bin/pg_backup_service.py /usr/local/bin/pg_backup_service.py"
  file:
    state: link
    src: /usr/local/venvs/pg_backup_service/bin/pg_backup_service.py
    dest: /usr/local/bin/pg_backup_service.py
    force: true
  tags:
    - scripts
    - postgres
    - patroni

- name: "pg-backup.service systemd unit"
  copy:
    src: pg-backup.service
    dest: /etc/systemd/system/pg-backup.service
  notify: systemctl daemon-reload
  tags:
    - scripts
    - postgres
    - patroni

- name: "disable pg-backup.service"
  systemd:
    name: pg-backup
    enabled: false
  when: not ansible_check_mode
  tags:
    - scripts
    - postgres
    - patroni
