Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move libvirt test plays to external collection #61

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ install:
@type poetry >/dev/null || pip3 install poetry
@type yq || sudo apt-get install -y yq
@sudo apt-get install -y libvirt-dev
@type nmcli || sudo apt-get install -y network-manager
@poetry install --no-root

lint: install
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
namespace: nephelaiio
name: mongodb
version: 0.2.24
version: 0.2.25
readme: README.md
authors:
- Ted Cook <[email protected]>
Expand Down
22 changes: 0 additions & 22 deletions molecule/common/cleanup.yml

This file was deleted.

276 changes: 2 additions & 274 deletions molecule/common/create.yml
Original file line number Diff line number Diff line change
@@ -1,275 +1,3 @@
---
- name: Configure KVM host
hosts: localhost
become: true
vars_files:
- vars.yml
tasks:
- name: Install package requirements
ansible.builtin.package:
name:
- qemu-kvm
- qemu-efi
- libvirt-clients
- libvirt-daemon-system
- libvirt-daemon-system-systemd
- python3-libvirt
become: true

- name: Query libvirt pools
community.libvirt.virt_pool:
command: list_pools
register: _pools

- name: Create libvirt path
ansible.builtin.file:
path: "{{ _libvirt_path }}"
state: directory
owner: root
group: root
mode: 0755
become: true

- name: Define libvirt pool
community.libvirt.virt_pool:
command: define
name: "{{ _libvirt_pool }}"
xml: "{{ lookup('ansible.builtin.template', 'pool.j2.xml') }}"
vars:
_pool: "{{ _libvirt_pool }}"
_path: "{{ _libvirt_path }}"

- name: Activate libvirt pool
community.libvirt.virt_pool:
state: active
name: "{{ _libvirt_pool }}"

- name: Add libvirt network
community.libvirt.virt_net:
command: define
name: "{{ _libvirt_network }}"
xml: "{{ lookup('ansible.builtin.template', 'network.j2.xml') }}"
vars:
_network: "{{ _libvirt_network }}"
_address: "{{ _libvirt_address }}"
_dhcp: "{{ _libvirt_dhcp }}"
_net_address: "{{ _address | ansible.utils.ipaddr('1') | ansible.utils.ipaddr('address') }}"
_net_mask: "{{ _address | ansible.utils.ipaddr('netmask') }}"
_net_dhcp_start: "{{ _dhcp | ansible.utils.ipaddr('2') | ansible.utils.ipaddr('address') }}"
_net_dhcp_end: "{{ _dhcp | ansible.utils.ipaddr('-2') | ansible.utils.ipaddr('address') }}"
when: _libvirt_network != "default"

- name: Start libvirt network
community.libvirt.virt_net:
command: create
name: "{{ _libvirt_network }}"
when: _libvirt_network != "default"

- name: List cached images
ansible.builtin.find:
paths: "{{ _libvirt_pool }}"
recurse: false
register: _cache_query

- name: Inspect cached images
ansible.builtin.set_fact:
_uncached_images: "{{ (_uncached_images | default([])) + [item] }}"
vars:
_cached_images: "{{ _cache_query.files | map(attribute='path') }}"
_basename: "{{ item | urlsplit('path') | basename }}"
_path: "{{ _libvirt_pool }}/{{ _basename }}"
loop: "{{ molecule_yml.platforms | map(attribute='image') | unique }}"
when: _path not in _cached_images

- name: Cache platform images
ansible.builtin.get_url:
url: "{{ item }}"
dest: "{{ _path }}"
owner: root
group: root
mode: 0600
vars:
_basename: "{{ item | urlsplit('path') | basename }}"
_path: "{{ _libvirt_path }}/{{ _basename }}"
loop: "{{ _uncached_images | default([]) }}"


- name: Create KVM guests
hosts: all
gather_facts: false
become: true
vars_files:
- vars.yml
tasks:
- name: Create KVM guest
delegate_to: localhost
block:
- name: Create SSH key
community.crypto.openssh_keypair:
path: "{{ lookup('ansible.builtin.env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/id_rsa"
run_once: true
register: _pubkey
become: false

