Tools: setup-unit: Use trap(1) to handle cleanup

This allows listening to command exit statuses.  Before this change, we
had to ignore the exit status of curl(1) (and a few other commands),
since otherwise the script would go kaboom and not cleanup the ssh(1)
tunnels.

Fixes: 543d478e12 ("Tools: setup-unit: ctl: added "edit" subcommand.")
Fixes: 3778877eb3 ("Tools: Added subcommands to setup-unit.")
Reviewed-by: Andrew Clayton <a.clayton@nginx.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2024-02-09 12:37:14 +01:00
parent 2765522b94
commit cca2c46e49

View File

@@ -29,6 +29,7 @@ test -v BASH_VERSION \
test -v ZSH_VERSION \ test -v ZSH_VERSION \
&& setopt sh_word_split; && setopt sh_word_split;
export LC_ALL=C export LC_ALL=C
dry_run='no'; dry_run='no';
@@ -164,6 +165,18 @@ dry_run_eval()
fi; fi;
} }
run_trap()
{
trap -p "$1" \
| tr -d '\n' \
| sed "s/[^']*'\(.*\)'[^']*/\1/" \
| sed "s/'\\\\''/'/g" \
| read -r trap_cmd;
eval $trap_cmd;
trap - "$1";
}
help_unit_cmd() help_unit_cmd()
{ {
@@ -399,14 +412,16 @@ unit_ctl_edit()
-o 'ExitOnForwardFailure yes' \ -o 'ExitOnForwardFailure yes' \
-L "$local_sock:$remote_sock" "$remote"; -L "$local_sock:$remote_sock" "$remote";
trap "ssh -S '$ssh_ctrl' -O exit '$remote' 2>/dev/null;
unlink '$local_sock'" EXIT;
sock="unix:$local_sock"; sock="unix:$local_sock";
fi; fi;
local tmp="$(mktemp ||:)"; local tmp="$(mktemp)";
unit_ctl_http ---s "$sock" -c --no-progress-meter GET "$req_path" \ unit_ctl_http ---s "$sock" -c --no-progress-meter GET "$req_path" \
</dev/null >"$tmp" \ </dev/null >"$tmp";
||:;
$( $(
((test -v VISUAL && test -n "$VISUAL") && printf '%s\n' "$VISUAL") \ ((test -v VISUAL && test -n "$VISUAL") && printf '%s\n' "$VISUAL") \
@@ -415,15 +430,12 @@ unit_ctl_edit()
|| command -v vi \ || command -v vi \
|| command -v vim \ || command -v vim \
|| echo ed; || echo ed;
) "$tmp" \ ) "$tmp";
||:;
unit_ctl_http ---s "$sock" PUT "$req_path" <"$tmp" \ unit_ctl_http ---s "$sock" PUT "$req_path" <"$tmp";
||:;
if test -v remote; then if test -v remote; then
ssh -S "$ssh_ctrl" -O exit "$remote" 2>/dev/null; run_trap EXIT;
unlink "$local_sock";
fi; fi;
} }
@@ -519,16 +531,17 @@ unit_ctl_http()
-o 'ExitOnForwardFailure yes' \ -o 'ExitOnForwardFailure yes' \
-L "$local_sock:$remote_sock" "$remote"; -L "$local_sock:$remote_sock" "$remote";
trap "ssh -S '$ssh_ctrl' -O exit '$remote' 2>/dev/null;
unlink '$local_sock'" EXIT;
sock="unix:$local_sock"; sock="unix:$local_sock";
fi; fi;
curl $curl_options -X $method -d@- \ curl $curl_options -X $method -d@- \
$(echo "$sock" | unit_sock_filter -c)${req_path} \ $(echo "$sock" | unit_sock_filter -c)${req_path};
||:;
if test -v remote; then if test -v remote; then
ssh -S "$ssh_ctrl" -O exit "$remote" 2>/dev/null; run_trap EXIT;
unlink "$local_sock";
fi; fi;
} }
@@ -603,22 +616,22 @@ unit_ctl_insert()
-o 'ExitOnForwardFailure yes' \ -o 'ExitOnForwardFailure yes' \
-L "$local_sock:$remote_sock" "$remote"; -L "$local_sock:$remote_sock" "$remote";
trap "ssh -S '$ssh_ctrl' -O exit '$remote' 2>/dev/null;
unlink '$local_sock'" EXIT;
sock="unix:$local_sock"; sock="unix:$local_sock";
fi; fi;
local old="$(mktemp ||:)"; local old="$(mktemp)";
unit_ctl_http ---s "$sock" -c --no-progress-meter GET "$req_path" \ unit_ctl_http ---s "$sock" -c --no-progress-meter GET "$req_path" \
</dev/null >"$old" \ </dev/null >"$old";
||:;
unit_json_ins "$old" "$idx" \ unit_json_ins "$old" "$idx" \
| unit_ctl_http ---s "$sock" PUT "$req_path" \ | unit_ctl_http ---s "$sock" PUT "$req_path";
||:;
if test -v remote; then if test -v remote; then
ssh -S "$ssh_ctrl" -O exit "$remote" 2>/dev/null; run_trap EXIT;
unlink "$local_sock";
fi; fi;
} }