diff options
author | Alejandro Colomar <alx@nginx.com> | 2023-06-21 13:39:40 +0200 |
---|---|---|
committer | Alejandro Colomar <alx@nginx.com> | 2023-06-30 14:35:59 +0200 |
commit | 543d478e12364b685aec5a903f6cd97fc75ad667 (patch) | |
tree | 58a29476a718d75a1f316abf4911bccb76214250 /tools/setup-unit | |
parent | d73526d27ca296400fa8f08aa0a23b8ea3ab7a15 (diff) | |
download | unit-543d478e12364b685aec5a903f6cd97fc75ad667.tar.gz unit-543d478e12364b685aec5a903f6cd97fc75ad667.tar.bz2 |
Tools: setup-unit: ctl: added "edit" subcommand.
Almost equivalent to b42f6b1d ("Tools: unitc edit mode for interactive
configuration."), implemented by Liam in tools/unitc.
I chose to give preference to vi(1) over vim(1) because Debian has vi(1)
as part of update-alternatives(1), so that sysadmins can configure it to
be a symlink to their favourite vi(1) implementation or variant.
We're ignoring the errors of the commands due to having the SSH tunnel
open. I should fix the script to use traps to close the tunnel on any
error, so we don't leak tunnels. Then, we'll be able to not ignore
curl(1) or editor errors. That will also probably allow moving the
tunneling code to the ctl command, thus deduplicating code.
Cc: Liam Crilly <liam@nginx.com>
Cc: Andrew Clayton <a.clayton@nginx.com>
Signed-off-by: Alejandro Colomar <alx@nginx.com>
Diffstat (limited to 'tools/setup-unit')
-rwxr-xr-x | tools/setup-unit | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/tools/setup-unit b/tools/setup-unit index d2cb127c..38592fe3 100755 --- a/tools/setup-unit +++ b/tools/setup-unit @@ -77,6 +77,7 @@ SYNOPSIS Subcommands ├── cmd [-h] ├── ctl [-h] [-s SOCK] SUBCOMMAND [ARGS] + │ ├── edit [-h] PATH │ ├── http [-h] [-c CURLOPT] METHOD PATH │ └── insert [-h] PATH INDEX ├── freeport [-h] @@ -208,6 +209,7 @@ SYNOPSIS $0 ctl [-h] [-s SOCK] SUBCOMMAND [ARGS] Subcommands + ├── edit [-h] PATH ├── http [-h] [-c CURLOPT] METHOD PATH └── insert [-h] PATH INDEX @@ -218,6 +220,8 @@ DESCRIPTION subcommand. SUBCOMMANDS + edit Edit the unitd(8) configuration with an editor. + http Send an HTTP request to the control API socket. insert Insert an element at the specified index into an array in the @@ -298,6 +302,10 @@ unit_ctl() fi; case $1 in + edit) + shift; + unit_ctl_edit ${remote:+ ---r $remote} ---s "$sock" $@; + ;; http) shift; unit_ctl_http ${remote:+ ---r $remote} ---s "$sock" $@; @@ -313,6 +321,110 @@ unit_ctl() } +help_unit_ctl_edit() +{ + cat <<__EOF__ ; +SYNOPSIS + $0 ctl [CTL-OPTS] edit [-h] PATH + +DESCRIPTION + Edit the JSON configuration with an editor. The current configuration + is downloaded into a temporary file, open with the editor, and then + sent back to the control API socket. + + The following editors are tried in this order of preference: \$VISUAL, + \$EDITOR, editor(1), vi(1), vim(1), ed(1). + + +OPTIONS + -h, --help + Print this help. + +ENVIRONMENT + VISUAL + EDITOR + See environ(7). + +SEE ALSO + $0 ctl http -h; + + update-alternatives(1) + +__EOF__ +} + + +unit_ctl_edit() +{ + while test $# -ge 1; do + case "$1" in + -h | --help) + help_unit_ctl_edit; + exit 0; + ;; + ---r | ----remote) + local remote="$2"; + shift; + ;; + ---s | ----sock) + local sock="$2"; + shift; + ;; + -*) + err "ctl: edit: $1: Unknown option."; + ;; + *) + break; + ;; + esac; + shift; + done; + + if ! test $# -ge 1; then + err 'ctl: insert: PATH: Missing argument.'; + fi; + local req_path="$1"; + + if test -v remote; then + local remote_sock="$(echo "$sock" | unit_sock_filter -s)"; + local local_sock="$(mktemp -u -p /var/run/unit/)"; + local ssh_ctrl="$(mktemp -u -p /var/run/unit/)"; + + mkdir -p /var/run/unit/; + + ssh -fMNnT -S "$ssh_ctrl" \ + -o 'ExitOnForwardFailure yes' \ + -L "$local_sock:$remote_sock" "$remote"; + + sock="unix:$local_sock"; + fi; + + local tmp="$(mktemp ||:)"; + + unit_ctl_http ---s "$sock" -c --no-progress-meter GET "$req_path" \ + </dev/null >"$tmp" \ + ||:; + + $( + ((test -v VISUAL && test -n "$VISUAL") && printf '%s\n' "$VISUAL") \ + || ((test -v EDITOR && test -n "$EDITOR") && printf '%s\n' "$EDITOR") \ + || command -v editor \ + || command -v vi \ + || command -v vim \ + || echo ed; + ) "$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"; + fi; +} + + help_unit_ctl_http() { cat <<__EOF__ ; |