Skip to content

Commit

Permalink
Add static partitioning test (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyphreak authored Oct 8, 2023
1 parent 505d122 commit f65d405
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 280 deletions.
10 changes: 7 additions & 3 deletions .ansible-lint-ignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# This file contains ignores rule violations for ansible-lint
meta/main.yml schema[meta]
molecule/common/cleanup.yml no-changed-when
molecule/common/converge.yml ignore-errors
molecule/common/converge.yml no-changed-when
molecule/common/converge.yml run-once[task]
molecule/common/create.yml risky-file-permissions
molecule/common/kvm.yml no-changed-when
molecule/common/prepare.yml ignore-errors
molecule/common/prepare.yml no-changed-when
molecule/common/prepare.yml run-once[task]
molecule/common/verify.yml no-changed-when
molecule/common/verify.yml risky-file-permissions
molecule/common/verify.yml run-once[task]
tasks/lvm.yml jinja[invalid]
tasks/lvm.yml no-changed-when
tasks/lvm.yml risky-file-permissions
tasks/metadata/storage.yml jinja[invalid]
tasks/static.yml no-changed-when
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GITHUB_REPO = $$(echo ${GITHUB_REPOSITORY} | cut -d/ -f 2)
all: install version lint test

test: lint
poetry run molecule test -s ${MOLECULE_SCENARIO}
poetry run molecule $@ -s ${MOLECULE_SCENARIO}

install:
@type poetry >/dev/null || pip3 install poetry
Expand All @@ -23,7 +23,10 @@ lint: install
poetry run molecule syntax

dependency create prepare converge idempotence side-effect verify destroy login reset:
MOLECULE_DOCKER_IMAGE=${MOLECULE_DOCKER_IMAGE} poetry run molecule $@ -s ${MOLECULE_SCENARIO}
poetry run molecule $@ -s ${MOLECULE_SCENARIO}

ignore:
poetry run ansible-lint --generate-ignore

clean: destroy reset
poetry env remove $$(which python)
Expand Down
16 changes: 12 additions & 4 deletions molecule/common/cleanup.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
---
- name: Destroy KVM guests

hosts: localhost

become: true

tasks:

- name: Destroy KVM guest
Expand Down Expand Up @@ -32,7 +29,18 @@
path: "{{ guest_disk }}"
state: absent
vars:
guest_disk: "{{ playbook_dir }}/{{ guest.installer_hostname }}.img"
guest_disk: "{{ cache_dir }}/{{ guest.installer_hostname }}.img"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guests }}"

- name: Destroy KVM extra disks
ansible.builtin.file:
path: "{{ guest_disk }}"
state: absent
vars:
guest_disk: "{{ cache_dir }}/{{ guest.installer_hostname }}.add.img"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
Expand Down
130 changes: 0 additions & 130 deletions molecule/common/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,137 +4,7 @@


- name: Reconfigure guest disks

hosts: guests

gather_facts: false

become: true

roles:
- nephelaiio.growfs

pre_tasks:

- name: Query KVM guest info
community.libvirt.virt:
command: info
register: libvirt_info
delegate_to: localhost
run_once: true

- name: Shut down guest
when:
- inventory_hostname in libvirt_info
- libvirt_info[inventory_hostname].state == "running"
block:

- name: Wait for connection
ansible.builtin.wait_for_connection:
timeout: 1200

- name: Shut down guest
community.general.shutdown:

- name: Wait for shutdown to complete
ansible.builtin.pause:
seconds: 30

- name: Force kvm guest poweroff
ansible.builtin.command: virsh destroy {{ inventory_hostname }}
ignore_errors: true
failed_when: false
changed_when: false
delegate_to: localhost

- name: Wait for poweroff to complete
ansible.builtin.pause:
seconds: 30

- name: Reconfigure guest disks
delegate_to: localhost
block:

- name: Stat kvm base disk
ansible.builtin.stat:
path: "{{ cache_dir }}/{{ inventory_hostname }}.img"
register: guest_disk_query

- name: Debug base disk info
ansible.builtin.debug:
msg: "{{ guest_disk_query.stat }}"

- name: Resize kvm base disks
ansible.builtin.command: "qemu-img resize -f raw {{ guest_disk }} {{ guest_disk_size }}"
vars:
guest_disk: "{{ cache_dir }}/{{ inventory_hostname }}.img"
guest_disk_size: "{{ 2 * (disk_size | int) }}"
when: (guest_disk_size | int) > (guest_disk_query.stat.size | int)

- name: Stat additional kvm disk
ansible.builtin.stat:
path: "{{ cache_dir }}/{{ inventory_hostname }}.add.img"
register: guest_addisk_query