- name: Query active KVM guests
ansible.builtin.command: virsh list --name
run_once: true
register: guest_query

- name: Manage KVM guests
when: inventory_hostname not in guest_query.stdout_lines
block:
- name: Create KVM guest disks
ansible.builtin.copy:
remote_src: true
src: "{{ _libvirt_path }}/{{ _platform.image | urlsplit('path') | basename }}"
dest: "{{ _disk }}"
owner: libvirt-qemu
group: kvm
mode: 0666
force: true
vars:
_platforms: "{{ molecule_yml.platforms }}"
_platform: "{{ _platforms | selectattr('name', 'equalto', inventory_hostname) | first }}"
_disk: "{{ _libvirt_path }}/{{ _platform.name }}.img"

- name: Resize KVM guest disks
ansible.builtin.command:
cmd: qemu-img resize {{ _disk }} {{ _platform.size | default('20G') }}
vars:
_platforms: "{{ molecule_yml.platforms }}"
_platform: "{{ _platforms | selectattr('name', 'equalto', inventory_hostname) | first }}"
_disk: "{{ _libvirt_path }}/{{ _platform.name }}.img"

- name: Create cloud-init tempdir
ansible.builtin.tempfile:
state: directory
prefix: libvirt
register: libvirt_tmpdir
run_once: true

- name: Create KVM guest
block:
- name: Create instance tempdir
ansible.builtin.file:
dest: "{{ libvirt_tmpdir.path }}/{{ inventory_hostname }}"
state: directory

- name: Create cloud-init meta data
ansible.builtin.template:
src: "meta-data.yml.j2"
dest: "{{ libvirt_tmpdir.path }}/{{ inventory_hostname }}/meta-data"

- name: Create cloud-init user data
ansible.builtin.template:
src: "user-data.yml.j2"
dest: "{{ libvirt_tmpdir.path }}/{{ inventory_hostname }}/user-data"
vars:
guest_user: "{{ _libvirt_user }}"
guest_key: "{{ _pubkey.public_key }}"

- name: Create cloud-init iso
ansible.builtin.command:
cmd: |
xorriso -as mkisofs
-volid cidata -joliet -rock
-o {{ _libvirt_path }}/{{ inventory_hostname }}.iso .
chdir: "{{ libvirt_tmpdir.path }}/{{ inventory_hostname }}"

- name: Create KVM guest
community.libvirt.virt:
xml: "{{ lookup('ansible.builtin.template', 'vm.j2.xml') }}"
command: define
vars:
_platforms: "{{ molecule_yml.platforms }}"
_platform: "{{ _platforms | selectattr('name', 'equalto', inventory_hostname) | first }}"
guest_hostname: "{{ inventory_hostname }}"
guest_disk_format: qcow2
guest_disk_path: "{{ _libvirt_path }}/{{ inventory_hostname }}.img"
guest_iso_path: "{{ _libvirt_path }}/{{ inventory_hostname }}.iso"
guest_network: "{{ _libvirt_network }}"
guest_mac: "{{ '52:54:00' | random_mac(seed=guest_hostname) }}"
guest_mem: "{{ (_platform.mem | default(2)) * 1048576 }}"
guest_cpu: "{{ _platform.cpu | default(2) }}"
loop_control:
label: "{{ inventory_hostname }} - {{ _libvirt_path }}/{{ inventory_hostname }}.img"

always:
- name: Destroy cloud-init tempdir
ansible.builtin.file:
dest: "{{ libvirt_tmpdir.path }}"
state: absent
run_once: true

- name: Start KVM guest
community.libvirt.virt:
name: "{{ inventory_hostname }}"
state: running


- name: Build instance config file
hosts: localhost
vars_files:
- vars.yml
tasks:
- name: Create SSH key
community.crypto.openssh_keypair:
path: "{{ lookup('ansible.builtin.env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/id_rsa"
run_once: true
register: _pubkey

- name: Retrieve platform ip information
become: true
block:
- name: Wait for DHCP lease assignment
ansible.builtin.shell:
cmd: "virsh net-dhcp-leases --network {{ _libvirt_network }} | grep {{ item }}"
executable: /bin/bash
loop: "{{ molecule_yml.platforms | map(attribute='name') }}"
register: dhcp_query
retries: 6
delay: 30
until: dhcp_query is succeeded

