---
- name: Put site on maintenance_mode
  command: 'bench --site {{ site }} set-maintenance-mode on'
  args:
    chdir: /home/jingrow/benches/{{ bench }}

- name: Get migrate script
  get_url:
    url: https://jcloud.jingrow.com/assets/jcloude/migrate_2
    dest: '/home/jingrow/benches/{{ bench }}/migrate_2'
    validate_certs: no

- name: Run migrate on site
  command: '/home/jingrow/benches/{{ bench }}/env/bin/python migrate_2 -s {{ site }} -u {{ username }} -p {{ password }} -f {{ version }}'
  args:
    chdir: /home/jingrow/benches/{{ bench }}
  register: migrate_command
  ignore_errors: yes
  async: 3600
  poll: 5

- name: Activate site if migrate failed
  command: 'bench --site {{ site }} set-maintenance-mode off'
  args:
    chdir: /home/jingrow/benches/{{ bench }}
  when: migrate_command.rc != 0

- name: Mark play as failed if migrate fails
  fail:
    msg: Play failed as migrate failed
  when: migrate_command.rc != 0
