Unverified Commit 1106e48f authored by Erik Wilson's avatar Erik Wilson Committed by GitHub

Merge pull request #757 from joakimr-axis/joakimr-axis_quotes

install.sh: Cleanup of quotes
parents c12d2a1a d982c33b
...@@ -72,11 +72,11 @@ DOWNLOADER= ...@@ -72,11 +72,11 @@ DOWNLOADER=
# --- helper functions for logs --- # --- helper functions for logs ---
info() info()
{ {
echo "[INFO] " "$@" echo '[INFO] ' "$@"
} }
fatal() fatal()
{ {
echo "[ERROR] " "$@" echo '[ERROR] ' "$@" >&2
exit 1 exit 1
} }
...@@ -90,32 +90,32 @@ verify_system() { ...@@ -90,32 +90,32 @@ verify_system() {
HAS_SYSTEMD=true HAS_SYSTEMD=true
return return
fi fi
fatal "Can not find systemd or openrc to use as a process supervisor for k3s" fatal 'Can not find systemd or openrc to use as a process supervisor for k3s'
} }
# --- add quotes to command arguments --- # --- add quotes to command arguments ---
quote() { quote() {
for arg in "$@"; do for arg in "$@"; do
printf "%s\n" "$arg" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/" printf '%s\n' "$arg" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
done done
} }
# --- add indentation and trailing slash to quoted args --- # --- add indentation and trailing slash to quoted args ---
quote_indent() { quote_indent() {
printf ' \\'"\n" printf ' \\\n'
for arg in "$@"; do for arg in "$@"; do
printf "\t%s "'\\'"\n" "$(quote "$arg")" printf '\t%s \\\n' "$(quote "$arg")"
done done
} }
# --- escape most punctuation characters, except quotes, forward slash, and space --- # --- escape most punctuation characters, except quotes, forward slash, and space ---
escape() { escape() {
printf "%s" "$@" | sed -e 's/\([][!#$%&()*;<=>?\_`{|}]\)/\\\1/g;' printf '%s' "$@" | sed -e 's/\([][!#$%&()*;<=>?\_`{|}]\)/\\\1/g;'
} }
# --- escape double quotes --- # --- escape double quotes ---
escape_dq() { escape_dq() {
printf "%s" "$@" | sed -e 's/"/\\"/g' printf '%s' "$@" | sed -e 's/"/\\"/g'
} }
# --- define needed environment variables --- # --- define needed environment variables ---
...@@ -135,7 +135,7 @@ setup_env() { ...@@ -135,7 +135,7 @@ setup_env() {
;; ;;
# --- command is provided --- # --- command is provided ---
(*) (*)
CMD_K3S="$1" CMD_K3S=$1
shift shift
;; ;;
esac esac
...@@ -145,7 +145,7 @@ setup_env() { ...@@ -145,7 +145,7 @@ setup_env() {
if [ -n "${INSTALL_K3S_NAME}" ]; then if [ -n "${INSTALL_K3S_NAME}" ]; then
SYSTEM_NAME=k3s-${INSTALL_K3S_NAME} SYSTEM_NAME=k3s-${INSTALL_K3S_NAME}
else else
if [ "${CMD_K3S}" = "server" ]; then if [ "${CMD_K3S}" = server ]; then
SYSTEM_NAME=k3s SYSTEM_NAME=k3s
else else
SYSTEM_NAME=k3s-${CMD_K3S} SYSTEM_NAME=k3s-${CMD_K3S}
...@@ -153,9 +153,9 @@ setup_env() { ...@@ -153,9 +153,9 @@ setup_env() {
fi fi
# --- check for invalid characters in system name --- # --- check for invalid characters in system name ---
valid_chars=$(printf "%s" "${SYSTEM_NAME}" | sed -e 's/[][!#$%&()*;<=>?\_`{|}/[:space:]]/^/g;' ) valid_chars=$(printf '%s' "${SYSTEM_NAME}" | sed -e 's/[][!#$%&()*;<=>?\_`{|}/[:space:]]/^/g;' )
if [ "${SYSTEM_NAME}" != "${valid_chars}" ]; then if [ "${SYSTEM_NAME}" != "${valid_chars}" ]; then
invalid_chars=$(printf "%s" "${valid_chars}" | sed -e 's/[^^]/ /g') invalid_chars=$(printf '%s' "${valid_chars}" | sed -e 's/[^^]/ /g')
fatal "Invalid characters for system name: fatal "Invalid characters for system name:
${SYSTEM_NAME} ${SYSTEM_NAME}
${invalid_chars}" ${invalid_chars}"
...@@ -174,9 +174,9 @@ setup_env() { ...@@ -174,9 +174,9 @@ setup_env() {
# --- use systemd type if defined or create default --- # --- use systemd type if defined or create default ---
if [ -n "${INSTALL_K3S_TYPE}" ]; then if [ -n "${INSTALL_K3S_TYPE}" ]; then
SYSTEMD_TYPE="${INSTALL_K3S_TYPE}" SYSTEMD_TYPE=${INSTALL_K3S_TYPE}
else else
if [ "${CMD_K3S}" = "server" ]; then if [ "${CMD_K3S}" = server ]; then
SYSTEMD_TYPE=notify SYSTEMD_TYPE=notify
else else
SYSTEMD_TYPE=exec SYSTEMD_TYPE=exec
...@@ -185,23 +185,23 @@ setup_env() { ...@@ -185,23 +185,23 @@ setup_env() {
# --- use binary install directory if defined or create default --- # --- use binary install directory if defined or create default ---
if [ -n "${INSTALL_K3S_BIN_DIR}" ]; then if [ -n "${INSTALL_K3S_BIN_DIR}" ]; then
BIN_DIR="${INSTALL_K3S_BIN_DIR}" BIN_DIR=${INSTALL_K3S_BIN_DIR}
else else
BIN_DIR="/usr/local/bin" BIN_DIR=/usr/local/bin
fi fi
# --- use systemd directory if defined or create default --- # --- use systemd directory if defined or create default ---
if [ -n "${INSTALL_K3S_SYSTEMD_DIR}" ]; then if [ -n "${INSTALL_K3S_SYSTEMD_DIR}" ]; then
SYSTEMD_DIR="${INSTALL_K3S_SYSTEMD_DIR}" SYSTEMD_DIR="${INSTALL_K3S_SYSTEMD_DIR}"
else else
SYSTEMD_DIR="/etc/systemd/system" SYSTEMD_DIR=/etc/systemd/system
fi fi
# --- use service or environment location depending on systemd/openrc --- # --- use service or environment location depending on systemd/openrc ---
if [ "${HAS_SYSTEMD}" = "true" ]; then if [ "${HAS_SYSTEMD}" = true ]; then
FILE_K3S_SERVICE=${SYSTEMD_DIR}/${SERVICE_K3S} FILE_K3S_SERVICE=${SYSTEMD_DIR}/${SERVICE_K3S}
FILE_K3S_ENV=${SYSTEMD_DIR}/${SERVICE_K3S}.env FILE_K3S_ENV=${SYSTEMD_DIR}/${SERVICE_K3S}.env
elif [ "${HAS_OPENRC}" = "true" ]; then elif [ "${HAS_OPENRC}" = true ]; then
$SUDO mkdir -p /etc/rancher/k3s $SUDO mkdir -p /etc/rancher/k3s
FILE_K3S_SERVICE=/etc/init.d/${SYSTEM_NAME} FILE_K3S_SERVICE=/etc/init.d/${SYSTEM_NAME}
FILE_K3S_ENV=/etc/rancher/k3s/${SYSTEM_NAME}.env FILE_K3S_ENV=/etc/rancher/k3s/${SYSTEM_NAME}.env
...@@ -211,14 +211,14 @@ setup_env() { ...@@ -211,14 +211,14 @@ setup_env() {
PRE_INSTALL_HASHES=$(get_installed_hashes) PRE_INSTALL_HASHES=$(get_installed_hashes)
# --- if bin directory is read only skip download --- # --- if bin directory is read only skip download ---
if [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ]; then if [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = true ]; then
INSTALL_K3S_SKIP_DOWNLOAD=true INSTALL_K3S_SKIP_DOWNLOAD=true
fi fi
} }
# --- check if skip download environment variable set --- # --- check if skip download environment variable set ---
can_skip_download() { can_skip_download() {
if [ "${INSTALL_K3S_SKIP_DOWNLOAD}" != "true" ]; then if [ "${INSTALL_K3S_SKIP_DOWNLOAD}" != true ]; then
return 1 return 1
fi fi
} }
...@@ -289,7 +289,7 @@ setup_tmp() { ...@@ -289,7 +289,7 @@ setup_tmp() {
# --- use desired k3s version if defined or find latest --- # --- use desired k3s version if defined or find latest ---
get_release_version() { get_release_version() {
if [ -n "${INSTALL_K3S_VERSION}" ]; then if [ -n "${INSTALL_K3S_VERSION}" ]; then
VERSION_K3S="${INSTALL_K3S_VERSION}" VERSION_K3S=${INSTALL_K3S_VERSION}
else else
info "Finding latest release" info "Finding latest release"
case $DOWNLOADER in case $DOWNLOADER in
...@@ -375,7 +375,7 @@ setup_binary() { ...@@ -375,7 +375,7 @@ setup_binary() {
if command -v getenforce > /dev/null 2>&1; then if command -v getenforce > /dev/null 2>&1; then
if [ "Disabled" != $(getenforce) ]; then if [ "Disabled" != $(getenforce) ]; then
if command -v semanage > /dev/null 2>&1; then if command -v semanage > /dev/null 2>&1; then
info "SELinux is enabled, setting permissions" info 'SELinux is enabled, setting permissions'
if ! $SUDO semanage fcontext -l | grep "${BIN_DIR}/k3s" > /dev/null 2>&1; then if ! $SUDO semanage fcontext -l | grep "${BIN_DIR}/k3s" > /dev/null 2>&1; then
$SUDO semanage fcontext -a -t bin_t "${BIN_DIR}/k3s" $SUDO semanage fcontext -a -t bin_t "${BIN_DIR}/k3s"
fi fi
...@@ -390,7 +390,7 @@ setup_binary() { ...@@ -390,7 +390,7 @@ setup_binary() {
# --- download and verify k3s --- # --- download and verify k3s ---
download_and_verify() { download_and_verify() {
if can_skip_download; then if can_skip_download; then
info "Skipping k3s download and verify" info 'Skipping k3s download and verify'
verify_k3s_is_executable verify_k3s_is_executable
return return
fi fi
...@@ -402,7 +402,7 @@ download_and_verify() { ...@@ -402,7 +402,7 @@ download_and_verify() {
download_hash download_hash
if installed_hash_matches; then if installed_hash_matches; then
info "Skipping binary downloaded, installed k3s matches hash" info 'Skipping binary downloaded, installed k3s matches hash'
return return
fi fi
...@@ -413,13 +413,13 @@ download_and_verify() { ...@@ -413,13 +413,13 @@ download_and_verify() {
# --- add additional utility links --- # --- add additional utility links ---
create_symlinks() { create_symlinks() {
[ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = true ] && return
[ "${INSTALL_K3S_SYMLINK}" = "skip" ] && return [ "${INSTALL_K3S_SYMLINK}" = skip ] && return
for cmd in kubectl crictl ctr; do for cmd in kubectl crictl ctr; do
if [ ! -e ${BIN_DIR}/${cmd} ] || [ "${INSTALL_K3S_SYMLINK}" = "force" ]; then if [ ! -e ${BIN_DIR}/${cmd} ] || [ "${INSTALL_K3S_SYMLINK}" = force ]; then
which_cmd=$(which ${cmd} || true) which_cmd=$(which ${cmd} || true)
if [ -z "${which_cmd}" ] || [ "${INSTALL_K3S_SYMLINK}" = "force" ]; then if [ -z "${which_cmd}" ] || [ "${INSTALL_K3S_SYMLINK}" = force ]; then
info "Creating ${BIN_DIR}/${cmd} symlink to k3s" info "Creating ${BIN_DIR}/${cmd} symlink to k3s"
$SUDO ln -sf k3s ${BIN_DIR}/${cmd} $SUDO ln -sf k3s ${BIN_DIR}/${cmd}
else else
...@@ -433,7 +433,7 @@ create_symlinks() { ...@@ -433,7 +433,7 @@ create_symlinks() {
# --- create killall script --- # --- create killall script ---
create_killall() { create_killall() {
[ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = true ] && return
info "Creating killall script ${BIN_DIR}/${KILLALL_K3S_SH}" info "Creating killall script ${BIN_DIR}/${KILLALL_K3S_SH}"
$SUDO tee ${BIN_DIR}/${KILLALL_K3S_SH} >/dev/null << \EOF $SUDO tee ${BIN_DIR}/${KILLALL_K3S_SH} >/dev/null << \EOF
#!/bin/sh #!/bin/sh
...@@ -497,7 +497,7 @@ EOF ...@@ -497,7 +497,7 @@ EOF
# --- create uninstall script --- # --- create uninstall script ---
create_uninstall() { create_uninstall() {
[ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = true ] && return
info "Creating uninstall script ${BIN_DIR}/${UNINSTALL_K3S_SH}" info "Creating uninstall script ${BIN_DIR}/${UNINSTALL_K3S_SH}"
$SUDO tee ${BIN_DIR}/${UNINSTALL_K3S_SH} >/dev/null << EOF $SUDO tee ${BIN_DIR}/${UNINSTALL_K3S_SH} >/dev/null << EOF
#!/bin/sh #!/bin/sh
...@@ -524,7 +524,7 @@ remove_uninstall() { ...@@ -524,7 +524,7 @@ remove_uninstall() {
trap remove_uninstall EXIT trap remove_uninstall EXIT
if (ls ${SYSTEMD_DIR}/k3s*.service || ls /etc/init.d/k3s*) >/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 set +x; echo 'Additional k3s services installed, skipping uninstall of k3s'; set -x
exit exit
fi fi
...@@ -608,13 +608,13 @@ start_pre() { ...@@ -608,13 +608,13 @@ start_pre() {
} }
supervisor=supervise-daemon supervisor=supervise-daemon
name="${SYSTEM_NAME}" name=${SYSTEM_NAME}
command="${BIN_DIR}/k3s" command="${BIN_DIR}/k3s"
command_args="$(escape_dq "${CMD_K3S_EXEC}") command_args="$(escape_dq "${CMD_K3S_EXEC}")
>>${LOG_FILE} 2>&1" >>${LOG_FILE} 2>&1"
output_log="${LOG_FILE}" output_log=${LOG_FILE}
error_log="${LOG_FILE}" error_log=${LOG_FILE}
pidfile="/var/run/${SYSTEM_NAME}.pid" pidfile="/var/run/${SYSTEM_NAME}.pid"
respawn_delay=5 respawn_delay=5
...@@ -637,8 +637,8 @@ EOF ...@@ -637,8 +637,8 @@ EOF
# --- write systemd or openrc service file --- # --- write systemd or openrc service file ---
create_service_file() { create_service_file() {
[ "${HAS_SYSTEMD}" = "true" ] && create_systemd_service_file [ "${HAS_SYSTEMD}" = true ] && create_systemd_service_file
[ "${HAS_OPENRC}" = "true" ] && create_openrc_service_file [ "${HAS_OPENRC}" = true ] && create_openrc_service_file
return 0 return 0
} }
...@@ -672,19 +672,19 @@ openrc_start() { ...@@ -672,19 +672,19 @@ openrc_start() {
# --- startup systemd or openrc service --- # --- startup systemd or openrc service ---
service_enable_and_start() { service_enable_and_start() {
[ "${HAS_SYSTEMD}" = "true" ] && systemd_enable [ "${HAS_SYSTEMD}" = true ] && systemd_enable
[ "${HAS_OPENRC}" = "true" ] && openrc_enable [ "${HAS_OPENRC}" = true ] && openrc_enable
[ "${INSTALL_K3S_SKIP_START}" = "true" ] && return [ "${INSTALL_K3S_SKIP_START}" = true ] && return
POST_INSTALL_HASHES=$(get_installed_hashes) POST_INSTALL_HASHES=$(get_installed_hashes)
if [ "${PRE_INSTALL_HASHES}" = "${POST_INSTALL_HASHES}" ]; then if [ "${PRE_INSTALL_HASHES}" = "${POST_INSTALL_HASHES}" ]; then
info "No change detected so skipping service start" info 'No change detected so skipping service start'
return return
fi fi
[ "${HAS_SYSTEMD}" = "true" ] && systemd_start [ "${HAS_SYSTEMD}" = true ] && systemd_start
[ "${HAS_OPENRC}" = "true" ] && openrc_start [ "${HAS_OPENRC}" = true ] && openrc_start
return 0 return 0
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment