Skip to content

Commit 53653ce

Browse files
committed
WIP: emulation scripts
1 parent ce85e85 commit 53653ce

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# Stop the running VM
4+
echo "Stopping the VM..."
5+
kill -9 $(pgrep -f qemu-system-x86_64)
6+
7+
# Clean up generated files; TODO parameterize this
8+
echo "Deleting generated files..."
9+
rm -f user-data meta-data cloud-init.iso ubuntu-node.qcow2
10+
11+
# remove kube node from the cluster
12+
echo "Removing new node from the cluster..."
13+
kubectl delete node kube-node
14+
echo "Cleanup complete."
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/bash
2+
3+
# Parameters
4+
K3S_SERVER_URL=${1:-"https://172.18.107.219:6443"} # Default K3S server URL
5+
K3S_SERVER_URL=${1:-"https://$(hostname -I | awk '{print $1}'):6443"} # Default K3S server URL
6+
K3S_TOKEN=${2:-$(sudo cat /var/lib/rancher/k3s/server/node-token)} # Retrieve K3S token
7+
NODE_NAME=${3:-"kube-node"} # Default node name
8+
OS_IMAGE_URL="https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img" # Ubuntu OS image
9+
QCOW2_IMAGE_FILE="ubuntu-cloud.qcow2" # QCOW2 image file name
10+
CLOUD_INIT_ISO="cloud-init.iso" # Cloud-init ISO name
11+
MEMORY=${4:-4096} # Default memory size (in MB)
12+
CPUS=${5:-2} # Default number of CPUs
13+
TIMEOUT=${6:-600} # Timeout for waiting for the node to be ready (in seconds)
14+
15+
# Function: Log a message with timestamp and level
16+
function log_message {
17+
local level="$1"
18+
local message="$2"
19+
echo -e "$(date +'%Y-%m-%d %H:%M:%S') [${level}] ${message}"
20+
}
21+
22+
# Function: Check command availability
23+
function check_command {
24+
if ! command -v "$1" &>/dev/null; then
25+
log_message "ERROR" "$1 is not installed or not in PATH."
26+
exit 1
27+
fi
28+
}
29+
30+
# Ensure required commands are available
31+
log_message "INFO" "Checking required commands..."
32+
check_command wget
33+
check_command qemu-img
34+
check_command qemu-system-x86_64
35+
check_command kubectl
36+
check_command cloud-localds
37+
38+
# Step 1: Create user-data file for cloud-init
39+
log_message "INFO" "Creating user-data file for cloud-init..."
40+
# cat <<EOF > user-data
41+
# #cloud-config
42+
# runcmd:
43+
# - export K3S_URL=$K3S_SERVER_URL
44+
# - export K3S_TOKEN=$K3S_TOKEN
45+
# - curl -sfL https://get.k3s.io | sh -
46+
# EOF
47+
cat <<EOF > user-data
48+
#cloud-config
49+
runcmd:
50+
- curl -sfL https://get.k3s.io | K3S_URL=$K3S_SERVER_URL K3S_TOKEN=$K3S_TOKEN sh -
51+
EOF
52+
53+
# Step 2: Create meta-data file for cloud-init
54+
log_message "INFO" "Creating meta-data file for cloud-init..."
55+
cat <<EOF > meta-data
56+
# meta-data
57+
instance-id: $NODE_NAME
58+
local-hostname: $NODE_NAME
59+
EOF
60+
61+
# Step 3: Create cloud-init ISO
62+
log_message "INFO" "Creating cloud-init ISO..."
63+
cloud-localds $CLOUD_INIT_ISO user-data meta-data
64+
65+
# Step 4: Download and prepare the OS image
66+
if [ ! -f "$QCOW2_IMAGE_FILE" ]; then
67+
log_message "INFO" "Downloading and resizing the OS image..."
68+
wget $OS_IMAGE_URL -O $QCOW2_IMAGE_FILE && qemu-img resize $QCOW2_IMAGE_FILE 10G
69+
else
70+
log_message "INFO" "OS image already exists. Skipping download."
71+
fi
72+
73+
# Step 5: Start the VM in the background
74+
log_message "INFO" "Starting the VM with name: $NODE_NAME..."
75+
qemu-system-x86_64 -m $MEMORY -smp cpus=$CPUS \
76+
-drive file=$QCOW2_IMAGE_FILE,if=virtio \
77+
-drive file=$CLOUD_INIT_ISO,format=raw,if=virtio \
78+
-netdev user,id=mynet0 \
79+
-device e1000,netdev=mynet0 \
80+
-nographic -serial none -monitor none &
81+
82+
# Step 6: Wait for the VM to initialize
83+
log_message "INFO" "Waiting for Kubernetes node $NODE_NAME to be ready..."
84+
sleep 60
85+
86+
# Step 7: Wait for the Kubernetes node to be ready
87+
START_TIME=$(date +%s)
88+
while true; do
89+
if kubectl get nodes "$NODE_NAME" 2>/dev/null | grep -q " Ready "; then
90+
log_message "SUCCESS" "Node $NODE_NAME is now ready!"
91+
break
92+
fi
93+
94+
# Check timeout
95+
CURRENT_TIME=$(date +%s)
96+
if (( CURRENT_TIME - START_TIME > TIMEOUT )); then
97+
log_message "ERROR" "Timed out waiting for node $NODE_NAME to be ready. Exiting."
98+
exit 1
99+
fi
100+
101+
sleep 5
102+
done
103+
104+
# Step 8: Assign worker role to the new node
105+
log_message "INFO" "Assigning worker role to the new node..."
106+
kubectl label node $NODE_NAME node-role.kubernetes.io/worker=worker
107+
108+
log_message "SUCCESS" "New node $NODE_NAME has been successfully added to the cluster!"
109+
log_message "INFO" "Use the cleanup script to remove the node when it is no longer needed."

0 commit comments

Comments
 (0)