Browse Source

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

install.sh: Cleanup of quotes
pull/883/head
Erik Wilson 5 years ago committed by GitHub
parent
commit
1106e48fc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 86
      install.sh

86
install.sh

@ -72,11 +72,11 @@ DOWNLOADER=
# --- helper functions for logs ---
info()
{
echo "[INFO] " "$@"
echo '[INFO] ' "$@"
}
fatal()
{
echo "[ERROR] " "$@"
echo '[ERROR] ' "$@" >&2
exit 1
}
@ -90,32 +90,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 ---
@ -135,7 +135,7 @@ setup_env() {
;;
# --- command is provided ---
(*)
CMD_K3S="$1"
CMD_K3S=$1
shift
;;
esac
@ -145,7 +145,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}
@ -153,9 +153,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}"
@ -174,9 +174,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
@ -185,23 +185,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
@ -211,14 +211,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
}
@ -289,7 +289,7 @@ 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"
case $DOWNLOADER in
@ -375,7 +375,7 @@ setup_binary() {
if command -v getenforce > /dev/null 2>&1; then
if [ "Disabled" != $(getenforce) ]; 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
$SUDO semanage fcontext -a -t bin_t "${BIN_DIR}/k3s"
fi
@ -390,7 +390,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
@ -402,7 +402,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
@ -413,13 +413,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
@ -433,7 +433,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
@ -497,7 +497,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
@ -524,7 +524,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
@ -608,13 +608,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
@ -637,8 +637,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
}
@ -672,19 +672,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
}

Loading…
Cancel
Save