File rke2-upgrade.sh of Package elemental-base-os
#!/bin/bash
# This script is just a PoC to upgrade RKE2 as an extension
# This is not robust enough and it is not production ready
set -e
declare output
declare tmpdir
declare old_rke2
declare new_rke2
declare url="${1}"
tmpdir=$(mktemp -d -t rke2-ext.XXXX)
echo "Downloading rke2 from ${url}"
pushd ${tmpdir} > /dev/null
new_rke2=$(curl -sSLO -w "%{filename_effective}" "${url}")
popd > /dev/null
echo "Stopping RKE2 service"
systemctl mask rke2-server.service
systemctl stop rke2-server.service
old_rke2=$(systemd-sysext list | grep rke2 | awk '{print $3}')
echo "Unmerge extensions"
systemd-sysext unmerge
echo "Add/remove extensions"
mv "${tmpdir}/${new_rke2}" /var/lib/extensions
mv "${old_rke2}" "${tmpdir}"
echo "Merge extensions and start rke2 again"
systemd-sysext merge
systemctl unmask rke2-server.service
# Waiting for the service to be active might be good for testing
# and demoing, but this logic does not necessarily belongs here
echo "Wait for rke2-server service to be active again"
# Wait for rke2-server to be active
# 10 min timeout
for i in {1..120}; do
sleep 5
if output=$(systemctl is-active rke2-server); then
echo "rke2-service is now active"
break
else
echo "rke2-server not active yet: ${output}"
fi
done
if [ "${output}" != "active" ]; then
>&2 echo "failed, rke2-service is not active"
exit 1
fi
rm -rf "${tmpdir}"
echo "RKE2 upgraded with ${new_rke2} extension"