Skip to content

Commit

Permalink
tidy scripts (kubeflow#293)
Browse files Browse the repository at this point in the history
* scripts: tidy and fix shebangs

Signed-off-by: Isabella do Amaral <[email protected]>

* Makefile: restore staged file check on server rebuild

Signed-off-by: Isabella do Amaral <[email protected]>

---------

Signed-off-by: Isabella do Amaral <[email protected]>
  • Loading branch information
isinyaaa authored Aug 22, 2024
1 parent 62d173f commit f29704c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 67 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 34 additions & 31 deletions scripts/build_deploy.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

Expand All @@ -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
22 changes: 11 additions & 11 deletions scripts/gen_openapi_server.sh
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 10 additions & 9 deletions scripts/gen_type_asserts.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,7 +15,8 @@ fi
# Create an empty file
touch "$ASSERT_FILE_PATH"

INITIAL_CONTENT=$(cat <<EOF
INITIAL_CONTENT=$(
cat <<EOF
/*
* Model Registry REST API
*
Expand All @@ -39,19 +40,19 @@ EOF
)

# Create the file and initialize it with the specified content
echo -e "$INITIAL_CONTENT" > "$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"
28 changes: 14 additions & 14 deletions scripts/install_protoc.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f29704c

Please sign in to comment.