1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-11-12 19:37:16 +00:00

Refactor hack script due to multistage building

This commit is contained in:
R. Richard 2019-09-05 18:23:57 +02:00
parent e016fc0a24
commit 813d1e7b40
4 changed files with 8 additions and 239 deletions

View File

@ -1,191 +0,0 @@
#!/usr/bin/env bash
set -eu
set -o pipefail
set -o errtrace
shopt -s expand_aliases
[ "$(uname)" == "Darwin" ] && alias sed="gsed" && alias grep="ggrep" || :
HERE="$(pwd)"
CHALLENGE=${CHALLENGE:-test-tutorial-framework}
BASEIMAGE_NAME="${BASEIMAGE_NAME:-eu.gcr.io/avatao-challengestore/tutorial-framework}"
BASEIMAGE_REPO="${BASEIMAGE_REPO:-baseimage-tutorial-framework}"
FRONTEND_REPO="${FRONTEND_REPO:-frontend-tutorial-framework}"
LOGFILE=/tmp/bootstrap_tfw.log
REMOTEBASE="git@github.com:avatao-content"
run() {
trap 'exit 1' INT
trap handle_exit EXIT
: > "${LOGFILE}"
check_dependencies
clone_required_repos_ask_ssh_or_https
local tag
tag="${BASEIMAGE_ONLY:-$(remotebase="${REMOTEBASE}" baseimage_latest_upstream_tag)}"
tag=${tag} verify_baseimage_tag
tag=${tag} build_baseimage
if [ -z "${BASEIMAGE_ONLY:-}" ]; then
tag=${tag} pin_baseimage
install_frontend_deps
if [ "${TFWDEV:-0}" == "0" ]; then
cleanup_repos
merge_repos
fi
else
delete_repos
fi
echo
if [ -z "${BASEIMAGE_ONLY:-}" ]; then
echo "You can build & start TFW by executing the command: ${CHALLENGE}/hack/tfw.sh start"
else
echo "Baseimage version ${BASEIMAGE_ONLY} built successfully!"
fi
}
handle_exit() {
if [[ $? -ne 0 ]]; then
delete_repos
showlog
fi
cleanlog
}
logged() { "$@" >> "${LOGFILE}" 2>&1; }
showlog() { echo && echo "Error! Showing logs:" && cat "${LOGFILE}"; }
cleanlog() { rm "${LOGFILE}"; }
delete_repos() { cd "${HERE}" && rm -rf "${BASEIMAGE_REPO}" "${FRONTEND_REPO}" "${CHALLENGE}"; }
check_dependencies() {
local dependencies=("git" "docker" "yarn" "ng")
local 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}" == "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:-0}" == "1" ]] && REMOTEBASE="https://github.com/avatao-content"
echo -n "Cloning TFW repositories... "
echo -n "baseimage... " && logged git clone ${REMOTEBASE}/${BASEIMAGE_REPO}.git
if [ -z "${BASEIMAGE_ONLY:-}" ]; then
echo -n "frontend... " && logged git clone ${REMOTEBASE}/${FRONTEND_REPO}.git
echo -n "test... " && logged git clone ${REMOTEBASE}/${CHALLENGE}.git
fi
echo "Done!"
}
baseimage_latest_upstream_tag() {
echo -n "$(git ls-remote --tags ${remotebase}/${BASEIMAGE_REPO}.git |
cut -f2 |
grep -oP '(?<=refs/tags/)\w+-\d{8}$' |
sort -t '-' -k2 |
tail -n 1)"
}
verify_baseimage_tag() {
pushd "${BASEIMAGE_REPO}"
local statuscode
if ! git rev-parse --quiet \
--verify \
"refs/tags/${tag}" &> /dev/null; then
logged echo "${tag} is not a valid tag!"
exit 1
fi
popd
}
pin_baseimage() {
echo -n "Pinning TFW baseimage version... "
echo -n "which is ${tag}... "
sed -i "1 s/.*/&:${tag}/" "${CHALLENGE}/solvable/Dockerfile"
echo "Done!"
}
build_baseimage() {
echo -n "Building baseimage at ${tag}... "
pushd "${BASEIMAGE_REPO}"
logged git checkout "${tag}"
spinned logged docker build -t "${BASEIMAGE_NAME}:${tag}" .
popd
echo "Done!"
}
install_frontend_deps() {
echo -n "Installing frontend dependencies... "
pushd "${FRONTEND_REPO}"
spinned logged yarn install
popd
echo "Done!"
}
cleanup_repos() {
rm -rf "${BASEIMAGE_REPO}"
rm -rf "${CHALLENGE}/.git"
rm -rf "${FRONTEND_REPO}/.git"
}
merge_repos() {
echo -n "Merging repositories... "
NESTED_FRONTEND="${CHALLENGE}/solvable/frontend"
rm -rf "${NESTED_FRONTEND}"
mv "${FRONTEND_REPO}" "${NESTED_FRONTEND}"
echo "Done!"
}
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
}
pushd() {
command pushd "$@" > /dev/null
}
popd() {
command popd "$@" > /dev/null
}
run

