upgrade existing installation; version check in kiwi launcher

This commit is contained in:
Jörn-Michael Miehe 2020-08-26 05:12:28 +02:00
parent 0c35480170
commit 3faa629a13
2 changed files with 143 additions and 34 deletions

View file

@ -6,70 +6,90 @@
# default installation directory # default installation directory
INSTALL_DIR_DEFAULT="/usr/local/bin" INSTALL_DIR_DEFAULT="/usr/local/bin"
# URI of "kiwi" launcher script
KIWI_URI="https://raw.githubusercontent.com/ldericher/kiwi-config/master/kiwi"
#############
# FUNCTIONS #
#############
############ # prompt yes/no question (default yes)
# CLI ARGS # yes_no() {
############ # prompt and read from terminal
printf "%s [Y|n] " "${1}"
read -r answer </dev/tty || answer=""
# installation directory # check first character
install_dir="${1}" answer="$(printf '%.1s' "${answer}")"
# adjust default if given if [ "${answer}" = "N" ] || [ "${answer}" = "n" ]; then
INSTALL_DIR_DEFAULT="${1:-${INSTALL_DIR_DEFAULT}}" # negative
return 1
else
# positive
return 0
fi
}
# exit with error
die() {
echo "ERROR: ${1}!" >/dev/stderr
exit 1
}
######## ########
# MAIN # # MAIN #
######## ########
# prompt for installation directory # check if already installed
install_kiwi="$(command -v kiwi)"
if [ -x "${install_kiwi}" ]; then
# kiwi is installed: Choose that directory
install_dir="$(dirname "${install_kiwi}")"
if ! yes_no "kiwi-config found in '${install_dir}'. Overwrite?"; then
die "Uninstall existing '${install_kiwi}' first"
fi
elif [ ${#} -gt 0 ]; then
# install dir candidate given as CLI argument
install_dir="${1}"
shift 1
fi
# check dir given by argument
while [ ! -d "${install_dir}" ]; do while [ ! -d "${install_dir}" ]; do
# prompt user for installation directory
printf "Select installation directory [Default: '%s']: " "${INSTALL_DIR_DEFAULT}" printf "Select installation directory [Default: '%s']: " "${INSTALL_DIR_DEFAULT}"
read -r install_dir </dev/tty || install_dir="${INSTALL_DIR_DEFAULT}" read -r install_dir </dev/tty || install_dir="${INSTALL_DIR_DEFAULT}"
install_dir="${install_dir:-${INSTALL_DIR_DEFAULT}}" install_dir="${install_dir:-${INSTALL_DIR_DEFAULT}}"
# check if given dir exists # check dir given on terminal
if [ ! -d "${install_dir}" ]; then if [ ! -d "${install_dir}" ]; then
printf "Install directory doesn't exist. Try creating? [Y|n] " # fail if install dir can't be created
read -r yesno </dev/tty || yesno="yes" if yes_no "Install directory doesn't exist. Try creating?"; then
yesno=$(printf '%.1s' "${yesno}") if ! mkdir -p "${install_dir}" >/dev/null 2>/dev/null; then
die "Couldn't create install directory"
if [ ! "${yesno}" = "N" ] && [ ! "${yesno}" = "n" ]; then
# fail this script if we can't create the install dir
if ! mkdir -p "${install_dir}"; then
echo "Invalid install directory." >/dev/stderr
exit 1
fi fi
fi fi
fi fi
done done
if [ ! -d "${install_dir}" ]; then
echo "wtf?"
exit 1
fi
# start actual installation # start actual installation
printf "Installing into '%s' ... " "${install_dir}" printf "Installing into '%s' ... " "${install_dir}"
# install "kiwi" script
uri="https://raw.githubusercontent.com/ldericher/kiwi-config/master/kiwi"
tmp_file="$(mktemp)" tmp_file="$(mktemp)"
if ! curl --proto '=https' --tlsv1.2 -sSf -o "${tmp_file}" "${uri}" >/dev/null 2>/dev/null; then if ! curl --proto '=https' --tlsv1.2 -sSf -o "${tmp_file}" "${KIWI_URI}" >/dev/null 2>/dev/null; then
rm "${tmp_file}" rm "${tmp_file}"
echo "Download 'kiwi' failed!" >/dev/stderr die "Downloading 'kiwi' failed"
exit 1
fi fi
if ! install -m 0755 "${tmp_file}" "${install_dir}/kiwi" >/dev/null 2>/dev/null; then if ! install -m 0755 "${tmp_file}" "${install_dir}/kiwi" >/dev/null 2>/dev/null; then
rm "${tmp_file}" rm "${tmp_file}"
echo "Install 'kiwi' failed!" >/dev/stderr die "Installing 'kiwi' failed"
exit 1
fi fi
rm "${tmp_file}"
# finalization # finalization
rm "${tmp_file}"
echo "OK" echo "OK"
exit 0 exit 0

91
kiwi
View file

@ -21,11 +21,23 @@ KIWI_REPO="https://github.com/ldericher/kiwi-config"
# use latest version by default # use latest version by default
KIWI_VERSION="master" KIWI_VERSION="master"
# URI of "kiwi" launcher script
KIWI_URI="https://raw.githubusercontent.com/ldericher/kiwi-config/master/kiwi"
INSTALLER_URI="https://raw.githubusercontent.com/ldericher/kiwi-config/master/install.sh"
# canary file: limit curl requests
CANARY_FILENAME="/var/lock/kiwi-config.canary"
CANARY_MAXAGE=600
################### ###################
# DYNAMIC STRINGS # # DYNAMIC STRINGS #
################### ###################
# uri of correct kiwi-config archive
kiwi_archive_uri() {
echo "${KIWI_REPO}/archive/${KIWI_VERSION}.tar.gz"
}
# directory of correct installation # directory of correct installation
kiwi_installdir() { kiwi_installdir() {
echo "${KIWI_BASEDIR}/${KIWI_VERSION}" echo "${KIWI_BASEDIR}/${KIWI_VERSION}"
@ -45,6 +57,28 @@ kiwi_executable() {
WORKDIR="$(pwd)" WORKDIR="$(pwd)"
#############
# FUNCTIONS #
#############
# prompt yes/no question (default yes)
yes_no() {
# prompt and read from terminal
printf "%s [Y|n] " "${1}"
read -r answer </dev/tty || answer=""
# check first character
answer="$(printf '%.1s' "${answer}")"
if [ "${answer}" = "N" ] || [ "${answer}" = "n" ]; then
# negative
return 1
else
# positive
return 0
fi
}
################## ##################
# PER-USER SETUP # # PER-USER SETUP #
################## ##################
@ -72,6 +106,61 @@ done
# MAIN # # MAIN #
######## ########
# check if we should check for new kiwi version
if [ -f "${CANARY_FILENAME}" ]; then
# check canary age
current_time="$(date '+%s')"
canary_mtime="$(date -r "${CANARY_FILENAME}" '+%s')"
canary_age="$(( current_time - canary_mtime ))"
if [ ${canary_age} -gt ${CANARY_MAXAGE} ]; then
# canary file too old!
run_kiwi_check="yes"
fi
else
# no canary file!
run_kiwi_check="yes"
fi
# run check for new kiwi version
if [ "${run_kiwi_check}" = "yes" ]; then
# hash this script and the master version
hash_local="$(md5sum < "$(readlink "${0}")")"
hash_remote="$(curl --proto '=https' --tlsv1.2 -sSfL "${KIWI_URI}" | md5sum)"
# warn if different
if [ "${hash_local}" != "${hash_remote}" ]; then
if yes_no "Your kiwi launcher is outdated. Update now?" >/dev/stderr; then
# should reinstall, so download installer
installer="$(curl --proto '=https' --tlsv1.2 -sSfL "${INSTALLER_URI}")"
if yes_no "Use sudo to run as root?"; then
# enable system-wide install
echo "${installer}" | sudo sh
else
# per-user install
echo "${installer}" | sh
fi
else
echo "" >/dev/stderr
echo "####################" >/dev/stderr
echo "Please manually update your kiwi launcher by re-running the installation process:" >/dev/stderr
echo "https://github.com/ldericher/kiwi-config/#installation" >/dev/stderr
echo "####################" >/dev/stderr
echo "" >/dev/stderr
fi
fi
# refresh canary
touch "${CANARY_FILENAME}"
fi
# check if pwd is a kiwi folder # check if pwd is a kiwi folder
if [ -f "./${KIWI_CONF_NAME}" ]; then if [ -f "./${KIWI_CONF_NAME}" ]; then
# determine needed kiwi-config version # determine needed kiwi-config version
@ -88,7 +177,7 @@ if [ ! -x "$(kiwi_executable)" ]; then
cd "${tmpdir}" || : cd "${tmpdir}" || :
# download archive # download archive
curl --proto '=https' --tlsv1.2 -sSfL "${KIWI_REPO}/archive/${KIWI_VERSION}.tar.gz" | tar xz curl --proto '=https' --tlsv1.2 -sSfL "$(kiwi_archive_uri)" | tar xz
# read archive version tag # read archive version tag
cd "kiwi-config-${KIWI_VERSION}" || : cd "kiwi-config-${KIWI_VERSION}" || :