From 35776e7d7f7a765d286f3268f8bfa5aacca9ea8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 15 Jun 2018 15:18:46 +0200 Subject: [PATCH] Refactor challenge building to libhack --- hack/libhack/baseimage.sh | 5 ++ hack/libhack/challenge.sh | 47 +++++++++++++++++ hack/tfw.sh | 103 +++++++++++++------------------------- 3 files changed, 86 insertions(+), 69 deletions(-) create mode 100644 hack/libhack/challenge.sh diff --git a/hack/libhack/baseimage.sh b/hack/libhack/baseimage.sh index 742a697..5dd5f8c 100644 --- a/hack/libhack/baseimage.sh +++ b/hack/libhack/baseimage.sh @@ -2,6 +2,7 @@ # - BASEIMAGE_PATH: absolute path of baseimage repo BASEIMAGE_NAME="${BASEIMAGE_NAME:-eu.gcr.io/avatao-challengestore/tutorial-framework}" + libhack_dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" source "${libhack_dir}/common.sh" @@ -20,6 +21,10 @@ baseimage::build() { popd } +baseimage::build_if_exists() { + [[ -d "$BASEIMAGE_PATH" ]] && baseimage::build_as_latest +} + baseimage::tag_as_latest() { docker tag "${BASEIMAGE_NAME}:$(releasename)" "${BASEIMAGE_NAME}:latest" } diff --git a/hack/libhack/challenge.sh b/hack/libhack/challenge.sh new file mode 100644 index 0000000..0f5ddb7 --- /dev/null +++ b/hack/libhack/challenge.sh @@ -0,0 +1,47 @@ +# Requires context: +# - TFW_PATH: absolute path of the parent directory of TFW repos +# - BASEIMAGE_PATH: absolute path of baseimage repo +# - CHALLENGE_PATH: absolute path of challenge repo + +IMAGE_NAME="${IMAGE_NAME:-"$(basename "${CHALLENGE_PATH}")"}" +BUILD_CONTEXT="${BUILD_CONTEXT:-solvable}" +CHALLENGE_PORT="${CHALLENGE_PORT:-8888}" +AVATAO_SECRET="${AVATAO_SECRET:-secret}" + +libhack_dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" +source "${libhack_dir}/common.sh" + + +challenge::build() { + pushd "${CHALLENGE_PATH}" + ARGS="--build-arg NOFRONTEND=1" build_challenge_internal + popd +} + +challenge::build_with_frontend() { + pushd "${CHALLENGE_PATH}" + ARGS="--no-cache" build_challenge_internal + popd +} + +build_challenge_internal() { + docker build -t "${IMAGE_NAME}" \ + -f "${BUILD_CONTEXT}/Dockerfile" \ + --build-arg BUILD_CONTEXT="${BUILD_CONTEXT}" \ + ${ARGS} . +} + +challenge::run() { + pushd "${TFW_PATH}" + if [ "${HOTRELOAD:-0}" == "1" ]; then + [ -d "${BASEIMAGE_PATH}" ] && mount_baseimage="-v ${BASEIMAGE_PATH}/lib/tfw:/usr/local/lib/tfw" + mount_challenge="-v ${CHALLENGE_PATH}/solvable/src:/srv/.tfw" + mount_volumes="${mount_baseimage:-} ${mount_challenge}" + fi + popd + docker run --rm \ + -p ${CHALLENGE_PORT}:${CHALLENGE_PORT} \ + ${mount_volumes:-} \ + ${@:-} \ + -e AVATAO_SECRET="${AVATAO_SECRET}" ${IMAGE_NAME} +} diff --git a/hack/tfw.sh b/hack/tfw.sh index 3f4c59a..9ebd188 100755 --- a/hack/tfw.sh +++ b/hack/tfw.sh @@ -5,117 +5,82 @@ set -o errtrace shopt -s expand_aliases [ "$(uname)" == "Darwin" ] && alias readlink="greadlink" || : -here="$(pwd)" SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" -TAO_PATH="${TAO_PATH:-$SCRIPT_DIR/../..}" +TFW_PATH="${TFW_PATH:-$SCRIPT_DIR/../..}" + BASEIMAGE_REPO="${BASEIMAGE_REPO:-baseimage-tutorial-framework}" -TEST_REPO="${TEST_REPO:-"$(basename "$(realpath "${SCRIPT_DIR}/..")")"}" +CHALLENGE_REPO="${CHALLENGE_REPO:-"$(basename "$(realpath "${SCRIPT_DIR}/..")")"}" FRONTEND_REPO="${FRONTEND_REPO:-frontend-tutorial-framework}" -BASEIMAGE_PATH="${TAO_PATH}/${BASEIMAGE_REPO}" -TEST_PATH="${TAO_PATH}/${TEST_REPO}" -FRONTEND_PATH="${TAO_PATH}/${FRONTEND_REPO}" -IMAGE_NAME="${IMAGE_NAME:-test-tutorial-framework}" -TEST_PORT="${TEST_PORT:-8888}" -AVATAO_SECRET="${AVATAO_SECRET:-secret}" -BUILD_CONTEXT="${BUILD_CONTEXT:-solvable}" +BASEIMAGE_PATH="${TFW_PATH}/${BASEIMAGE_REPO}" +CHALLENGE_PATH="${TFW_PATH}/${CHALLENGE_REPO}" +FRONTEND_PATH="${TFW_PATH}/${FRONTEND_REPO}" + source "${SCRIPT_DIR}/libhack/baseimage.sh" +source "${SCRIPT_DIR}/libhack/challenge.sh" -build_test_internal() -{ - [[ -d "$BASEIMAGE_PATH" ]] && baseimage::build_as_latest - cd $TEST_PATH - docker build -t $IMAGE_NAME \ - -f ${BUILD_CONTEXT}/Dockerfile \ - --build-arg BUILD_CONTEXT=$BUILD_CONTEXT \ - ${ARGS} . -} - -build_test() -{ - ARGS="--build-arg NOFRONTEND=1" build_test_internal -} - -build_test_withfrontend() -{ - ARGS="--no-cache" build_test_internal -} - -run_test() -{ - cd "$TAO_PATH" - if [ "${HOTRELOAD:-0}" == "1" ]; then - [ -d "$BASEIMAGE_PATH" ] && mount_baseimage="-v ${BASEIMAGE_PATH}/lib/tfw:/usr/local/lib/tfw" - mount_test="-v ${TEST_PATH}/solvable/src:/srv/.tfw" - mount_volumes="${mount_baseimage:-} ${mount_test}" - fi - docker run --rm \ - -p $TEST_PORT:$TEST_PORT \ - ${mount_volumes:-} \ - ${@:-} \ - -e AVATAO_SECRET=$AVATAO_SECRET $IMAGE_NAME -} - -run_frontend() -{ - FRONTEND_IN_TEST="${TEST_PATH}/solvable/frontend" - if [ "$(find "$FRONTEND_IN_TEST" | wc -l)" -gt 2 ] +run_frontend() { + FRONTEND_IN_CHALLENGE="${CHALLENGE_PATH}/solvable/frontend" + if [ "$(find "$FRONTEND_IN_CHALLENGE" | wc -l)" -gt 2 ] then - cd $FRONTEND_IN_TEST + cd $FRONTEND_IN_CHALLENGE else cd $FRONTEND_PATH fi yarn start } -start_test() -{ +start_challenge_and_frontend() { trap 'exit' INT TERM trap 'kill 0' EXIT [[ "${RUN_FRONTEND:-1}" == "1" ]] && run_frontend & - [[ "${BUILD:-1}" == "1" ]] && build_test - run_test $@ + [[ "${BUILD:-1}" == "1" ]] && challenge::build + challenge::run $@ wait } case ${1:-} in start) - RUN_FRONTEND=1 start_test ${@:2} + baseimage::build_if_exists + BUILD=1 RUN_FRONTEND=1 start_challenge_and_frontend ${@:2} ;; run) - BUILD=0 RUN_FRONTEND=1 start_test ${@:2} + BUILD=0 RUN_FRONTEND=1 start_challenge_and_frontend ${@:2} ;; startcontainer) - RUN_FRONTEND=0 start_test ${@:2} + baseimage::build_if_exists + BUILD=1 RUN_FRONTEND=0 start_challenge_and_frontend ${@:2} ;; runcontainer) - BUILD=0 RUN_FRONTEND=0 start_test ${@:2} + BUILD=0 RUN_FRONTEND=0 start_challenge_and_frontend ${@:2} ;; buildtfw) - [[ -d "$BASEIMAGE_PATH" ]] && baseimage::build_as_latest + baseimage::build_if_exists ;; build) - build_test + baseimage::build_if_exists + challenge::build ;; buildwithfrontend) - build_test_withfrontend + baseimage::build_if_exists + challenge::build_with_frontend ;; releasetfw) [[ -d "$BASEIMAGE_PATH" ]] && baseimage::release ;; builddocs) - baseimage::builddocs + [[ -d "$BASEIMAGE_PATH" ]] && baseimage::builddocs ;; *) - echo "Usage: tfw.sh [start|buildtfw|build|buildwithfrontend|releasetfw]" - echo " |--- start: build & run TFW test and Angular frontend" - echo " |--- run: run TFW test and Angular frontend" - echo " |--- startcontainer: build & run TFW test, container only" - echo " |--- runcontainer: run TFW test, container only" + 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 " |--- buildtfw: build TFW baseimage" - echo " |--- build: build TFW baseimage and test" - echo " |--- buildwithfrontend: build TFW baseimage and test, include frontend in image" + 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)" ;;