diff --git a/.dockerignore b/.dockerignore index dc61a674e..5842871b3 100644 --- a/.dockerignore +++ b/.dockerignore @@ -13,10 +13,11 @@ charts/ # Prevent copying scripts that are unused in docker scripts/ -!scripts/test.sh !scripts/activate-venv.sh +!scripts/get-root.sh !scripts/install-build-tools.sh !scripts/setup-dependencies.sh +!scripts/test.sh !scripts/test-transaction.sh !scripts/wait-for-it.sh diff --git a/Dockerfile b/Dockerfile index 55d5c288d..753dd0783 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,7 @@ RUN mkdir -p /opt/tx-processor/scripts COPY requirements.txt /opt/tx-processor/requirements.txt COPY scripts/activate-venv.sh /opt/tx-processor/scripts/activate-venv.sh +COPY scripts/get-root.sh /opt/tx-processor/scripts/get-root.sh COPY scripts/install-build-tools.sh /opt/tx-processor/scripts/install-build-tools.sh COPY scripts/setup-dependencies.sh /opt/tx-processor/scripts/setup-dependencies.sh diff --git a/scripts/benchmarks.sh b/scripts/benchmarks.sh index 030f4f901..31ae20c55 100755 --- a/scripts/benchmarks.sh +++ b/scripts/benchmarks.sh @@ -62,8 +62,8 @@ done # environment variable, assume it's named 'build' and is located in the # top-level directory of the repo. By defining the top-level directory relative # to the location of this script, the user can run this script from any folder. -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -REPO_TOP_DIR="${SCRIPT_DIR}/.." +SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +REPO_TOP_DIR="$( "$SCRIPT_DIR"/get-root.sh )" if [[ -z "${BUILD_DIR+x}" ]] then BUILD_DIR="${REPO_TOP_DIR}/build" diff --git a/scripts/build-docker.sh b/scripts/build-docker.sh index 68ec4b0d0..10cc230e5 100755 --- a/scripts/build-docker.sh +++ b/scripts/build-docker.sh @@ -2,7 +2,8 @@ set -e -SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +ROOT="$( "$SCRIPT_DIR"/get-root.sh )" DOCKER_IMAGE_TAG_BASE=${DOCKER_IMAGE_TAG:-opencbdc-tx-base} DOCKER_IMAGE_TAG_BUILDER=${DOCKER_IMAGE_TAG:-opencbdc-tx-builder} DOCKER_IMAGE_TAG_ATOMIZER=${DOCKER_IMAGE_TAG:-opencbdc-tx-atomizer} @@ -12,7 +13,7 @@ DOCKER_IMAGE_TAG_TWOPHASE=${DOCKER_IMAGE_TAG:-opencbdc-tx-twophase} git submodule init && git submodule update # Build docker image -docker build --target base -t $DOCKER_IMAGE_TAG_BASE -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/.. -docker build --target builder --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_BUILDER -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/.. -docker build --target twophase --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_TWOPHASE -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/.. -docker build --target atomizer --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_ATOMIZER -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/.. +docker build --target base -t $DOCKER_IMAGE_TAG_BASE -f "$ROOT"/Dockerfile "$ROOT" +docker build --target builder --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_BUILDER -f "$ROOT"/Dockerfile "$ROOT" +docker build --target twophase --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_TWOPHASE -f "$ROOT"/Dockerfile "$ROOT" +docker build --target atomizer --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_ATOMIZER -f "$ROOT"/Dockerfile "$ROOT" diff --git a/scripts/build.sh b/scripts/build.sh index 90d07991d..63cede0a5 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -33,8 +33,10 @@ fi echo "Building..." +SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +ROOT="$( "$SCRIPT_DIR"/get-root.sh )" # see PREFIX in ./scripts/setup-dependencies.sh -PREFIX="$(cd "$(dirname "$0")"/.. && pwd)/prefix" +PREFIX=""$ROOT"/prefix" if [ -z ${BUILD_DIR+x} ]; then export BUILD_DIR=build diff --git a/scripts/get-root.sh b/scripts/get-root.sh new file mode 100755 index 000000000..d1b658505 --- /dev/null +++ b/scripts/get-root.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set -e + +# calling scripts can use this script to capture the project ROOT dir +# SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +# ROOT="$( "$SCRIPT_DIR"/get-root.sh )" + +# returns the fullpath to the ROOT dir without a slash at the end +ROOT= +# if we are in a git repository +if git rev-parse --show-toplevel >/dev/null 2>&1; then + ROOT="$( git rev-parse --show-toplevel )" +else + # $BASH_SOURCE[0] is the full path of the script being executed + SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )" + # scripts dir will always be one level below ROOT level: "$ROOT"/scripts + ROOT="$( cd -- "$SCRIPT_DIR"/.. && pwd )" +fi + +# check for directory existence +if [[ ! -d "$ROOT" ]]; then + echo "Error: ROOT '${ROOT}' not found." + exit 1 +fi + +# use other scripts in the script dir to capture the ROOT fullpath +printf "%s\n" "$ROOT" + diff --git a/scripts/install-build-tools.sh b/scripts/install-build-tools.sh index 523789d99..94052b0e0 100755 --- a/scripts/install-build-tools.sh +++ b/scripts/install-build-tools.sh @@ -30,7 +30,8 @@ for PY_VERS in "${PYTHON_VERSIONS[@]}"; do fi done -ROOT="$(cd "$(dirname "$0")"/.. && pwd)" +SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +ROOT="$( "$SCRIPT_DIR"/get-root.sh )" ENV_NAME=".py_venv" # make a virtual environement to install python packages diff --git a/scripts/native-system-benchmark.sh b/scripts/native-system-benchmark.sh index 1b7923fa2..391ce7f53 100755 --- a/scripts/native-system-benchmark.sh +++ b/scripts/native-system-benchmark.sh @@ -10,8 +10,8 @@ DBG="${DBG:-$GDB}" DURATION=30 CWD=$(pwd) COMMIT=$(git rev-parse --short HEAD) -TL=$(git rev-parse --show-toplevel) -RT="${TL:-$CWD}" +SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +RT="$( "$SCRIPT_DIR"/get-root.sh )" BLD="$RT"/build SEEDDIR="$BLD"/preseeds TESTDIR="$BLD"/test-$(date +"%s") diff --git a/scripts/pylint.sh b/scripts/pylint.sh index 9c22df164..c0a42e249 100755 --- a/scripts/pylint.sh +++ b/scripts/pylint.sh @@ -1,50 +1,51 @@ #!/usr/bin/env bash -ROOT="$(cd "$(dirname "$0")"/.. && pwd)" +SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +ROOT="$( "$SCRIPT_DIR"/get-root.sh )" PREFIX="${ROOT}"/prefix MIN_CODE_QUALITY=8.0 get_code_score() { - if [ -n "$1" ]; then - # set minimum quality to user input (int/float) if provided and (5.0 <= input <= 10.0) - if [[ $1 =~ ^([0-9]+)*([\.][0-9])?$ ]]; then - if (( $(echo "$1 >= 5.0" | bc -l) )) && (( $(echo "$1 <= 10.0" | bc -l) )); then - MIN_CODE_QUALITY=$1 - else - # In the future, we want code quality to be at minimum 8.0/10.0 - echo "Code quality score must be between 5.0 and 10.0, inclusive." - echo "Recommended code quality score is >= 8.0." - exit 1 - fi - else - echo "Code quality score must be an integer or floating point number." - exit 1 + if [ -n "$1" ]; then + # set minimum quality to user input (int/float) if provided and (5.0 <= input <= 10.0) + if [[ $1 =~ ^([0-9]+)*([\.][0-9])?$ ]]; then + if (( $(echo "$1 >= 5.0" | bc -l) )) && (( $(echo "$1 <= 10.0" | bc -l) )); then + MIN_CODE_QUALITY=$1 + else + # In the future, we want code quality to be at minimum 8.0/10.0 + echo "Code quality score must be between 5.0 and 10.0, inclusive." + echo "Recommended code quality score is >= 8.0." + exit 1 + fi + else + echo "Code quality score must be an integer or floating point number." + exit 1 + fi fi - fi - echo "Linting Python code with minimum quality of $MIN_CODE_QUALITY/10.0..." + echo "Linting Python code with minimum quality of $MIN_CODE_QUALITY/10.0..." } check_pylint() { - if ! command -v pylint &>/dev/null; then - echo "pylint is not installed." - echo "Run 'sudo ./scripts/install-build-tools.sh' to install pylint." - exit 1 - fi + if ! command -v pylint &>/dev/null; then + echo "pylint is not installed." + echo "Run 'sudo ./scripts/install-build-tools.sh' to install pylint." + exit 1 + fi } get_code_score $1 if source "${ROOT}/scripts/activate-venv.sh"; then - echo "Virtual environment activated." + echo "Virtual environment activated." else - echo "Failed to activate virtual environment." - exit 1 + echo "Failed to activate virtual environment." + exit 1 fi check_pylint if ! pylint scripts src tests tools --rcfile=.pylintrc \ - --fail-under=$MIN_CODE_QUALITY $(git ls-files '*.py'); then + --fail-under=$MIN_CODE_QUALITY $(git ls-files '*.py'); then echo "Linting failed, please fix the issues and rerun." - exit 1 + exit 1 else - echo "Linting passed." + echo "Linting passed." fi diff --git a/scripts/setup-dependencies.sh b/scripts/setup-dependencies.sh index ea4b4c771..3ddab1c12 100755 --- a/scripts/setup-dependencies.sh +++ b/scripts/setup-dependencies.sh @@ -10,7 +10,9 @@ set -e # install in a custom prefix rather than /usr/local. by default, this # chooses "prefix" directory alongside "scripts" directory. -PREFIX="$(cd "$(dirname "$0")"/.. && pwd)/prefix" +SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +ROOT="$( "$SCRIPT_DIR"/get-root.sh )" +PREFIX="$ROOT"/prefix echo "Will install local dependencies in the following prefix: $PREFIX" mkdir -p "$PREFIX"/{lib,include} diff --git a/scripts/test-e2e-minikube.sh b/scripts/test-e2e-minikube.sh index d25900163..62366517e 100755 --- a/scripts/test-e2e-minikube.sh +++ b/scripts/test-e2e-minikube.sh @@ -1,7 +1,8 @@ #!/usr/bin/env bash set -e -SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +ROOT="$( "$SCRIPT_DIR"/get-root.sh )" MINIKUBE_PROFILE=${MINIKUBE_PROFILE:-opencbdc} BUILD_DOCKER=${TESTRUN_BUILD_DOCKER:-1} @@ -36,13 +37,13 @@ if [[ $BUILD_DOCKER -eq 1 ]]; then fi # Change to the test directory and fetch dependencies -cd $SCRIPT_DIR/../charts/tests +cd "$ROOT"/charts/tests echo "🔄 Downloading Go dependencies for testing..." go mod download -x # Set testrun_id and path to store logs from testrun and containers TESTRUN_ID=${TESTRUN_ID:-$(uuidgen)} -TESTRUN_PATH=$SCRIPT_DIR/../testruns/$TESTRUN_ID +TESTRUN_PATH="$ROOT"/testruns/$TESTRUN_ID TESTRUN_LOG_PATH="$TESTRUN_PATH/testrun.log" mkdir -p $TESTRUN_PATH diff --git a/scripts/test.sh b/scripts/test.sh index 615a8acb5..568e143f8 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -114,7 +114,7 @@ done # top-level directory of the repo. By defining the top-level directory relative # to the location of this script, the user can run this script from any folder. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -REPO_TOP_DIR="${SCRIPT_DIR}/.." +REPO_TOP_DIR="$( "$SCRIPT_DIR"/get-root.sh )" if [[ -z "${BUILD_DIR+x}" ]] then BUILD_DIR="${REPO_TOP_DIR}/build"