summaryrefslogtreecommitdiffhomepage
path: root/tools/setup-unit
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2024-02-09 12:37:14 +0100
committerAlejandro Colomar <alx@kernel.org>2024-02-20 16:04:23 +0100
commitcca2c46e4995497c091073d51d7148eac5cf140a (patch)
treec5d4c15c8f66477a428503902a6cfe4119ba681e /tools/setup-unit
parent2765522b94e748d01e449d5508f83ea4a5b46ea5 (diff)
downloadunit-cca2c46e4995497c091073d51d7148eac5cf140a.tar.gz
unit-cca2c46e4995497c091073d51d7148eac5cf140a.tar.bz2
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: 543d478e1236 ("Tools: setup-unit: ctl: added "edit" subcommand.") Fixes: 3778877eb3be ("Tools: Added subcommands to setup-unit.") Reviewed-by: Andrew Clayton <a.clayton@nginx.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
Diffstat (limited to 'tools/setup-unit')
-rwxr-xr-xtools/setup-unit53
1 files changed, 33 insertions, 20 deletions
diff --git a/tools/setup-unit b/tools/setup-unit
index e9ad5896..5b5b3ccc 100755
--- a/tools/setup-unit
+++ b/tools/setup-unit
@@ -29,6 +29,7 @@ test -v BASH_VERSION \
test -v ZSH_VERSION \
&& setopt sh_word_split;
+
export LC_ALL=C
dry_run='no';
@@ -164,6 +165,18 @@ dry_run_eval()
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()
{
@@ -399,14 +412,16 @@ unit_ctl_edit()
-o 'ExitOnForwardFailure yes' \
-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";
fi;
- local tmp="$(mktemp ||:)";
+ local tmp="$(mktemp)";
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") \
@@ -415,15 +430,12 @@ unit_ctl_edit()
|| command -v vi \
|| command -v vim \
|| 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
- ssh -S "$ssh_ctrl" -O exit "$remote" 2>/dev/null;
- unlink "$local_sock";
+ run_trap EXIT;
fi;
}
@@ -519,16 +531,17 @@ unit_ctl_http()
-o 'ExitOnForwardFailure yes' \
-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";
fi;
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
- ssh -S "$ssh_ctrl" -O exit "$remote" 2>/dev/null;
- unlink "$local_sock";
+ run_trap EXIT;
fi;
}
@@ -603,22 +616,22 @@ unit_ctl_insert()
-o 'ExitOnForwardFailure yes' \
-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";
fi;
- local old="$(mktemp ||:)";
+ local old="$(mktemp)";
unit_ctl_http ---s "$sock" -c --no-progress-meter GET "$req_path" \
- </dev/null >"$old" \
- ||:;
+ </dev/null >"$old";
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
- ssh -S "$ssh_ctrl" -O exit "$remote" 2>/dev/null;
- unlink "$local_sock";
+ run_trap EXIT;
fi;
}