Files
ScadaBridge/docker/seed-flow-log-demo.sh

84 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
# Seed the Flow Debugger STEP-LOGS demo on the docker rig (plan
# docs/plans/2026-09-09-flow-debugger-step-logs.md, Task 15): one site Call script that writes a
# line at three levels, writes a static attribute, warns, and then throws — so a traced run shows
# BOTH halves of the step-logs feature on one graph:
#
# * the ScriptRun node ends Failed and carries the whole exception (errorDetail = ex.ToString()),
# rendered as a red `InvalidOperationException: demo failure` subtitle plus the Log tab's
# exception block;
# * the info / debug / warn lines fold onto their owning step as ScriptTrace/ScriptLog rows —
# the badge reads `✕ 0 ! 1 ≡ 2`-style counts, never separate nodes.
#
# What it creates:
# - Script FlowLogDemo (Call, no parameters) on template SimMachine (docker/seed-delmia-sim.sh),
# then redeploys DelmiaSim01 so the site actually has it.
#
# IDEMPOTENT: re-running UPDATES the script in place (template script update) instead of failing on
# the unique name, so this is the script to re-run after a rig rebuild.
#
# Prerequisites: bash docker/deploy.sh; docker/seed-sites.sh; docker/seed-delmia-sim.sh.
#
# Try:
# scadabridge --url http://localhost:9000 --username multi-role --password password \
# flow-debug run-script --instance-id <id> --script FlowLogDemo --show-logs
# then open /deployment/flow-debugger?executionId=<rootExecutionId>
# Covered by docker/verify-delmia-notifier.sh step J.
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
CLI="$PROJECT_ROOT/src/ZB.MOM.WW.ScadaBridge.CLI/bin/Debug/net10.0/scadabridge"
A=(--url "${SCADABRIDGE_URL:-http://localhost:9000}" --format=json --username "${SCADABRIDGE_USERNAME:-multi-role}" --password "${SCADABRIDGE_PASSWORD:-password}")
INSTANCE_NAME="${INSTANCE_NAME:-DelmiaSim01}"
TEMPLATE_NAME="${TEMPLATE_NAME:-SimMachine}"
SCRIPT_NAME="${SCRIPT_NAME:-FlowLogDemo}"
# A static attribute that exists on SimMachine through the composed DelmiaReceiver child, and that
# nothing triggers on (RecipeDownloadFlag would fire the simulator script and muddy the graph).
WRITE_ATTRIBUTE="${WRITE_ATTRIBUTE:-RecipeProcessResultText}"
find_id() { # stdin = JSON array, $1 = key, $2 = value
python3 -c 'import json,sys; k,v=sys.argv[1],sys.argv[2]; print(next(x["id"] for x in json.load(sys.stdin) if x.get(k)==v))' "$1" "$2"; }
# The demo body. Log.* is the leveled per-run logging global (design 2026-09-09 D4): every line
# always reaches the node ILogger, and becomes a ScriptLog trace row only when the run is traced.
read -r -d '' DEMO_CODE <<'CS' || true
// Flow Debugger step-logs demo. Three levels, one real static write between them, then a throw:
// the lines fold onto the steps that produced them, and the throw gives the ScriptRun node its
// exception block. Nothing here is debugger-specific — Log.* is an ordinary script API.
Log.Info("FlowLogDemo starting");
Log.Debug("about to write");
await Children["DelmiaReceiver"].Attributes.SetAsync("RecipeProcessResultText", "42");
Log.Warn("demo warning after the write");
throw new System.InvalidOperationException("demo failure");
CS
echo "1/3 Locating template $TEMPLATE_NAME..."
TEMPLATE_ID=$("$CLI" template list "${A[@]}" | find_id name "$TEMPLATE_NAME")
echo " template id=$TEMPLATE_ID"
echo "2/3 Adding or updating script $SCRIPT_NAME (Call)..."
EXISTING_ID=$("$CLI" template get "${A[@]}" --id "$TEMPLATE_ID" | python3 -c '
import json, sys
t = json.load(sys.stdin)
name = sys.argv[1]
print(next((s["id"] for s in (t.get("scripts") or []) if s.get("name") == name), ""))' "$SCRIPT_NAME")
if [ -n "$EXISTING_ID" ]; then
"$CLI" template script update "${A[@]}" --id "$EXISTING_ID" --name "$SCRIPT_NAME" --trigger-type Call --code "$DEMO_CODE" > /dev/null
echo " updated script id=$EXISTING_ID"
else
EXISTING_ID=$("$CLI" template script add "${A[@]}" --template-id "$TEMPLATE_ID" --name "$SCRIPT_NAME" --trigger-type Call --code "$DEMO_CODE" \
| python3 -c 'import json,sys; d=json.load(sys.stdin); print(d.get("id",""))')
echo " added script id=$EXISTING_ID"
fi
echo "3/3 Redeploying $INSTANCE_NAME so the site has $SCRIPT_NAME..."
INSTANCE_ID=$("$CLI" instance list "${A[@]}" | find_id uniqueName "$INSTANCE_NAME")
"$CLI" instance deploy "${A[@]}" --id "$INSTANCE_ID" > /dev/null
echo " instance id=$INSTANCE_ID"
echo
echo "FLOW_LOG_DEMO_TEMPLATE_ID=$TEMPLATE_ID FLOW_LOG_DEMO_INSTANCE_ID=$INSTANCE_ID SCRIPT=$SCRIPT_NAME (writes $WRITE_ATTRIBUTE)"
echo "Try: $CLI --url http://localhost:9000 --username multi-role --password password flow-debug run-script --instance-id $INSTANCE_ID --script $SCRIPT_NAME --show-logs"