2020-08-25 12:34:30 +00:00
|
|
|
#!/bin/sh
|
2020-08-04 00:56:29 +00:00
|
|
|
|
|
|
|
#############
|
|
|
|
# CONSTANTS #
|
2020-08-20 22:04:26 +00:00
|
|
|
#############
|
2020-08-04 00:56:29 +00:00
|
|
|
|
2020-08-04 14:38:51 +00:00
|
|
|
# base config filename
|
2020-08-20 15:38:25 +00:00
|
|
|
KIWI_CONF_NAME="kiwi.yml"
|
|
|
|
# version tag filename
|
|
|
|
KIWI_VERSION_TAG="etc/version_tag"
|
|
|
|
|
2020-08-26 11:56:51 +00:00
|
|
|
# dependencies to run kiwi-scp
|
2020-08-26 12:33:00 +00:00
|
|
|
KIWI_DEPENDENCIES="python3 pipenv less docker docker-compose"
|
2020-08-20 22:04:26 +00:00
|
|
|
# base install dir
|
2020-08-26 12:57:36 +00:00
|
|
|
KIWI_BASEDIR="${HOME}/.cache/kiwi-scp"
|
2020-08-20 15:38:25 +00:00
|
|
|
# per-user env setup script
|
2020-08-26 12:33:00 +00:00
|
|
|
KIWI_PROFILE="${HOME}/.kiwi_profile"
|
2020-08-20 15:38:25 +00:00
|
|
|
|
2020-08-04 00:56:29 +00:00
|
|
|
# repository uri
|
2020-08-26 11:56:51 +00:00
|
|
|
KIWI_REPO="https://github.com/ldericher/kiwi-scp"
|
2020-08-20 15:38:25 +00:00
|
|
|
# use latest version by default
|
|
|
|
KIWI_VERSION="master"
|
|
|
|
|
2020-08-26 03:12:28 +00:00
|
|
|
# URI of "kiwi" launcher script
|
2020-08-26 11:56:51 +00:00
|
|
|
KIWI_URI="https://raw.githubusercontent.com/ldericher/kiwi-scp/master/kiwi"
|
|
|
|
INSTALLER_URI="https://raw.githubusercontent.com/ldericher/kiwi-scp/master/install.sh"
|
2020-08-26 03:12:28 +00:00
|
|
|
# canary file: limit curl requests
|
2020-08-26 11:56:51 +00:00
|
|
|
CANARY_FILENAME="/var/lock/kiwi-scp.canary"
|
2020-08-26 12:33:00 +00:00
|
|
|
CANARY_MAX_AGE=600
|
2020-08-25 12:34:30 +00:00
|
|
|
|
2020-08-20 15:38:25 +00:00
|
|
|
###################
|
|
|
|
# DYNAMIC STRINGS #
|
|
|
|
###################
|
|
|
|
|
2020-08-26 11:56:51 +00:00
|
|
|
# uri of correct kiwi-scp archive
|
2020-08-26 03:12:28 +00:00
|
|
|
kiwi_archive_uri() {
|
|
|
|
echo "${KIWI_REPO}/archive/${KIWI_VERSION}.tar.gz"
|
|
|
|
}
|
|
|
|
|
2020-08-20 15:38:25 +00:00
|
|
|
# directory of correct installation
|
2020-08-26 12:33:00 +00:00
|
|
|
kiwi_install_dir() {
|
2020-08-20 15:38:25 +00:00
|
|
|
echo "${KIWI_BASEDIR}/${KIWI_VERSION}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# src directory in installed version
|
2020-08-25 12:34:30 +00:00
|
|
|
kiwi_root() {
|
2020-08-26 12:33:00 +00:00
|
|
|
echo "$(kiwi_install_dir)/src"
|
2020-08-20 15:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# main script in installed version
|
2020-08-25 12:34:30 +00:00
|
|
|
kiwi_executable() {
|
2020-08-26 11:56:51 +00:00
|
|
|
echo "$(kiwi_root)/kiwi-scp.py"
|
2020-08-20 15:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# cache current work directory
|
|
|
|
WORKDIR="$(pwd)"
|
|
|
|
|
2020-08-26 03:12:28 +00:00
|
|
|
#############
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
2020-08-20 15:38:25 +00:00
|
|
|
##################
|
|
|
|
# PER-USER SETUP #
|
|
|
|
##################
|
|
|
|
|
|
|
|
# add in environment setup
|
2020-08-26 12:33:00 +00:00
|
|
|
if [ -f "${KIWI_PROFILE}" ]; then
|
|
|
|
# shellcheck source=$HOME/.kiwi_profile
|
|
|
|
. "${KIWI_PROFILE}"
|
2020-08-20 15:38:25 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
##########
|
|
|
|
# CHECKS #
|
|
|
|
##########
|
|
|
|
|
2020-08-26 12:33:00 +00:00
|
|
|
for dep in ${KIWI_DEPENDENCIES}; do
|
2020-08-25 12:34:30 +00:00
|
|
|
if ! command -v "${dep}" >/dev/null 2>/dev/null; then
|
2020-08-24 15:01:16 +00:00
|
|
|
echo "Dependency '${dep}' not found, please install!" >/dev/stderr
|
2020-08-20 15:38:25 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
2020-08-04 00:56:29 +00:00
|
|
|
|
|
|
|
########
|
|
|
|
# MAIN #
|
|
|
|
########
|
|
|
|
|
2020-08-26 03:12:28 +00:00
|
|
|
# check if we should check for new kiwi version
|
|
|
|
if [ -f "${CANARY_FILENAME}" ]; then
|
|
|
|
# check canary age
|
|
|
|
current_time="$(date '+%s')"
|
2020-08-26 12:33:00 +00:00
|
|
|
canary_mod_time="$(date -r "${CANARY_FILENAME}" '+%s')"
|
|
|
|
canary_age="$((current_time - canary_mod_time))"
|
2020-08-26 03:12:28 +00:00
|
|
|
|
2020-08-26 12:33:00 +00:00
|
|
|
if [ ${canary_age} -gt ${CANARY_MAX_AGE} ]; then
|
2020-08-26 03:12:28 +00:00
|
|
|
# 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
|
2020-08-26 12:57:36 +00:00
|
|
|
hash_local="$(md5sum <"$(readlink -f "${0}")")"
|
2020-08-26 03:12:28 +00:00
|
|
|
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
|
2020-08-26 11:56:51 +00:00
|
|
|
echo "https://github.com/ldericher/kiwi-scp/#installation" >/dev/stderr
|
2020-08-26 03:12:28 +00:00
|
|
|
echo "####################" >/dev/stderr
|
|
|
|
echo "" >/dev/stderr
|
|
|
|
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# refresh canary
|
|
|
|
touch "${CANARY_FILENAME}"
|
|
|
|
fi
|
|
|
|
|
2020-08-04 00:56:29 +00:00
|
|
|
# check if pwd is a kiwi folder
|
|
|
|
if [ -f "./${KIWI_CONF_NAME}" ]; then
|
2020-08-26 11:56:51 +00:00
|
|
|
# determine needed kiwi-scp version
|
2020-08-20 22:04:26 +00:00
|
|
|
re_version_line='version\s*:\s*'
|
2020-08-24 12:48:50 +00:00
|
|
|
eval "$(grep -E "${re_version_line}" "./${KIWI_CONF_NAME}" | sed -r "s/${re_version_line}/KIWI_VERSION=/")"
|
2020-08-04 00:56:29 +00:00
|
|
|
fi
|
|
|
|
|
2020-08-26 11:56:51 +00:00
|
|
|
# install if kiwi-scp not found
|
2020-08-20 15:38:25 +00:00
|
|
|
if [ ! -x "$(kiwi_executable)" ]; then
|
2020-08-26 11:56:51 +00:00
|
|
|
printf "Installing kiwi-scp v%s into %s ... " "${KIWI_VERSION}" "${KIWI_BASEDIR}"
|
2020-08-04 00:56:29 +00:00
|
|
|
|
2020-08-24 15:01:16 +00:00
|
|
|
# switch to temp dir
|
2020-08-26 12:33:00 +00:00
|
|
|
tmp_dir=$(mktemp -d)
|
|
|
|
cd "${tmp_dir}" || :
|
2020-08-04 00:56:29 +00:00
|
|
|
|
2020-08-24 15:01:16 +00:00
|
|
|
# download archive
|
2020-08-26 03:12:28 +00:00
|
|
|
curl --proto '=https' --tlsv1.2 -sSfL "$(kiwi_archive_uri)" | tar xz
|
2020-08-04 00:56:29 +00:00
|
|
|
|
2020-08-24 15:01:16 +00:00
|
|
|
# read archive version tag
|
2020-08-26 11:56:51 +00:00
|
|
|
cd "kiwi-scp-${KIWI_VERSION}" || :
|
2020-08-24 15:01:16 +00:00
|
|
|
KIWI_VERSION=$(cat "./src/${KIWI_VERSION_TAG}")
|
2020-08-04 00:56:29 +00:00
|
|
|
|
2020-08-24 17:40:55 +00:00
|
|
|
if [ -x "$(kiwi_executable)" ]; then
|
|
|
|
# after version update: no need to install
|
2020-08-25 12:34:30 +00:00
|
|
|
echo "v${KIWI_VERSION} already installed!"
|
2020-08-24 17:40:55 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
# install archive
|
2020-08-26 12:33:00 +00:00
|
|
|
mkdir -p "$(kiwi_install_dir)"
|
|
|
|
mv "src" "Pipfile" "Pipfile.lock" "$(kiwi_install_dir)/"
|
2020-08-24 17:40:55 +00:00
|
|
|
echo "OK"
|
|
|
|
fi
|
2020-08-04 00:56:29 +00:00
|
|
|
|
2020-08-24 15:01:16 +00:00
|
|
|
# discard temp dir
|
|
|
|
cd "${WORKDIR}" || :
|
2020-08-26 12:33:00 +00:00
|
|
|
rm -rf "${tmp_dir}"
|
2020-08-04 00:56:29 +00:00
|
|
|
fi
|
|
|
|
|
2020-08-20 15:38:25 +00:00
|
|
|
# go back to the original work directory
|
2020-08-20 22:04:26 +00:00
|
|
|
cd "${WORKDIR}" || :
|
2020-08-20 15:38:25 +00:00
|
|
|
|
|
|
|
# setup main environment
|
|
|
|
KIWI_ROOT="$(kiwi_root)"
|
2020-08-24 18:18:46 +00:00
|
|
|
PIPENV_VERBOSITY=-1
|
2020-08-26 12:33:00 +00:00
|
|
|
PIPENV_PIPFILE="$(kiwi_install_dir)/Pipfile"
|
2020-08-20 15:38:25 +00:00
|
|
|
|
|
|
|
export KIWI_CONF_NAME
|
2020-08-25 12:34:30 +00:00
|
|
|
export KIWI_ROOT
|
2020-08-24 18:18:46 +00:00
|
|
|
export PIPENV_VERBOSITY
|
|
|
|
export PIPENV_PIPFILE
|
2020-08-04 14:38:51 +00:00
|
|
|
|
2020-08-25 12:34:30 +00:00
|
|
|
# check virtualenv
|
2020-08-26 12:33:00 +00:00
|
|
|
cd "$(kiwi_install_dir)" || :
|
2020-08-25 12:34:30 +00:00
|
|
|
if ! pipenv --venv >/dev/null 2>/dev/null; then
|
|
|
|
# install virtualenv
|
|
|
|
printf "Preparing virtualenv ... "
|
|
|
|
pipenv sync >/dev/null 2>/dev/null
|
|
|
|
echo "OK"
|
|
|
|
fi
|
|
|
|
|
2020-08-04 00:56:29 +00:00
|
|
|
# run main script
|
2020-08-20 22:04:26 +00:00
|
|
|
exec pipenv run "$(kiwi_executable)" "${@}"
|