---
- hosts: localhost
  gather_facts: no
  tasks:
  - name: calculate input image
    command: az image list -g "{{ openshift_azure_input_image_ns }}" --query "[?starts_with(name, '{{ openshift_azure_input_image_prefix }}-') && tags.valid=='true'] | sort_by(@, &name) | [-1]"
    register: input_image

  - name: provision resource group
    import_tasks: tasks/provision_instance.yml
    vars:
      create_args: --data-disk-sizes-gb 128

  - set_fact:
      openshift_node_bootstrap: True

- hosts: nodes
  tasks:
  - set_fact:
      openshift_deployment_type: "{{ 'openshift-enterprise' if ansible_distribution == 'RedHat' else 'origin' }}"
      openshift_enable_origin_repo: False
      openshift_node_bootstrap: True
      skip_node_svc_handlers: True
      openshift_master_fire_handlers: False
      openshift_master_systemd_enable: False
      openshift_additional_repos: "{{ azure_node_repos[ansible_distribution] }}"

  - set_fact:
      openshift_additional_repos: "{{ openshift_additional_repos + [{'name': 'install_repo', 'baseurl': openshift_azure_install_repo, 'enabled': true, 'gpgcheck': false}] }}"
    when: openshift_azure_install_repo is defined and openshift_azure_install_repo

  - name: install centos-release-paas-common rpm
    yum:
      name: centos-release-paas-common
      state: present
    when: ansible_distribution == "CentOS"

  - name: configure yum repositories
    import_tasks: tasks/yum_certs.yml

  - name: update rpms
    import_role:
      name: os_update_latest
    vars:
      os_update_latest_reboot: True

- name: install openshift
  import_playbook: ../../openshift-node/private/image_prep.yml

- hosts: nodes
  tasks:
  - import_role:
      name: openshift_master_facts

  - name: setup systemd units
    import_role:
      name: openshift_master
      tasks_from: systemd_units.yml

  - meta: flush_handlers

  - name: deconfigure yum repositories
    import_tasks: tasks/remove_yum.yml

  - name: add get-node-logs script
    copy:
      src: files/get-node-logs
      dest: /usr/local/bin/get-node-logs
      owner: root
      group: root
      mode: 0755

  - name: record installed rpms
    yum:
      list: installed
    register: yum

  - name: disable waagent data disk management
    lineinfile:
      path: /etc/waagent.conf
      regexp: "{{ item.regexp }}"
      line: "{{ item.line }}"
    with_items:
    - { regexp: '^ResourceDisk\.Format=', line: 'ResourceDisk.Format=n' }

  - name: persist oreg_url
    copy:
      dest: "/etc/origin/oreg_url"
      content: "{{ oreg_url }}"
    when: oreg_url is defined

  - name: run waagent deprovision
    command: waagent -deprovision+user -force
    args:
      chdir: /

- hosts: localhost
  gather_facts: no
  tasks:
  - set_fact:
      openshift_rpm: "{{ hostvars[groups['nodes'][0]]['yum'].results | selectattr('name', 'match', '^(origin|atomic-openshift)$') | first }}"

  - name: create image
    import_tasks: tasks/create_image_from_vm.yml
    vars:
      image_resource_group: "{{ openshift_azure_output_image_ns }}"
      image_name: "{{ openshift_azure_output_image_name }}"
      image_tags:
        base_image: "{{ (input_image.stdout | from_json).name }}"
        kernel: "{{ hostvars[groups['nodes'][0]]['ansible_kernel'] }}"
        openshift: "{{ openshift_rpm.name }}-{{ openshift_rpm.version }}-{{ openshift_rpm.release }}.{{ openshift_rpm.arch }}"

  - name: create blob
    import_tasks: tasks/create_blob_from_vm.yml
    vars:
      image_name: "{{ openshift_azure_output_image_name }}"
    when: openshift_azure_storage_account is defined and openshift_azure_storage_account
