diff --git a/Makefile b/Makefile index 0e6c6e38..ed5503bd 100644 --- a/Makefile +++ b/Makefile @@ -77,10 +77,10 @@ openapi/validate: bin/openapi-generator-cli # generate the openapi server implementation .PHONY: gen/openapi-server gen/openapi-server: bin/openapi-generator-cli openapi/validate - @if git diff --exit-code --name-only | grep -q "api/openapi/model-registry.yaml" || \ + @if git diff --cached --exit-code --name-only | grep -q "api/openapi/model-registry.yaml" || \ git diff --exit-code --name-only | grep -q "api/openapi/model-registry.yaml" || \ [ -n "${FORCE_SERVER_GENERATION}" ]; then \ - ROOT_FOLDER="." ./scripts/gen_openapi_server.sh; \ + ./scripts/gen_openapi_server.sh; \ else \ echo "INFO api/openapi/model-registry.yaml is not staged or modified, will not re-generate server"; \ fi diff --git a/scripts/build_deploy.sh b/scripts/build_deploy.sh index 3a3dbf37..91fc4914 100755 --- a/scripts/build_deploy.sh +++ b/scripts/build_deploy.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e @@ -25,48 +25,51 @@ PUSH_IMAGE="${PUSH_IMAGE:-false}" SKIP_IF_EXISTING="${SKIP_IF_EXISTING:-false}" # assure docker exists -docker -v foo >/dev/null 2>&1 || { echo >&2 "::error:: Docker is required. Aborting."; exit 1; } +docker -v foo >/dev/null 2>&1 || { + echo >&2 "::error:: Docker is required. Aborting." + exit 1 +} # if quay.io, can opt to skip if image already existing if [[ "${SKIP_IF_EXISTING,,}" == "true" && "${IMG_REGISTRY,,}" == "quay.io" ]]; then - TAGS=$(curl --request GET "https://$IMG_REGISTRY/api/v1/repository/${IMG_ORG}/${IMG_REPO}/tag/?specificTag=${VERSION}") - LATEST_TAG_HAS_END_TS=$(echo $TAGS | jq .tags - | jq 'sort_by(.start_ts) | reverse' | jq '.[0].end_ts') - NOT_EMPTY=$(echo ${TAGS} | jq .tags - | jq any) + TAGS=$(curl --request GET "https://$IMG_REGISTRY/api/v1/repository/${IMG_ORG}/${IMG_REPO}/tag/?specificTag=${VERSION}") + LATEST_TAG_HAS_END_TS=$(echo "$TAGS" | jq .tags - | jq 'sort_by(.start_ts) | reverse' | jq '.[0].end_ts') + NOT_EMPTY=$(echo "${TAGS}" | jq .tags - | jq any) - # Image only exists if there is a tag that does not have "end_ts" (i.e. it is still present). - if [[ "$NOT_EMPTY" == "true" && $LATEST_TAG_HAS_END_TS == "null" ]]; then - echo "::error:: The image ${IMG_ORG}/${IMG_REPO}:${VERSION} already exists" - exit 1 - else - echo "Image does not exist...proceeding with build & push." - fi + # Image only exists if there is a tag that does not have "end_ts" (i.e. it is still present). + if [[ "$NOT_EMPTY" == "true" && $LATEST_TAG_HAS_END_TS == "null" ]]; then + echo "::error:: The image ${IMG_ORG}/${IMG_REPO}:${VERSION} already exists" + exit 1 + else + echo "Image does not exist...proceeding with build & push." + fi fi # build docker image, login is not required at this step if [[ "${BUILD_IMAGE,,}" == "true" ]]; then - echo "Building container image.." - make \ - IMG_REGISTRY="${IMG_REGISTRY}" \ - IMG_ORG="${IMG_ORG}" \ - IMG_REPO="${IMG_REPO}" \ - IMG_VERSION="${VERSION}" \ - image/build + echo "Building container image.." + make \ + IMG_REGISTRY="${IMG_REGISTRY}" \ + IMG_ORG="${IMG_ORG}" \ + IMG_REPO="${IMG_REPO}" \ + IMG_VERSION="${VERSION}" \ + image/build else - echo "Skip container image build." + echo "Skip container image build." fi # push container image to registry, requires login if [[ "${PUSH_IMAGE,,}" == "true" ]]; then - echo "Pushing container image.." - make \ - IMG_REGISTRY="${IMG_REGISTRY}" \ - IMG_ORG="${IMG_ORG}" \ - IMG_REPO="${IMG_REPO}" \ - IMG_VERSION="${VERSION}" \ - DOCKER_USER="${DOCKER_USER}"\ - DOCKER_PWD="${DOCKER_PWD}" \ - docker/login \ - image/push + echo "Pushing container image.." + make \ + IMG_REGISTRY="${IMG_REGISTRY}" \ + IMG_ORG="${IMG_ORG}" \ + IMG_REPO="${IMG_REPO}" \ + IMG_VERSION="${VERSION}" \ + DOCKER_USER="${DOCKER_USER}" \ + DOCKER_PWD="${DOCKER_PWD}" \ + docker/login \ + image/push else - echo "Skip container image push." + echo "Skip container image push." fi diff --git a/scripts/gen_openapi_server.sh b/scripts/gen_openapi_server.sh index 94ce1493..00de5010 100755 --- a/scripts/gen_openapi_server.sh +++ b/scripts/gen_openapi_server.sh @@ -1,28 +1,28 @@ -#!/bin/bash +#!/usr/bin/env bash set -e echo "Generating the OpenAPI server" -ROOT_FOLDER="${ROOT_FOLDER:-..}" +PROJECT_ROOT=$(realpath "$(dirname "$0")"/..) openapi-generator-cli generate \ - -i $ROOT_FOLDER/api/openapi/model-registry.yaml -g go-server -o $ROOT_FOLDER/internal/server/openapi --package-name openapi --global-property models,apis \ - --ignore-file-override $ROOT_FOLDER/.openapi-generator-ignore --additional-properties=outputAsLibrary=true,enumClassPrefix=true,router=chi,sourceFolder=,onlyInterfaces=true,isGoSubmodule=true,enumClassPrefix=true,useOneOfDiscriminatorLookup=true \ - --template-dir $ROOT_FOLDER/templates/go-server + -i "$PROJECT_ROOT"/api/openapi/model-registry.yaml -g go-server -o "$PROJECT_ROOT"/internal/server/openapi --package-name openapi --global-property models,apis \ + --ignore-file-override "$PROJECT_ROOT"/.openapi-generator-ignore --additional-properties=outputAsLibrary=true,enumClassPrefix=true,router=chi,sourceFolder=,onlyInterfaces=true,isGoSubmodule=true,enumClassPrefix=true,useOneOfDiscriminatorLookup=true \ + --template-dir "$PROJECT_ROOT"/templates/go-server if [[ $(uname) == "Darwin" ]]; then - # introduce -i parameter for Mac OSX sed compatibility - sed -i '' 's/, orderByParam/, model.OrderByField(orderByParam)/g' $ROOT_FOLDER/internal/server/openapi/api_model_registry_service.go - sed -i '' 's/, sortOrderParam/, model.SortOrder(sortOrderParam)/g' $ROOT_FOLDER/internal/server/openapi/api_model_registry_service.go + # introduce -i parameter for Mac OSX sed compatibility + sed -i '' 's/, orderByParam/, model.OrderByField(orderByParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go + sed -i '' 's/, sortOrderParam/, model.SortOrder(sortOrderParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go else - sed -i 's/, orderByParam/, model.OrderByField(orderByParam)/g' $ROOT_FOLDER/internal/server/openapi/api_model_registry_service.go - sed -i 's/, sortOrderParam/, model.SortOrder(sortOrderParam)/g' $ROOT_FOLDER/internal/server/openapi/api_model_registry_service.go + sed -i 's/, orderByParam/, model.OrderByField(orderByParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go + sed -i 's/, sortOrderParam/, model.SortOrder(sortOrderParam)/g' "$PROJECT_ROOT"/internal/server/openapi/api_model_registry_service.go fi echo "Assembling type_assert Go file" ./scripts/gen_type_asserts.sh -gofmt -w $ROOT_FOLDER/internal/server/openapi +gofmt -w "$PROJECT_ROOT"/internal/server/openapi echo "OpenAPI server generation completed" diff --git a/scripts/gen_type_asserts.sh b/scripts/gen_type_asserts.sh index a9a3458c..d4b06c28 100755 --- a/scripts/gen_type_asserts.sh +++ b/scripts/gen_type_asserts.sh @@ -1,11 +1,11 @@ -#!/bin/bash +#!/usr/bin/env bash set -e -ROOT_FOLDER="${ROOT_FOLDER:-..}" +PROJECT_ROOT=$(realpath "$(dirname "$0")"/..) -ASSERT_FILE_PATH="internal/server/openapi/type_asserts.go" -PATCH="./patches/type_asserts.patch" +ASSERT_FILE_PATH="${PROJECT_ROOT}/internal/server/openapi/type_asserts.go" +PATCH="${PROJECT_ROOT}/patches/type_asserts.patch" # Remove the existing file identified by env.ASSERT_FILE_PATH if [ -f "$ASSERT_FILE_PATH" ]; then @@ -15,7 +15,8 @@ fi # Create an empty file touch "$ASSERT_FILE_PATH" -INITIAL_CONTENT=$(cat < "$ASSERT_FILE_PATH" +echo -e "$INITIAL_CONTENT" >"$ASSERT_FILE_PATH" # Iterate over files starting with "model_" in the internal/server/openapi/ folder -for file in internal/server/openapi/model_*; do +for file in "$PROJECT_ROOT"/internal/server/openapi/model_*; do # Check if the file is a regular file if [ -f "$file" ]; then # Ignore first 15 lines containing license, package and imports - sed -n '13,$p' "$file" >> "$ASSERT_FILE_PATH" + sed -n '13,$p' "$file" >>"$ASSERT_FILE_PATH" # Remove the merged file rm "$file" fi done -gofmt -w $ROOT_FOLDER/internal/server/openapi/type_asserts.go +gofmt -w "$ASSERT_FILE_PATH" git apply "$PATCH" diff --git a/scripts/install_protoc.sh b/scripts/install_protoc.sh index 3eec5d1c..6f8fc3b5 100755 --- a/scripts/install_protoc.sh +++ b/scripts/install_protoc.sh @@ -1,22 +1,22 @@ -#! /bin/bash +#!/usr/bin/env bash set -euxo pipefail -SCRIPT_PARENT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd ) - -VERSION="24.3" +OSTYPE=$(uname -s) OS="linux" -if [[ "$OSTYPE" == "darwin"* ]]; then - # Mac OSX - OS="osx" +if [[ ${OSTYPE,,} =~ darwin ]]; then + OS="osx" fi + ARCH="x86_64" -if [[ "$(uname -m)" == "arm"* ]]; then - ARCH="aarch_64" +if [[ $(uname -m) =~ arm ]]; then + ARCH="aarch_64" fi -mkdir -p ${SCRIPT_PARENT_DIR}/bin +PROJECT_ROOT=$(realpath "$(dirname "$0")"/..) -wget -q https://github.com/protocolbuffers/protobuf/releases/download/v${VERSION}/protoc-${VERSION}-${OS}-${ARCH}.zip -O ${SCRIPT_PARENT_DIR}/protoc.zip && \ - unzip -qo ${SCRIPT_PARENT_DIR}/protoc.zip -d ${SCRIPT_PARENT_DIR} && \ - bin/protoc --version && \ - rm ${SCRIPT_PARENT_DIR}/protoc.zip +VERSION="24.3" +URL=https://github.com/protocolbuffers/protobuf/releases/download/v${VERSION}/protoc-${VERSION}-${OS}-${ARCH}.zip +wget -qx "$URL" -O "$PROJECT_ROOT"/protoc.zip && + unzip -qo "$PROJECT_ROOT"/protoc.zip -d "$PROJECT_ROOT" && + "$PROJECT_ROOT"/bin/protoc --version && + rm "$PROJECT_ROOT"/protoc.zip