From eabc1154b1e2fd132e6529a23859de82461d744c Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Fri, 19 Apr 2019 17:05:43 -0700 Subject: [PATCH 1/5] Add OpenRC support to installer --- install.sh | 145 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 115 insertions(+), 30 deletions(-) diff --git a/install.sh b/install.sh index f023ef59ef..7f5a1cecde 100755 --- a/install.sh +++ b/install.sh @@ -68,11 +68,17 @@ fatal() exit 1 } -# --- fatal if no systemd --- -verify_systemd() { - if [ ! -d /run/systemd ]; then - fatal "Can not find systemd to use as a process supervisor for k3s" +# --- fatal if no systemd or openrc --- +verify_system() { + if [ -x /sbin/openrc-run ]; then + HAS_OPENRC=true + return fi + if [ -d /run/systemd ]; then + HAS_SYSTEMD=true + return + fi + fatal "Can not find systemd or openrc to use as a process supervisor for k3s" } # --- define needed environment variables --- @@ -102,16 +108,16 @@ setup_env() { # --- use systemd name if defined or create default --- if [ -n "${INSTALL_K3S_NAME}" ]; then - SYSTEMD_NAME=k3s-${INSTALL_K3S_NAME} + SYSTEM_NAME=k3s-${INSTALL_K3S_NAME} else if [ "${CMD_K3S}" = "server" ]; then - SYSTEMD_NAME=k3s + SYSTEM_NAME=k3s else - SYSTEMD_NAME=k3s-${CMD_K3S} + SYSTEM_NAME=k3s-${CMD_K3S} fi fi - SERVICE_K3S=${SYSTEMD_NAME}.service - UNINSTALL_K3S_SH=${SYSTEMD_NAME}-uninstall.sh + SERVICE_K3S=${SYSTEM_NAME}.service + UNINSTALL_K3S_SH=${SYSTEM_NAME}-uninstall.sh # --- use systemd type if defined or create default --- if [ -n "${INSTALL_K3S_TYPE}" ]; then @@ -138,6 +144,16 @@ setup_env() { SYSTEMD_DIR="/etc/systemd/system" fi + # --- use servive or environment location depending on systemd/openrc --- + if [ "${HAS_SYSTEMD}" = "true" ]; then + FILE_K3S_SERVICE=${SYSTEMD_DIR}/${SERVICE_K3S} + FILE_K3S_ENV=${SYSTEMD_DIR}/${SERVICE_K3S}.env + elif [ "${HAS_OPENRC}" = "true" ]; then + $SUDO mkdir -p /etc/rancher/k3s + FILE_K3S_SERVICE=/etc/init.d/${SYSTEM_NAME} + FILE_K3S_ENV=/etc/rancher/k3s/${SYSTEM_NAME}.env + fi + # --- use sudo if we are not already root --- SUDO=sudo if [ `id -u` = 0 ]; then @@ -317,19 +333,21 @@ create_uninstall() { $SUDO tee ${BIN_DIR}/${UNINSTALL_K3S_SH} >/dev/null << EOF #!/bin/sh set -x -systemctl kill ${SYSTEMD_NAME} -systemctl disable ${SYSTEMD_NAME} -systemctl reset-failed ${SYSTEMD_NAME} -systemctl daemon-reload -rm -f ${SYSTEMD_DIR}/${SERVICE_K3S} -rm -f ${SYSTEMD_DIR}/${SERVICE_K3S}.env +if which systemctl; then + systemctl kill ${SYSTEM_NAME} + systemctl disable ${SYSTEM_NAME} + systemctl reset-failed ${SYSTEM_NAME} + systemctl daemon-reload +fi +rm -f ${FILE_K3S_SERVICE} +rm -f ${FILE_K3S_ENV} remove_uninstall() { rm -f ${BIN_DIR}/${UNINSTALL_K3S_SH} } trap remove_uninstall EXIT -if ls ${SYSTEMD_DIR}/k3s*.service >/dev/null 2>&1; then +if (ls ${SYSTEMD_DIR}/k3s*.service || ls /etc/init.d/k3s*) >/dev/null 2>&1; then set +x; echo "Additional k3s services installed, skipping uninstall of k3s"; set -x exit fi @@ -369,22 +387,22 @@ EOF systemd_disable() { $SUDO rm -f /etc/systemd/system/${SERVICE_K3S} || true $SUDO rm -f /etc/systemd/system/${SERVICE_K3S}.env || true - $SUDO systemctl disable ${SYSTEMD_NAME} >/dev/null 2>&1 || true + $SUDO systemctl disable ${SYSTEM_NAME} >/dev/null 2>&1 || true } # --- capture current env and create file containing k3s_ variables --- create_env_file() { - info "systemd: Creating environment file ${SYSTEMD_DIR}/${SERVICE_K3S}.env" + info "env: Creating environment file ${FILE_K3S_ENV}" UMASK=`umask` umask 0377 - env | grep '^K3S_' | $SUDO tee ${SYSTEMD_DIR}/${SERVICE_K3S}.env >/dev/null + env | grep '^K3S_' | $SUDO tee ${FILE_K3S_ENV} >/dev/null umask $UMASK } -# --- write service file --- -create_service_file() { - info "systemd: Creating service file ${SYSTEMD_DIR}/${SERVICE_K3S}" - $SUDO tee ${SYSTEMD_DIR}/${SERVICE_K3S} >/dev/null << EOF +# --- write systemd service file --- +create_systemd_service_file() { + info "systemd: Creating service file ${FILE_K3S_SERVICE}" + $SUDO tee ${FILE_K3S_SERVICE} >/dev/null << EOF [Unit] Description=Lightweight Kubernetes Documentation=https://k3s.io @@ -392,7 +410,7 @@ After=network.target [Service] Type=${SYSTEMD_TYPE} -EnvironmentFile=${SYSTEMD_DIR}/${SERVICE_K3S}.env +EnvironmentFile=${FILE_K3S_ENV} ExecStartPre=-/sbin/modprobe br_netfilter ExecStartPre=-/sbin/modprobe overlay ExecStart=${BIN_DIR}/k3s ${CMD_K3S_EXEC} @@ -409,19 +427,86 @@ WantedBy=multi-user.target EOF } +# --- write openrc service file --- +create_openrc_service_file() { + LOG_FILE=/var/log/${SYSTEM_NAME}.log + + info "openrc: Creating service file ${FILE_K3S_SERVICE}" + $SUDO tee ${FILE_K3S_SERVICE} >/dev/null << EOF +#!/sbin/openrc-run + +depend() { + after net-online + need net +} + +start_pre() { + rm -f /tmp/k3s.* +} + +supervisor=supervise-daemon +name="${SYSTEM_NAME}" +command="/usr/local/bin/k3s" +command_args="${CMD_K3S_EXEC} >>${LOG_FILE} 2>&1" +pidfile="/var/run/${SYSTEM_NAME}.pid" +respawn_delay=5 + +set -o allexport +if [ -f /etc/environment ]; then source /etc/environment; fi +if [ -f ${FILE_K3S_ENV} ]; then source ${FILE_K3S_ENV}; fi +set +o allexport +EOF + $SUDO chmod 0755 ${FILE_K3S_SERVICE} + + $SUDO tee /etc/logrotate.d/${SYSTEM_NAME} >/dev/null << EOF +${LOG_FILE} { + missingok + notifempty + copytruncate +} +EOF +} + +# --- write systemd or openrc service file --- +create_service_file() { + if [ "${HAS_SYSTEMD}" = "true" ]; then + create_systemd_service_file + elif [ "${HAS_OPENRC}" = "true" ]; then + create_openrc_service_file + fi +} + # --- enable and start systemd service --- systemd_enable_and_start() { - info "systemd: Enabling ${SYSTEMD_NAME} unit" - $SUDO systemctl enable ${SYSTEMD_DIR}/${SERVICE_K3S} >/dev/null + info "systemd: Enabling ${SYSTEM_NAME} unit" + $SUDO systemctl enable ${FILE_K3S_SERVICE} >/dev/null $SUDO systemctl daemon-reload >/dev/null - info "systemd: Starting ${SYSTEMD_NAME}" - $SUDO systemctl restart ${SYSTEMD_NAME} + info "systemd: Starting ${SYSTEM_NAME}" + $SUDO systemctl restart ${SYSTEM_NAME} +} + +# --- enable and start openrc service --- +openrc_enable_and_start() { + info "openrc: Enabling ${SYSTEM_NAME} service for default runlevel" + $SUDO rc-update add ${SYSTEM_NAME} default >/dev/null + + info "openrc: Starting ${SYSTEM_NAME}" + $SUDO ${FILE_K3S_SERVICE} restart +} + +# --- startup systemd or openrc service --- +service_enable_and_start() { + if [ "${HAS_SYSTEMD}" = "true" ]; then + systemd_enable_and_start + elif [ "${HAS_OPENRC}" = "true" ]; then + openrc_enable_and_start + fi } # --- run the install process -- { - verify_systemd + verify_system setup_env ${INSTALL_K3S_EXEC} $@ download_and_verify create_symlinks @@ -429,5 +514,5 @@ systemd_enable_and_start() { systemd_disable create_env_file create_service_file - systemd_enable_and_start + service_enable_and_start } From 100e2f6c2f5b61b897951360fa5717ba4a54790c Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Tue, 23 Apr 2019 13:24:02 -0700 Subject: [PATCH 2/5] Provide INSTALL_K3S_SKIP_START environment variable --- install.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/install.sh b/install.sh index 7f5a1cecde..0f443d9ef5 100755 --- a/install.sh +++ b/install.sh @@ -22,6 +22,9 @@ set -e # - INSTALL_K3S_SKIP_DOWNLOAD # If set to true will not download k3s hash or binary. # +# - INSTALL_K3S_SKIP_START +# If set to true will not start k3s service. +# # - INSTALL_K3S_VERSION # Version of k3s to download from github. Will attempt to download the # latest version if not specified. @@ -482,6 +485,7 @@ systemd_enable_and_start() { $SUDO systemctl enable ${FILE_K3S_SERVICE} >/dev/null $SUDO systemctl daemon-reload >/dev/null + [ "${INSTALL_K3S_SKIP_START}" = "true" ] && return info "systemd: Starting ${SYSTEM_NAME}" $SUDO systemctl restart ${SYSTEM_NAME} } @@ -491,6 +495,7 @@ openrc_enable_and_start() { info "openrc: Enabling ${SYSTEM_NAME} service for default runlevel" $SUDO rc-update add ${SYSTEM_NAME} default >/dev/null + [ "${INSTALL_K3S_SKIP_START}" = "true" ] && return info "openrc: Starting ${SYSTEM_NAME}" $SUDO ${FILE_K3S_SERVICE} restart } From 6a80350ddae40f381e3809304054723969fd8056 Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Wed, 24 Apr 2019 11:45:05 -0700 Subject: [PATCH 3/5] OpenRC install to BIN_DIR --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 0f443d9ef5..505dea27ae 100755 --- a/install.sh +++ b/install.sh @@ -449,7 +449,7 @@ start_pre() { supervisor=supervise-daemon name="${SYSTEM_NAME}" -command="/usr/local/bin/k3s" +command="${BIN_DIR}/k3s" command_args="${CMD_K3S_EXEC} >>${LOG_FILE} 2>&1" pidfile="/var/run/${SYSTEM_NAME}.pid" respawn_delay=5 From 3543ba4b56a1cab98afc63250dc37afed7149bf0 Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Thu, 25 Apr 2019 10:06:22 -0700 Subject: [PATCH 4/5] Only start service if something changed --- install.sh | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/install.sh b/install.sh index 505dea27ae..c027150ee9 100755 --- a/install.sh +++ b/install.sh @@ -162,6 +162,8 @@ setup_env() { if [ `id -u` = 0 ]; then SUDO= fi + + PRE_INSTALL_HASHES=`get_installed_hashes` } # --- check if skip download environment variable set --- @@ -472,41 +474,53 @@ EOF # --- write systemd or openrc service file --- create_service_file() { - if [ "${HAS_SYSTEMD}" = "true" ]; then - create_systemd_service_file - elif [ "${HAS_OPENRC}" = "true" ]; then - create_openrc_service_file - fi + [ "${HAS_SYSTEMD}" = "true" ] && create_systemd_service_file + [ "${HAS_OPENRC}" = "true" ] && create_openrc_service_file +} + +# --- get hashes of the current k3s bin and service files +get_installed_hashes() { + sha256sum ${BIN_DIR}/k3s ${FILE_K3S_SERVICE} ${FILE_K3S_ENV} 2>&1 || true } # --- enable and start systemd service --- -systemd_enable_and_start() { +systemd_enable() { info "systemd: Enabling ${SYSTEM_NAME} unit" $SUDO systemctl enable ${FILE_K3S_SERVICE} >/dev/null $SUDO systemctl daemon-reload >/dev/null +} - [ "${INSTALL_K3S_SKIP_START}" = "true" ] && return +systemd_start() { info "systemd: Starting ${SYSTEM_NAME}" $SUDO systemctl restart ${SYSTEM_NAME} } # --- enable and start openrc service --- -openrc_enable_and_start() { +openrc_enable() { info "openrc: Enabling ${SYSTEM_NAME} service for default runlevel" $SUDO rc-update add ${SYSTEM_NAME} default >/dev/null +} - [ "${INSTALL_K3S_SKIP_START}" = "true" ] && return +openrc_start() { info "openrc: Starting ${SYSTEM_NAME}" $SUDO ${FILE_K3S_SERVICE} restart } # --- startup systemd or openrc service --- service_enable_and_start() { - if [ "${HAS_SYSTEMD}" = "true" ]; then - systemd_enable_and_start - elif [ "${HAS_OPENRC}" = "true" ]; then - openrc_enable_and_start + [ "${HAS_SYSTEMD}" = "true" ] && systemd_enable + [ "${HAS_OPENRC}" = "true" ] && openrc_enable + + [ "${INSTALL_K3S_SKIP_START}" = "true" ] && return + + POST_INSTALL_HASHES=`get_installed_hashes` + if [ "${PRE_INSTALL_HASHES}" = "${POST_INSTALL_HASHES}" ]; then + info "No change detected so skipping service start" + return fi + + [ "${HAS_SYSTEMD}" = "true" ] && systemd_start + [ "${HAS_OPENRC}" = "true" ] && openrc_start } # --- run the install process -- From fa673468cefbf38f3b46ddbae58ebfc7cc60b806 Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Thu, 25 Apr 2019 14:18:16 -0700 Subject: [PATCH 5/5] Add INSTALL_K3S_BIN_DIR_READ_ONLY variable --- install.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/install.sh b/install.sh index c027150ee9..cc4d2b9e4b 100755 --- a/install.sh +++ b/install.sh @@ -33,6 +33,10 @@ set -e # Directory to install k3s binary, links, and uninstall script to, or use # /usr/local/bin as the default # +# - INSTALL_K3S_BIN_DIR_READ_ONLY +# If set to true will not write files to INSTALL_K3S_BIN_DIR, forces +# setting INSTALL_K3S_SKIP_DOWNLOAD=true +# # - INSTALL_K3S_SYSTEMD_DIR # Directory to install systemd service and environment files to, or use # /etc/systemd/system as the default @@ -164,6 +168,9 @@ setup_env() { fi PRE_INSTALL_HASHES=`get_installed_hashes` + if [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ]; then + INSTALL_K3S_SKIP_DOWNLOAD=true + fi } # --- check if skip download environment variable set --- @@ -321,6 +328,7 @@ download_and_verify() { # --- add additional utility links --- create_symlinks() { + [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return if [ ! -e ${BIN_DIR}/kubectl ]; then info "Creating ${BIN_DIR}/kubectl symlink to k3s" $SUDO ln -s k3s ${BIN_DIR}/kubectl @@ -334,6 +342,7 @@ create_symlinks() { # --- create uninstall script --- create_uninstall() { + [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return info "Creating uninstall script ${BIN_DIR}/${UNINSTALL_K3S_SH}" $SUDO tee ${BIN_DIR}/${UNINSTALL_K3S_SH} >/dev/null << EOF #!/bin/sh