-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupgrade_k3s_master.sh
64 lines (52 loc) · 2.02 KB
/
upgrade_k3s_master.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# upgrade-k3s-master.sh
# Script to upgrade K3s master node while preserving original installation arguments
# First create backups of the service file and the service environment file.
mkdir -p /root/backup
cp /etc/systemd/system/k3s.service /root/backup/
cp /etc/systemd/system/k3s.service.env /root/backup
# Logic to extract existing K3S args from the /etc/systemd/system/k3s.service file
function get_k3s_server_args() {
if [ -f "/etc/systemd/system/k3s.service" ]; then
# Extract all lines between ExecStart and the next empty line or section
# Only include lines that start with '--' and handle quotes properly
INSTALL_ARGS=$(awk '/ExecStart=\/usr\/local\/bin\/k3s/,/^$/ {print}' /etc/systemd/system/k3s.service | \
grep "^[[:space:]]*'--\|^[[:space:]]*--" | \
sed 's/^[[:space:]]*//; s/[[:space:]]*$//' | \
sed "s/'//g" | \
tr -d '\\' | \
tr '\n' ' ' | \
sed 's/[[:space:]]*$//')
echo "$INSTALL_ARGS"
else
echo "Error: K3s server service file not found"
exit 1
fi
}
# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
# Check if this is actually a master node
if [ ! -f "/etc/systemd/system/k3s.service" ]; then
echo "Error: This doesn't appear to be a K3s master node"
exit 1
fi
# Get installation arguments
INSTALL_ARGS=$(get_k3s_server_args)
echo "Detected installation arguments: $INSTALL_ARGS"
echo "Detected environment variables: $ENV_VARS"
# Construct and execute upgrade command, forcing IPv4
UPGRADE_CMD="curl -4 -sfL https://get.k3s.io | INSTALL_K3S_EXEC=\"server $INSTALL_ARGS\" sh -"
echo "Executing upgrade command:"
echo "$UPGRADE_CMD"
echo "Continue with upgrade? (y/n)"
read -r confirm
if [ "$confirm" = "y" ]; then
eval "$UPGRADE_CMD"
echo "Upgrade completed. Please check system logs for any errors."
else
echo "Upgrade cancelled."
exit 0
fi