View File

@ -14,11 +14,11 @@ source "${libhack_dir}/common.sh"
challenge::build() {
pushd "${CHALLENGE_PATH}"
args="--build-arg NOFRONTEND=1" build_challenge_internal
args="" build_challenge_internal
popd
}
challenge::build_with_frontend() {
challenge::build_no_cache() {
pushd "${CHALLENGE_PATH}"
args="--no-cache" build_challenge_internal
popd

View File

@ -1,14 +0,0 @@
# Requires context:
# - FRONTEND_PATH: absolute path of frontend repo
# - CHALLENGE_PATH: absolute path of challenge repo
frontend::run() {
local frontend_in_challenge="${CHALLENGE_PATH}/solvable/frontend"
if [[ "$(find "${frontend_in_challenge}" | wc -l)" -gt 2 ]];then
cd "${frontend_in_challenge}"
else
cd "${FRONTEND_PATH}"
fi
yarn start
}

View File

@ -18,46 +18,23 @@ FRONTEND_PATH="${TFW_PATH}/${FRONTEND_REPO}"
source "${SCRIPT_DIR}/libhack/baseimage.sh"
source "${SCRIPT_DIR}/libhack/challenge.sh"
source "${SCRIPT_DIR}/libhack/frontend.sh"
start_challenge_and_frontend() {
trap 'exit' INT TERM
trap 'kill 0' EXIT
[[ "${RUN_FRONTEND:-1}" == "1" ]] && frontend::run &
[[ "${BUILD:-1}" == "1" ]] && challenge::build
challenge::run $@
wait
}
case ${1:-} in
start)
baseimage::build_if_exists
BUILD=1 RUN_FRONTEND=1 start_challenge_and_frontend ${@:2}
challenge::build
challenge::run ${@:2}
;;
run)
BUILD=0 RUN_FRONTEND=1 start_challenge_and_frontend ${@:2}
;;
startcontainer)
baseimage::build_if_exists
BUILD=1 RUN_FRONTEND=0 start_challenge_and_frontend ${@:2}
;;
runcontainer)
BUILD=0 RUN_FRONTEND=0 start_challenge_and_frontend ${@:2}
;;
runfrontend)
frontend::run
challenge:run ${@:2}
;;
buildtfw)
baseimage::build_if_exists
;;
build)
baseimage::build_if_exists
challenge::build
;;
buildwithfrontend)
baseimage::build_if_exists
challenge::build_with_frontend
challenge::build_no_cache
;;
releasetfw)
if [[ -d "$BASEIMAGE_PATH" ]]; then
@ -71,13 +48,10 @@ case ${1:-} in
;;
*)
echo "Usage: tfw.sh [COMMAND]"
echo " |--- start: build & run TFW challenge and Angular frontend"
echo " |--- run: run TFW challenge and Angular frontend"
echo " |--- startcontainer: build & run TFW challenge, container only"
echo " |--- runcontainer: run TFW challenge, container only"
echo " |--- start: build & run TFW challenge"
echo " |--- run: run TFW challenge"
echo " |--- buildtfw: build TFW baseimage"
echo " |--- build: build TFW baseimage and challenge"
echo " |--- buildwithfrontend: build TFW baseimage and challenge, include frontend in image"
echo " |--- releasetfw: tag TFW baseimage and push to upstream"
echo " |--- builddocs: build baseimage documentation (in docs/build/html)"
;;