Commit Diff


commit - b7a0ae1ebc8b8dd85f911aba64f1839699679ac8
commit + 46924c0185b07e50677c0d0cb04b2cd5e63ee4c8
blob - b36e88e8c4dffc7934cba2da2b1664f4b00d2509
blob + 406aaba2dbdbd5e2443b1b7434a210441c929cf9
--- Makefile
+++ Makefile
@@ -1,4 +1,85 @@
+.POSIX:
 PREFIX:=/usr/local
+SHELLS=dash bash ksh ksh93 zsh yash sh
+SHELLS=ksh yash sh
+UTILITY_VERSION=0.0.1
+UTILITY=$(PWD)/r7
+.DEFAULT: all
+
+all: tests
+
 install:
 	cp ./r7 $(PREFIX)/bin/
 	chmod 755 $(PREFIX)/bin/r7
+
+# Test suite for r7
+R=${SHELL} ${UTILITY}
+test_arguments: usage version debug
+test_workdirs: \
+	workdir0-arguments \
+	workdir0-digraph \
+	workdir1-init \
+	workdir1-populate
+
+tests:
+	@echo
+	make test_arguments
+	make test_workdirs
+	@echo == ALL TESTS PASSED
+
+poly:
+	@for shell in ${SHELLS}; do \
+		ashell=$$(command -v $$shell || true); \
+		if test -x "$$ashell"; then \
+				if time make tests SHELL=$$ashell; then \
+						: \
+				else \
+						failed="$$(basename "$$ashell") $$failed"; \
+				fi; \
+		else \
+			notfound="$$shell $$notfound";\
+		fi; \
+	done; \
+	echo; \
+	echo "== POLY TESTS RESULTS"; \
+	echo "     spec: $(SHELLS)"; \
+	echo "not-found: $$notfound"; \
+	echo "   failed: $$failed"; \
+	test -z "$$failed" -a -z "$$notfound" || (echo; false;)
+
+usage:
+	$R -h 2>&1 | grep -q -e usage: -e '\['
+
+version:
+	$R -h 2>&1 | grep -q -e "$(UTILITY_VERSION)"
+
+debug:
+	$R -dn 2>&1 run-config | grep -q "R7 DEBUG $(UTILITY_VERSION)"
+
+workdir0-arguments:
+	rm -fr testdir/workdir0/_run
+	$R -w testdir/workdir0 -dn run-config 2>&1 | tee testdir/wd01
+	grep -q -e '^LOADING $(PWD)/testdir/workdir0/site-config' testdir/wd01
+	grep -q -e '^CREATED $(PWD)/testdir/workdir0/_run' testdir/wd01
+	grep -q -e '^PROCESSING GROUP common' testdir/wd01
+	grep -q -e '^PROCESSING GROUP test0' testdir/wd01
+
+workdir0-digraph:
+	$R -w testdir/workdir0 -dn digraph-host 2>/dev/null | \
+		tee testdir/wd01.dot.produced
+	cmp testdir/wd01.dot testdir/wd01.dot.produced
+
+workdir1-init:
+	rm -fr testdir/workdir1
+	$R -dw testdir/workdir1 init
+	test -d testdir/workdir1
+	find testdir/workdir1 | tee testdir/wd1-init-find.produced
+	cmp testdir/wd1-init-find testdir/wd1-init-find.produced
+
+workdir1-populate: workdir1-init
+	cd testdir/workdir1 && \
+		$R run-config && \
+		$R digraph-host && \
+		$R output-dump && \
+		$R output && \
+		$R group
blob - 899fdae135d956aae81070df1b240e70b42c1bef
blob + 3ffd131a752e1bba60a69c79ff07a2dd6f0a1f04
--- r7
+++ r7
@@ -1,28 +1,83 @@
 #!/bin/sh
-# shellcheck disable=SC2046
+# shellcheck disable=SC2046,SC2031
 
 # Global configuration
 # ====================
 
-: "${DEBUG:=no}"
-: "${DRYRUN:=no}"
+VERSION=0.0.1
 : "${HOSTNAME:=$(uname -n)}"
-: "${PARALLEL:=no}"
-: "${SHOWOUTPUT:=no}"
-: "${WORKDIR:=$PWD}"
-: "${SITE_CONFIG:=$WORKDIR/site-config}"
-: "${RUNDIR:=$WORKDIR/_run}"
-: "${OUTPUTDIR:=$WORKDIR/_output}"
-: "${SSH_CONFIG_FILE:=$WORKDIR/.ssh/config}"
-: "${SSH_CONNECT_TIMEOUT:=3}"
-: "${SSH_IDENTITY_FILE:=$WORKDIR/.ssh/id_ed25519_r7}"
-: "${PREFIX:=install state doc}"
+readonly VERSION HOSTNAME
 
