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
INSTALL_DIR_DEFAULT="/usr/local/bin"
# URI of "kiwi" launcher script
KIWI_URI="https://raw.githubusercontent.com/ldericher/kiwi-config/master/kiwi"
#############
# FUNCTIONS #
#############
############
# CLI ARGS #
############
# prompt yes/no question (default yes)
yes_no() {
# prompt and read from terminal
printf "%s [Y|n] " "${1}"
read -r answer </dev/tty || answer=""
# installation directory
install_dir="${1}"
# adjust default if given
INSTALL_DIR_DEFAULT="${1:-${INSTALL_DIR_DEFAULT}}"
# check first character
answer="$(printf '%.1s' "${answer}")"
if [ "${answer}" = "N" ] || [ "${answer}" = "n" ]; then
# negative
return 1
else
# positive
return 0
fi
}
# exit with error
die() {
echo "ERROR: ${1}!" >/dev/stderr
exit 1
}
########
# 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
# prompt user for installation directory
printf "Select installation directory [Default: '%s']: " "${INSTALL_DIR_DEFAULT}"
read -r install_dir </dev/tty || 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
printf "Install directory doesn't exist. Try creating? [Y|n] "
read -r yesno </dev/tty || yesno="yes"
yesno=$(printf '%.1s' "${yesno}")
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
# fail if install dir can't be created
if yes_no "Install directory doesn't exist. Try creating?"; then
if ! mkdir -p "${install_dir}" >/dev/null 2>/dev/null; then
die "Couldn't create install directory"
fi
fi
fi
done
if [ ! -d "${install_dir}" ]; then
echo "wtf?"
exit 1
fi
# start actual installation
printf "Installing into '%s' ... " "${install_dir}"
# install "kiwi" script
uri="https://raw.githubusercontent.com/ldericher/kiwi-config/master/kiwi"
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}"
echo "Download 'kiwi' failed!" >/dev/stderr
exit 1
die "Downloading 'kiwi' failed"
fi
if ! install -m 0755 "${tmp_file}" "${install_dir}/kiwi" >/dev/null 2>/dev/null; then
rm "${tmp_file}"
echo "Install 'kiwi' failed!" >/dev/stderr
exit 1
die "Installing 'kiwi' failed"
fi
rm "${tmp_file}"
# finalization
rm "${tmp_file}"
echo "OK"
exit 0

91
kiwi
View file

@ -21,11 +21,23 @@ KIWI_REPO="https://github.com/ldericher/kiwi-config"
# use latest version by default
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 #
###################
# uri of correct kiwi-config archive
kiwi_archive_uri() {
echo "${KIWI_REPO}/archive/${KIWI_VERSION}.tar.gz"
}
# directory of correct installation
kiwi_installdir() {
echo "${KIWI_BASEDIR}/${KIWI_VERSION}"
@ -45,6 +57,28 @@ kiwi_executable() {
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 #
##################
@ -72,6 +106,61 @@ done
# 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
if [ -f "./${KIWI_CONF_NAME}" ]; then
# determine needed kiwi-config version
@ -88,7 +177,7 @@ if [ ! -x "$(kiwi_executable)" ]; then
cd "${tmpdir}" || :
# 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
cd "kiwi-config-${KIWI_VERSION}" || :