Skip to content

Commit

Permalink
Start of QEMU K3s based CI
Browse files Browse the repository at this point in the history
  • Loading branch information
richiejp committed Apr 5, 2024
1 parent c03f655 commit 786cd4d
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
152 changes: 152 additions & 0 deletions script/vms/k3s.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/bin/sh -eu

set_vars() {
echo "QEMU K3s Ports(SSH=${K3S_SSH_PORT:=2222} API=${K3S_API_PORT:=16443} web=${K3S_WEB_PORT:=8080})"
echo "Kubeconfig: ${KUBECONFIG:-kubeconfig}"
echo "Image: ${IMG:-container.tar}"

rund=${XDG_RUNTIME_DIR-/tmp/$USER}/prem-operator
pid_file="${rund}/qemu.pid"
here=$(dirname "$0")

echo "Runtime dir: $rund"
}

# Function for 'boot-qemu-daemon' subcommand
boot_qemu_daemon() {
echo "Booting QEMU as a daemon..."

mkdir -p $rund

if [ -f "$pid_file" ] && ps -p $(cat "$pid_file") > /dev/null 2>&1; then
echo "QEMU is already running. Exiting."
exit 1
fi

rm -f temp.img
qemu-img create -F qcow2 -f qcow2 -b k3s-base.img temp.img

qemu-system-x86_64 \
-machine accel=kvm,type=q35 \
-cpu host \
-m 8G \
-device virtio-net-pci,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::${K3S_SSH_PORT}-:22,hostfwd=tcp::${K3S_API_PORT}-:6443,hostfwd=tcp::${K3S_WEB_PORT}-:80 \
-drive if=virtio,format=qcow2,file=temp.img,cache=none \
-drive if=virtio,format=raw,file=seed.img \
-serial file:serial.log \
-pidfile "$pid_file" \
-display none \
-daemonize

echo "Started QEMU waiting for SSH"
ssh_opts="-o BatchMode=yes -o ConnectTimeout=10 -o ConnectionAttempts=120"

if ! ssh ${ssh_opts} -p "${K3S_SSH_PORT}" [email protected] true; then
echo "SSH connection failed."
echo "Serial log output:"
tail -n 100 serial.log
echo "Killing QEMU..."
kill $(cat "$pid_file")
rm -f "$pid_file"
exit 1
fi

echo "SSH connection established."
}

# Function for 'boot-qemu' subcommand
boot_qemu() {
echo "Booting QEMU..."
# Your boot-qemu code here
}

stop_qemu() {
if [ -f "$pid_file" ]; then
pid=$(cat "$pid_file")

if ps -p $pid > /dev/null 2>&1; then
# Check if the process is QEMU
if ps -p $pid -o comm= | grep -q "qemu-system"; then
echo "QEMU pid $pid found. Killing it..."

kill $pid
else
echo "PID file found, but the process is not QEMU. Skipping termination."
fi
else
echo "PID file found, but the process is not running. Removing the PID file..."
fi

rm -f "$pid_file"
else
echo "No QEMU PID file found. QEMU is not running."
fi
}

# Function for 'install-k3s' subcommand
install_k3s() {
echo "Installing k3s..."

k3sup install --ip ${K3S_IP-0.0.0.0} --ssh-port ${K3S_SSH_PORT-22} --user ${K3S_USER-ubuntu}
}

# Function for 'setup-image' subcommand
setup_image() {
echo "Setting up image..."
# Your setup-image code here
}

# Default function when no valid subcommand is provided
show_help() {
echo "Usage: $0 <command> [options]"
echo ""
echo "Commands:"
echo " boot-qemu-daemon Boot QEMU as a daemon"
echo " boot-qemu Boot QEMU"
echo " install-k3s Install k3s"
echo " setup-image Setup image"
echo " help Show this help message"
}

# Check if at least one argument is provided
if [ $# -lt 1 ]; then
show_help
exit 1
fi

original_pwd=$(pwd)
cd $(dirname $0)

cleanup() {
cd "$original_pwd"
}
trap cleanup EXIT

set_vars

case "$1" in
boot-qemu-daemon)
boot_qemu_daemon "${@:2}"
;;
boot-qemu)
boot_qemu "${@:2}"
;;
stop-qemu)
stop_qemu "${@:2}"
;;
install-k3s)
install_k3s "${@:2}"
;;
setup-image)
setup_image "${@:2}"
;;
help)
show_help
;;
*)
echo "Error: Unknown command '$1'"
show_help
exit 1
;;
esac
3 changes: 3 additions & 0 deletions script/vms/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
instance-id: pou3
local-hostname: pou3

18 changes: 18 additions & 0 deletions script/vms/privileged-pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Pod
metadata:
name: privileged-pod
spec:
containers:
- name: privileged-container
image: alpine
command: ["/bin/sh", "-c", "sleep infinity"]
securityContext:
privileged: true
volumeMounts:
- name: host-path
mountPath: /host
volumes:
- name: host-path
hostPath:
path: /
9 changes: 9 additions & 0 deletions script/vms/user-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#cloud-config
# TODO: replace with generated key
ssh_import_id:
- gh:richiejp

growpart:
mode: auto
devices: ["/"]

0 comments on commit 786cd4d

Please sign in to comment.