From d86f6affac7edc62db3c02aa5b17339364753d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 12 Jun 2018 14:31:27 +0200 Subject: [PATCH] Refactor baseimage related stuff to libhack/baseimage.sh --- hack/libhack/baseimage.sh | 91 +++++++++++++++++++++++++++++++++++++++ hack/libhack/common.sh | 7 +++ hack/tfw.sh | 80 ++-------------------------------- 3 files changed, 102 insertions(+), 76 deletions(-) create mode 100644 hack/libhack/baseimage.sh create mode 100644 hack/libhack/common.sh diff --git a/hack/libhack/baseimage.sh b/hack/libhack/baseimage.sh new file mode 100644 index 0000000..486659d --- /dev/null +++ b/hack/libhack/baseimage.sh @@ -0,0 +1,91 @@ +# Requires context: +# - 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" + + +baseimage::build_as_latest() { + pushd "${BASEIMAGE_PATH}" + baseimage::build + baseimage::tag_as_latest + popd +} + +baseimage::build() { + pushd "${BASEIMAGE_PATH}" + docker build -t "${BASEIMAGE_NAME}:$(releasename)" \ + . + popd +} + +baseimage::tag_as_latest() { + docker tag "${BASEIMAGE_NAME}:$(releasename)" "${BASEIMAGE_NAME}:latest" +} + +baseimage::release() { + pushd "${BASEIMAGE_PATH}" + check_drone_releasename + + local tag + tag="$(releasename)" + read -p "Tag and push new TFW version \"${tag}\"? [y/N]" -r && echo + if [[ $REPLY =~ ^(y|Y|yes|Yes|YES)$ ]] + then + prompt_for_tag_description # This command sets $description + tag="${tag}" description="${description}" force_push_tag + fi + popd +} + +releasename() { + local version + local date + version="$(cat VERSION | head -n 1)" + date="$(date +%Y%m%d)" + printf "${version}-${date}" +} + +check_drone_releasename() { + local drone_releasename + local actual_releasename + drone_releasename="$(grep -oP '(?<=branch:\srefs/tags/).+(?=-.+)' .drone.yml)" + actual_releasename="$(baseimage_releasename | grep -oP ".+(?=-\d{8})")" + if [ "${drone_releasename}" != "${actual_releasename}" ]; then + echo "Release name '${drone_releasename}' in .drone.yml does not match \ + actual release name '${actual_releasename}' in VERSION." + echo "Please make them match before releasing!" + exit 1 + fi +} + +prompt_for_tag_description() { + local editor + local tempfile + editor="$(git config --global core.editor)" + tempfile="$(mktemp)" + + printf "\n\n" >> "${tempfile}" + echo "# Please enter the description for this release." >> "${tempfile}" + echo "# Lines starting with '#' will be ignored." >> "${tempfile}" + echo "# An empty description aborts the release." >> "${tempfile}" + + ${editor} "${tempfile}" + sed -i -e 's/#.*$//' -e '/^$/d' "${tempfile}" + sed -i 's/\n/ /g' "${tempfile}" + if [[ ! -s "${tempfile}" ]]; then + echo "Aborting release due to empty description." + exit 1 + fi + + description="$(tr '\n' ' ' < "${tempfile}")" + rm "${tempfile}" +} + +force_push_tag() { + git tag -d "${tag}" > /dev/null 2>&1 || : + git push --delete origin "${tag}" > /dev/null 2>&1 || : + git tag -s -m "${description}" "${tag}" + git push origin "${tag}" +} diff --git a/hack/libhack/common.sh b/hack/libhack/common.sh new file mode 100644 index 0000000..95a908b --- /dev/null +++ b/hack/libhack/common.sh @@ -0,0 +1,7 @@ +pushd() { + command pushd "$@" > /dev/null +} + +popd() { + command popd "$@" > /dev/null +} diff --git a/hack/tfw.sh b/hack/tfw.sh index dad09a4..c75d28d 100755 --- a/hack/tfw.sh +++ b/hack/tfw.sh @@ -15,88 +15,16 @@ BASEIMAGE_PATH="${TAO_PATH}/${BASEIMAGE_REPO}" TEST_PATH="${TAO_PATH}/${TEST_REPO}" FRONTEND_PATH="${TAO_PATH}/${FRONTEND_REPO}" -BASEIMAGE_NAME="${BASEIMAGE_NAME:-eu.gcr.io/avatao-challengestore/tutorial-framework}" IMAGE_NAME="${IMAGE_NAME:-test-tutorial-framework}" TEST_PORT="${TEST_PORT:-8888}" AVATAO_SECRET="${AVATAO_SECRET:-secret}" BUILD_CONTEXT="${BUILD_CONTEXT:-solvable}" +source "${SCRIPT_DIR}/libhack/baseimage.sh" -build_baseimage() -{ - [ ! -d "$BASEIMAGE_PATH" ] && return || : - cd $BASEIMAGE_PATH - docker build -t "${BASEIMAGE_NAME}:${TFWTAG:-$(baseimage_releasename)}" \ - -t "${BASEIMAGE_NAME}:latest" \ - . -} - -release_baseimage() -{ - [ ! -d "$BASEIMAGE_PATH" ] && return || : - cd $BASEIMAGE_PATH - check_drone_releasename - - TAG="$(baseimage_releasename)" - read -p "Tag and push new TFW version \"${TAG}\"? [y/N]" -r && echo - if [[ $REPLY =~ ^(y|Y|yes|Yes|YES)$ ]] - then - description="" - prompt_for_tag_description # This command overwrites $description - TAG="$TAG" DESCRIPTION="$description" force_push_tag - fi -} - -check_drone_releasename() -{ - DRONEYML_RELEASENAME="$(grep -oP '(?<=branch:\srefs/tags/).+(?=-.+)' .drone.yml)" - ACTUAL_RELEASENAME="$(baseimage_releasename | grep -oP ".+(?=-\d{8})")" - if [ "$DRONEYML_RELEASENAME" != "$ACTUAL_RELEASENAME" ]; then - echo "Release name '${DRONEYML_RELEASENAME}' in .drone.yml does not match actual release name '${ACTUAL_RELEASENAME}' in VERSION." - echo "Please make them match before releasing!" - exit 1 - fi -} - -prompt_for_tag_description() -{ - editor="$(git config --global core.editor)" - tempfile="$(mktemp)" - - printf "\n\n" >> "$tempfile" - echo "# Please enter the description for this release." >> "$tempfile" - echo "# Lines starting with '#' will be ignored." >> "$tempfile" - echo "# An empty description aborts the release." >> "$tempfile" - - $editor "$tempfile" - sed -i -e 's/#.*$//' -e '/^$/d' "$tempfile" - sed -i 's/\n/ /g' "$tempfile" - if [[ ! -s "$tempfile" ]]; then - echo "Aborting release due to empty description." - exit 1 - fi - - description="$(tr '\n' ' ' < "$tempfile")" - rm "$tempfile" -} - -force_push_tag() -{ - git tag -d "$TAG" > /dev/null 2>&1 || : - git push --delete origin "$TAG" > /dev/null 2>&1 || : - git tag -s -m "$DESCRIPTION" "$TAG" - git push origin "$TAG" -} - -baseimage_releasename() -{ - VERSION="$(cat VERSION | head -n 1)" - DATE="$(date +%Y%m%d)" - printf "${VERSION}-${DATE}" -} build_test_internal() { - build_baseimage + [[ -d "$BASEIMAGE_PATH" ]] && baseimage::build_as_latest cd $TEST_PATH docker build -t $IMAGE_NAME \ -f ${BUILD_CONTEXT}/Dockerfile \ @@ -187,7 +115,7 @@ case ${1:-} in BUILD=0 RUN_FRONTEND=0 start_test ${@:2} ;; buildtfw) - build_baseimage + [[ -d "$BASEIMAGE_PATH" ]] && baseimage::build_as_latest ;; build) build_test @@ -196,7 +124,7 @@ case ${1:-} in build_test_withfrontend ;; releasetfw) - release_baseimage + [[ -d "$BASEIMAGE_PATH" ]] && baseimage::release ;; builddocs) build_docs