diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/README.md | 2 | ||||
-rwxr-xr-x | tools/setup-unit | 176 | ||||
-rwxr-xr-x | tools/unitc | 10 |
3 files changed, 81 insertions, 107 deletions
diff --git a/tools/README.md b/tools/README.md index 883bc107..e7caae34 100644 --- a/tools/README.md +++ b/tools/README.md @@ -33,7 +33,7 @@ web page with NGINX Unit. | Options | | |---------|-| -| filename … | Read configuration data consequently from the specified files instead of stdin. +| filename … | Read configuration data consecutively from the specified files instead of stdin. | _HTTP method_ | It is usually not required to specify a HTTP method. `GET` is used to read the configuration. `PUT` is used when making configuration changes unless a specific method is provided. | `edit` | Opens **URI** in the default editor for interactive configuration. The [jq](https://stedolan.github.io/jq/) tool is required for this option. | `INSERT` | A _virtual_ HTTP method that prepends data when the URI specifies an existing array. The [jq](https://stedolan.github.io/jq/) tool is required for this option. diff --git a/tools/setup-unit b/tools/setup-unit index 38592fe3..688e87a1 100755 --- a/tools/setup-unit +++ b/tools/setup-unit @@ -3,7 +3,8 @@ ##################################################################### # # Copyright (C) NGINX, Inc. -# Author: NGINX Unit Team, F5 Inc. +# Author: NGINX Unit Team, F5 Inc. +# Copyright 2024, Alejandro Colomar <alx@kernel.org> # ##################################################################### @@ -28,6 +29,7 @@ test -v BASH_VERSION \ test -v ZSH_VERSION \ && setopt sh_word_split; + export LC_ALL=C dry_run='no'; @@ -36,7 +38,7 @@ help_unit() { cat <<__EOF__ ; SYNOPSIS - $0 [-h] COMMAND [ARGS] + $0 [-h[h]] COMMAND [ARGS] Subcommands ├── repo-config [-hn] [PKG-MANAGER OS-NAME OS-VERSION] @@ -61,9 +63,8 @@ OPTIONS -h, --help Print this help. - --help-more - Print help for more commands. They are experimental. Using - these isn't recommended, unless you know what you're doing. + -hh, --help-more + Print help for more (advanced) commands. __EOF__ } @@ -72,14 +73,14 @@ help_more_unit() { cat <<__EOF__ ; SYNOPSIS - $0 [-h] COMMAND [ARGS] + $0 [-h[h]] COMMAND [ARGS] Subcommands ├── cmd [-h] ├── ctl [-h] [-s SOCK] SUBCOMMAND [ARGS] - │ ├── edit [-h] PATH - │ ├── http [-h] [-c CURLOPT] METHOD PATH - │ └── insert [-h] PATH INDEX + │ ├── edit [-h] PATH + │ ├── http [-h] [-c CURLOPT] METHOD PATH + │ └── insert [-h] PATH INDEX ├── freeport [-h] ├── json-ins [-hn] JSON INDEX ├── os-probe [-h] @@ -87,8 +88,8 @@ SYNOPSIS ├── repo-config [-hn] [PKG-MANAGER OS-NAME OS-VERSION] ├── restart [-hls] ├── sock [-h] SUBCOMMAND [ARGS] - │ ├── filter [-chs] - │ └── find [-h] + │ ├── filter [-chs] + │ └── find [-h] └── welcome [-hn] DESCRIPTION @@ -118,6 +119,9 @@ COMMANDS Configure your package manager with the NGINX Unit repository for later installation. + restart + Restart all running unitd(8) instances. + sock Print the control API socket address. welcome @@ -128,12 +132,17 @@ OPTIONS -h, --help Print basic help (some commands are hidden). - --help-more + -hh, --help-more Print the hidden help with more commands. __EOF__ } +info() +{ + >&2 echo "$(basename "$0"): info: $*"; +} + warn() { >&2 echo "$(basename "$0"): error: $*"; @@ -161,6 +170,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() { @@ -299,25 +320,44 @@ unit_ctl() if echo $sock | grep '^ssh://' >/dev/null; then local remote="$(echo $sock | sed 's,\(ssh://[^/]*\).*,\1,')"; local sock="$(echo $sock | sed 's,ssh://[^/]*\(.*\),unix:\1,')"; + + 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"; + + trap "ssh -S '$ssh_ctrl' -O exit '$remote' 2>/dev/null; + unlink '$local_sock';" EXIT; + + sock="unix:$local_sock"; fi; case $1 in edit) shift; - unit_ctl_edit ${remote:+ ---r $remote} ---s "$sock" $@; + unit_ctl_edit ---s "$sock" $@; ;; http) shift; - unit_ctl_http ${remote:+ ---r $remote} ---s "$sock" $@; + unit_ctl_http ---s "$sock" $@; ;; insert) shift; - unit_ctl_insert ${remote:+ ---r $remote} ---s "$sock" $@; + unit_ctl_insert ---s "$sock" $@; ;; *) err "ctl: $1: Unknown argument."; ;; esac; + + if test -v remote; then + run_trap EXIT; + fi; } @@ -362,10 +402,6 @@ unit_ctl_edit() help_unit_ctl_edit; exit 0; ;; - ---r | ----remote) - local remote="$2"; - shift; - ;; ---s | ----sock) local sock="$2"; shift; @@ -381,29 +417,19 @@ unit_ctl_edit() done; if ! test $# -ge 1; then - err 'ctl: insert: PATH: Missing argument.'; + err 'ctl: edit: 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 ||:)"; + echo "$req_path" \ + | sed 's%^/js_modules/.*%.js%' \ + | sed 's%^/config\>.*%.json%' \ + | sed 's%^/.*%.txt%' \ + | xargs mktemp --suffix \ + | read -r tmp; 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") \ @@ -412,16 +438,13 @@ unit_ctl_edit() || command -v vi \ || command -v vim \ || echo ed; - ) "$tmp" \ - ||:; + ) "$tmp"; - unit_ctl_http ---s "$sock" PUT "$req_path" <"$tmp" \ - ||:; + trap "info 'ctl: edit: Invalid configuration saved in <$tmp>.'" ERR - if test -v remote; then - ssh -S "$ssh_ctrl" -O exit "$remote" 2>/dev/null; - unlink "$local_sock"; - fi; + unit_ctl_http ---s "$sock" PUT "$req_path" <"$tmp"; + + trap - ERR; } @@ -477,10 +500,6 @@ unit_ctl_http() help_unit_ctl_http; exit 0; ;; - ---r | ----remote) - local remote="$2"; - shift; - ;; ---s | ----sock) local sock="$2"; shift; @@ -505,28 +524,8 @@ unit_ctl_http() fi; local req_path="$2"; - 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; - - curl $curl_options -X $method -d@- \ - $(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"; - fi; + curl --fail-with-body $curl_options -X $method -d@- \ + $(echo "$sock" | unit_sock_filter -c)${req_path}; } @@ -561,10 +560,6 @@ unit_ctl_insert() help_unit_ctl_insert; exit 0; ;; - ---r | ----remote) - local remote="$2"; - shift; - ;; ---s | ----sock) local sock="$2"; shift; @@ -589,34 +584,13 @@ unit_ctl_insert() fi; local idx="$2"; - 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 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" \ - ||:; - - if test -v remote; then - ssh -S "$ssh_ctrl" -O exit "$remote" 2>/dev/null; - unlink "$local_sock"; - fi; + | unit_ctl_http ---s "$sock" PUT "$req_path"; } @@ -786,7 +760,7 @@ unit_ctl_welcome() <hr> <p><a href="https://unit.nginx.org/?referer=welcome">NGINX Unit — the universal web app server</a><br> - NGINX, Inc. © 2023</p> + NGINX, Inc. © 2024</p> </body> </html> __EOF__'; @@ -1634,7 +1608,7 @@ while test $# -ge 1; do help_unit; exit 0; ;; - --help-more) + -hh | --help-more) help_more_unit; exit 0; ;; diff --git a/tools/unitc b/tools/unitc index 4ab5f663..9fba4c6d 100755 --- a/tools/unitc +++ b/tools/unitc @@ -1,7 +1,7 @@ #!/bin/bash # unitc - a curl wrapper for configuring NGINX Unit -# https://github.com/nginx/unit/tree/master/tools -# NGINX, Inc. (c) 2023 +# https://github.com/nginx/unit/tree/master/tools +# NGINX, Inc. (c) 2024 # Defaults # @@ -186,9 +186,9 @@ if [ $REMOTE -eq 0 ]; then echo "${0##*/}: WARNING: unable to identify unitd command line parameters for PID $PID, assuming unitd defaults from \$PATH" PARAMS=unitd fi - CTRL_ADDR=$(echo "$PARAMS" | grep '\--control' | cut -f2 -d' ') + CTRL_ADDR=$(echo "$PARAMS" | grep '\--control ' | cut -f2 -d' ') if [ "$CTRL_ADDR" = "" ]; then - CTRL_ADDR=$($(echo "$PARAMS") --help | grep -A1 '\--control' | tail -1 | cut -f2 -d\") + CTRL_ADDR=$($(echo "$PARAMS") --help | grep -A1 '\--control ADDRESS' | tail -1 | cut -f2 -d\") fi if [ "$CTRL_ADDR" = "" ]; then echo "${0##*/}: ERROR: cannot detect control socket. Did you start unitd with a relative path? Try starting unitd with --control option." @@ -292,7 +292,7 @@ else exit 1 fi NEW_ELEMENT=$(cat ${CONF_FILES[@]}) - echo $NEW_ELEMENT | jq > /dev/null || exit $? # Test the input is valid JSON before proceeding + echo $NEW_ELEMENT | jq > /dev/null || exit $? # Test the input is valid JSON before proceeding OLD_ARRAY=$($RPC_CMD curl -s $UNIT_CTRL$URI) if [ "$(echo $OLD_ARRAY | jq -r type)" = "array" ]; then echo $OLD_ARRAY | jq ". |= [$NEW_ELEMENT] + ." | $RPC_CMD curl -X PUT --data-binary @- $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ | $OUTPUT |