heat_template_version: rocky

description: External tasks definition for Skydive

parameters:
  ServiceData:
    default: {}
    description: Dictionary packing service data
    type: json
  ServiceNetMap:
    default: {}
    description: Mapping of service_name -> network name. Typically set
                 via parameter_defaults in the resource registry.  This
                 mapping overrides those in ServiceNetMapDefaults.
    type: json
  DefaultPasswords:
    default: {}
    type: json
  RoleName:
    default: ''
    description: Role name on which the service is applied
    type: string
  RoleParameters:
    default: {}
    description: Parameters specific to the role
    type: json
  EndpointMap:
    default: {}
    description: Mapping of service endpoint -> protocol. Typically set
                 via parameter_defaults in the resource registry.
    type: json
  KeystoneRegion:
    type: string
    default: 'regionOne'
    description: Keystone region for endpoint
  EnableInternalTLS:
    type: boolean
    default: false
  SkydiveAnsiblePlaybook:
    type: string
    description: Path to the skydive-ansible playbook to execute
    default: /usr/share/skydive-ansible/playbook.yml.sample
  SkydiveVars:
    type: json
    description: Variables used by skydive-ansible
    default: {}
  ContainerSkydiveAnalyzerImage:
    description: Analyzer docker image
    type: string
  ContainerSkydiveAgentImage:
    description: Agent docker image
    type: string

outputs:
  role_data:
    description: Role data for Skydive services.
    value:
      service_name: skydive_analyzer
      firewall_rules:
        '150 skydive_analyzer':
          dport:
            - 8082
            - 12379
            - 12380
      upgrade_tasks: []
      docker_config: {}
      external_deploy_tasks:
        - name: Skydive deployment
          when: step|int == 5
          block:
            - name: create skydive temp dirs
              become: true
              file:
                path: "{{item}}"
                state: directory
                owner: "{{ansible_user}}"
              with_items:
                - "{{playbook_dir}}/skydive"

            - name: Skydive global vars fact
              set_fact:
                os_auth_url: {get_param: [EndpointMap, KeystoneInternal, uri_no_suffix]}
                os_region_name: {get_param: KeystoneRegion}
                skydive_ansible_playbook: {get_param: SkydiveAnsiblePlaybook}
                skydive_analyzer_docker_image: {get_param: ContainerSkydiveAnalyzerImage}
                skydive_agent_docker_image: {get_param: ContainerSkydiveAgentImage}
                skydive_vars: {get_param: SkydiveVars}

            - name: Skydive global defaults
              copy:
                dest: "{{playbook_dir}}/skydive/global_defaults.yml"
                content: |
                  skydive_docker_registry: {{skydive_analyzer_docker_image | regex_replace("([^/]*).*", "\1")}}
                  skydive_analyzer_docker_image_tag: {{skydive_analyzer_docker_image | regex_replace(".*:")}}
                  skydive_analyzer_docker_image: {{skydive_analyzer_docker_image | regex_replace("[^/]*/([^:]*).*", "\1")}}
                  skydive_analyzer_docker_command: "/usr/bin/skydive analyzer --conf /etc/skydive.yml"
                  skydive_agent_docker_image_tag: {{skydive_agent_docker_image | regex_replace(".*:")}}
                  skydive_agent_docker_image: {{skydive_agent_docker_image | regex_replace("[^/]*/([^:]*).*", "\1")}}
                  skydive_agent_docker_command: "/usr/bin/skydive agent --conf /etc/skydive.yml"
                  os_auth_url: {{overcloud_keystone_url}}/v3
                  os_username: {{username}}
                  os_password: {{overcloud_admin_password}}
                  os_tenant_name: {{project_name}}
                  skydive_auth_type: keystone
                  skydive_os_auth_url: {{os_auth_url}}/v3
                  skydive_os_service_region_name: {{os_region_name}}
                  skydive_deployment_mode: container
                  skydive_deployment_source: tripleo
                  skydive_flow_protocol: websocket
                  skydive_topology_probes:
                    - neutron
                    - ovsdb
                    - runc

            - name: generate Skydive global vars
              copy:
                dest: "{{playbook_dir}}/skydive/global_vars.yml"
                content: "{{skydive_vars.globals|default({})|to_nice_yaml}}"

            - name: generate inventory
              copy:
                dest: "{{playbook_dir}}/skydive/inventory.yml"
                content: |
                  analyzers:
                    hosts:
                      {% for host in groups['skydive_analyzer'] -%}
                      {{hostvars.raw_get(host)['ansible_hostname']}}:
                        ansible_user: {{ hostvars.raw_get(host)['ansible_user'] | default(hostvars.raw_get(host)['ansible_ssh_user']) | default('root') }}
                        ansible_host: {{ hostvars.raw_get(host)['ansible_host'] | default(host) }}
                        ansible_become: true
                        skydive_listen_ip: {{hostvars.raw_get(host)['ctlplane_ip']}}
                        {% if skydive_vars.analyzers is defined -%}
                        {{skydive_vars.analyzers|to_nice_yaml(indent=6)}}
                        {%- endif %}

                      {% endfor %}

                  agents:
                    hosts:
                      {% for host in groups['skydive_agent'] -%}
                      {{hostvars.raw_get(host)['ansible_hostname']}}:
                        ansible_user: {{ hostvars.raw_get(host)['ansible_user'] | default(hostvars.raw_get(host)['ansible_ssh_user']) | default('root') }}
                        ansible_host: {{ hostvars.raw_get(host)['ansible_host'] | default(host) }}
                        ansible_become: true
                        skydive_listen_ip: {{hostvars.raw_get(host)['ctlplane_ip']}}
                        {% if skydive_vars.agents is defined -%}
                        {{skydive_vars.agents|to_nice_yaml(indent=6)}}
                        {%- endif %}

                      {% endfor %}

            - name: write Skydive deploy script
              copy:
                content: >
                  #!/bin/bash

                  set -e

                  ANSIBLE_CONFIG="{{playbook_dir}}/ansible.cfg"
                  ANSIBLE_HOST_KEY_CHECKING=False
                  ansible-playbook
                  {% if ansible_ssh_private_key_file is defined %}--private-key '{{ansible_ssh_private_key_file}}'{% endif %}
                  -i '{{playbook_dir}}/skydive/inventory.yml'
                  --extra-vars '@{{playbook_dir}}/skydive/global_defaults.yml'
                  --extra-vars '@{{playbook_dir}}/skydive/global_vars.yml'
                  '{{skydive_ansible_playbook}}'
                dest: '{{playbook_dir}}/skydive/skydive-deploy.sh'
                mode: 0700

            - name: run skydive-deploy.sh (immediate log at {{playbook_dir}}/skydive/playbook.log)
              become: true
              shell: |
                {{playbook_dir}}/skydive/skydive-deploy.sh 2>&1 | tee {{playbook_dir}}/skydive/playbook.log
                exit ${PIPESTATUS[0]}