- name: Create additional kvm disk for lvm instances
ansible.builtin.command: "qemu-img create -f raw {{ guest_disk }} {{ guest_disk_size }}"
args:
creates: "{{ guest_disk }}"
vars:
guest_disk: "{{ cache_dir }}/{{ inventory_hostname }}.add.img"
guest_disk_size: "{{ 2 * (disk_size | int) }}"
when:
- not guest_addisk_query.stat.exists
- partitioning_method == 'lvm'
notify: attach kvm guest disk

- name: Manage permissions for extra kvm guest disk
ansible.builtin.file:
path: "{{ guest_disk }}"
owner: libvirt-qemu
group: kvm
mode: 0660
vars:
guest_disk: "{{ cache_dir }}/{{ inventory_hostname }}.add.img"
when: partitioning_method == 'lvm'

- name: Flush handlers
ansible.builtin.meta: flush_handlers

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

- name: Wait for connection
ansible.builtin.wait_for_connection:
timeout: 120

- name: Gather facts
ansible.builtin.setup:
tags: always

handlers:

- name: Create definition for additional kvm guest disk
ansible.builtin.template:
src: disk.xml.j2
dest: "{{ guest_disk_xml }}"
owner: libvirt-qemu
group: kvm
mode: 0660
vars:
guest_hostname: "{{ inventory_hostname }}"
guest_disk_extra_path: "{{ cache_dir }}/{{ inventory_hostname }}.add.img"
guest_disk_format: raw
guest_disk_xml: "{{ cache_dir }}/{{ guest_hostname }}.disk.xml"
delegate_to: localhost
listen: attach kvm guest disk

- name: Attach additional kvm guest disk
ansible.builtin.command: "virsh attach-device {{ guest_hostname }} --file {{ guest_disk_xml }} --config"
vars:
guest_hostname: "{{ inventory_hostname }}"
guest_disk_xml: "{{ cache_dir }}/{{ guest_hostname }}.disk.xml"
delegate_to: localhost
listen: attach kvm guest disk
7 changes: 4 additions & 3 deletions molecule/common/create.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
---
- name: Spawn KVM guests
- name: Configure KVM virtualization
ansible.builtin.import_playbook: kvm.yml

hosts: localhost

- name: Spawn KVM guests
hosts: localhost
become: true

pre_tasks:

- name: Install disk utils
Expand Down
3 changes: 0 additions & 3 deletions molecule/common/inventory.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
---
- name: Add guests to inventory

hosts: localhost

become: true

tasks:

- name: Add guest to inventory
Expand Down
80 changes: 80 additions & 0 deletions molecule/common/kvm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
- name: Prepare host networking
hosts: localhost
become: true
tasks:

- name: Update apt package cache
ansible.builtin.apt:
update_cache: true
when: ansible_os_family == 'Debian'

- name: Create molecule cache dir
ansible.builtin.file:
path: "{{ cache_dir }}"
state: directory
mode: 0777
become: false

- name: Create KVM keypair
ansible.builtin.command:
cmd: "ssh-keygen -t rsa -f {{ kvm_keypair }} -q -P '' -C ''"
creates: "{{ kvm_keypair }}"
become: false

- name: Create dummy bridge
ansible.builtin.command: "ip link add {{ bridge_name }} type bridge"
failed_when: false

- name: Create dummy interface
ansible.builtin.command: "ip link add {{ bridge_iface }} type dummy"
failed_when: false

- name: Create dummy bridge slave
ansible.builtin.command: "ip link set {{ bridge_iface }} master {{ bridge_name }}"
failed_when: false

- name: Set bridge interface ip address
ansible.builtin.command: "ip addr add {{ bridge_host }}/{{ bridge_prefix }} dev {{ bridge_name }}"
failed_when: false

- name: Start bridge interface
ansible.builtin.command: "ip link set dev {{ bridge_name }} up"
failed_when: false

- name: Masquerade bridge
ansible.builtin.iptables:
table: nat
chain: POSTROUTING
out_interface: "{{ ansible_default_ipv4.interface }}"
source: "{{ bridge_net }}"
destination: 0.0.0.0/0
jump: MASQUERADE
protocol: all
state: present

- name: Enable ipv4 forwarding
ansible.posix.sysctl:
name: net.ipv4.ip_forward
value: '1'
state: present
reload: True

- name: Install Debian packages
when: ansible_os_family == 'Debian'
block:

- name: Install libvirt packages
ansible.builtin.apt:
name:
- libvirt-clients
- libvirt-daemon-system
- libvirt-daemon-system-systemd
- python3-libvirt

- name: Install qemu packages
ansible.builtin.apt:
name:
- qemu-efi
- qemu-system-x86
- qemu-utils
Loading

0 comments on commit f65d405

Please sign in to comment.