kiwi-scp/kiwi

137 lines
2.9 KiB
Text
Raw Permalink Normal View History

#!/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
KIWI_CONF_NAME="kiwi.yml"
# version tag filename
KIWI_VERSION_TAG="etc/version_tag"
# dependencies to run kiwi-config
KIWI_DEPS="python3 pipenv less docker docker-compose"
2020-08-20 22:04:26 +00:00
# base install dir
KIWI_BASEDIR="${HOME}/.local/lib/kiwi-config"
# per-user env setup script
KIWI_ENVFILE="${HOME}/.kiwienv"
2020-08-04 00:56:29 +00:00
# repository uri
KIWI_REPO="https://github.com/ldericher/kiwi-config"
# use latest version by default
KIWI_VERSION="master"
###################
# DYNAMIC STRINGS #
###################
# directory of correct installation
kiwi_installdir() {
echo "${KIWI_BASEDIR}/${KIWI_VERSION}"
}
# src directory in installed version
kiwi_root() {
echo "$(kiwi_installdir)/src"
}
# main script in installed version
kiwi_executable() {
echo "$(kiwi_root)/kiwi-config.py"
}
# cache current work directory
WORKDIR="$(pwd)"
##################
# PER-USER SETUP #
##################
# add in environment setup
if [ -f "${KIWI_ENVFILE}" ]; then
2020-08-20 22:04:26 +00:00
# shellcheck source=$HOME/.kiwienv
. "${KIWI_ENVFILE}"
fi
##########
# CHECKS #
##########
for dep in ${KIWI_DEPS}; do
if ! command -v "${dep}" >/dev/null 2>/dev/null; then
echo "Dependency '${dep}' not found, please install!" >/dev/stderr
exit 1
fi
done
2020-08-04 00:56:29 +00:00
2020-08-04 00:56:29 +00:00
########
# MAIN #
########
# check if pwd is a kiwi folder
if [ -f "./${KIWI_CONF_NAME}" ]; then
2020-08-20 22:04:26 +00:00
# determine needed kiwi-config version
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
# install if kiwi-config not found
if [ ! -x "$(kiwi_executable)" ]; then
printf "Installing kiwi-config v%s into %s ... " "${KIWI_VERSION}" "${KIWI_BASEDIR}"
2020-08-04 00:56:29 +00:00
# switch to temp dir
tmpdir=$(mktemp -d)
cd "${tmpdir}" || :
2020-08-04 00:56:29 +00:00
# download archive
2020-08-24 18:34:41 +00:00
curl --proto '=https' --tlsv1.2 -sSfL "${KIWI_REPO}/archive/${KIWI_VERSION}.tar.gz" | tar xz
2020-08-04 00:56:29 +00:00
# read archive version tag
cd "kiwi-config-${KIWI_VERSION}" || :
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
echo "v${KIWI_VERSION} already installed!"
2020-08-24 17:40:55 +00:00
else
# install archive
mkdir -p "$(kiwi_installdir)"
mv "src" "Pipfile" "Pipfile.lock" "$(kiwi_installdir)/"
echo "OK"
fi
2020-08-04 00:56:29 +00:00
# discard temp dir
cd "${WORKDIR}" || :
rm -rf "${tmpdir}"
2020-08-04 00:56:29 +00:00
fi
# go back to the original work directory
2020-08-20 22:04:26 +00:00
cd "${WORKDIR}" || :
# setup main environment
KIWI_ROOT="$(kiwi_root)"
PIPENV_VERBOSITY=-1
PIPENV_PIPFILE="$(kiwi_installdir)/Pipfile"
export KIWI_CONF_NAME
export KIWI_ROOT
export PIPENV_VERBOSITY
export PIPENV_PIPFILE
2020-08-04 14:38:51 +00:00
# check virtualenv
cd "$(kiwi_installdir)" || :
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)" "${@}"