Update snap-upgrade.sh

This commit is contained in:
Ares 2024-03-13 03:04:34 +00:00
parent 32f30dbe09
commit 3963e1c763
1 changed files with 60 additions and 28 deletions

View File

@ -1,39 +1,71 @@
#!/bin/bash #!/bin/bash
SNAP_CHANNEL="1.21/stable" # Constants
ALL_SNAPS="kube-apiserver kube-scheduler kube-controller-manager kube-proxy kubectl kubelet cdk-addons" ALL_SNAPS="kube-apiserver kube-scheduler kube-controller-manager kube-proxy kubectl kubelet cdk-addons"
MASTER_SNAPS="kube-apiserver kube-scheduler kube-controller-manager kube-proxy kubectl cdk-addons" MASTER_SNAPS="kube-apiserver kube-scheduler kube-controller-manager kube-proxy kubectl cdk-addons"
WORKER_SNAPS="kube-proxy kubelet kubectl" WORKER_SNAPS="kube-proxy kubelet kubectl"
# Download Juju 2.9 # Prompt for channels
snap download --channel=2.9 juju read -p "Enter the SNAP_CHANNEL (e.g. 1.21/stable): " SNAP_CHANNEL
read -p "Enter the JUJU_CHANNEL (e.g. 2.9/stable): " JUJU_CHANNEL
# Download latest snaps from designated channel # Function to download snaps from a specific channel
for snap in $ALL_SNAPS download_snaps() {
do local snaps=$1
snap download --channel=$SNAP_CHANNEL $snap local channel=$2
done for snap in $snaps; do
echo "Downloading $snap from channel $channel..."
snap download --channel="$channel" "$snap" || exit 1
done
}
# Attach new snaps to master units # Function to attach snaps to units
for snap in $MASTER_SNAPS attach_snaps_to_units() {
do local snaps=$1
juju attach kubernetes-master $snap=`ls ${snap}_*.snap` local service=$2
done for snap in $snaps; do
local snap_file
snap_file=$(ls "${snap}"_*.snap | head -n 1)
if [[ -f "$snap_file" ]]; then
echo "Attaching $snap_file to $service..."
# juju attach "$service" "$snap"="$snap_file" || exit 1
else
echo "Error: $snap_file not found."
exit 1
fi
done
}
# Attach new snaps to worker units # Function to upgrade units of a service
for snap in $WORKER_SNAPS upgrade_service_units() {
do local service=$1
juju attach kubernetes-worker $snap=`ls ${snap}_*.snap` local unit_names
done unit_names=$(juju status --format json | jq -r ".applications|.[\"$service\"].units | keys[]")
for unit in $unit_names; do
echo "Upgrading $unit..."
# juju run-action "$unit" upgrade --wait || exit 1
done
}
# Upgrade to new snaps on masters, one at a time # Download Juju
for unit in `juju status --format json | jq -r '.applications|.["kubernetes-master"].units | keys[]'` echo "Downloading Juju from channel $JUJU_CHANNEL..."
do snap download --channel="$JUJU_CHANNEL" juju || exit 1
juju run-action $unit upgrade --wait
done
# Upgrade to new snaps on workers, one at a time # Download Kubernetes snaps
for unit in `juju status --format json | jq -r '.applications|.["kubernetes-worker"].units | keys[]'` download_snaps "$ALL_SNAPS" "$SNAP_CHANNEL"
do
juju run-action $unit upgrade --wait echo "Downloading Juju from channel $JUJU_CHANNEL..."
done snap download --channel="$JUJU_CHANNEL" juju || exit 1
# Download Kubernetes snaps
download_snaps "$ALL_SNAPS" "$SNAP_CHANNEL"
# Attach new snaps to Kubernetes master and worker units
attach_snaps_to_units "$MASTER_SNAPS" "kubernetes-master"
attach_snaps_to_units "$WORKER_SNAPS" "kubernetes-worker"
# Upgrade Kubernetes master and worker units
upgrade_service_units "kubernetes-master"
upgrade_service_units "kubernetes-worker"
echo "Upgrade process completed successfully."