1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-11-14 22:07:17 +00:00
test-tutorial-framework/hack/bootstrap_tfw_dev.sh

194 lines
4.5 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -eu
2018-04-15 19:22:56 +00:00
set -o pipefail
set -o errtrace
shopt -s expand_aliases
[ "$(uname)" == "Darwin" ] && alias sed="gsed" || :
pushd() { command pushd "$@" > /dev/null; }
popd() { command popd "$@" > /dev/null; }
TFWDEV="${TFWDEV:-0}"
HTTPS_REMOTES="${HTTPS_REMOTES:-}"
2018-06-08 09:40:22 +00:00
2018-03-24 10:47:38 +00:00
TFW_POSTFIX=tutorial-framework
BASEDIR="$(pwd)"
2018-03-24 10:47:38 +00:00
BASEIMAGE=baseimage-${TFW_POSTFIX}
2018-03-30 21:56:14 +00:00
TEST=test-${TFW_POSTFIX}
2018-03-24 10:47:38 +00:00
FRONTEND=frontend-${TFW_POSTFIX}
2018-06-08 09:40:22 +00:00
BASEIMAGE_NAME="${BASEIMAGE_NAME:-eu.gcr.io/avatao-challengestore/tutorial-framework}"
2018-03-24 10:47:38 +00:00
LOGFILE=/tmp/bootstrap_tfw.log
2018-04-27 09:40:34 +00:00
remotebase="git@github.com:"
run()
{
2018-05-03 12:29:41 +00:00
trap 'exit 1' INT
trap handle_exit EXIT
: > $LOGFILE
check_dependencies
clone_required_repos_ask_ssh_or_https
TAG="${BASEIMAGE_ONLY:-$(fetch_latest_tag)}"
TAG=$TAG build_baseimage
if [ -z "${BASEIMAGE_ONLY:-}" ]; then
TAG=$TAG pin_baseimage
install_frontend_deps
if [ "$TFWDEV" == "0" ]; then
cleanup_repos
merge_repos
fi
else
delete_repos
fi
echo
2018-06-08 09:40:22 +00:00
if [ -z "${BASEIMAGE_ONLY:-}" ]; then
echo "You can build & start TFW by executing the command: ${TEST}/hack/tfw.sh start"
else
echo "Baseimage version ${BASEIMAGE_ONLY} built successfully!"
fi
}
handle_exit()
{
if [[ $? -ne 0 ]]; then
delete_repos
showlog
fi
cleanlog
}
showlog() { echo && echo "Error! Showing logs:" && cat $LOGFILE; }
cleanlog() { rm $LOGFILE; }
delete_repos() { pushd "$BASEDIR" && rm -rf "$BASEIMAGE" "$FRONTEND" "$TEST" && popd; }
check_dependencies()
{
dependencies=("git" "docker" "yarn" "ng")
missing="0"
for dep in ${dependencies[@]}; do
if ! type "$dep" > /dev/null 2>&1; then
logged echo "Dependency '${dep}' not found!"
missing="1"
fi
done
if [ "$missing" == "1" ]; then
logged echo "Please install the missing packages and retry!"
exit 1
fi
if [ "$TFWDEV" == "0" ]; then
if ! docker info > /dev/null 2>&1; then
logged echo "The Docker daemon appears to be stopped. Please start it and try again!"
exit 1
fi
fi
}
clone_required_repos_ask_ssh_or_https()
{
if [[ -z "$HTTPS_REMOTES" ]]; then
read -p "Repos are pulled over SSH by default. Should I use HTTPS instead? [y/N]" -r -t 5 || :
echo
if [[ $REPLY =~ ^(y|Y|yes|Yes|YES)$ ]]; then
HTTPS_REMOTES=1 clone_required_repos
else
HTTPS_REMOTES=0 clone_required_repos
fi
else
clone_required_repos
fi
}
clone_required_repos()
{
[[ "$HTTPS_REMOTES" == "1" ]] && remotebase="https://github.com/"
echo -n "Cloning TFW repositories... "
echo -n "baseimage... " && logged git clone ${remotebase}avatao-content/${BASEIMAGE}.git
2018-06-08 09:40:22 +00:00
if [ -z "${BASEIMAGE_ONLY:-}" ]; then
echo -n "frontend... " && logged git clone ${remotebase}avatao-content/${FRONTEND}.git
echo -n "test... " && logged git clone ${remotebase}avatao-content/${TEST}.git
fi
echo "Done!"
}
install_frontend_deps()
{
echo -n "Installing frontend dependencies... "
pushd "$FRONTEND"
2018-04-27 09:40:34 +00:00
spinned logged yarn install
popd
echo "Done!"
}
fetch_latest_tag()
{
echo -n "$(git ls-remote --tags ${remotebase}avatao-content/${BASEIMAGE}.git |
cut -f2 |
grep -oP '(?<=refs/tags/)\w+-\d{8}$' |
sort -t '-' -k2 |
tail -n 1)"
}
pin_baseimage()
{
echo -n "Pinning TFW baseimage version... "
echo -n "which is ${TAG}... "
sed -i "1 s/.*/&:${TAG}/" "${TEST}/solvable/Dockerfile"
2018-03-31 19:22:33 +00:00
echo "Done!"
}
build_baseimage()
{
echo -n "Building baseimage at ${TAG}... "
pushd "$BASEIMAGE"
2018-06-08 09:40:22 +00:00
logged git checkout "$TAG"
spinned logged docker build -t "${BASEIMAGE_NAME}:${TAG}" .
popd
echo "Done!"
}
cleanup_repos()
{
rm -rf "${BASEIMAGE}"
rm -rf "${TEST}/.git"
rm -rf "${FRONTEND}/.git"
}
merge_repos()
{
2018-03-31 19:22:33 +00:00
echo -n "Merging repositories... "
NESTED_FRONTEND="${TEST}/solvable/frontend"
rm -rf $NESTED_FRONTEND
mv "$FRONTEND" "$NESTED_FRONTEND"
2018-03-31 19:22:33 +00:00
echo "Done!"
}
logged()
{
"$@" >> $LOGFILE 2>&1
}
spinned()
{
"$@" &
pid=$!
spin=("-" "\\" "|" "/")
echo -n "${spin[0]}"
while kill -0 $pid &> /dev/null; do
for i in "${spin[@]}"; do
echo -ne "\b$i"
sleep 0.1
done
done
echo -ne "\b"
wait $pid
}
run