Skip to content

Commit

Permalink
[NU-1734] misc improvements (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-software-pl authored Sep 6, 2024
1 parent 0ce2fb7 commit 36a8d2a
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ You can disable all examples embedded into library by setting `DISABLE_EMBEDDED_

You can disable only data generation for all the examples from the library by setting `DISABLE_DATA_GENERATION: true`

#### Disabling scenario deployment

You can disable scenario deployment (in fact, the scenario will be deployed but then it will be canceled) for a specific
example by setting e.g. `LOAN_REQUEST_DEPLOY: false` - this ENV ensures that the `loan-request' scenario example is not
active when the data data generation is started.

## What's underneath and how it works

### Scenario Examples Library
Expand Down
4 changes: 3 additions & 1 deletion scenario-examples-bootstrapper/run-mocks-setup-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ if /app/mocks/db/is-postgres-ready.sh && /app/mocks/http-service/is-wiremock-rea

if are_embedded_examples_active; then
mkdir -p /scenario-examples
mv /tmp/scenario-examples/* /scenario-examples/
if [ "$(ls -A /tmp/scenario-examples)" ]; then
mv /tmp/scenario-examples/* /scenario-examples/
fi
fi

/app/mocks/configure.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function import_and_deploy_scenario() {

../../utils/nu/load-scenario-from-json-file.sh "$EXAMPLE_SCENARIO_NAME" "$EXAMPLE_SCENARIO_FILE"
../../utils/nu/deploy-scenario-and-wait-for-running-state.sh "$EXAMPLE_SCENARIO_NAME"

if ! should_deploy_scenario "$SCENARIO_EXAMPLE_DIR_PATH"; then
../../utils/nu/cancel-scenario-and-wait-for-canceled-state.sh "$EXAMPLE_SCENARIO_NAME"
fi
}

echo "Starting to import and deploy example scenarios..."
Expand Down
4 changes: 2 additions & 2 deletions scenario-examples-bootstrapper/setup/run-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ cd "$(dirname "$0")"

source ../utils/lib.sh

shopt -s nullglob

magenta_echo "-------- SETUP STAGE is starting... -------\n"

shopt -s nullglob

for FOLDER in /scenario-examples/*; do
if is_scenario_enabled "$FOLDER"; then
echo -e "Starting to configure and run example scenarios from ${GREEN}$FOLDER${RESET} directory...\n\n"
Expand Down
3 changes: 2 additions & 1 deletion scenario-examples-bootstrapper/utils/kafka/purge-topic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ fi
TOPIC_NAME=$1

kaf --brokers="$KAFKA_ADDRESS" topic delete "$TOPIC_NAME" > /dev/null
./create-topic-idempotently.sh
sleep 0.5
./create-topic-idempotently.sh "$TOPIC_NAME"
24 changes: 24 additions & 0 deletions scenario-examples-bootstrapper/utils/kafka/read-from-topic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash -e

cd "$(dirname "$0")"

source ../lib.sh

if [ "$#" -ne 1 ]; then
red_echo "ERROR: One parameter required: 1) topic name\n"
exit 1
fi

if ! [ -v KAFKA_ADDRESS ] || [ -z "$KAFKA_ADDRESS" ]; then
red_echo "ERROR: required variable KAFKA_ADDRESS not set or empty\n"
exit 2
fi

TOPIC_NAME=$1

if kaf --brokers="$KAFKA_ADDRESS" topics ls | awk '{print $1}' | grep "^$TOPIC_NAME$" > /dev/null 2>&1; then
kaf --brokers="$KAFKA_ADDRESS" consume "$TOPIC_NAME" --offset oldest --output raw
else
red_echo "ERROR: Topic name '$TOPIC_NAME' not found\n"
exit 3
fi
6 changes: 3 additions & 3 deletions scenario-examples-bootstrapper/utils/kafka/send-to-topic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cd "$(dirname "$0")"
source ../lib.sh

if [ "$#" -ne 2 ]; then
red_echo "ERROR: Two parameters required: 1) topic name, 2) message\n"
red_echo "ERROR: Two parameters required: 1) topic name, 2) messages (message per line)\n"
exit 1
fi

Expand All @@ -15,10 +15,10 @@ if ! [ -v KAFKA_ADDRESS ] || [ -z "$KAFKA_ADDRESS" ]; then
fi

TOPIC_NAME=$1
MESSAGE=$2
MESSAGES=$2

if kaf --brokers="$KAFKA_ADDRESS" topics ls | awk '{print $1}' | grep "^$TOPIC_NAME$" > /dev/null 2>&1; then
echo "$MESSAGE" | kaf --brokers="$KAFKA_ADDRESS" produce "$TOPIC_NAME" > /dev/null
echo "$MESSAGES" | kaf --brokers="$KAFKA_ADDRESS" produce "$TOPIC_NAME" > /dev/null
else
red_echo "ERROR: Topic name '$TOPIC_NAME' not found\n"
exit 3
Expand Down
36 changes: 34 additions & 2 deletions scenario-examples-bootstrapper/utils/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ function strip_extension() {
}

function is_scenario_enabled() {
set -e

if [ "$#" -ne 1 ]; then
red_echo "ERROR: One parameter required: 1) scenario folder path\n"
return 1
Expand All @@ -90,14 +92,43 @@ function is_scenario_enabled() {
SCENARIO_DIR=$1
SCENARIO_NAME=$(basename "$SCENARIO_DIR")

IS_DISABLED=$(echo "${SCENARIO_NAME}_DISABLED" | tr '-' '_' | awk '{print toupper($0)}')
IS_DISABLED=$(format_env_name "${SCENARIO_NAME}_DISABLED")
if [[ "${!IS_DISABLED,,}" == "true" ]]; then
return 2
fi

return 0
}

function should_deploy_scenario() {
set -e

if [ "$#" -ne 1 ]; then
red_echo "ERROR: One parameter required: 1) scenario folder path\n"
return 1
fi

SCENARIO_DIR=$1
SCENARIO_NAME=$(basename "$SCENARIO_DIR")

TO_DEPLOY=$(format_env_name "${SCENARIO_NAME}_DEPLOY")
if [[ "${!TO_DEPLOY,,}" == "false" ]]; then
return 2
fi

return 0
}

function format_env_name() {
if [ "$#" -ne 1 ]; then
red_echo "ERROR: One parameter required: 1) ENV name candidate string\n"
return 1
fi

NAME=$1
echo "${NAME}" | tr '-' '_' | awk '{print toupper($0)}'
}

function are_embedded_examples_active() {
if [[ "${DISABLE_EMBEDDED_EXAMPLES,,}" == "true" ]]; then
return 1
Expand All @@ -112,4 +143,5 @@ function is_data_generation_active() {
else
return 0
fi
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/bash -e

cd "$(dirname "$0")"

source ../lib.sh

if [ "$#" -lt 1 ]; then
red_echo "ERROR: One parameter required: 1) scenario name\n"
exit 1
fi

if ! [ -v NU_DESIGNER_ADDRESS ] || [ -z "$NU_DESIGNER_ADDRESS" ]; then
red_echo "ERROR: required variable NU_DESIGNER_ADDRESS not set or empty\n"
exit 2
fi

SCENARIO_NAME=$1
TIMEOUT_SECONDS=${2:-60}
WAIT_INTERVAL=5

function cancel_scenario() {
if [ "$#" -ne 1 ]; then
red_echo "ERROR: One parameter required: 1) scenario name\n"
exit 11
fi

set -e

local SCENARIO_NAME=$1

local RESPONSE
RESPONSE=$(curl -s -L -w "\n%{http_code}" -u admin:admin \
-X POST "http://${NU_DESIGNER_ADDRESS}/api/processManagement/cancel/$SCENARIO_NAME"
)

local HTTP_STATUS
HTTP_STATUS=$(echo "$RESPONSE" | tail -n 1)

if [ "$HTTP_STATUS" != "200" ]; then
local RESPONSE_BODY
RESPONSE_BODY=$(echo "$RESPONSE" | sed \$d)
red_echo "ERROR: Cannot cancel scenario $SCENARIO_NAME deployment.\nHTTP status: $HTTP_STATUS, response body: $RESPONSE_BODY\n"
exit 12
fi

echo "Scenario $SCENARIO_NAME deployment cancelled..."
}

function check_cancellation_status() {
if [ "$#" -ne 1 ]; then
red_echo "ERROR: One parameter required: 1) scenario name\n"
exit 21
fi

set -e

local SCENARIO_NAME=$1

local RESPONSE
RESPONSE=$(curl -s -L -w "\n%{http_code}" -u admin:admin \
-X GET "http://${NU_DESIGNER_ADDRESS}/api/processes/$SCENARIO_NAME/status"
)

local HTTP_STATUS
HTTP_STATUS=$(echo "$RESPONSE" | tail -n 1)
local RESPONSE_BODY
RESPONSE_BODY=$(echo "$RESPONSE" | sed \$d)

if [ "$HTTP_STATUS" != "200" ]; then
red_echo "ERROR: Cannot check scenario $SCENARIO_NAME deployment status.\nHTTP status: $HTTP_STATUS, response body: $RESPONSE_BODY\n"
exit 22
fi

local SCENARIO_STATUS
SCENARIO_STATUS=$(echo "$RESPONSE_BODY" | jq -r '.status.name')
echo "$SCENARIO_STATUS"
}

echo "Canceling scenario $SCENARIO_NAME..."

START_TIME=$(date +%s)
END_TIME=$((START_TIME + TIMEOUT_SECONDS))

cancel_scenario "$SCENARIO_NAME"

while true; do
DEPLOYMENT_STATUS=$(check_cancellation_status "$SCENARIO_NAME")

if [ "$DEPLOYMENT_STATUS" == "CANCELED" ]; then
break
fi

CURRENT_TIME=$(date +%s)
if [ $CURRENT_TIME -gt $END_TIME ]; then
red_echo "ERROR: Timeout for waiting for the CANCELED state of $SCENARIO_NAME deployment reached!\n"
exit 3
fi

echo "$SCENARIO_NAME deployment state is $DEPLOYMENT_STATUS. Checking again in $WAIT_INTERVAL seconds..."
sleep $WAIT_INTERVAL
done

echo "Scenario $SCENARIO_NAME is CANCELED!"
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
LIBRARY_DOCKER_IMAGE_VERSION=0.2.0
LIBRARY_DOCKER_IMAGE_VERSION=0.3.0

0 comments on commit 36a8d2a

Please sign in to comment.