---
version: '2.0'
name: tripleo.validations.v1
description: TripleO Validations Workflows v1

workflows:

  run_validation:
    input:
      - validation_name
      - plan: overcloud
      - queue_name: tripleo

    tags:
      - tripleo-common-managed

    tasks:

      notify_running:
        workflow: tripleo.messaging.v1.send
        input:
          queue_name: <% $.queue_name %>
          type: <% execution().name %>
          status: RUNNING
          execution: <% execution() %>
          plan_name: <% $.plan %>
          payload:
            validation_name: <% $.validation_name %>
            plan: <% $.plan %>
        on-complete: run_validation

      run_validation:
        on-success: send_message
        on-error: set_status_failed
        action: tripleo.validations.run_validation validation=<% $.validation_name %> plan=<% $.plan %>
        publish:
          status: SUCCESS
          stdout: <% task().result.stdout %>
          stderr: <% task().result.stderr %>

      set_status_failed:
        on-complete: send_message
        publish:
          status: FAILED
          stdout: <% task(run_validation).result.stdout %>
          stderr: <% task(run_validation).result.stderr %>

      send_message:
        workflow: tripleo.messaging.v1.send
        input:
          queue_name: <% $.queue_name %>
          type: <% execution().name %>
          status: <% $.get('status', 'SUCCESS') %>
          execution: <% execution() %>
          plan_name: <% $.plan %>
          payload:
            validation_name: <% $.validation_name %>
            plan: <% $.plan %>
            stdout: <% $.stdout %>
            stderr: <% $.stderr %>

  run_validations:
    input:
      - validation_names: []
      - plan: overcloud
      - queue_name: tripleo

    tags:
      - tripleo-common-managed

    tasks:

      notify_running:
        workflow: tripleo.messaging.v1.send
        input:
          queue_name: <% $.queue_name %>
          type: <% execution().name %>
          status: RUNNING
          execution: <% execution() %>
          plan_name: <% $.plan %>
          payload:
            validation_names: <% $.validation_names %>
            plan: <% $.plan %>
        on-complete: run_validations

      run_validations:
        on-success: send_message
        on-error: set_status_failed
        workflow: tripleo.validations.v1.run_validation validation_name=<% $.validation %> plan=<% $.plan %> queue_name=<% $.queue_name %>
        with-items: validation in <% $.validation_names %>
        publish:
          status: SUCCESS

      set_status_failed:
        on-complete: send_message
        publish:
          status: FAILED

      send_message:
        workflow: tripleo.messaging.v1.send
        input:
          queue_name: <% $.queue_name %>
          type: <% execution().name %>
          status: <% $.get('status', 'SUCCESS') %>
          execution: <% execution() %>
          plan_name: <% $.plan %>
          payload:
            validation_names: <% $.validation_names %>
            plan: <% $.plan %>

  run_groups:
    input:
      - group_names: []
      - plan: overcloud
      - queue_name: tripleo

    tags:
      - tripleo-common-managed

    tasks:

      find_validations:
        on-success: notify_running
        action: tripleo.validations.list_validations groups=<% $.group_names %>
        publish:
          validations: <% task().result %>

      notify_running:
        workflow: tripleo.messaging.v1.send
        input:
          queue_name: <% $.queue_name %>
          type: <% execution().name %>
          status: RUNNING
          execution: <% execution() %>
          plan_name: <% $.plan %>
          payload:
            group_names: <% $.group_names %>
            validation_names: <% $.validations %>
            plan: <% $.plan %>
        on-complete: run_validation_group

      run_validation_group:
        on-success: send_message
        on-error: set_status_failed
        workflow: tripleo.validations.v1.run_validation validation_name=<% $.validation %> plan=<% $.plan %> queue_name=<% $.queue_name %>
        with-items: validation in <% $.validations.id %>
        publish:
          status: SUCCESS

      set_status_failed:
        on-complete: send_message
        publish:
          status: FAILED

      send_message:
        workflow: tripleo.messaging.v1.send
        input:
          queue_name: <% $.queue_name %>
          type: <% execution().name %>
          status: <% $.get('status', 'SUCCESS') %>
          execution: <% execution() %>
          plan_name: <% $.plan %>
          payload:
            group_names: <% $.group_names %>
            validation_names: <% $.validations %>
            plan: <% $.plan %>

  list:
    input:
      - group_names: []
      - queue_name: tripleo
    output:
      validations: <% $.validations %>
    tags:
      - tripleo-common-managed
    tasks:
      find_validations:
        on-success: send_message
        action: tripleo.validations.list_validations groups=<% $.group_names %>
        publish:
          status: SUCCESS
          message: <% task().result %>
          validations: <% task().result %>
        publish-on-error:
          status: FAILED
          message: <% task().result %>

      send_message:
        workflow: tripleo.messaging.v1.send
        input:
          queue_name: <% $.queue_name %>
          type: <% execution().name %>
          execution: <% execution() %>
          status: <% $.status %>
          message: <% $.get('message', '') %>
          payload:
            validations: <% $.get('validations', []) %>

  list_groups:
    output:
      groups: <% task(find_groups).result %>
    tags:
      - tripleo-common-managed
    tasks:
      find_groups:
        action: tripleo.validations.list_groups

  add_validation_ssh_key_parameter:
    input:
     - container
     - queue_name: tripleo

    tags:
      - tripleo-common-managed

    tasks:
      test_validations_enabled:
        action: tripleo.validations.enabled
        on-success: get_pubkey
        on-error: unset_validation_key_parameter

      get_pubkey:
        action: tripleo.validations.get_pubkey
        on-success: set_validation_key_parameter
        publish:
          pubkey: <% task().result %>

      set_validation_key_parameter:
        action: tripleo.parameters.update
        input:
          parameters:
            node_admin_extra_ssh_keys: <% $.pubkey %>
          container: <% $.container %>

      # NOTE(shadower): We need to clear keys from a previous deployment
      unset_validation_key_parameter:
        action: tripleo.parameters.update
        input:
          parameters:
            node_admin_extra_ssh_keys: ""
          container: <% $.container %>

  copy_ssh_key:
    input:
      # FIXME: we should stop using heat-admin as e.g. split-stack
      # environments (where Nova didn't create overcloud nodes) don't
      # have it present
      - overcloud_admin: heat-admin
      - queue_name: tripleo
    tags:
      - tripleo-common-managed
    tasks:
      get_servers:
        action: nova.servers_list
        on-success: get_pubkey
        publish:
          servers: <% task().result._info %>

      get_pubkey:
        action: tripleo.validations.get_pubkey
        on-success: deploy_ssh_key
        publish:
          pubkey: <% task().result %>

      deploy_ssh_key:
        workflow: tripleo.deployment.v1.deploy_on_server
        with-items: server in <% $.servers %>
        input:
          server_name: <% $.server.name %>
          server_uuid: <% $.server.id %>
          config: |
            #!/bin/bash
            if ! grep "<% $.pubkey %>" /home/<% $.overcloud_admin %>/.ssh/authorized_keys; then
              echo "<% $.pubkey %>" >> /home/<% $.overcloud_admin %>/.ssh/authorized_keys
            fi
          config_name: copy_ssh_key
          group: script
          queue_name: <% $.queue_name %>

  upload_validations:
    tags:
      - tripleo-common-managed

    input:
      - queue_name: tripleo

    tasks:
      upload_validations:
        action: tripleo.validations.upload
        on-complete: send_message
        publish-on-error:
          status: FAILED
          message: <% task().result %>
        publish:
          status: SUCCESS
          message: <% task().result %>

      send_message:
        workflow: tripleo.messaging.v1.send
        input:
          queue_name: <% $.queue_name %>
          type: <% execution().name %>
          status: <% $.status %>
          execution: <% execution() %>
          message: <% $.get('message', '') %>
