---
- name: Register roles data file location if exists
  shell: "grep '\\-r\\ \\|\\-\\-roles' {{ overcloud_deploy_script }} | awk {'print $2'}"
  register: custom_roles_file
  ignore_errors: true

- name: Check if roles data has already been adjusted
  stat:
    path: "{{ custom_roles_file.stdout }}.pre_queens_ffu_upgrade"
  register: custom_roles_adjusted

- block:
    - name: Make a copy of the custom roles data file
      copy:
        src: "{{ custom_roles_file.stdout }}"
        dest: "{{ custom_roles_file.stdout }}.pre_queens_ffu_upgrade"
        remote_src: true

    - name: Cleanup deprecated resources
      shell: |
          {{ json_dump }} < {{ custom_roles_file.stdout }} |\
          jq 'del(.[] | .ServicesDefault[] | select (. =="OS::TripleO::Services::VipHosts"))' |\
          jq 'del(.[] | .ServicesDefault[] | select (. =="OS::TripleO::Services::ManilaBackendGeneric"))' |\
          jq 'del(.[] | .ServicesDefault[] | select (. =="OS::TripleO::Services::FluentdClient"))' |\
          jq 'del(.[] | .ServicesDefault[] | select (. =="OS::TripleO::Services::Core"))' |\
          {{ yaml_dump }} > {{ custom_roles_file.stdout }}.cleared

    - name: Assigns deprecated params to Controller, Compute and ObjectStorage role
      shell: |
          {{ json_dump }} < {{ custom_roles_file.stdout }}.cleared |\
          jq "map( if .name | contains (\"Compute\") then .deprecated_server_resource_name=\"NovaCompute\" else . end)" |\
          jq "map( if .name | contains (\"Compute\") then .deprecated_param_ips=\"NovaComputeIPs\" else . end)" |\
          jq "map( if .name | contains (\"Compute\") then .deprecated_param_scheduler_hints=\"NovaComputeSchedulerHints\" else . end)" |\
          jq "map( if .name | contains (\"Compute\") then .deprecated_param_metadata=\"NovaComputeServerMetadata\" else . end)" |\
          jq "map( if .name | contains (\"Compute\") then .deprecated_param_extraconfig=\"NovaComputeExtraConfig\" else . end)" |\
          jq "map( if .name | contains (\"Compute\") then .deprecated_param_image=\"NovaImage\" else . end)" |\
          jq "map( if .name | contains (\"Compute\") then .uses_deprecated_params=\"True\" else . end)" |\
          jq "map( if .name | contains (\"Controller\") then .deprecated_param_image=\"controllerImage\" else . end)" |\
          jq "map( if .name | contains (\"Controller\") then .deprecated_param_flavor=\"OvercloudControlFlavor\" else . end)" |\
          jq "map( if .name | contains (\"Controller\") then .deprecated_param_extraconfig=\"controllerExtraConfig\" else . end)" |\
          jq "map( if .name | contains (\"Controller\") then .uses_deprecated_params=\"True\" else . end)" |\
          jq "map( if .name | contains (\"ObjectStorage\") then .deprecated_param_metadata=\"SwiftStorageServerMetadata\" else . end)" |\
          jq "map( if .name | contains (\"ObjectStorage\") then .deprecated_param_ips=\"SwiftStorageIPs\" else . end)" |\
          jq "map( if .name | contains (\"ObjectStorage\") then .deprecated_param_image=\"SwiftStorageImage\" else . end)" |\
          jq "map( if .name | contains (\"ObjectStorage\") then .deprecated_param_flavor=\"OvercloudSwiftStorageFlavor\" else . end)" |\
          jq "map( if .name | contains (\"ObjectStorage\") then .uses_deprecated_params=\"True\" else . end)" |\
          {{ yaml_dump }} > {{ custom_roles_file.stdout }}.compute_deprecated

    - name: Add new common services
      shell: |
          {{ json_dump }} < {{ custom_roles_file.stdout }}.compute_deprecated |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::MySQLClient"] | unique))' |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::CertmongerUser"] | unique))' |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::Docker"] | unique))' |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::ContainersLogrotateCrond"] | unique))' |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::Tuned"] | unique))' |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::AuditD"] | unique))' |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::Collectd"] | unique))' |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::Rhsm"] | unique))' |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::RsyslogSidecar"] | unique))' |\
          jq 'map(.ServicesDefault |= (. += ["OS::TripleO::Services::Securetty"] | unique))' |\
          {{ yaml_dump }} > {{ custom_roles_file.stdout }}.extended
      register: roles_with_common_services

    - name: Add selected services based on preexisting ones
      shell: |
          {{ json_dump }} < {{ custom_roles_file.stdout }}.extended |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::NovaApi"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::NovaPlacement"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::NovaCompute"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::NovaMigrationTarget"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::AodhApi"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::PankoApi"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::MySQL"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::Clustercheck"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::BlockStorageCinderVolume"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::Iscsid"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::CinderVolume"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::Iscsid"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::NovaCompute"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::Iscsid"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::NovaApi"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::Ec2Api"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::CephMon"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::CephMgr"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::CephMon"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::CephMds"] | unique)
                   else . end)' |\
          jq 'map( if .ServicesDefault | contains (["OS::TripleO::Services::CephMon"]) then
                      .ServicesDefault |= (. += ["OS::TripleO::Services::CephRbdMirror"] | unique)
                   else . end)' |\
          {{ yaml_dump }} > {{ custom_roles_file.stdout }}
  when: custom_roles_file.stdout|length > 0 and not custom_roles_adjusted.stat.exists
  vars:
    json_dump: "python -c 'import sys, yaml, simplejson as json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)'"
    yaml_dump: "python -c 'import simplejson, sys, yaml; print yaml.dump(simplejson.loads(str(sys.stdin.read())), default_flow_style=False)'"
