#!/usr/bin/env bash # nc-create-thread.sh — high-level: create a new thread in a NetConfig and # (optionally) wire a route from another thread to it. # # Combines nc-make-jump's emit helpers + nc-insert-protocol for a single # user-facing operation. Goes through the journal. # # Usage: # nc-create-thread.sh --name NEW_THREAD --site SITE --netconfig PATH # --type tcpip|file # --direction inbound|outbound # --port PORT [--host HOST] # [--process PROC] # [--encoding ASCII] # [--connect-from EXISTING_THREAD] # add a route on EXISTING → NEW # [--route-type raw|xlate|generate] # default raw # [--xlate XLATENAME] # if route-type=xlate # [--trxid REGEX] # default .* # # Example: create a new outbound thread `to_metrics` in process `metrics` on # 10.0.0.50:51999, raw-route from existing `IB_ADT_muxS` to it. set -o pipefail NC_SELF="$0" LIB_DIR="$(cd "$(dirname "$NC_SELF")" && pwd)" NCP="$LIB_DIR/nc-parse.sh" NCI="$LIB_DIR/nc-insert-protocol.sh" die() { printf 'nc-create-thread: %s\n' "$*" >&2; exit 1; } NAME=""; SITE="${HCISITE:-}"; NC="" TYPE="tcpip"; DIRECTION="outbound"; PORT=""; HOST="" PROCESS=""; ENCODING="ASCII" CONNECT_FROM=""; ROUTE_TYPE="raw"; XLATE=""; TRXID=".*" while [ $# -gt 0 ]; do case "$1" in --name) shift; NAME="$1" ;; --site) shift; SITE="$1" ;; --netconfig) shift; NC="$1" ;; --type) shift; TYPE="$1" ;; --direction) shift; DIRECTION="$1" ;; --port) shift; PORT="$1" ;; --host) shift; HOST="$1" ;; --process) shift; PROCESS="$1" ;; --encoding) shift; ENCODING="$1" ;; --connect-from) shift; CONNECT_FROM="$1" ;; --route-type) shift; ROUTE_TYPE="$1" ;; --xlate) shift; XLATE="$1" ;; --trxid) shift; TRXID="$1" ;; -h|--help) sed -n '2,20p' "$NC_SELF"; exit 0 ;; *) die "unknown arg: $1" ;; esac shift done [ -n "$NAME" ] || die "missing --name" [ -n "$PORT" ] || die "missing --port" [ -n "$NC" ] || NC="${HCIROOT:-}/${SITE}/NetConfig" [ -f "$NC" ] || die "no such netconfig: $NC" [ -z "$PROCESS" ] && PROCESS="$SITE" is_server=0 outonly=1 obib=0 case "$DIRECTION" in inbound) is_server=1; outonly=0; obib=1 ;; outbound) is_server=0; outonly=1; obib=0 ;; *) die "bad --direction" ;; esac # Build the protocol block build_block() { cat < "$BLOCK_FILE" printf '\n=== Generated protocol block for %s ===\n\n' "$NAME" cat "$BLOCK_FILE" printf '\n' # Insert it "$NCI" insert "$NC" "$BLOCK_FILE" # If --connect-from, also splice a route into the source thread's DATAXLATE if [ -n "$CONNECT_FROM" ]; then ROUTE_FILE=$(mktemp); build_route_entry > "$ROUTE_FILE" printf '\n=== Route to splice into %s DATAXLATE ===\n\n' "$CONNECT_FROM" cat "$ROUTE_FILE" printf '\n' "$NCI" add-route "$NC" "$CONNECT_FROM" "$ROUTE_FILE" rm -f "$ROUTE_FILE" fi rm -f "$BLOCK_FILE"