---
# Initial Step:
#
# Schedule a new job giving a specific topic and specifying the remote CI.
# The return of this action contains all the data associated with the job,
# we hence register those data for later consumptions
#
- hosts: localhost
  gather_facts: false
  tasks:

    - include_tasks: 'new.yml'

    - block:
      - name: echo 'New'
        shell: echo 'New'

      rescue:
      - name: Fail properly
        fail:
          msg: 'Something went wrong with the installation'


# Pre-run state
#
# User is free to do whaterver she needs before entering pre-run state.
# Usually this is used to spawn the undercloud vm
#
- hosts: localhost
  gather_facts: false
  any_errors_fatal: true
  vars:
    dci_status: 'pre-run'
    dci_comment: 'Pre-run state commands'
  tasks:
    - block:
      - name: echo 'Pre-run'
        shell: echo 'pre-run'

      rescue:
      - name: Fail properly
        fail:
          msg: 'Something went wrong with the installation'

# Running state
#
# User is free to do whaterver she needs before entering running state.
# Usually this is used to provision the undercloud and the overcloud
#
- hosts: localhost
  gather_facts: false
  any_errors_fatal: true
  vars:
    dci_status: 'running'
    dci_comment: 'Running state commands'
  tasks:
    - block:
      - name: echo 'Running'
        shell: echo 'Running'

      rescue:
      - name: Tear me down
        shell: echo 'Tear me down'

      - name: Fail properly
        fail:
          msg: 'Something went wrong with the installation'


# Post-run state
#
# User is free to do whaterver she needs before entering post-run state.
# Usually this is used to run tests on the overcloud
#
- hosts: localhost
  gather_facts: false
  any_errors_fatal: true
  vars:
    dci_status: 'post-run'
    dci_comment: 'Post-run state commands'
  tasks:
    - block:
      - name: echo 'Post-run'
        shell: echo 'Post-run'

      rescue:
      - name: Tear me down
        shell: echo 'Tear me down'

      - name: Fail properly
        fail:
          msg: 'Something went wrong with the installation'


# Success state
# 
# User is free to do whaterver she needs before entering pre-run state.
# Usually this is used to teardown the plateform
#
- hosts: localhost
  gather_facts: false
  any_errors_fatal: true
  vars:
    dci_status: 'success'
    dci_comment: 'Success state commands'
  tasks:
    - name: echo 'Success'
      shell: echo 'Succes'

    - name: "Final step"
      debug:
        msg: "The job is now finished. Review the log at: {{ lookup('env','DCI_UI_URL') | default('https://www.distributed-ci.io', True) }}/jobs/{{ job_info.job.id }}/jobStates"
