---
- name: create a comptatible deployment scripts from oooq
  include: use_oooq.yaml
  tags: use_oooq
  when: use_oooq|bool

- name: Convert CLI options to parameters
  include: ../common/convert_cli_opts_params.yaml

- name: get auxiliary facts for major upgrades
  include: ../common/auxilary-facts.yaml

- name: check if undercloud is ssl enabled
  command: "grep -Fq OS_AUTH_URL=https {{ undercloud_rc }}"
  register: undercloud_ssl
  ignore_errors: true

- include: undercloud_ssl_camap.yaml
  when: undercloud_ssl|succeeded

- name: generate inventory file
  shell: |
    source {{ undercloud_rc }} ;
    tripleo-ansible-inventory --plan {{ overcloud_stack_name }} --static-yaml-inventory /tmp/fake-oc-hosts.yaml ;
    awk -F':' '/tripleo_role_name/ {gsub("[[:space:][:punct:]]", "", $2); print $2}' /tmp/fake-oc-hosts.yaml
  register: oc_roles

- name: store roles
  vars:
    controller_role_name: "{{ oc_roles.stdout|regex_search('[A-Za-z0-9]*[Cc]ontroller[A-Za-z0-9]*') }}"
  set_fact:
    oc_roles: "{{ [controller_role_name] +  oc_roles.stdout_lines|difference([controller_role_name]) }}"

- name: create workload launch script
  template:
    src: "{{ workload_launch_template }}"
    dest: "{{ workload_launch_script }}"
    mode: 0775

- name: create upgrade workaround scripts
  template:
    src: workarounds.sh.j2
    dest: "{{working_dir}}/{{ item }}.sh"
    mode: 0775
  with_items:
     - 'pre_overcloud_upgrade_prepare_workarounds'
     - 'post_overcloud_upgrade_prepare_workarounds'
     - 'pre_overcloud_upgrade_workarounds'
     - 'post_overcloud_upgrade_workarounds'
     - 'pre_overcloud_upgrade_converge_workarounds'
     - 'post_overcloud_upgrade_converge_workarounds'
  when: upgrade_workarounds

- include: ../common/create_l3_agent_connectivity_check_script.yml

- name: create the custom upgrade init commands
  template:
    src: "{{ upgrade_init_command_template }}"
    dest: "{{upgrade_init_command_dest}}"
    mode: 0775
  when: upgrade_init_command is string


- name: create overcloud upgrade script
  template:
    src: "{{ overcloud_upgrade_run_template }}"
    dest: "{{ overcloud_upgrade_run_script_base }}-{{ item }}.sh"
    mode: 0775
    force: true
  with_items:
    - "{{ oc_roles|default('all') }}"

- name: create overcloud converge script
  template:
    src: "{{ overcloud_upgrade_converge_template }}"
    dest: "{{ overcloud_upgrade_converge_script }}"
    mode: 0775
    force: true

- name: create ceph upgrade script
  template:
    src: "ceph-upgrade-run.sh.j2"
    dest: "{{ working_dir }}/ceph-upgrade-run.sh"
    mode: 0755
    force: true

- name: create Remove-Unused-Rpm env-file
  template:
    src: "remove-packages.yaml.j2"
    dest: "{{ working_dir }}/remove-packages.yaml"
    mode: 0755
    force: true
  when: upgrade_remove_rpm|bool

- include: ../common/ssh_config_skip_host.yml

- name: Create post upgrade scripts for controller nodes
  include: ../common/controller_post_scripts.yml

- name: Create L3-agent failover scripts
  include: ../common/create_l3_agent_failover_check_script.yml

- name: create nova actions check script
  include: ../common/create_nova_actions_check_script.yml
