diff options
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/setup-unit | 88 | ||||
-rwxr-xr-x | tools/unitc | 18 |
2 files changed, 52 insertions, 54 deletions
diff --git a/tools/setup-unit b/tools/setup-unit index 79dab850..de1d4f5f 100755 --- a/tools/setup-unit +++ b/tools/setup-unit @@ -9,11 +9,13 @@ if test -n ${BASH_VERSION} && test "${BASH_VERSINFO[0]}" -eq 3; then - >&2 echo 'Your version of bash(1) isn't supported by this script.'; - >&2 echo "You're probably running on macOS. We recommend that you either"; - >&2 echo 'install a newer version of bash(1) or run this script with'; - >&2 echo 'another shell, such as zsh(1):'; - >&2 echo " $ zsh ${SUDO_USER:+sudo }$0 ..."; + >&2 cat <<__EOF__ ; +Your version of bash(1) isn't supported by this script. You're probably +running on macOS. We recommend that you either install a newer version +of bash(1) or run this script with another shell, such as zsh(1): + + $ ${SUDO_USER:+sudo }zsh $0 ... +__EOF__ exit 1; fi; @@ -23,6 +25,9 @@ set -Eefuo pipefail; test -v BASH_VERSION \ && shopt -s lastpipe; +test -v ZSH_VERSION \ +&& setopt sh_word_split; + export LC_ALL=C program_name="$0"; @@ -37,8 +42,8 @@ SYNOPSIS $program_name [-h] COMMAND [ARGS] Subcommands - +-- repo-config [-hn] [PKG-MANAGER OS-NAME OS-VERSION] - +-- welcome [-hn] + ├── repo-config [-hn] [PKG-MANAGER OS-NAME OS-VERSION] + └── welcome [-hn] DESCRIPTION This script simplifies installing and configuring an NGINX Unit server @@ -73,19 +78,19 @@ SYNOPSIS $program_name [-h] COMMAND [ARGS] Subcommands - +-- cmd [-h] - +-- ctl [-h] [-s SOCK] SUBCOMMAND [ARGS] - | +-- http [-h] [-c CURLOPT] METHOD PATH - | +-- insert [-h] PATH INDEX - +-- freeport [-h] - +-- json-ins [-hn] JSON INDEX - +-- os-probe [-h] - +-- ps [-h] [-t TYPE] - +-- repo-config [-hn] [PKG-MANAGER OS-NAME OS-VERSION] - +-- sock [-h] SUBCOMMAND [ARGS] - | +-- filter [-chs] - | +-- find [-h] - +-- welcome [-hn] + ├── cmd [-h] + ├── ctl [-h] [-s SOCK] SUBCOMMAND [ARGS] + │ ├── http [-h] [-c CURLOPT] METHOD PATH + │ └── insert [-h] PATH INDEX + ├── freeport [-h] + ├── json-ins [-hn] JSON INDEX + ├── os-probe [-h] + ├── ps [-h] [-t TYPE] + ├── repo-config [-hn] [PKG-MANAGER OS-NAME OS-VERSION] + ├── sock [-h] SUBCOMMAND [ARGS] + │ ├── filter [-chs] + │ └── find [-h] + └── welcome [-hn] DESCRIPTION This script simplifies installing and configuring @@ -205,8 +210,8 @@ SYNOPSIS $program_name ctl [-h] [-s SOCK] SUBCOMMAND [ARGS] Subcommands - +-- http [-h] [-c CURLOPT] METHOD PATH - +-- insert [-h] PATH INDEX + ├── http [-h] [-c CURLOPT] METHOD PATH + └── insert [-h] PATH INDEX DESCRIPTION Control a running unitd(8) instance through its control API socket. @@ -285,7 +290,11 @@ unit_ctl() err 'ctl: Missing subcommand.'; fi; - if test -v sock && echo $sock | grep '^ssh://' >/dev/null; then + if ! test -v sock; then + local sock="$(unit_sock_find)"; + fi; + + 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,')"; fi; @@ -293,11 +302,11 @@ unit_ctl() case $1 in http) shift; - unit_ctl_http ${remote:+ ---r $remote} ${sock:+ ---s $sock} $@; + unit_ctl_http ${remote:+ ---r $remote} ---s "$sock" $@; ;; insert) shift; - unit_ctl_insert ${remote:+ ---r $remote} ${sock:+ ---s $sock} $@; + unit_ctl_insert ${remote:+ ---r $remote} ---s "$sock" $@; ;; *) err "ctl: $1: Unknown argument."; @@ -398,9 +407,6 @@ unit_ctl_http() -L "$local_sock:$remote_sock" "$remote"; sock="unix:$local_sock"; - - elif ! test -v sock; then - local sock="$(unit_sock_find)"; fi; curl $curl_options -X $method -d@- \ @@ -485,9 +491,6 @@ unit_ctl_insert() -L "$local_sock:$remote_sock" "$remote"; sock="unix:$local_sock"; - - elif ! test -v sock; then - local sock="$(unit_sock_find)"; fi; local old="$(mktemp ||:)"; @@ -555,15 +558,14 @@ unit_ctl_welcome() shift; done; - id -u \ - | xargs test 0 -ne \ - && err 'welcome: This script requires root privileges to run.'; - command -v curl >/dev/null \ || err 'welcome: curl(1) not found in PATH. It must be installed to run this script.'; www='/srv/www/unit/index.html'; if test -e "$www" && ! test -v force || ! test -w /srv; then + www="$HOME/srv/www/unit/index.html"; + fi; + if test -e "$www" && ! test -v force; then www="$(mktemp)"; mv "$www" "$www.html"; www="$www.html" @@ -594,14 +596,14 @@ unit_ctl_welcome() # Check unitd is not configured already. echo "$cmd" \ - | if grep '\--state' >/dev/null; then + | if grep '\--statedir' >/dev/null; then echo "$cmd" \ | sed 's/ --/\n--/g' \ - | grep '\--state' \ + | grep '\--statedir' \ | cut -d' ' -f2; else $cmd --help \ - | sed -n '/\--state/,+1p' \ + | sed -n '/\--statedir/,+1p' \ | grep 'default:' \ | sed 's/ *default: "\(.*\)"/\1/'; fi \ @@ -626,7 +628,7 @@ unit_ctl_welcome() dry_run_echo 'Create a file to serve:'; dry_run_eval "mkdir -p $(dirname $www);"; - dry_run_eval "cat >'$www'"' <<__EOF__; + dry_run_eval "tee '$www' >/dev/null"' <<__EOF__; <!DOCTYPE html> <html> <head> @@ -1177,10 +1179,6 @@ __EOF__"; command -v curl >/dev/null \ || err 'repo-config: curl(1) not found in PATH. It must be installed to run this script.'; - id -u \ - | xargs test 0 -ne \ - && err 'repo-config: This script requires root privileges to run.'; - echo 'This script sets up the NGINX Unit repository'; if ! test $# -ge 3; then @@ -1231,8 +1229,8 @@ SYNOPSIS $program_name sock [-h] SUBCOMMAND [ARGS] Subcommands - +-- filter [-ch] - +-- find [-h] + ├── filter [-ch] + └── find [-h] DESCRIPTION Print the control API socket address of running unitd(8) diff --git a/tools/unitc b/tools/unitc index 838f7ebf..9973e62d 100755 --- a/tools/unitc +++ b/tools/unitc @@ -43,7 +43,7 @@ while [ $# -gt 0 ]; do ;; *) - if [ -r $1 ]; then + if [ -f $1 ] && [ -r $1 ]; then CONF_FILES+=($1) elif [ "${1:0:1}" = "/" ] || [ "${1:0:4}" = "http" ] && [ "$URI" = "" ]; then URI=$1 @@ -121,9 +121,9 @@ if [ $REMOTE -eq 0 ]; then if [ -r /tmp/${0##*/}.$PID.env ]; then source /tmp/${0##*/}.$PID.env else - # Check we have unitd in $PATH (and all the other tools we will need) + # Check we have all the tools we will need (that we didn't already use) # - MISSING=$(hash unitd curl ps grep tr cut sed tail sleep 2>&1 | cut -f4 -d: | tr -d '\n') + MISSING=$(hash curl tr cut sed tail sleep 2>&1 | cut -f4 -d: | tr -d '\n') if [ "$MISSING" != "" ]; then echo "${0##*/}: ERROR: cannot find$MISSING: please install or add to \$PATH" exit 1 @@ -131,10 +131,10 @@ if [ $REMOTE -eq 0 ]; then # Get control address # - PARAMS=$(ps $PID | grep unitd | cut -f2- -dv | tr '[]' ' ' | cut -f4- -d ' ' | sed -e 's/ --/\n--/g') + PARAMS=$(ps $PID | grep unitd | cut -f2- -dv | tr '[]' ' ' | cut -f3- -d ' ' | sed -e 's/ --/\n--/g') CTRL_ADDR=$(echo "$PARAMS" | grep '\--control' | cut -f2 -d' ') if [ "$CTRL_ADDR" = "" ]; then - CTRL_ADDR=$(unitd --help | grep -A1 '\--control' | tail -1 | cut -f2 -d\") + CTRL_ADDR=$($(echo "$PARAMS" | grep unitd) --help | grep -A1 '\--control' | tail -1 | cut -f2 -d\") fi # Prepare for network or Unix socket addressing @@ -156,12 +156,12 @@ if [ $REMOTE -eq 0 ]; then # ERROR_LOG=$(echo "$PARAMS" | grep '\--log' | cut -f2 -d' ') if [ "$ERROR_LOG" = "" ]; then - ERROR_LOG=$(unitd --help | grep -A1 '\--log' | tail -1 | cut -f2 -d\") + ERROR_LOG=$($(echo "$PARAMS" | grep unitd) --help | grep -A1 '\--log' | tail -1 | cut -f2 -d\") fi # Cache the discovery for this unit PID (and cleanup any old files) # - rm /tmp/${0##*/}.* 2> /dev/null + rm -f /tmp/${0##*/}.* 2> /dev/null echo UNIT_CTRL=\"${UNIT_CTRL}\" > /tmp/${0##*/}.$PID.env echo ERROR_LOG=${ERROR_LOG} >> /tmp/${0##*/}.$PID.env fi @@ -221,11 +221,11 @@ if [ $CURL_STATUS -ne 0 ]; then echo "${0##*/}: Check that you have permission to access the Unit control socket, or try again with sudo(8)" else echo "${0##*/}: Trying to access $UNIT_CTRL$URI" - cat /tmp/${0##*/}.$$ && rm /tmp/${0##*/}.$$ + cat /tmp/${0##*/}.$$ && rm -f /tmp/${0##*/}.$$ fi exit 4 fi -rm /tmp/${0##*/}.$$ 2> /dev/null +rm -f /tmp/${0##*/}.$$ 2> /dev/null if [ $SHOW_LOG -gt 0 ] && [ $NOLOG -eq 0 ] && [ $QUIET -eq 0 ]; then echo -n "${0##*/}: Waiting for log..." |