---
# This playbook is an ad-hoc task used to deploy kvm nodes for OCP4
# It used the following variables, declared through inventory (group/host vars):
#  rhcos_version: 4.5.6 # upstream version to fetch from mirror.openshift.com in local httpd dir for deployment
#  rhcos_install_img_url: # Where to grab rhcos install http://<host>/<path>/rhcos/rhcos-4.3.8-x86_64-metal.x86_64.raw.gz
#  rhcos_ignition_file_url: # where to find ignition file http://<host>/config/rhcos/bootstrap.ign #to be modified based on group or host level, for bootstrap, master, etc
#  rhcos_install_url: #where kernel/initrd.img and .treeinfo are to be found, internal
#  kvm_guest_vcpus:
#  kvm_guest_memory:
#  kvm_guest_disk_size:  #in GiB, not to be added, so example: 120
#  kvm_host_bridge: # which defined bridged to use for the guest vnic
#  kvm_guest_vnic: ens3
#  ip:
#  nameservers:
#    - 192.168.1.1
#    - 192.168.2.2
#  gateway
#  deploy_host: # http server serving ignition files
- hosts: os_control_stg:os_control:ocp_controlplane_stg:ocp_controlplane:ocp_bootstrap:ocp_boostrap_stg:ocp_vms_stg:ocp_vms
  user: root
  gather_facts: false

  tasks:
    - block:
        - name: == OCP KVM provisioning == Ensuring we have needed httpd dir
          file:
            path: "/var/www/html/rhcos"
            state: directory
            mode: 0755
        - name: == OCP KVM provisioning == Importing generated Ignition files
          template:
            src: "/srv/web/infra/bigfiles/openshiftboot/{{ env }}/{{ item }}"
            dest: "/var/www/html/rhcos/{{ item }}"
            mode: 0755
          with_items:
            - bootstrap.ign
            - controlplane.ign
            - worker.ign
        - name: == OCP KVM provisioning == Creating treeinfo for virt-install --location
          template:
            src: "/srv/web/infra/bigfiles/openshiftboot/ocp-treeinfo.j2"
            dest: "/var/www/html/rhcos/.treeinfo"
            mode: 0644
        - name: == OCP KVM provisioning == Copy kubeadmin config files to bastion
          copy:
            src: "/srv/web/infra/bigfiles/openshiftboot/{{ env }}/auth/"
            dest: "/root/ocp4/ocp-{{env}}/"
        - name: == OCP KVM provisioning == Downloading RHCOS deploy files
          get_url:
            dest: /var/www/html/rhcos/
            mode: 0755
            url: "https://mirror.openshift.com/pub/openshift-v4/dependencies/rhcos/latest/{{ rhcos_version }}/{{ item }}"
#          run_once: True
          ignore_errors: True
          with_items:
            - "rhcos-{{ rhcos_version }}-x86_64-live-initramfs.x86_64.img"
            - "rhcos-{{ rhcos_version }}-x86_64-live-rootfs.x86_64.img"
            - "rhcos-{{ rhcos_version }}-x86_64-metal.x86_64.raw.gz"
            - "rhcos-{{ rhcos_version }}-x86_64-live-kernel-x86_64"
        - name: == OCP KVM provisioning == Ensuring symlink for correct version {{ rhcos_version }}
          file:
            state: link
            src: "/var/www/html/rhcos/rhcos-{{ rhcos_version }}-x86_64-metal.x86_64.raw.gz"
            dest: "/var/www/html/rhcos/rhcos-metal.raw"
      tags:
        - ignition
        - repo
      when: inventory_hostname in groups['os_control_stg'] or inventory_hostname in groups['os_control']

    - name: == OCP KVM provisioning == Downloading RHCOS deploy files
      get_url:
        dest: /srv/web/infra/bigfiles/tftpboot/rhcos/
        mode: 0755
        url: "https://mirror.openshift.com/pub/openshift-v4/dependencies/rhcos/latest/{{ rhcos_version }}/{{ item }}"
      run_once: True
      ignore_errors: True
      with_items:
        - "rhcos-{{ rhcos_version }}-x86_64-live-initramfs.x86_64.img"
        - "rhcos-{{ rhcos_version }}-x86_64-live-kernel-x86_64"
      delegate_to: 127.0.0.1
      tags:
        - ignition
        - repo

    - name: == OCP KVM provisioning == Checking OCP Virtual Guest exist[s]
      virt:
        name: "{{ inventory_hostname }}"
        state: running
      ignore_errors: yes
      delegate_to: "{{ vmhost }}"
      register: vm_exists
      tags:
        - kvm_deploy
      when: inventory_hostname in groups['ocp_vms_stg'] or inventory_hostname in groups['ocp_vms']
    - block:
      - name: ensure the lv for the guest is made
        lvol: lv={{ inventory_hostname }} vg={{ volgroup }} size={{ lvm_size }} state=present
        delegate_to: "{{ vmhost }}"
        tags:
          - kvm_deploy
        when: inventory_hostname in groups['ocp_vms_stg'] or inventory_hostname in groups['ocp_vms']
      - name: == OCP KVM provisioning == Deploying the OCP Virtual Guest[s]
        command: "{{ virt_install_command }}"
        delegate_to: "{{ vmhost }}"
        tags:
          - kvm_deploy
        when: inventory_hostname in groups['ocp_vms_stg'] or inventory_hostname in groups['ocp_vms']
      when: vm_exists is failed