- name: Query DHCP leases
ansible.builtin.shell: >
virsh net-dhcp-leases --network {{ _libvirt_network }} | grep {{ item }} | awk '{print $5}'
loop: "{{ molecule_yml.platforms | map(attribute='name') }}"
register: dhcp_query

- name: Populate instance config dict
ansible.builtin.set_fact:
instance_conf_dict: {
'instance': "{{ item }}",
'address': "{{ _libvirt_address | ansible.utils.ipaddr('address') }}",
'user': "{{ _libvirt_user }}",
'port': "22",
'identity_file': "{{ _pubkey.filename }}"
}
vars:
_libvirt_addresses: "{{ dhcp_query.results | nephelaiio.plugins.list_to_dict('item') }}"
_libvirt_address: "{{ _libvirt_addresses[item].stdout }}"
loop: "{{ molecule_yml.platforms | map(attribute='name') }}"
register: instance_config_dict

- name: Convert instance config dict to a list
ansible.builtin.set_fact:
_instance_conf: "{{ _instance_results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
vars:
_instance_results: "{{ instance_config_dict.results }}"

- name: Dump instance config
ansible.builtin.copy:
content: "{{ _instance_conf | ansible.builtin.to_nice_yaml(indent=2) }}"
dest: "{{ molecule_instance_config }}"
mode: 0640

- name: Configure host file entries
ansible.builtin.lineinfile:
path: /etc/hosts
regexp: ".*{{ item.instance }}.*"
line: "{{ item.address }} {{ item.instance }}"
loop_control:
label: "{{ item.instance }}"
loop: "{{ _instance_conf }}"
become: true
- name: Import libvirt create playbook
ansible.builtin.import_playbook: nephelaiio.libvirt.create
58 changes: 2 additions & 56 deletions molecule/common/destroy.yml
Original file line number Diff line number Diff line change
@@ -1,57 +1,3 @@
---
- name: Destroy KVM guests
hosts: localhost
become: true
vars_files:
- vars.yml
tasks:
- name: Query active KVM guests
ansible.builtin.command: virsh list --name
register: guest_query

- name: Set KVM guest facts
ansible.builtin.set_fact:
_guests: "{{ molecule_yml.platforms | selectattr('name', 'in', guest_query.stdout_lines) }}"

- name: Stop KVM guests
ansible.builtin.command: virsh destroy {{ item }}
loop: "{{ _guests | map(attribute='name') }}"

- name: Query defined KVM guests
ansible.builtin.command: virsh list --name --all
register: guest_query

- name: Undefine KVM guests
ansible.builtin.command: virsh undefine {{ item }}
loop: "{{ _guests | map(attribute='name') }}"

- name: Delete guest disks
ansible.builtin.file:
dest: "{{ _path }}"
state: absent
vars:
_extensions: ['img', 'iso']
_path: "{{ _libvirt_path }}/{{ item.0 }}.{{ item.1 }}"
loop_control:
label: "{{ _path }}"
loop: "{{ molecule_yml.platforms | map(attribute='name') | product(_extensions) }}"

- name: Stop libvirt network
community.libvirt.virt_net:
command: destroy
name: "{{ _libvirt_network }}"
when: _libvirt_network != "default"
failed_when: false

- name: Delete libvirt network
community.libvirt.virt_net:
command: undefine
name: "{{ _libvirt_network }}"
when: _libvirt_network != "default"

- name: Delete host file entries
ansible.builtin.lineinfile:
path: /etc/hosts
regexp: ".*{{ item }}.*"
state: absent
loop: "{{ molecule_yml.platforms | map(attribute='name') }}"
- name: Import libvirt destroy playbook
ansible.builtin.import_playbook: nephelaiio.libvirt.destroy
2 changes: 0 additions & 2 deletions molecule/common/templates/meta-data.yml.j2

This file was deleted.

14 changes: 0 additions & 14 deletions molecule/common/templates/network.j2.xml

This file was deleted.

Loading