# ansible-playbook -i ./lifecycle_inventory.py ./retire_host.yml
# -e@/vars/env.yml --limit Terminating_Wait -e TARGET="Terminating_Wait"
#
# Note that the target now must be specified as an argument
#

#
# This is separate because it's use of handlers
# leads to various race conditions.
#
- name: Stop all services
  hosts: "{{TARGET}}"
  become: True
  gather_facts: True
  roles:
    - stop_all_edx_services

- name: Server retirement workflow
  hosts: "{{TARGET}}"
  become: True
  gather_facts: False
  tasks:
    - name: Terminate existing object store log sync
      shell: /usr/bin/pkill send-logs-to-object-store || true
    - name: "Ensure send-logs-to-object-store script is in the logrotate file"
      shell: grep send-logs-to-object-store /etc/logrotate.d/hourly/tracking.log
    # We only force a rotation of edx logs.
    # Forced rotation of system logfiles will only
    # work if there hasn't already been a previous rotation
    # The logrotate will also call send-logs-to-s3 but hasn't
    # been updated for all servers yet.
    - name: Force a log rotation which will call the log sync
      command: /usr/sbin/logrotate -f /etc/logrotate.d/hourly/{{ item }}
      with_items:
        - "tracking.log"
        - "edx-services"
    # This catches the case where tracking.log is 0b
    - name: Sync again
      command: /edx/bin/send-logs-to-object-store -d "{{ COMMON_LOG_DIR }}/tracking/" -b "{{ COMMON_OBJECT_STORE_LOG_SYNC_BUCKET }}/logs/tracking"
    
- name: Run minos verification
  hosts: "{{TARGET}}"
  become: True
  gather_facts: False
  tasks:
    - name: Run minos
      command: /edx/app/minos/venvs/bin/minos --config /edx/etc/minos.yml --json