<commit_before>---
# This will install rhos-release,
# and gather information about latest puddles
# by letting it create repo files in tmp folder
- name: Run rhos-release to get puddle numbers
  hosts: virthost
  gather_facts: no
  any_errors_fatal: true
  vars:
      tmp_repos: "/tmp/uc_snapshot_repos"
  pre_tasks:
      - name: create temporary directory for repo files
        file:
          path: "{{ tmp_repos }}"
          state: directory
  roles:
      - role: installer/rhos-release
        rr_target_directory: "{{ tmp_repos }}"
        rr_rpm_url: "{{ installer.product.rpm }}"
        director_version: "{{ installer.product.version }}"
        director_build: "{{ installer.product.build }}"
        core_version: "{{ installer.product.core.version }}"
        core_build: "{{ installer.product.core.build }}"
        mirror: "{{ installer.mirror | default(omit) }}"
  tasks:
      - name: remove temporary directory for repo files
        file:
          path: "{{ tmp_repos }}"
          state: absent

# Any step that should happen before initiating the osp-d playbook
# This could be validation of the hosts, package installation that is assumed as per the guide, etc..
- name: Prepare our undercloud
  hosts: virthost
  gather_facts: no
  any_errors_fatal: true
  vars:
      undercloud_image_file: "undercloud-quickstart-{{ director_puddle_version | default(core_puddle_version) }}"
      # todo(yfried): add external refs so users can override these values and customize the Undercloud
      undercloud_node:
          name: 'undercloud-0'
          disk_cache: "default"
          memory: '16384'
          cpu: '4'
          os:
              type: linux
              variant: rhel7
      undercloud_disk_path: "/var/lib/libvirt/images/{{ undercloud_node.name }}-disk1.qcow2"

  tasks:
      - block:
        - block:
            - name: download the undercloud image file
              get_url:
                  url: "http://{{ private.storage.oc_image_repo.server }}/{{ private.storage.oc_image_repo.path }}/{{ installer.product.version }}/{{ undercloud_image_file }}.qcow2"
                  dest: "~/{{ undercloud_image_file }}.qcow2"

          # Old images are tar'ed
          rescue:
            - name: download the undercloud tar file
              get_url:
                  url: "http://{{ private.storage.oc_image_repo.server }}/{{ private.storage.oc_image_repo.path }}/{{ installer.product.version }}/{{ undercloud_image_file }}.tar"
                  dest: "~/{{ undercloud_image_file }}.tar"

            - name: untar the undercloud quickstart
              unarchive:
                  src: "~/{{ undercloud_image_file }}.tar"
                  dest: "~/"
                  copy: no
                  list_files: yes
              register: tar

            - name: remove the old XML
              file:
                  state: absent
                  path: "{{ undercloud_node.name }}.xml"
        rescue:
          - fail: msg="Image couldn't be downloaded - check first, if it was even built. Possible causes are
                       a) Check if phase1 for your version of OSPd (OSPd-{{ installer.product.version }}) passed
                       b) Check if job for image build (qe-uc-snapshot-XY) passed."

      - name: copy the image into libvirt images folder
        vars:
            image_filename: "~/{{ (tar is defined) | ternary(undercloud_node.name + '-disk1', undercloud_image_file) }}.qcow2"
        copy:
            remote_src: yes
            force: yes
            src: "{{ image_filename }}"
            dest: "{{ undercloud_disk_path }}"
            owner: qemu
            group: qemu

      - name: install libguestfs-tool / virt-customize
        yum:
            name: libguestfs-tools
            state: present

      - name: inject our key into the undercloud image
        shell: "virt-customize -a {{ undercloud_disk_path }} --root-password password:redhat --mkdir /root/.ssh --chmod 0700:/root/.ssh --upload /root/.ssh/id_rsa.pub:/root/.ssh/authorized_keys --selinux-relabel"

      - name: create Undercloud VM from snapshot
        shell: |
            virt-install --name {{ undercloud_node.name }} \
               --disk path={{ undercloud_disk_path }},device=disk,bus=virtio,format=qcow2,cache={{ undercloud_node.disk_cache }} \
               --network network:data \
               --network network:management \
               --network network:external \
               --virt-type kvm \
               --cpu host-model \
               --ram {{ undercloud_node.memory }} \
               --vcpus {{ undercloud_node.cpu }} \
               --os-variant {{ undercloud_node.os.variant }} \
               --import \
               --noautoconsole \
               --autostart \
               --vnc \
               --dry-run --print-xml
        register: virt_xml

      - name: define the undercloud VM
        virt:
            name: "{{ undercloud_node.name }}"
            command: define
            xml: "{{ virt_xml.stdout }}"

      - name: get correct MAC of external interface for undercloud (in case we have multiple dhcp leases in libvirt's db)
        shell: |
            virsh dumpxml undercloud-0 | grep external -B 1 | grep mac | cut -d\' -f2
        register: mac_ext

      - name: start the undercloud VM
        virt:
            name: "{{ undercloud_node.name }}"
            state: running

      #TODO(yfried): cleanup should be in a block/allways format
      - name: remove unarchived files from machine
        vars:
            image_filename: "~/{{ (tar is defined) | ternary(undercloud_node.name + '-disk1', undercloud_image_file) }}.qcow2"
        file:
            state: absent
            path: "{{ image_filename }}"

      - name: remove quickstart tar file
        file:
            state: absent
            path: "{{ undercloud_image_file }}.{{ (tar is defined) | ternary('tar', 'qcow2') }}"

      - name: wait for the undercloud IP to become available
        shell: |
            virsh net-dhcp-leases external | grep {{ mac_ext.stdout }} | awk '($4 == "ipv4") && ($6 =="{{ undercloud_node.name }}") {print $5}'
        register: undercloud_ip
        until: undercloud_ip.stdout != ''
        retries: 15
        delay: 15

      - name: waiting for the undercloud to be SSH available
        wait_for:
            port: 22
            host: "{{ undercloud_ip.stdout | ipaddr('address') }}"
            search_regex: OpenSSH
        delay: 10

      - name: add undercloud to host list
        add_host:
            name: "{{ undercloud_node.name }}"
            hostname: "undercloud-0"
            node_label: "undercloud-0"
            groups: "undercloud,tester,openstack_nodes"
            ansible_ssh_user: "root"
            ansible_ssh_private_key_file: "{{ hostvars[groups['overcloud_nodes'][0]].ansible_ssh_private_key_file }}"
            ansible_ssh_host: "{{ undercloud_ip.stdout | ipaddr('address') }}"
            ansible_ssh_common_args: "
                -o ForwardAgent=yes
                -o ServerAliveInterval=30
                -o ControlMaster=auto
                -o ControlPersist=30m
                -o StrictHostKeyChecking=no
                -o UserKnownHostsFile=/dev/null
                -o ProxyCommand=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -W %h:%p -i {{ ansible_ssh_private_key_file }} {{ ansible_ssh_user }}@{{ ansible_ssh_host }}\""
        notify:
            - update hosts
            - Link to new Inventory file
            - update /etc/hosts with undercloud's details<commit_msg>RHOSINFRA-453 - ssh muffin UC as stack user<commit_after>---
# This will install rhos-release,
# and gather information about latest puddles
# by letting it create repo files in tmp folder
- name: Run rhos-release to get puddle numbers
  hosts: virthost
  gather_facts: no
  any_errors_fatal: true
  vars:
      tmp_repos: "/tmp/uc_snapshot_repos"
  pre_tasks:
      - name: create temporary directory for repo files
        file:
          path: "{{ tmp_repos }}"
          state: directory
  roles:
      - role: installer/rhos-release
        rr_target_directory: "{{ tmp_repos }}"
        rr_rpm_url: "{{ installer.product.rpm }}"
        director_version: "{{ installer.product.version }}"
        director_build: "{{ installer.product.build }}"
        core_version: "{{ installer.product.core.version }}"
        core_build: "{{ installer.product.core.build }}"
        mirror: "{{ installer.mirror | default(omit) }}"
  tasks:
      - name: remove temporary directory for repo files
        file:
          path: "{{ tmp_repos }}"
          state: absent

# Any step that should happen before initiating the osp-d playbook
# This could be validation of the hosts, package installation that is assumed as per the guide, etc..
- name: Prepare our undercloud
  hosts: virthost
  gather_facts: no
  any_errors_fatal: true
  vars:
      undercloud_image_file: "undercloud-quickstart-{{ director_puddle_version | default(core_puddle_version) }}"
      # todo(yfried): add external refs so users can override these values and customize the Undercloud
      undercloud_node:
          name: 'undercloud-0'
          disk_cache: "default"
          memory: '16384'
          cpu: '4'
          os:
              type: linux
              variant: rhel7
      undercloud_disk_path: "/var/lib/libvirt/images/{{ undercloud_node.name }}-disk1.qcow2"

  tasks:
      - block:
        - block:
            - name: download the undercloud image file
              get_url:
                  url: "http://{{ private.storage.oc_image_repo.server }}/{{ private.storage.oc_image_repo.path }}/{{ installer.product.version }}/{{ undercloud_image_file }}.qcow2"
                  dest: "~/{{ undercloud_image_file }}.qcow2"

          # Old images are tar'ed
          rescue:
            - name: download the undercloud tar file
              get_url:
                  url: "http://{{ private.storage.oc_image_repo.server }}/{{ private.storage.oc_image_repo.path }}/{{ installer.product.version }}/{{ undercloud_image_file }}.tar"
                  dest: "~/{{ undercloud_image_file }}.tar"

            - name: untar the undercloud quickstart
              unarchive:
                  src: "~/{{ undercloud_image_file }}.tar"
                  dest: "~/"
                  copy: no
                  list_files: yes
              register: tar

            - name: remove the old XML
              file:
                  state: absent
                  path: "{{ undercloud_node.name }}.xml"
        rescue:
          - fail: msg="Image couldn't be downloaded - check first, if it was even built. Possible causes are
                       a) Check if phase1 for your version of OSPd (OSPd-{{ installer.product.version }}) passed
                       b) Check if job for image build (qe-uc-snapshot-XY) passed."

      - name: copy the image into libvirt images folder
        vars:
            image_filename: "~/{{ (tar is defined) | ternary(undercloud_node.name + '-disk1', undercloud_image_file) }}.qcow2"
        copy:
            remote_src: yes
            force: yes
            src: "{{ image_filename }}"
            dest: "{{ undercloud_disk_path }}"
            owner: qemu
            group: qemu

      - name: install libguestfs-tool / virt-customize
        yum:
            name: libguestfs-tools
            state: present

      - name: inject our key into the undercloud image
        shell: "virt-customize -a {{ undercloud_disk_path }} --root-password <PASSWORD> --mkdir /root/.ssh --chmod 0700:/root/.ssh --upload /root/.ssh/id_rsa.pub:/root/.ssh/authorized_keys --selinux-relabel"

      - name: create Undercloud VM from snapshot
        shell: |
            virt-install --name {{ undercloud_node.name }} \
               --disk path={{ undercloud_disk_path }},device=disk,bus=virtio,format=qcow2,cache={{ undercloud_node.disk_cache }} \
               --network network:data \
               --network network:management \
               --network network:external \
               --virt-type kvm \
               --cpu host-model \
               --ram {{ undercloud_node.memory }} \
               --vcpus {{ undercloud_node.cpu }} \
               --os-variant {{ undercloud_node.os.variant }} \
               --import \
               --noautoconsole \
               --autostart \
               --vnc \
               --dry-run --print-xml
        register: virt_xml

      - name: define the undercloud VM
        virt:
            name: "{{ undercloud_node.name }}"
            command: define
            xml: "{{ virt_xml.stdout }}"

      - name: get correct MAC of external interface for undercloud (in case we have multiple dhcp leases in libvirt's db)
        shell: |
            virsh dumpxml undercloud-0 | grep external -B 1 | grep mac | cut -d\' -f2
        register: mac_ext

      - name: start the undercloud VM
        virt:
            name: "{{ undercloud_node.name }}"
            state: running

      #TODO(yfried): cleanup should be in a block/allways format
      - name: remove unarchived files from machine
        vars:
            image_filename: "~/{{ (tar is defined) | ternary(undercloud_node.name + '-disk1', undercloud_image_file) }}.qcow2"
        file:
            state: absent
            path: "{{ image_filename }}"

      - name: remove quickstart tar file
        file:
            state: absent
            path: "{{ undercloud_image_file }}.{{ (tar is defined) | ternary('tar', 'qcow2') }}"

      - name: wait for the undercloud IP to become available
        shell: |
            virsh net-dhcp-leases external | grep {{ mac_ext.stdout }} | awk '($4 == "ipv4") && ($6 =="{{ undercloud_node.name }}") {print $5}'
        register: undercloud_ip
        until: undercloud_ip.stdout != ''
        retries: 15
        delay: 15

      - name: waiting for the undercloud to be SSH available
        wait_for:
            port: 22
            host: "{{ undercloud_ip.stdout | ipaddr('address') }}"
            search_regex: OpenSSH
        delay: 10

      - name: add undercloud to host list
        add_host:
            name: "{{ undercloud_node.name }}"
            hostname: "undercloud-0"
            node_label: "undercloud-0"
            groups: "undercloud,tester,openstack_nodes"
            ansible_ssh_user: "{{ installer.user.name }}"
            ansible_ssh_private_key_file: "{{ hostvars[groups['overcloud_nodes'][0]].ansible_ssh_private_key_file }}"
            ansible_ssh_host: "{{ undercloud_ip.stdout | ipaddr('address') }}"
            ansible_ssh_common_args: "
                -o ForwardAgent=yes
                -o ServerAliveInterval=30
                -o ControlMaster=auto
                -o ControlPersist=30m
                -o StrictHostKeyChecking=no
                -o UserKnownHostsFile=/dev/null
                -o ProxyCommand=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -W %h:%p -i {{ ansible_ssh_private_key_file }} {{ ansible_ssh_user }}@{{ ansible_ssh_host }}\""
        notify:
            - update hosts
            - Link to new Inventory file
            - update /etc/hosts with undercloud's details
{'fraction_non_alphanumeric': 0.108767087276551, 'fraction_numerical': 0.00611198738170347, 'mean_word_length': 1.8689668174962293, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4547398', 'n_tokens_mistral': 4493, 'n_tokens_neox': 4259, 'n_words': 1139}