---
- 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

- hosts: nodes
  tasks:
  - set_fact:
      openshift_deployment_type: "{{ 'openshift-enterprise' if ansible_distribution == 'RedHat' else 'origin' }}"
      openshift_enable_origin_repo: False
      skip_node_svc_handlers: True
      openshift_additional_repos: "{{ azure_node_repos[ansible_distribution] }}"
      openshift_node_include_control_plane: True
      openshift_control_plane_static_pod_location: /etc/origin/node/disabled/
      openshift_node_group_cloud_provider: azure
      openshift_node_image_prep_packages:
      - strace
      - tcpdump
      etcd_ip: ETCD_IP_REPLACE
      etcd_hostname: ETCD_HOSTNAME_REPLACE
      etcdctlv2: ETCD_CTL2_REPLACE

  - name: add insights-client to package installs when on rhel
    set_fact:
      openshift_node_image_prep_packages: "{{ openshift_node_image_prep_packages | union(['insights-client']) }}"
    when: openshift_deployment_type == 'openshift-enterprise'

  - name: add skopeo and crio to package installs when crio enabled
    set_fact:
      openshift_node_image_prep_packages: "{{ openshift_node_image_prep_packages | union(['skopeo', 'crio']) }}"
    when: openshift_use_crio

  - 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
    include_role:
      name: os_update_latest
    vars:
      os_update_latest_reboot: True

- name: install openshift
  import_playbook: ../../openshift-node/private/image_prep.yml
  vars:
    etcd_image: "{{ etcd_image_dict[openshift_deployment_type] }}"

- hosts: nodes
  tasks:
  - name: set seboolean for samba use
    seboolean:
      name: virt_use_samba
      state: yes
      persistent: yes

  - 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: create systemd directory for waagent
    file:
      path: /etc/systemd/system/waagent.service.d
      state: directory
      mode: 0755
      owner: root
      group: root

  # set waagent to start after docker
  - name: set waagent systemd file
    copy:
      dest: /etc/systemd/system/waagent.service.d/custom.conf
      src: files/waagent.service
      owner: root
      group: root
      mode: 0644

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

  - name: create a file with image name
    copy:
      content: |
        ---
        image_name: {{ openshift_azure_output_image_name }}
        base_image: {{ (hostvars['localhost'].input_image.stdout | from_json).name }}
        subscription: {{ lookup('env', 'AZURE_SUBSCRIPTION_ID') }}
        resource_group: {{ openshift_azure_output_image_ns }}
      dest: /etc/origin/image.yml
      owner: root
      group: root
      mode: '0440'

  - name: run waagent deprovision
    shell: sleep 2 && waagent -deprovision+user -force
    async: 1
    poll: 0

- hosts: localhost
  gather_facts: no
  tasks:
  - set_fact:
      openshift_rpm: "{{ hostvars[groups['nodes'][0]]['yum'].results | selectattr('name', 'match', '^(origin-hyperkube|atomic-openshift-hyperkube)$') | 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.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