+# shellcheck disable=SC2031
+_init_env() {
+	_debug _INIT_ENV
+
+	# Here is the default configuration
+	: "${R7_DEBUG:=no}"
+	: "${R7_DRYRUN:=no}"
+	: "${R7_PARALLEL:=no}"
+	: "${R7_SHOWOUTPUT:=no}"
+	: "${R7_WORKDIR:=$PWD}"
+	: "${R7_OUTPUTDIR:=$R7_WORKDIR/_output}"
+	: "${R7_PREFIX:=install state doc}"
+	: "${R7_SITE_CONFIG:=$R7_WORKDIR/site-config}"
+	: "${SSH_CONFIG_FILE:=$R7_WORKDIR/.ssh/config}"
+	: "${SSH_CONNECT_TIMEOUT:=3}"
+	: "${SSH_IDENTITY_FILE:=$R7_WORKDIR/.ssh/id_ed25519_r7}"
+
+	_debug ENVIRONMENT
+
+	# Validate environment
+	if test "$R7_DEBUG" = yes; then
+		set | grep -e '^R7_' -e '^SSH' 1>&2
+	fi
+
+	for param in "$R7_DEBUG" "$R7_DRYRUN" "$R7_PARALLEL"; do
+		case $param in
+		yes | no) : ;;
+		*) _fail "Invalid parameter value: '$param'" ;;
+		esac
+	done
+
+	R7_WORKDIR_tmp=$(_resolv_dir "$R7_WORKDIR")
+	test -d "$R7_WORKDIR_tmp" || _fail "Working directory not found: '$R7_WORKDIR'"
+	R7_WORKDIR=$R7_WORKDIR_tmp
+	R7_RUNDIR=$R7_WORKDIR/_run
+	R7_OUTPUTDIR=$R7_WORKDIR/_output
+
+	R7_SITE_CONFIG_tmp=$(_resolv_file "$R7_SITE_CONFIG")
+	test -f "$R7_SITE_CONFIG_tmp" || _fail "Site configuration not found: '$R7_SITE_CONFIG'"
+	R7_SITE_CONFIG=$R7_SITE_CONFIG_tmp
+
+	SSH_CONFIG_FILE_tmp=$(_resolv_file "$SSH_CONFIG_FILE")
+	test -f "$SSH_CONFIG_FILE_tmp" || _fail "Unable to read SSH configuration: '$SSH_CONFIG_FILE'"
+	SSH_CONFIG_FILE=$SSH_CONFIG_FILE_tmp
+
+	SSH_IDENTITY_FILE_tmp=$(_resolv_file "$SSH_IDENTITY_FILE")
+	test -f "$SSH_IDENTITY_FILE_tmp" || _fail "Unable to read SSH identity file: '$SSH_IDENTITY_FILE'"
+	SSH_IDENTITY_FILE=$SSH_IDENTITY_FILE_tmp
+
+	unset R7_WORKDIR_tmp R7_SITE_CONFIG_tmp SSH_CONFIG_FILE_tmp SSH_IDENTITY_FILE_tmp
+}
+
 # Interface
 # =========
 
+_fail() {
+	printf "FAILURE: %s\n" "$*"
+	exit 99
+}
+
+_debug() {
+	if test "$R7_DEBUG" = yes; then
+		printf %s\\n "${*:-$(cat -)}" >&2
+	fi
+}
+
+alias help=usage
 usage() {
 	cat <<eof
+version: 0.0.1
 usage: $0 [options] command
 usage options:
 	-h	Show usage and exit
@@ -53,27 +108,33 @@ eof
 }
 
 # shellcheck disable=SC2086
-main() {
+_main() {
 	while getopts 'P:F:r:o:i:w:c:npdOh' arg; do
 		case $arg in
 		h)
 			usage
 			exit 0
 			;;
-		d) DEBUG=yes ;;
+		d)
+			R7_DEBUG=yes
+			_debug "R7 DEBUG $VERSION"
+			;;
 		i) SSH_IDENTITY_FILE=$OPTARG ;;
 		F) SSH_CONFIG_FILE=$OPTARG ;;
-		w) WORKDIR=$OPTARG ;;
-		r) RUNDIR=$OPTARG ;;
-		o) OUTPUTDIR=$OPTARG ;;
-		c) SITE_CONFIG=$OPTARG ;;
-		n) DRYRUN=yes ;;
-		p) PARALLEL=yes ;;
+		w) R7_WORKDIR=$OPTARG ;;
+		r) R7_RUNDIR=$OPTARG ;;
+		o) R7_OUTPUTDIR=$OPTARG ;;
+		c) R7_SITE_CONFIG=$OPTARG ;;
+		n)
+			R7_DRYRUN=yes
+			_debug "MODE DRYRUN"
+			;;
+		p) R7_PARALLEL=yes ;;
 		P)
-			PREFIX=$OPTARG
-			prefix $PREFIX
+			R7_PREFIX=$OPTARG
+			prefix $R7_PREFIX
 			;;
