install.sh: Cleanup of quotes

Where no variable expansion is done, " is replaced with '.
Copying content of one variable to another variable never needs quotes.
Some calls to echo and fatal need no quotes at all, but have been left
with ' to make it easier to see where the string begins and ends.
The fatal function now also sends output to stderr.

Change-Id: I2504707e5c550bc498e7663cedf58c224cd0a15b
Signed-off-by: Joakim Roubert <joakimr@axis.com>
This commit is contained in:
Joakim Roubert 2019-08-23 15:11:25 +02:00
parent ffe0288b68
commit 7a96fbb3f8

View File

@ -71,11 +71,11 @@ GITHUB_URL=https://github.com/rancher/k3s/releases
# --- helper functions for logs ---
info()
{
echo "[INFO] " "$@"
echo '[INFO] ' "$@"
}
fatal()
{
echo "[ERROR] " "$@"
echo '[ERROR] ' "$@" 2>&1
exit 1
}
@ -89,32 +89,32 @@ verify_system() {
HAS_SYSTEMD=true
return
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 ---
quote() {
for arg in "$@"; do
printf "%s\n" "$arg" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
printf '%s\n' "$arg" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
done
}
# --- add indentation and trailing slash to quoted args ---
quote_indent() {
printf ' \\'"\n"
printf ' \\\n'
for arg in "$@"; do
printf "\t%s "'\\'"\n" "$(quote "$arg")"
printf '\t%s \\\n' "$(quote "$arg")"
done
}
# --- escape most punctuation characters, except quotes, forward slash, and space ---
escape() {
printf "%s" "$@" | sed -e 's/\([][!#$%&()*;<=>?\_`{|}]\)/\\\1/g;'
printf '%s' "$@" | sed -e 's/\([][!#$%&()*;<=>?\_`{|}]\)/\\\1/g;'
}
# --- escape double quotes ---
escape_dq() {
printf "%s" "$@" | sed -e 's/"/\\"/g'
printf '%s' "$@" | sed -e 's/"/\\"/g'
}
# --- define needed environment variables ---
@ -134,7 +134,7 @@ setup_env() {
;;
# --- command is provided ---
(*)
CMD_K3S="$1"
CMD_K3S=$1
shift
;;
esac
@ -144,7 +144,7 @@ setup_env() {
if [ -n "${INSTALL_K3S_NAME}" ]; then
SYSTEM_NAME=k3s-${INSTALL_K3S_NAME}
else
if [ "${CMD_K3S}" = "server" ]; then
if [ "${CMD_K3S}" = server ]; then
SYSTEM_NAME=k3s
else
SYSTEM_NAME=k3s-${CMD_K3S}
@ -152,9 +152,9 @@ setup_env() {
fi
# --- 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
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:
${SYSTEM_NAME}
${invalid_chars}"
@ -173,9 +173,9 @@ setup_env() {
# --- use systemd type if defined or create default ---
if [ -n "${INSTALL_K3S_TYPE}" ]; then
SYSTEMD_TYPE="${INSTALL_K3S_TYPE}"
SYSTEMD_TYPE=${INSTALL_K3S_TYPE}
else
if [ "${CMD_K3S}" = "server" ]; then
if [ "${CMD_K3S}" = server ]; then
SYSTEMD_TYPE=notify
else
SYSTEMD_TYPE=exec
@ -184,23 +184,23 @@ setup_env() {
# --- use binary install directory if defined or create default ---
if [ -n "${INSTALL_K3S_BIN_DIR}" ]; then
BIN_DIR="${INSTALL_K3S_BIN_DIR}"
BIN_DIR=${INSTALL_K3S_BIN_DIR}
else
BIN_DIR="/usr/local/bin"
BIN_DIR=/usr/local/bin
fi
# --- use systemd directory if defined or create default ---
if [ -n "${INSTALL_K3S_SYSTEMD_DIR}" ]; then
SYSTEMD_DIR="${INSTALL_K3S_SYSTEMD_DIR}"
else
SYSTEMD_DIR="/etc/systemd/system"
SYSTEMD_DIR=/etc/systemd/system
fi
# --- 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_ENV=${SYSTEMD_DIR}/${SERVICE_K3S}.env
elif [ "${HAS_OPENRC}" = "true" ]; then
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
@ -210,14 +210,14 @@ setup_env() {
PRE_INSTALL_HASHES=`get_installed_hashes`
# --- 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
fi
}
# --- check if skip download environment variable set ---
can_skip_download() {
if [ "${INSTALL_K3S_SKIP_DOWNLOAD}" != "true" ]; then
if [ "${INSTALL_K3S_SKIP_DOWNLOAD}" != true ]; then
return 1
fi
}
@ -263,7 +263,7 @@ setup_verify_arch() {
# --- fatal if no curl ---
verify_curl() {
if [ -z `which curl || true` ]; then
fatal "Can not find curl for downloading files"
fatal 'Can not find curl for downloading files'
fi
}
@ -285,10 +285,10 @@ setup_tmp() {
# --- use desired k3s version if defined or find latest ---
get_release_version() {
if [ -n "${INSTALL_K3S_VERSION}" ]; then
VERSION_K3S="${INSTALL_K3S_VERSION}"
VERSION_K3S=${INSTALL_K3S_VERSION}
else
info "Finding latest release"
VERSION_K3S=`curl -w "%{url_effective}" -I -L -s -S ${GITHUB_URL}/latest -o /dev/null | sed -e 's|.*/||'`
info 'Finding latest release'
VERSION_K3S=`curl -w '%{url_effective}' -I -L -s -S ${GITHUB_URL}/latest -o /dev/null | sed -e 's|.*/||'`
fi
info "Using ${VERSION_K3S} as release"
}
@ -297,7 +297,7 @@ get_release_version() {
download_hash() {
HASH_URL=${GITHUB_URL}/download/${VERSION_K3S}/sha256sum-${ARCH}.txt
info "Downloading hash ${HASH_URL}"
curl -o ${TMP_HASH} -sfL ${HASH_URL} || fatal "Hash download failed"
curl -o ${TMP_HASH} -sfL ${HASH_URL} || fatal 'Hash download failed'
HASH_EXPECTED=`grep " k3s${SUFFIX}$" ${TMP_HASH} | awk '{print $1}'`
}
@ -316,12 +316,12 @@ installed_hash_matches() {
download_binary() {
BIN_URL=${GITHUB_URL}/download/${VERSION_K3S}/k3s${SUFFIX}
info "Downloading binary ${BIN_URL}"
curl -o ${TMP_BIN} -sfL ${BIN_URL} || fatal "Binary download failed"
curl -o ${TMP_BIN} -sfL ${BIN_URL} || fatal 'Binary download failed'
}
# --- verify downloaded binary hash ---
verify_binary() {
info "Verifying binary download"
info 'Verifying binary download'
HASH_BIN=`sha256sum ${TMP_BIN} | awk '{print $1}'`
if [ "${HASH_EXPECTED}" != "${HASH_BIN}" ]; then
fatal "Download sha256 does not match ${HASH_EXPECTED}, got ${HASH_BIN}"
@ -336,8 +336,8 @@ setup_binary() {
$SUDO mv -f ${TMP_BIN} ${BIN_DIR}/k3s
if command -v getenforce > /dev/null 2>&1; then
if [ "Disabled" != `getenforce` ]; then
info "SeLinux is enabled, setting permissions"
if [ Disabled != `getenforce` ]; then
info 'SeLinux is enabled, setting permissions'
if ! $SUDO semanage fcontext -l | grep "${BIN_DIR}/k3s" > /dev/null 2>&1; then
$SUDO semanage fcontext -a -t bin_t "${BIN_DIR}/k3s"
fi
@ -349,7 +349,7 @@ setup_binary() {
# --- download and verify k3s ---
download_and_verify() {
if can_skip_download; then
info "Skipping k3s download and verify"
info 'Skipping k3s download and verify'
verify_k3s_is_executable
return
fi
@ -361,7 +361,7 @@ download_and_verify() {
download_hash
if installed_hash_matches; then
info "Skipping binary downloaded, installed k3s matches hash"
info 'Skipping binary downloaded, installed k3s matches hash'
return
fi
@ -372,13 +372,13 @@ download_and_verify() {
# --- add additional utility links ---
create_symlinks() {
[ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return
[ "${INSTALL_K3S_SYMLINK}" = "skip" ] && return
[ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = true ] && return
[ "${INSTALL_K3S_SYMLINK}" = skip ] && return
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)
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"
$SUDO ln -sf k3s ${BIN_DIR}/${cmd}
else
@ -392,7 +392,7 @@ create_symlinks() {
# --- create killall script ---
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}"
$SUDO tee ${BIN_DIR}/${KILLALL_K3S_SH} >/dev/null << \EOF
#!/bin/sh
@ -448,7 +448,7 @@ EOF
# --- create uninstall script ---
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}"
$SUDO tee ${BIN_DIR}/${UNINSTALL_K3S_SH} >/dev/null << EOF
#!/bin/sh
@ -475,7 +475,7 @@ remove_uninstall() {
trap remove_uninstall EXIT
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
fi
@ -560,13 +560,13 @@ start_pre() {
}
supervisor=supervise-daemon
name="${SYSTEM_NAME}"
name=${SYSTEM_NAME}
command="${BIN_DIR}/k3s"
command_args="$(escape_dq "${CMD_K3S_EXEC}")
>>${LOG_FILE} 2>&1"
output_log="${LOG_FILE}"
error_log="${LOG_FILE}"
output_log=${LOG_FILE}
error_log=${LOG_FILE}
pidfile="/var/run/${SYSTEM_NAME}.pid"
respawn_delay=5
@ -589,8 +589,8 @@ EOF
# --- write systemd or openrc service file ---
create_service_file() {
[ "${HAS_SYSTEMD}" = "true" ] && create_systemd_service_file
[ "${HAS_OPENRC}" = "true" ] && create_openrc_service_file
[ "${HAS_SYSTEMD}" = true ] && create_systemd_service_file
[ "${HAS_OPENRC}" = true ] && create_openrc_service_file
return 0
}
@ -624,19 +624,19 @@ openrc_start() {
# --- startup systemd or openrc service ---
service_enable_and_start() {
[ "${HAS_SYSTEMD}" = "true" ] && systemd_enable
[ "${HAS_OPENRC}" = "true" ] && openrc_enable
[ "${HAS_SYSTEMD}" = true ] && systemd_enable
[ "${HAS_OPENRC}" = true ] && openrc_enable
[ "${INSTALL_K3S_SKIP_START}" = "true" ] && return
[ "${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"
info 'No change detected so skipping service start'
return
fi
[ "${HAS_SYSTEMD}" = "true" ] && systemd_start
[ "${HAS_OPENRC}" = "true" ] && openrc_start
[ "${HAS_SYSTEMD}" = true ] && systemd_start
[ "${HAS_OPENRC}" = true ] && openrc_start
return 0
}