65 lines
1.5 KiB
Bash
65 lines
1.5 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
host_root="/host"
|
||
|
|
env_file="${host_root}/etc/systemd/system/k3s.service.env"
|
||
|
|
manifest_dir="${host_root}/var/lib/rancher/k3s/server/manifests"
|
||
|
|
|
||
|
|
changed=0
|
||
|
|
|
||
|
|
ensure_disable_flag() {
|
||
|
|
mkdir -p "$(dirname "${env_file}")"
|
||
|
|
if [ ! -f "${env_file}" ]; then
|
||
|
|
printf 'K3S_DISABLE=traefik\n' > "${env_file}"
|
||
|
|
changed=1
|
||
|
|
return
|
||
|
|
fi
|
||
|
|
|
||
|
|
if grep -q '^K3S_DISABLE=' "${env_file}"; then
|
||
|
|
current="$(grep '^K3S_DISABLE=' "${env_file}" | tail -n1 | cut -d= -f2-)"
|
||
|
|
current="$(printf '%s' "${current}" | sed 's/^\"//;s/\"$//' | tr -d ' ')"
|
||
|
|
if ! printf '%s' "${current}" | grep -qw "traefik"; then
|
||
|
|
if [ -z "${current}" ]; then
|
||
|
|
updated="traefik"
|
||
|
|
else
|
||
|
|
updated="${current},traefik"
|
||
|
|
fi
|
||
|
|
sed -i "s/^K3S_DISABLE=.*/K3S_DISABLE=${updated}/" "${env_file}"
|
||
|
|
changed=1
|
||
|
|
fi
|
||
|
|
else
|
||
|
|
printf '\nK3S_DISABLE=traefik\n' >> "${env_file}"
|
||
|
|
changed=1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
remove_manifest() {
|
||
|
|
if [ -d "${manifest_dir}" ] && ls "${manifest_dir}"/traefik* >/dev/null 2>&1; then
|
||
|
|
rm -f "${manifest_dir}"/traefik*.yaml "${manifest_dir}"/traefik*.yml
|
||
|
|
changed=1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
restart_k3s() {
|
||
|
|
node_name="$(cat "${host_root}/etc/hostname" 2>/dev/null || hostname)"
|
||
|
|
delay=0
|
||
|
|
case "${node_name}" in
|
||
|
|
*0b) delay=60 ;;
|
||
|
|
*0c) delay=120 ;;
|
||
|
|
esac
|
||
|
|
if [ "${delay}" -gt 0 ]; then
|
||
|
|
sleep "${delay}"
|
||
|
|
fi
|
||
|
|
chroot "${host_root}" /bin/systemctl daemon-reload || true
|
||
|
|
chroot "${host_root}" /bin/systemctl restart k3s
|
||
|
|
}
|
||
|
|
|
||
|
|
ensure_disable_flag
|
||
|
|
remove_manifest
|
||
|
|
|
||
|
|
if [ "${changed}" -eq 1 ]; then
|
||
|
|
restart_k3s
|
||
|
|
fi
|
||
|
|
|
||
|
|
sleep infinity
|