From 0facefdb89942057b18df78cdc061fa94d407da0 Mon Sep 17 00:00:00 2001 From: Victor M Date: Fri, 26 May 2023 22:05:05 +0200 Subject: [PATCH 1/3] Housekeeping --- bootstrap.sh | 116 +++++++++++++++++++++++++-------------------------- 1 file changed, 57 insertions(+), 59 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index fd541a2c..fa9d3fc3 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -2,49 +2,72 @@ set -e -# check that unit_test.sh complies w/ best practices -URL="https://github.com/RedHatInsights/cicd-tools/tree/main/examples" -if test -f unit_test.sh; then - if grep 'exit $result' unit_test.sh; then - echo "----------------------------" - echo "ERROR: unit_test.sh is calling 'exit' improperly, refer to examples at $URL" - echo "----------------------------" - exit 1 +# Gives access to helper commands such as "oc_wrapper" +add_cicd_bin_to_path() { + if ! command -v oc_wrapper; then + export PATH=$PATH:${CICD_ROOT}/bin fi -fi +} + +check_available_server() { + echo "Checking connectivity to ephemeral cluster ..." + (curl -s $OC_LOGIN_SERVER > /dev/null) + RET_CODE=$? + if [ $RET_CODE -ge 1 ]; then + echo "Connectivity check failed" + fi + return $RET_CODE +} + +# Hotswap based on availability +login_to_available_server() { + if check_available_server; then + # log in to ephemeral cluster + oc_wrapper login --token=$OC_LOGIN_TOKEN --server=$OC_LOGIN_SERVER + echo "logging in to Ephemeral cluster" + else + # switch to crcd cluster + oc_wrapper login --token=$OC_LOGIN_TOKEN_DEV --server=$OC_LOGIN_SERVER_DEV + echo "logging in to CRCD cluster" + fi +} + +check_unit_test_script() { + + # check that unit_test.sh complies w/ best practices + local URL="https://github.com/RedHatInsights/cicd-tools/tree/main/examples" + + if test -f unit_test.sh; then + if grep 'exit $result' unit_test.sh; then + echo "----------------------------" + echo "ERROR: unit_test.sh is calling 'exit' improperly, refer to examples at $URL" + echo "----------------------------" + exit 1 + fi + fi +} export APP_ROOT=$(pwd) export WORKSPACE=${WORKSPACE:-$APP_ROOT} # if running in jenkins, use the build's workspace -export BONFIRE_ROOT=${WORKSPACE}/.bonfire -export CICD_ROOT=${BONFIRE_ROOT} +export CICD_ROOT=${WORKSPACE}/.cicd-tools export IMAGE_TAG=$(git rev-parse --short=7 HEAD) export BONFIRE_BOT="true" export BONFIRE_NS_REQUESTER="${JOB_NAME}-${BUILD_NUMBER}" -# which branch to fetch cicd scripts from in bonfire repo -export BONFIRE_REPO_BRANCH="${BONFIRE_REPO_BRANCH:-main}" -export BONFIRE_REPO_ORG="${BONFIRE_REPO_ORG:-RedHatInsights}" +# which branch to fetch cicd scripts from in cicd-tools repo +export CICD_REPO_BRANCH="${CICD_REPO_BRANCH:-main}" +export CICD_REPO_ORG="${CICD_REPO_ORG:-RedHatInsights}" -set -x # Set up docker cfg export DOCKER_CONFIG="$WORKSPACE/.docker" rm -fr $DOCKER_CONFIG mkdir $DOCKER_CONFIG -# Set up podman cfg -# No longer needed due to podman now using the DOCKER_CONFIG -#AUTH_CONF_DIR="$WORKSPACE/.podman" -#rm -fr $AUTH_CONF_DIR -#mkdir $AUTH_CONF_DIR -#export REGISTRY_AUTH_FILE="$AUTH_CONF_DIR/auth.json" - # Set up kube cfg export KUBECONFIG_DIR="$WORKSPACE/.kube" export KUBECONFIG="$KUBECONFIG_DIR/config" rm -fr $KUBECONFIG_DIR mkdir $KUBECONFIG_DIR -set +x - # if this is a PR, use a different tag, since PR tags expire if [ ! -z "$ghprbPullId" ]; then export IMAGE_TAG="pr-${ghprbPullId}-${IMAGE_TAG}" @@ -54,7 +77,6 @@ if [ ! -z "$gitlabMergeRequestIid" ]; then export IMAGE_TAG="pr-${gitlabMergeRequestIid}-${IMAGE_TAG}" fi - export GIT_COMMIT=$(git rev-parse HEAD) export ARTIFACTS_DIR="$WORKSPACE/artifacts" @@ -64,47 +86,23 @@ rm -fr $ARTIFACTS_DIR && mkdir -p $ARTIFACTS_DIR export LANG=en_US.utf-8 export LC_ALL=en_US.utf-8 -python3 -m venv .bonfire_venv -source .bonfire_venv/bin/activate +# TODO: decide removal +check_unit_test_script + +# clone repo to download cicd scripts +rm -fr "$CICD_ROOT" +echo "Fetching branch '$CICD_REPO_BRANCH' of https://github.com/${CICD_REPO_ORG}/cicd-tools.git" +git clone --branch "$CICD_REPO_BRANCH" "https://github.com/${CICD_REPO_ORG}/cicd-tools.git" "$CICD_ROOT" + +python3 -m venv "${CICD_ROOT}/.bonfire_venv" +source "${CICD_ROOT}/.bonfire_venv/bin/activate" python3 -m pip install --upgrade pip 'setuptools<58' wheel python3 -m pip install --upgrade 'crc-bonfire>=4.10.4' -# clone repo to download cicd scripts -rm -fr $BONFIRE_ROOT -echo "Fetching branch '$BONFIRE_REPO_BRANCH' of https://github.com/${BONFIRE_REPO_ORG}/cicd-tools.git" -git clone --branch "$BONFIRE_REPO_BRANCH" "https://github.com/${BONFIRE_REPO_ORG}/cicd-tools.git" "$BONFIRE_ROOT" - # Do a docker login to ensure our later 'docker pull' calls have an auth file created source ${CICD_ROOT}/_common_container_logic.sh login -# Gives access to helper commands such as "oc_wrapper" -add_cicd_bin_to_path() { - if ! command -v oc_wrapper; then export PATH=$PATH:${CICD_ROOT}/bin; fi -} - -check_available_server() { - echo "Checking connectivity to ephemeral cluster ..." - (curl -s $OC_LOGIN_SERVER > /dev/null) - RET_CODE=$? - if [ $RET_CODE -ge 1 ]; then echo "Connectivity check failed"; fi - return $RET_CODE -} - -# Hotswap based on availability -login_to_available_server() { - if check_available_server; then - # log in to ephemeral cluster - oc_wrapper login --token=$OC_LOGIN_TOKEN --server=$OC_LOGIN_SERVER - echo "logging in to Ephemeral cluster" - else - # switch to crcd cluster - oc_wrapper login --token=$OC_LOGIN_TOKEN_DEV --server=$OC_LOGIN_SERVER_DEV - echo "logging in to CRCD cluster" - fi -} - add_cicd_bin_to_path - login_to_available_server From edf0e4fb1292ee9ba74ed4853a5acfce4971e512 Mon Sep 17 00:00:00 2001 From: Victor M Date: Sat, 27 May 2023 00:03:12 +0200 Subject: [PATCH 2/3] second pass --- bootstrap.sh | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index fa9d3fc3..121d84bf 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -47,6 +47,9 @@ check_unit_test_script() { fi } +# TODO: create custom jenkins agent image that has a lot of this stuff pre-installed +export LANG=en_US.utf-8 +export LC_ALL=en_US.utf-8 export APP_ROOT=$(pwd) export WORKSPACE=${WORKSPACE:-$APP_ROOT} # if running in jenkins, use the build's workspace export CICD_ROOT=${WORKSPACE}/.cicd-tools @@ -56,17 +59,11 @@ export BONFIRE_NS_REQUESTER="${JOB_NAME}-${BUILD_NUMBER}" # which branch to fetch cicd scripts from in cicd-tools repo export CICD_REPO_BRANCH="${CICD_REPO_BRANCH:-main}" export CICD_REPO_ORG="${CICD_REPO_ORG:-RedHatInsights}" - -# Set up docker cfg export DOCKER_CONFIG="$WORKSPACE/.docker" -rm -fr $DOCKER_CONFIG -mkdir $DOCKER_CONFIG - -# Set up kube cfg export KUBECONFIG_DIR="$WORKSPACE/.kube" export KUBECONFIG="$KUBECONFIG_DIR/config" -rm -fr $KUBECONFIG_DIR -mkdir $KUBECONFIG_DIR +export GIT_COMMIT=$(git rev-parse HEAD) +export ARTIFACTS_DIR="$WORKSPACE/artifacts" # if this is a PR, use a different tag, since PR tags expire if [ ! -z "$ghprbPullId" ]; then @@ -77,17 +74,18 @@ if [ ! -z "$gitlabMergeRequestIid" ]; then export IMAGE_TAG="pr-${gitlabMergeRequestIid}-${IMAGE_TAG}" fi -export GIT_COMMIT=$(git rev-parse HEAD) -export ARTIFACTS_DIR="$WORKSPACE/artifacts" +# TODO: decide removal +check_unit_test_script -rm -fr $ARTIFACTS_DIR && mkdir -p $ARTIFACTS_DIR +# Set up docker cfg +rm -fr $DOCKER_CONFIG +mkdir $DOCKER_CONFIG -# TODO: create custom jenkins agent image that has a lot of this stuff pre-installed -export LANG=en_US.utf-8 -export LC_ALL=en_US.utf-8 +# Set up kube cfg +rm -fr $KUBECONFIG_DIR +mkdir $KUBECONFIG_DIR -# TODO: decide removal -check_unit_test_script +rm -fr $ARTIFACTS_DIR && mkdir -p $ARTIFACTS_DIR # clone repo to download cicd scripts rm -fr "$CICD_ROOT" From 98e9302fdf975812468d15c45628993c6b462fd1 Mon Sep 17 00:00:00 2001 From: Victor M Date: Mon, 29 May 2023 13:01:47 +0200 Subject: [PATCH 3/3] refactor of check_available_server --- bootstrap.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index 121d84bf..608f1dda 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -11,12 +11,10 @@ add_cicd_bin_to_path() { check_available_server() { echo "Checking connectivity to ephemeral cluster ..." - (curl -s $OC_LOGIN_SERVER > /dev/null) - RET_CODE=$? - if [ $RET_CODE -ge 1 ]; then + if ! curl -s $OC_LOGIN_SERVER > /dev/null; then echo "Connectivity check failed" + return 1 fi - return $RET_CODE } # Hotswap based on availability