Commit Diff


commit - /dev/null
commit + 03d8f5213dbe512a55fe90b3c5f9947fa9954d2d
blob - /dev/null
blob + b36e88e8c4dffc7934cba2da2b1664f4b00d2509 (mode 644)
--- /dev/null
+++ Makefile
@@ -0,0 +1,4 @@
+PREFIX:=/usr/local
+install:
+	cp ./r7 $(PREFIX)/bin/
+	chmod 755 $(PREFIX)/bin/r7
blob - /dev/null
blob + e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 (mode 644)
blob - /dev/null
blob + e0c4a899363bf2bd71af7b64f5cf24a5a78ff305 (mode 755)
--- /dev/null
+++ r7
@@ -0,0 +1,552 @@
+#!/bin/sh
+# shellcheck disable=SC2046
+
+# Global configuration
+# ====================
+
+: "${DEBUG:=no}"
+: "${DRYRUN:=no}"
+: "${HOSTNAME:=$(uname -n)}"
+: "${PARALLEL:=no}"
+: "${SHOWOUTPUT:=no}"
+: "${SSH_IDENTITY_FILE:=~/wt/private/sd/.ssh/managed/inda.re/id_ed25519_${HOSTNAME%%.*}}"
+: "${WORKDIR:=$PWD}"
+: "${SITE_CONFIG:=$WORKDIR/site-config}"
+: "${RUNDIR:=$WORKDIR/_run}"
+: "${OUTPUTDIR:=$WORKDIR/_output}"
+: "${SSH_CONFIG_FILE:=$WORKDIR/.ssh/config}"
+: "${SSH_CONNECT_TIMEOUT:=3}"
+: "${PREFIX:=install state doc}"
+
+# Interface
+# =========
+
+usage() {
+	cat <<eof
+usage: $0 [options] command
+usage options:
+	-h	Show usage and exit
+	-d	Debug
+	-i	SSH identity file
+	-F	SSH client configuration file
+	-w	Work directory where to find ./nodes and ./groups
+	-r	Run directory, default to ./_run
+	-o	Output directory, default to ./_output
+	-c	Site configuration file, default to ./site-config
+	-n	Dry run, prevent any host modification
+	-p	Run each host in parallel per group call
+	-O	Show output
+	-P	Prefix
+
+commands:
+	run-config		Run the site configuration
+	group			Deploy specified group to hosts
+	output-dump		Dump the run directory to the output directory
+	output			Consult the output
+	digraph-host		Produce digraph host
+
+site-config format:
+	prefix [prefix...] # Define the prefix to be executed in order
+	group groupname ['.*'|hostname...] # Deploy specified group to hosts
+
+eof
+}
+
+# shellcheck disable=SC2086
+main() {
+	while getopts 'P:F:r:o:i:w:c:npdOh' arg; do
+		case $arg in
+		h)
+			usage
+			exit 0
+			;;
+		d) DEBUG=yes ;;
+		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 ;;
+		P)
+			PREFIX=$OPTARG
+			prefix $PREFIX
+			;;
+		O) SHOWOUTPUT=yes ;;
+		*)
+			echo 1>&2 "Unknown option"
+			exit 2
+			;;
+		esac
+	done
+	shift $((OPTIND - 1))
+	case $1 in
+	run-config)
+		run_config
+		;;
+	group)
+		shift
+		trace_id=$(trace_id)
+		prefix $PREFIX
+		group "$@"
+		;;
+	output) output ;;
+	output-dump) output_dump ;;
+	digraph-host)
+		prefix $PREFIX
+		digraph_host
+		;;
+	*)
+		case $0 in
+		*r7)
+			echo 1>&2 "Unknown command or option"
+			exit 2
+			;;
+		*)
+			prefix $PREFIX
+			;;
+		esac
+		;;
+	esac
+}
+
+# SSH Functions
+# =============
+
+ssh() (
+	debug "SSH $*"
+	/usr/bin/ssh "-oConnectTimeout=$SSH_CONNECT_TIMEOUT" -F "$SSH_CONFIG_FILE" "$@"
+)
+
+unlock() {
+	pkill ssh-agent
+	eval "$(ssh-agent)"
+	ssh-add "$SSH_IDENTITY_FILE"
+}
+
+copy() (
+	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"
+			openrsync -e "/usr/bin/ssh -oConnectTimeout=$SSH_CONNECT_TIMEOUT -F $SSH_CONFIG_FILE" \
+				--rsync-path="$r" \
+				--delete \
+				-a "$1" "$2" && return 0
+		fi
+	fi
+	if r=$(ssh "$node" command -v rsync); then
+		if rsync -V 1>/dev/null; then
+			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"
+	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"
+)
+
+# Deployment functions
+# ====================
+
+trace_id() {
+	date +%s
+}
+
+nodes() (
+	cd "$WORKDIR" || return 2
+	find nodes -type d -maxdepth 1 | tail -n +2 | awk -F/ '{ print $2 }'
+)
+
+nodes_active() (
+	if test -s "$WORKDIR/.active_nodes"; then
+		cat "$WORKDIR/.active_nodes"
+	else
+		touch "$WORKDIR/.active_nodes"
+		for node in $(nodes); do
+			debug "ACTIVE $node"
+			if ssh "$node" :; then
+				printf "%s\\n" "$node" >>"$WORKDIR/.active_nodes"
+			fi
+		done
+		cat "$WORKDIR/.active_nodes"
+	fi
+)
+
+run_config() {
+	trace_id=$(trace_id)
+	# shellcheck disable=SC1091
+	test -f ./site-config && . ./site-config
+}
+
+# Inherits $target from group()
+# shellcheck disable=SC2031
+deploy() (
+	debug "DEPLOY $new_name $target"
+	cd "$WORKDIR" || exit 2
+	stem="groups/$new_name"
+	nodedir="nodes/$target"
+
+	if test -d "$stem.dir"; then
+		test "$DRYRUN" = no && {
+			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 -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
+			}
+		}
+	fi
+
+	if test -f "$stem"; then
+		test "$DRYRUN" = no && {
+			payload "$stem" | ssh 2>&1 "$target" /bin/sh - |
+				collect "$RUNDIR/$trace_id.$target.$new_name"
+		}
+	fi
+)
+
+members() (
+	set -f -- $(printf -- "-e %s " "$@")
+	nodes_active | grep "$@"
+)
+
+group() (
+	cd "$WORKDIR" || return 2
+	set -fm
+	umask 027
+	mkdir -p "$RUNDIR"
+	cd "$RUNDIR" || return 2
+	new_name=$1
+	shift
+	# shellcheck disable=SC2046
+	for target in $(members "$@"); do
+		case $PARALLEL in
+		yes)
+			deploy &
+			;;
+		*)
+			deploy
+			;;
+		esac
+	done
+	wait
+)
+
+functs() (
+	for pre in $prefix; do
+		grep -e "^${pre}_.*()" "$WORKDIR/groups/$1" | cut -d\( -f1
+	done
+)
+
+call() (
+	cd "$WORKDIR" || return 2
+	grep -e "^$2_.*()" "$1" |
+		cut -d\( -f1 | while read -r function; do
+		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() (
+	echo "trace_id=$trace_id"
+	cat <<-'eof'
+		trace() {
+			echo ":: $trace_id $(nodename) $*" 1>&2
+		}
+		error() {
+			echo "!! ERROR $*" 1>&2
+		}
+		nodename() {
+			hostname=$(uname -n)
+			echo "${hostname%%.*}"
+		}  
+		node() {
+			atom=/tmp/r7/$(nodename)/$1
+			test -f "$atom" || return 0
+			test -d "$atom" || return 0
+			return 31
+		}
+		nodedir() {
+			echo "/tmp/r7/$(nodename)"
+		}
+		nodeinstall() {
+			while getopts "o:m:" arg; do
+				case $arg in
+				o) owner="$OPTARG" ;;
+				m) mode="$OPTARG" ;;
+				*) ;;
+				esac
+			done
+			shift $(( OPTIND - 1 ))
+			source=$1
+			target=$2
+			umask 027
+			test -f "$source" || return 2
+			cmp -s "$source" "$target"
+			case "$?" in
+			0) return 30 ;;
+			1) 
+				diff -U 2 "$target" "$source"
+				set -e
+				cp "$source" "$target"
+				if test -n "$owner"; then
+					chown "$owner" "$target"
+				fi
+				if test -n "$mode"; then
+					chmod "$mode" "$target"
+				fi
+				return 0
+				;;
+			esac
+		}
+		cd /tmp/r7
+		trace CONNECTED $(date +%s)
+	eof
+	cat "$1"
+	debug "PREFIX $prefix"
+	for run in $prefix; do
+		call "$1" "$run"
+	done
+	cat <<-'eof'
+		trace DISCONNECT $(date +%s)
+	eof
+)
+
+# Interactive output function
+# ===========================
+
+debug() {
+	if test "$DEBUG" = "yes"; then
+		printf %s\\n "${*:-$(cat -)}" >&2
+	fi
+}
+
+collect() (
+	tee -a "$1" | while read -r line; do
+		set -- $line
+		case $line in
+		::*)
+			trace_id=$2
+			host=$3
+			group=$4
+			function=$5
+			status=$6
+			endcode=$7
+			case $status in
+			START)
+				in_function=1
+				if test "$SHOWOUTPUT" = yes; then
+					echo "- (output) $host:$group::$function"
+				fi
+				continue
+				;;
+			END)
+				in_function=0
+				case $endcode in
+				0 | 30 | 31 | 32) echo "- (ok) $host:$group::$function" ;;
+				*) echo "! (error $endcode) $host:$group::$function" ;;
+				esac
+				;;
+			esac
+			;;
+		!!*)
+			shift
+			echo "! (output) $host:$group::$function"
+			echo "$*"
+			;;
+		esac
+		if test "$SHOWOUTPUT" = yes; then
+			test "$in_function" -eq 1 &&
+				printf "%s\\n" "$line"
+		fi
+	done
+)
+
+# Text output functions
+# =====================
+
+output_dump() (
+	set -e -- $(output_latest)
+	mkdir -p "$OUTPUTDIR"
+	awk -voutput_dir="$OUTPUTDIR" '
+		BEGIN {
+			trace_id=""
+			hostname=""
+			funcname=""
+			groupname=""
+			in_function_block=0
+		}
+		/^:: [0-9].*/ {
+			if ($4 == "CONNECTED") {
+				trace_id=$2
+				hostname=$3
+				timestamp_connect=$5
+				system("mkdir -p " output_dir "/" hostname)
+			}
+		}
+		$2 == trace_id && $6 == "START" {
+			groupname=$4
+			funcname=$5
+			in_function_block=1
+			output_node = output_dir "/" hostname "/" groupname "/" funcname
+			output_status = output_node "/status"
+			output_output = output_node "/output"
+			system("rm -fr " output_node)
+			system("mkdir -p " output_node)
+			next
+		}
+		$2 == trace_id && $6 == "END" {
+			in_function_block=0
+			system("echo " $7 " >" output_status)
+		}
+		in_function_block == 1 {
+			print > output_output
+		}
+		$2 == trace_id && $4 == "DISCONNECT" {
+			timestamp_disconnect=$5
+			difference=timestamp_disconnect - timestamp_connect
+			output_duration = output_dir "/" hostname "/" groupname "/duration"
+			system("echo " difference " >" output_duration)
+		}
+	' "$@"
+)
+
+output_latest() {
+	latest=$(
+		# shellcheck disable=SC2012
+		ls -lt1 "$WORKDIR/_run/" | while read -r file; do
+			echo "${file%%.*}"
+			break
+		done
+	)
+	case $(uname) in
+	OpenBSD | FreeBSD | Darwin)
+		latest_date=$(date -r "$latest")
+		;;
+	Linux)
+		latest_date=$(date -d "@$latest")
+		;;
+	*)
+		latest_date=$(perl -e "print scalar localtime($latest)")
+		;;
+	esac
+	if test -n "$latest_date"; then
+		find "$WORKDIR/_run/" -type f -name "$latest.*"
+	fi
+}
+
+output_files() (
+	cd "$OUTPUTDIR" || return 2
+	find ./* -type f -name 'output'
+)
+
+status_files() (
+	cd "$OUTPUTDIR" || return 2
+	find ./* -type f -name 'status'
+)
+
+duration_files() (
+	cd "$OUTPUTDIR" || return 2
+	find ./* -type f -name 'duration'
+)
+
+output_status() (
+	cd "$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
+				echo "$host" "$groupname" "$funcname" "$(cat "$status")"
+			done
+		done
+	} | column -t
+)
+
+output_duration() (
+	cd "$OUTPUTDIR" || return 2
+	duration_files | {
+		echo HOST GROUP DURATION
+		while read -r duration; do
+			echo "$duration" | while IFS=/ read -r _ host groupname _; do
+				echo "$host" "$groupname" "$(cat "$duration")"
+			done
+		done
+	} | column -t
+)
+
+output() (
+	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"
+			echo
+		done
+	done
+)
+
+# Digraph functions
+# =================
+
+# Used in the digraph generation because inside a heredoc
+# context, memebers don't work as expected (OKSH).
+members2() (
+	case $1 in
+	"'.*'")
+		nodes | grep -e '.*'
+		return 0
+		;;
+	esac
+	set -f -- $(printf -- "-e %s " "$@")
+	nodes | grep "$@"
+)
+
+digraph_host() (
+	cat <<-eof
+		digraph host {
+			layout="dot"
+			splines="true"
+			graph [overlap="false", rankdir=LR, mode=KK]
+			$(
+			set -f
+			for node in $(nodes); do echo "\"$node\" [peripheries=2 color=blue]"; done
+			grep "^group" "$SITE_CONFIG" | while read -r _ groupname nodes; do
+				echo "\"$groupname\" [color=orange]"
+				for node in $(members2 $nodes); do
+					echo "\"$node\" -> \"$groupname\""
+				done
+				for funct in $(functs "$groupname"); do
+					echo "\"$funct\" [color=red]"
+					echo "\"$groupname\" -> \"$funct\""
+				done
+			done
+		)
+		}
+	eof
+)
+
+# Main
+# ====
+
+main "$@"