---
- name: Update Agent Repository Remote
  become: yes
  become_user: jingrow
  command: 'git remote set-url upstream {{ agent_repository_url }}'
  args:
    chdir: /home/jingrow/agent/repo

- name: Fetch Agent Upstream
  become: yes
  become_user: jingrow
  command: 'git fetch upstream'
  args:
    chdir: /home/jingrow/agent/repo

- name: Checkout specified commit or branch
  become: yes
  become_user: jingrow
  shell: |
    git checkout {{ agent_repository_branch_or_commit_ref }}
    git reset --hard {{ agent_repository_branch_or_commit_ref }}
    git clean -fd
  args:
    chdir: /home/jingrow/agent/repo

- name: Install Python Requirements
  become: yes
  become_user: jingrow
  command: './env/bin/pip install -r ./repo/requirements.txt'
  args:
    chdir: /home/jingrow/agent

- name: Install Agent
  command: './env/bin/pip install -e /home/jingrow/agent/repo'
  args:
    chdir: /home/jingrow/agent

- name: Update Agent database
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent setup database
  args:
    chdir: /home/jingrow/agent

- name: Run agent db patches
  become: yes
  become_user: jingrow
  command: /home/jingrow/agent/env/bin/agent run-patches
  args:
    chdir: /home/jingrow/agent

- name: Update Agent
  become: yes
  become_user: jingrow
  command: '/home/jingrow/agent/env/bin/agent update {{ agent_update_args }}'
  args:
    chdir: /home/jingrow/agent

- name: Set Ownership of Agent Logs to Jingrow user
  file:
    path: /home/jingrow/agent/logs
    owner: jingrow
    group: jingrow
    recurse: yes

- name: Ensure Agent processes are started
  supervisorctl:
    name: 'agent:'
    state: started