-		O) SHOWOUTPUT=yes ;;
+		O) R7_SHOWOUTPUT=yes ;;
 		*)
 			echo 1>&2 "Unknown option"
 			exit 2
@@ -81,57 +142,135 @@ main() {
 		esac
 	done
 	shift $((OPTIND - 1))
+
 	case $1 in
+	init | workdir-init)
+		_workdir_init
+		exit $?
+		;;
+	esac
+	_init_env
+
+	case $1 in
 	run-config)
 		run_config
 		;;
 	group)
 		shift
-		trace_id=$(trace_id)
-		prefix $PREFIX
+		trace_id=$(_trace_id_new)
+		prefix $R7_PREFIX
 		group "$@"
 		;;
 	output) output ;;
 	output-dump) output_dump ;;
 	digraph-host)
-		prefix $PREFIX
+		prefix $R7_PREFIX
 		digraph_host
 		;;
 	*)
 		case $0 in
+		-)
+			exit 33
+			;;
 		*r7)
-			echo 1>&2 "Unknown command or option"
+			echo 1>&2 "Unknown command '$1'"
+			usage
 			exit 2
 			;;
 		*)
-			prefix $PREFIX
+			prefix $R7_PREFIX
 			;;
 		esac
 		;;
 	esac
 }
 
-# SSH Functions
+_resolv_dir() {
+	if test -d "$1"; then
+		cd "$1" || return 2
+		printf %s "$PWD"
+	fi
+}
+
+_resolv_file() {
+	if test -f "$1"; then
+		cd "${1%/*}" || return 2
+		printf %s "$PWD/${1##*/}"
+	fi
+}
+
+# shellcheck disable=SC2030
+_workdir_init() (
+	mkdir -p "$R7_WORKDIR" || _fail "Unable to create directory: '$R7_WORKDIR'"
+	cd "$R7_WORKDIR" || _fail "Unable to cd to directory: '$R7_WORKDIR'"
+	_debug _WORKDIR_INIT "$PWD"
+	mkdir -p groups
+	mkdir -p nodes
+	mkdir -p .ssh
+	: "${SSH_CONFIG_FILE:=.ssh/config}"
+	: "${SSH_IDENTITY_FILE:=.ssh/id_ed25519_r7}"
+	R7_SITE_CONFIG=./site-config
+	if ! test -f "$R7_SITE_CONFIG"; then
+		cat >"$R7_SITE_CONFIG" <<-eof
+			# Example r7 site configuration
+			group common '*'
+		eof
+	fi
+	if ! test -f "$SSH_CONFIG_FILE"; then
+		cat >"$SSH_CONFIG_FILE" <<eof
+# Example SSH client configuration
+Host *
+	AddKeysToAgent yes
+	BatchMode yes
+	ConnectTimeout 3
+	ConnectionAttempts 1
+	ControlMaster auto
+	ControlPath $R7_WORKDIR/.ssh/control/%C
+	ControlPersist 2m
+	IdentityFile $SSH_IDENTITY_FILE
+	StrictHostKeyChecking yes
+	User root
+	UserKnownHostsFile $R7_WORKDIR/.ssh/known_hosts
+
+Host alpha
+	Hostname 10.10.0.1
+
+Host beta
+	Hostname 10.10.0.2
+
+Host gamma
+	Hostname 10.10.0.3
+eof
+	fi
+	touch "${SSH_IDENTITY_FILE}"
+	touch .ssh/known_hosts
+	echo Created:
+	find "$PWD" -exec printf -- "\t- %s\n" {} +
+)
+
+# SSH functions
 # =============
 
 ssh() (
-	debug "SSH $*"
+	_debug "SSH $*"
 	/usr/bin/ssh "-oConnectTimeout=$SSH_CONNECT_TIMEOUT" -F "$SSH_CONFIG_FILE" "$@"
 )
 
 unlock() {
 	pkill ssh-agent
+	set -e
 	eval "$(ssh-agent)"
 	ssh-add "$SSH_IDENTITY_FILE"
+	set +e
 }
 
 copy() (
-	debug "COPY $1 $2 BEGIN"
+	_debug "COPY $1 $2 BEGIN"
 	node=${2%%:*}
 	dir=${2##*:}
 	if r=$(ssh "$node" command -v openrsync); then
 		if openrsync -V 2>/dev/null; then
-			debug "COPY $1 $2 USE OPENRSYNC"
+			_debug "COPY $1 $2 USE OPENRSYNC"
 			openrsync -e "/usr/bin/ssh -oConnectTimeout=$SSH_CONNECT_TIMEOUT -F $SSH_CONFIG_FILE" \
 				--rsync-path="$r" \
 				--delete \
@@ -140,81 +279,88 @@ copy() (
 	fi
 	if r=$(ssh "$node" command -v rsync); then
 		if rsync -V 1>/dev/null; then
-			debug "COPY $1 $2 USE RSYNC"
+			_debug "COPY $1 $2 USE RSYNC"
 			rsync -e "/usr/bin/ssh -oConnectTimeout=$SSH_CONNECT_TIMEOUT -F $SSH_CONFIG_FILE" \
 				--rsync-path="$r" \
 				--delete \
 				-aq "$1" "$2" && return 0
 		fi
 	fi
-	debug "COPY $1 $2 USE SCP"
+	_debug "COPY $1 $2 USE SCP"
 	dir=${dir:-/tmp/r7}
 	ssh "$node" rm -fr "$dir/$1" 2>/dev/null
-	/usr/bin/scp "-oConnectTimeout=$SSH_CONNECT_TIMEOUT" -qF "$SSH_CONFIG_FILE" -r "$1" "$2"
-	debug "COPY $1 $2 END"
+	if /usr/bin/scp "-oConnectTimeout=$SSH_CONNECT_TIMEOUT" -qF "$SSH_CONFIG_FILE" -r "$1" "$2"; then
+		_debug "COPY $1 $2 END SUCCESS"
+	else
+		_debug "COPY $1 $2 END ERROR $?"
+	fi
 )
 
 # Deployment functions
 # ====================
 
-trace_id() {
-	date +%s
-}
+_trace_id_new() { date +%s; }
 
 nodes() (
-	cd "$WORKDIR" || return 2
+	cd "$R7_WORKDIR" || return 2
 	find nodes -type d -maxdepth 1 | tail -n +2 | awk -F/ '{ print $2 }'
 )
 
+alias nodes-active=nodes_active
+alias nodesact=nodes_active
 nodes_active() (
-	if test -s "$WORKDIR/.active_nodes"; then
-		cat "$WORKDIR/.active_nodes"
+	test "$R7_DRYRUN" = yes && return 0
+	if test -s "$R7_WORKDIR/.active_nodes"; then
+		cat "$R7_WORKDIR/.active_nodes"
 	else
-		touch "$WORKDIR/.active_nodes"
+		touch "$R7_WORKDIR/.active_nodes"
 		for node in $(nodes); do
-			debug "ACTIVE $node"
+			_debug "ACTIVE $node"
 			if ssh "$node" :; then
-				printf "%s\\n" "$node" >>"$WORKDIR/.active_nodes"
+				printf "%s\\n" "$node" >>"$R7_WORKDIR/.active_nodes"
 			fi
 		done
-		cat "$WORKDIR/.active_nodes"
+		cat "$R7_WORKDIR/.active_nodes"
 	fi
 )
 
+alias run-config=run_config
+alias rc=run_config
+# shellcheck disable=SC1090,SC2031
 run_config() {
-	trace_id=$(trace_id)
+	trace_id=$(_trace_id_new)
 	# shellcheck disable=SC1091
-	test -f ./site-config && . ./site-config
+	_debug LOADING "$R7_SITE_CONFIG"
+	test -f "$R7_SITE_CONFIG" && . "$R7_SITE_CONFIG"
 }
 
 # Inherits $target from group()
 # shellcheck disable=SC2031
-deploy() (
-	debug "DEPLOY $new_name $target"
-	cd "$WORKDIR" || exit 2
+_deploy() (
+	_debug "DEPLOY $new_name $target"
+	cd "$R7_WORKDIR" || exit 2
 	stem="groups/$new_name"
 	nodedir="nodes/$target"
-
 	if test -d "$stem.dir"; then
-		test "$DRYRUN" = no && {
+		test "$R7_DRYRUN" = yes || {
 			ssh "$target" 'test -d /tmp/r7 || { umask 027; mkdir -p /tmp/r7; }'
 			copy "$stem.dir" "$target:/tmp/r7" || return 3
 		}
 	fi
-
 	if test -d "$nodedir"; then
-		test "$DRYRUN" = no && {
+		test "$R7_DRYRUN" = yes || {
 			test -f /tmp/r7/"$trace_id.$target" || {
 				ssh "$target" 'test -d /tmp/r7 || { umask 027; mkdir -p /tmp/r7; }'
 				copy "$nodedir" "$target:/tmp/r7" || return 3
 			}
 		}
+	else
+		_fail "Unable to find nodedir: '$nodedir'"
 	fi
-
 	if test -f "$stem"; then
-		test "$DRYRUN" = no && {
-			payload "$stem" | ssh 2>&1 "$target" /bin/sh - |
-				collect "$RUNDIR/$trace_id.$target.$new_name"
+		test "$R7_DRYRUN" = yes || {
+			_payload "$stem" | ssh 2>&1 "$target" /bin/sh - |
+				_collect "$R7_RUNDIR/$trace_id.$target.$new_name"
 		}
 	fi
 )
@@ -225,72 +371,77 @@ members() (
 )
 
 group() (
-	cd "$WORKDIR" || return 2
+	test $# -eq 0 && return 0
+	cd "$R7_WORKDIR" || return 2
 	set -fm
 	umask 027
-	mkdir -p "$RUNDIR"
-	cd "$RUNDIR" || return 2
+	mkdir -p "$R7_RUNDIR"
+	cd "$R7_RUNDIR" || return 2
+	_debug "CREATED $R7_RUNDIR"
 	new_name=$1
+	_debug "PROCESSING GROUP $new_name"
 	shift
 	# shellcheck disable=SC2046
 	for target in $(members "$@"); do
-		case $PARALLEL in
+		case $R7_PARALLEL in
 		yes)
-			deploy &
+			_deploy &
 			;;
 		*)
-			deploy
+			_deploy
 			;;
 		esac
 	done
 	wait
 )
 
-functs() (
+alias group-functions=group_functions
+alias groupfuncs=group_functions
+group_functions() (
 	for pre in $prefix; do
-		grep -e "^${pre}_.*()" "$WORKDIR/groups/$1" | cut -d\( -f1
+		grep 2>/dev/null -e "^${pre}_.*()" "$R7_WORKDIR/groups/$1" | cut -d\( -f1
 	done
 )
 
-call() (
-	cd "$WORKDIR" || return 2
+prefix() {
+	prefix="$*"
+	export prefix
+}
+
+_call() (
+	cd "$R7_WORKDIR" || return 2
 	grep -e "^$2_.*()" "$1" |
 		cut -d\( -f1 | while read -r function; do
-		debug "CALL $1 $function"
+		_debug "CALL $1 $function"
 		echo trace "${1#*/}" "$function" START
 		echo "_ret=; $function || { _ret=\$?; : ;}"
 		echo trace "${1#*/}" "$function" END "\${_ret:-0}"
 	done
 )
 
-prefix() {
-	prefix="$*"
-	export prefix
-}
-
-payload() (
+_payload() (
 	echo "trace_id=$trace_id"
 	cat <<-'eof'
 		trace() {
 			echo ":: $trace_id $(nodename) $*" 1>&2
 		}
 		error() {
-			echo "!! ERROR $*" 1>&2
+			trace ERROR "$*" 1>&2
 		}
-		nodename() {
-			hostname=$(uname -n)
+		nodename() (
+			hostname=${hostname:-$(uname -n)}
 			echo "${hostname%%.*}"
-		}  
-		node() {
+		)
+		node() (
 			atom=/tmp/r7/$(nodename)/$1
 			test -f "$atom" && return 0
 			test -d "$atom" && return 0
 			return 31
-		}
+		)
 		nodedir() {
 			echo "/tmp/r7/$(nodename)"
 		}
-		nodeinstall() {
+		nodeinstall() (
 			while getopts "o:m:" arg; do
 				case $arg in
 				o) owner="$OPTARG" ;;
@@ -298,37 +449,69 @@ payload() (
 				*) ;;
 				esac
 			done
-			shift $(( OPTIND - 1 ))
+			shift $((OPTIND - 1))
 			source=$1
 			target=$2
 			source_basename=${1##*/}
 			source_dirname=${1%/*}
+			before=x
+			after=x
 			umask 027
-			test -f "$source" || { error "$source" not found ; return 2 ; }
-			cmp -s "$source" "$target/${source##$source_dirname/}"
-			case "$?" in
-			0) return 30 ;;
-			1) 
-				diff -U 2 "$target" "$source"
-				set -e
-				cp "$source" "$target"
+			if ! test -f "$source"; then
+				error "$source" not found
+				return 2
+			fi
+			if test -d "$target"; then
+				new_filename="$target/${source##$source_dirname/}"
+				test -f "$new_filename" && before=$(ls -l "$new_filename" | awk '{ print $1 " " $3 " " $4 }')
+				cmp -s "$source" "$new_filename"
+				ret=$?
+			else
+				new_filename=$target
+				test -f "$new_filename" && before=$(ls -l "$new_filename" | awk '{ print $1 " " $3 " " $4 }')
+				cmp -s "$source" "$target"
+				ret=$?
+			fi
+			if test -f "$new_filename"; then
 				if test -n "$owner"; then
-					chown "$owner" "$target"
+					chown "$owner" "$new_filename" || {
+						error unable to chown "$new_filename"
+						return 2
+					}
 				fi
 				if test -n "$mode"; then
-					chmod "$mode" "$target"
+					chmod "$mode" "$new_filename" || {
+						error unable to chmow "$new_filename"
+						return 2
+					}
 				fi
-				return 0
+				after=$(ls -l "$new_filename" | awk '{ print $1 " " $3 " " $4 }')
+				if test "$before" != "$after"; then
+					trace PERMISSIONS CHANGE "$before" "->" "$after" "on" "$new_filename"
+				fi
+			fi
+			case "$ret" in
+			0) return 30 ;;
+			1 | *)
+				if test -f "$new_filename"; then
+					trace DIFF "$new_filename" "$source"
+					diff -U 2 "$new_filename" "$source"
+				else
+					trace DIFF "/dev/null" "$source"
+					diff -U 2 /dev/null "$source"
+				fi
+				cp "$source" "$target" || { error copying "$source" "$target" return 2; }
 				;;
 			esac
-		}
-		cd /tmp/r7
+			return 0
+		)
+		cd /tmp/r7 || { error unable to cd to /tmp/r7; return 2; }
 		trace CONNECTED $(date +%s)
 	eof
 	cat "$1"
-	debug "PREFIX $prefix"
+	_debug "PREFIX $prefix"
 	for run in $prefix; do
-		call "$1" "$run"
+		_call "$1" "$run"
 	done
 	cat <<-'eof'
 		trace DISCONNECT $(date +%s)
@@ -338,17 +521,13 @@ payload() (
 # Interactive output function
 # ===========================
 
-debug() {
-	if test "$DEBUG" = "yes"; then
-		printf %s\\n "${*:-$(cat -)}" >&2
-	fi
-}
-
-collect() (
+# shellcheck disable=SC2086,SC2030
+_collect() (
 	tee -a "$1" | while read -r line; do
 		set -- $line
 		case $line in
 		::*)
+			# Traces from payload
 			trace_id=$2
 			host=$3
 			group=$4
@@ -357,8 +536,9 @@ collect() (
 			endcode=$7
 			case $status in
 			START)
+				save_group=$group save_function=$function
 				in_function=1
-				if test "$SHOWOUTPUT" = yes; then
+				if test "$R7_SHOWOUTPUT" = yes; then
 					echo "- (output) $host:$group::$function"
 				fi
 				continue
@@ -371,14 +551,20 @@ collect() (
 				esac
 				;;
 			esac
+			# Traces from functions
+			case $2 in
+			"$trace_id")
+				case $4 in
+				DIFF) echo "+ (diff) $host:$save_group::$save_function" ;;
+				PERMISSIONS) echo "+ (permissions) $host:$save_group::$save_function" ;;
+				ERROR) echo "! (error) $host:$save_group::$save_function" ;;
+				esac
+				;;
+			esac
 			;;
-		!!*)
-			shift
-			echo "! (output) $host:$group::$function"
-			echo "$*"
-			;;
 		esac
-		if test "$SHOWOUTPUT" = yes; then
+
+		if test "$R7_SHOWOUTPUT" = yes; then
 			test "$in_function" -eq 1 &&
 				printf "%s\\n" "$line"
 		fi
@@ -388,10 +574,13 @@ collect() (
 # Text output functions
 # =====================
 
+alias output-dump=output_dump
+alias outdump=output_dump
 output_dump() (
 	set -e -- $(output_latest)
-	mkdir -p "$OUTPUTDIR"
-	awk -voutput_dir="$OUTPUTDIR" '
+	test $# -eq 0 && return 0
+	mkdir -p "$R7_OUTPUTDIR"
+	awk -voutput_dir="$R7_OUTPUTDIR" '
 		BEGIN {
 			trace_id=""
 			hostname=""
@@ -434,14 +623,18 @@ output_dump() (
 	' "$@"
 )
 
-output_latest() {
+alias output-latest=output_latest
+alias outlatest=output_latest
+output_latest() (
+	set -e
 	latest=$(
 		# shellcheck disable=SC2012
-		ls -lt1 "$WORKDIR/_run/" | while read -r file; do
+		ls -lt1 "$R7_RUNDIR/" | while read -r file; do
 			echo "${file%%.*}"
 			break
 		done
 	)
+	test -z "$latest" && return 0
 	case $(uname) in
 	OpenBSD | FreeBSD | Darwin)
 		latest_date=$(date -r "$latest")
@@ -454,28 +647,30 @@ output_latest() {
 		;;
 	esac
 	if test -n "$latest_date"; then
-		find "$WORKDIR/_run/" -type f -name "$latest.*"
+		find "$R7_WORKDIR/_run/" -type f -name "$latest.*"
 	fi
-}
+)
 
-output_files() (
-	cd "$OUTPUTDIR" || return 2
+_output_files() (
+	cd "$R7_OUTPUTDIR" 2>/dev/null || return 2
 	find ./* -type f -name 'output'
 )
 
-status_files() (
-	cd "$OUTPUTDIR" || return 2
+_status_files() (
+	cd "$R7_OUTPUTDIR" 2>/dev/null || return 2
 	find ./* -type f -name 'status'
 )
 
-duration_files() (
-	cd "$OUTPUTDIR" || return 2
+_duration_files() (
+	cd "$R7_OUTPUTDIR" 2>/dev/null || return 2
 	find ./* -type f -name 'duration'
 )
 
+alias output-status=output_status
+alias outstat=output_status
 output_status() (
-	cd "$OUTPUTDIR" || return 2
-	status_files | {
+	cd "$R7_OUTPUTDIR" || return 2
+	_status_files | {
 		echo HOST GROUP FUNCTION STATUS
 		while read -r status; do
 			echo "$status" | while IFS=/ read -r _ host groupname funcname _; do
@@ -485,9 +680,11 @@ output_status() (
 	} | column -t
 )
 
+alias output-duration=output_duration
+alias outdur=output_duration
 output_duration() (
-	cd "$OUTPUTDIR" || return 2
-	duration_files | {
+	cd "$R7_OUTPUTDIR" || return 2
+	_duration_files | {
 		echo HOST GROUP DURATION
 		while read -r duration; do
 			echo "$duration" | while IFS=/ read -r _ host groupname _; do
@@ -497,36 +694,123 @@ output_duration() (
 	} | column -t
 )
 
+alias out=output
 output() (
-	output_files | while read -r output; do
+	_output_files | while read -r output; do
 		echo "$output" | while IFS=/ read -r _ host groupname funcname _; do
 			echo "- $host" "$groupname" "$funcname"
 			echo
-			sed 's/^/	/' "$OUTPUTDIR/$output"
+			sed 's/^/	/' "$R7_OUTPUTDIR/$output"
 			echo
 		done
 	done
 )
 
-summary() {
-	echo
+alias output-labels=output_labels
+alias outlabels=output_labels
+# shellcheck disable=SC2048,SC2086
+output_labels() (
+	set -- $(printf -- '-e %s ' "$@")
+	output | grep -e '^-' | grep $* |
+		while read -r _ host groupname funcname; do
+			echo "- $host" "$groupname" "$funcname"
+			echo
+			sed 's/^/	/' "$R7_OUTPUTDIR/$host/$groupname/$funcname/output"
+			echo
+		done
+)
+
+alias trace-id-last=trace_id_last
+alias lastid=trace_id_last
+trace_id_last() {
+	output_latest | while read -r output; do
+		output=${output##*/}
+		output=${output%%.*}
+		echo "$output"
+		break
+	done
+}
+
+# _output/tools0/r7test/r7test_diff/output::: 1748408055 tools0 DIFF /tmp/difftest r7test.dir/difftest
+alias output-diffs=output_diffs
+alias outdiffs=output_diffs
+# shellcheck disable=SC2048,SC2086
+output_diffs() (
+	trace_id=$(trace_id_last)
+	cd "$R7_WORKDIR"
+	grep -r "^:: ${trace_id:-} .* DIFF" _output | while read -r line; do
+		set -- $line
+		output_file=${1%%:::}
+		OIFS=$IFS
+		IFS=/
+		set -- $output_file
+		host=$2 groupname=$3 funcname=$4
+		IFS=$OIFS
+		echo "- (diff)" "in" "$host" "$groupname" "$funcname"
+		echo
+		sed 's/^/	/' "$R7_OUTPUTDIR/$host/$groupname/$funcname/output" | grep -v "^	:: $trace_id $host"
+		echo
+	done
+)
+
+# :: 1748415578 tools0 PERMISSIONS CHANGE -rw-r-xr-x root wheel -> -rw-r--r-- root wheel on /tmp/difftest
+alias output-permissions=output_permissions
+alias outperm=output_permissions
+# shellcheck disable=SC2048,SC2086
+output_permissions() (
+	trace_id=$(trace_id_last)
+	cd "$R7_WORKDIR"
+	grep -r "^:: ${trace_id:-} .* PERMISSIONS" _output | while read -r line; do
+		set -- $line
+		output_file=${1%%:::}
+		OIFS=$IFS
+		IFS=/
+		set -- $output_file
+		host=$2 groupname=$3 funcname=$4
+		IFS=$OIFS
+		echo "- (permissions)" "in" "$host" "$groupname" "$funcname"
+		echo
+		while read -r trace; do
+			set -- $trace
+			echo "	$6 $7 $8 -> ${10} ${11} ${12} on ${14}"
+			break
+		done <"$R7_OUTPUTDIR/$host/$groupname/$funcname/output"
+		echo
+	done
+)
+
+alias output-errors=output_errors
+alias outerr=output_errors
+output_errors() (
 	output_status | tail -n +2 | while read -r host groupname funcname status; do
 		case $status in
 		0 | 30 | 31 | 32) continue ;;
 		esac
-		echo "- $host" "$groupname" "$funcname" "(error $status):"
 		echo
-		sed 's/^/	/' "$OUTPUTDIR/$host/$groupname/$funcname/output"
+		echo Summary:
 		echo
+		echo "- (error $status)" "in" "$host" "$groupname" "$funcname"
+		if test -f "$R7_OUTPUTDIR/$host/$groupname/$funcname/output"; then
+			echo
+			sed 's/^/	/' "$R7_OUTPUTDIR/$host/$groupname/$funcname/output"
+			echo
+		fi
 	done
-}
+)
 
+summary() (
+	output_errors
+	output_diffs
+	output_permissions
+)
+
 # Digraph functions
 # =================
 
 # Used in the digraph generation because inside a heredoc
 # context, memebers don't work as expected (OKSH).
-members2() (
+# shellcheck disable=SC2048,SC2086
+_members2() (
 	case $1 in
 	"'.*'")
 		nodes | grep -e '.*'
@@ -534,10 +818,13 @@ members2() (
 		;;
 	esac
 	set -f -- $(printf -- "-e %s " "$@")
-	nodes | grep "$@"
+	nodes | grep $*
 )
 
+alias digraph-host=digraph_host
+# shellcheck disable=SC2086
 digraph_host() (
+	_debug "DIGRAPH HOST"
 	cat <<-eof
 		digraph host {
 			layout="dot"
@@ -546,12 +833,12 @@ digraph_host() (
 			$(
 			set -f
 			for node in $(nodes); do echo "\"$node\" [peripheries=2 color=blue]"; done
-			grep "^group" "$SITE_CONFIG" | while read -r _ groupname nodes; do
+			grep 2>/dev/null "^group" "$R7_SITE_CONFIG" | while read -r _ groupname nodes; do
 				echo "\"$groupname\" [color=orange]"
-				for node in $(members2 $nodes); do
+				for node in $(_members2 $nodes); do
 					echo "\"$node\" -> \"$groupname\""
 				done
-				for funct in $(functs "$groupname"); do
+				for funct in $(group_functions "$groupname"); do
 					echo "\"$funct\" [color=red]"
 					echo "\"$groupname\" -> \"$funct\""
 				done
@@ -564,4 +851,4 @@ digraph_host() (
 # Main
 # ====
 
-main "$@"
+_main "${@:-}"
blob - /dev/null
blob + 26967829f75f73db0c75d2e834d4761aa4558bc1 (mode 644)
--- /dev/null
+++ testdir/wd01.dot
@@ -0,0 +1,19 @@
+digraph host {
+layout="dot"
+splines="true"
+graph [overlap="false", rankdir=LR, mode=KK]
+"alpha" [peripheries=2 color=blue]
+"beta" [peripheries=2 color=blue]
+"gamma" [peripheries=2 color=blue]
+"common" [color=orange]
+"alpha" -> "common"
+"beta" -> "common"
+"gamma" -> "common"
+"state_test" [color=red]
+"common" -> "state_test"
+"state_test_2" [color=red]
+"common" -> "state_test_2"
+"test0" [color=orange]
+"beta" -> "test0"
+"gamma" -> "test0"
+}
blob - /dev/null
blob + 170d291df93cedc3e94f632f3340b6f791b0fb45 (mode 644)
--- /dev/null
+++ testdir/wd1-init-find
@@ -0,0 +1,8 @@
+testdir/workdir1
+testdir/workdir1/groups
+testdir/workdir1/nodes
+testdir/workdir1/.ssh
+testdir/workdir1/.ssh/config
+testdir/workdir1/.ssh/id_ed25519_r7
+testdir/workdir1/.ssh/known_hosts
+testdir/workdir1/site-config
blob - /dev/null
blob + e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 (mode 644)
blob - /dev/null
blob + 6fb37fd5ca1f64efd90dad3fdc99c53b148cdf2e (mode 644)
--- /dev/null
+++ testdir/workdir0/.ssh/config
@@ -0,0 +1,21 @@
+Host *
+	AddKeysToAgent yes
+	BatchMode yes
+	ConnectTimeout 3
+	ConnectionAttempts 1
+	ControlMaster auto
+	ControlPath ~/wt/private/inda.re-r7/.ssh/control/%C
+	ControlPersist 2m
+	StrictHostKeyChecking yes
+	User root
+	UserKnownHostsFile ~/wt/private/inda.re-r7/.ssh/known_hosts
+	IdentityFile ~/wt/private/sd/.ssh/managed/inda.re/id_ed25519_%L
+
+Host est1
+	Hostname 10.10.0.1
+
+Host campfire
+	Hostname 10.10.0.2
+
+Host pekmez
+	Hostname 10.10.0.3
blob - /dev/null
blob + e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 (mode 644)
blob - /dev/null
blob + 610fdbe679782b1f0f516f70e7ac26af714dfd0c (mode 644)
--- /dev/null
+++ testdir/workdir0/groups/common
@@ -0,0 +1,7 @@
+state_test() {
+	test 1 -eq 1
+}
+
+state_test_2() {
+	test 2 -eq 3
+}
blob - /dev/null
blob + e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 (mode 644)
blob - /dev/null
blob + 3274534dee3a05ef6ac0979e25268f031f42db68 (mode 644)
--- /dev/null
+++ testdir/workdir0/site-config
@@ -0,0 +1,3 @@
+prefix install state doc
+group common '.*'
+group test0 gamma beta