2023-03-22 19:52:00 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
function error_handler() {
|
2024-05-03 18:57:35 +00:00
|
|
|
echo >&2 "Exited with BAD EXIT CODE '${2}' in ${0} script at line: ${1}."
|
2023-03-22 19:52:00 +00:00
|
|
|
exit "$2"
|
|
|
|
}
|
|
|
|
trap 'error_handler ${LINENO} $?' ERR
|
|
|
|
set -o errtrace -o errexit -o nounset -o pipefail
|
|
|
|
|
2024-05-03 18:57:35 +00:00
|
|
|
script_dir="$(
|
|
|
|
cd -- "$(dirname "$0")" >/dev/null 2>&1
|
|
|
|
pwd -P
|
|
|
|
)"
|
2023-03-22 19:52:00 +00:00
|
|
|
|
|
|
|
if [[ -z "${KEYCLOAK_BASE_URL:-}" ]]; then
|
2023-11-16 21:15:49 +00:00
|
|
|
export KEYCLOAK_BASE_URL="http://localhost:7002"
|
2023-03-22 19:52:00 +00:00
|
|
|
fi
|
|
|
|
if [[ -z "${BACKEND_BASE_URL:-}" ]]; then
|
2023-11-16 21:15:49 +00:00
|
|
|
export BACKEND_BASE_URL=http://localhost:7000
|
2023-03-22 19:52:00 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
process_model_identifier="${1:-}"
|
|
|
|
username="${2:-admin}"
|
|
|
|
password="${3:-admin}"
|
|
|
|
realm_name="${4:-spiffworkflow}"
|
|
|
|
if [[ -z "${1:-}" ]]; then
|
2024-05-03 18:57:35 +00:00
|
|
|
echo >&2 "usage: $(basename "$0") [process_model_identifier] [username: OPTONAL] [password: OPTONAL] [realm_name: OPTONAL]"
|
2023-03-22 19:52:00 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
modified_process_model_identifier=$(tr '/' ':' <<<"$process_model_identifier")
|
|
|
|
|
2023-03-23 11:41:59 +00:00
|
|
|
function check_result_for_error() {
|
|
|
|
local result="$1"
|
|
|
|
error_code=$(jq '.error_code' <<<"$result")
|
2023-03-24 16:09:27 +00:00
|
|
|
if [[ -n "$error_code" && "$error_code" != "null" ]]; then
|
2024-05-03 18:57:35 +00:00
|
|
|
echo >&2 "ERROR: Failed to run process instance. Received error: $result"
|
2023-03-23 11:41:59 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-22 19:52:00 +00:00
|
|
|
function process_next_task() {
|
2024-05-03 18:57:35 +00:00
|
|
|
local task="$1"
|
2023-03-22 19:52:00 +00:00
|
|
|
|
2024-05-03 18:57:35 +00:00
|
|
|
if [[ -n "$task" && "$task" != "null" ]]; then
|
|
|
|
task_type=$(jq -r '.type' <<<"$task")
|
|
|
|
task_state=$(jq -r '.state' <<<"$task")
|
|
|
|
task_guid=$(jq -r '.id' <<<$"$task")
|
2023-03-22 19:52:00 +00:00
|
|
|
|
|
|
|
if grep -qE "Manual ?Task" <<<"$task_type" && [[ "${task_state}" == "READY" ]]; then
|
2024-05-03 18:57:35 +00:00
|
|
|
new_task=$(curl --silent -X PUT "${BACKEND_BASE_URL}/v1.0/tasks/${process_instance_id}/${task_guid}" -H "Authorization: Bearer $access_token")
|
|
|
|
check_result_for_error "$new_task"
|
|
|
|
process_next_task "$new_task"
|
|
|
|
elif grep -qE "User ?Task" <<<"$task_type" && [[ "${task_state}" == "READY" ]]; then
|
2023-03-22 19:52:00 +00:00
|
|
|
echo -e "\n\nThe next task is not a Manual Task and requires user input. It must be completed manually."
|
2024-05-03 18:57:35 +00:00
|
|
|
echo "$task"
|
|
|
|
exit
|
2023-03-22 19:52:00 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-05-03 18:57:35 +00:00
|
|
|
function check_on_proces_instance() {
|
|
|
|
local process_instance="$1"
|
|
|
|
status=$(jq -r '.status' <<<"$process_instance")
|
|
|
|
|
|
|
|
if [[ -n "$status" ]] && ! grep -qE '^(complete|error|suspendend)$' <<<"$status"; then
|
|
|
|
progress_result=$(curl --silent "${BACKEND_BASE_URL}/v1.0/tasks/progress/${process_instance_id}" -H "Authorization: Bearer $access_token")
|
|
|
|
task=$(jq '.task' <<<"$progress_result")
|
|
|
|
instructions=$(jq -r '.instructions' <<<"$progress_result")
|
|
|
|
new_process_instance=$(jq '.process_instance' <<<"$progress_result")
|
|
|
|
|
|
|
|
if [[ -n "$task" && "$task" != "null" ]]; then
|
|
|
|
process_next_task "$task"
|
|
|
|
elif [[ -n "$instructions" && "$instructions" != "null" && "$instructions" != '[]' ]]; then
|
|
|
|
echo "$instructions"
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
check_on_proces_instance "$new_process_instance"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-22 19:52:00 +00:00
|
|
|
access_token=$("${script_dir}/get_token" "$username" "$password" "$realm_name")
|
|
|
|
curl --silent -X POST "${BACKEND_BASE_URL}/v1.0/login_with_access_token?access_token=${access_token}" -H "Authorization: Bearer $access_token" >/dev/null
|
|
|
|
result=$(curl --silent -X POST "${BACKEND_BASE_URL}/v1.0/process-instances/${modified_process_model_identifier}" -H "Authorization: Bearer $access_token")
|
2023-03-24 16:09:27 +00:00
|
|
|
process_instance_id=$(jq -r '.id' <<<"$result")
|
2023-03-22 19:52:00 +00:00
|
|
|
if ! grep -qE '^[0-9]+$' <<<"$process_instance_id"; then
|
2024-05-03 18:57:35 +00:00
|
|
|
echo >&2 "ERROR: Did not receive valid process instance id when instantiating process model. result was ${result}"
|
2023-03-22 19:52:00 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
result=$(curl --silent -X POST "${BACKEND_BASE_URL}/v1.0/process-instances/${modified_process_model_identifier}/${process_instance_id}/run" -H "Authorization: Bearer $access_token")
|
2023-03-23 11:41:59 +00:00
|
|
|
check_result_for_error "$result"
|
2023-12-05 16:41:59 +00:00
|
|
|
if [[ "$(jq -r '.status' <<<"$result")" == "complete" ]]; then
|
|
|
|
echo "Process instance completed"
|
|
|
|
exit 0
|
|
|
|
fi
|
2024-05-03 18:57:35 +00:00
|
|
|
check_on_proces_instance "$result"
|