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

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

- name: create undercloud update script
  vars:
    log_prefix: "update"
  template:
    src: "{{ undercloud_upgrade_template }}"
    dest: "{{ undercloud_update_script }}"
    mode: 0775
    force: true

- name: create scripts with workarounds
  template:
    src: "{{ update_workarounds_template }}"
    dest: "{{ working_dir }}/{{ item.script }}"
    mode: 0755
    force: true
  with_items:
    - '{{ pre_undercloud_update_workarounds|default([]) }}'
    - '{{ post_undercloud_update_workarounds|default([]) }}'
    - '{{ pre_overcloud_update_prepare_workarounds|default([]) }}'
    - '{{ post_overcloud_update_prepare_workarounds|default([]) }}'
    - '{{ pre_overcloud_update_run_workarounds|default([]) }}'
    - '{{ post_overcloud_update_run_workarounds|default([]) }}'
  when: updates_workarounds|bool

- name: generate inventory file
  shell: |
    source {{ undercloud_rc }} ;
    tripleo-ansible-inventory --plan {{ overcloud_stack_name }} {% if overcloud_ssh_user != '' %} --ansible_ssh_user {{ overcloud_ssh_user }} {% endif %} --static-yaml-inventory /tmp/fake-oc-hosts.yaml ;
    awk -F':' '/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 overcloud update prepare script
  template:
    src: "{{ overcloud_update_prepare_template }}"
    dest: "{{ overcloud_update_prepare_script }}"
    mode: 0775
    force: true

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

- name: create docker container/images validate scripts
  template:
    src: "{{ overcloud_validate_images_template }}"
    dest: "{{ overcloud_validate_images_script }}"
    mode: 0755
    force: true
