summaryrefslogtreecommitdiffhomepage
path: root/tools/unitc
blob: e0e725ecae0c03276e6ceb0358476d327cc1b3b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
# unitc - a curl wrapper for configuring NGINX Unit
# https://github.com/nginx/unit/tree/master/tools
# NGINX, Inc. (c) 2023

# Defaults
#
ERROR_LOG=/dev/null
REMOTE=0
SHOW_LOG=1
NOLOG=0
QUIET=0
CONVERT=0
URI=""
SSH_CMD=""
METHOD=PUT
CONF_FILES=()

while [ $# -gt 0 ]; do
	OPTION=$(echo $1 | tr '[a-z]' '[A-Z]')
	case $OPTION in
		"-F" | "--FORMAT")
			case $(echo $2 | tr '[a-z]' '[A-Z]') in
				"YAML")
					CONVERT=1
					if hash yq 2> /dev/null; then
						CONVERT_TO_JSON="yq eval -P --output-format=json"
						CONVERT_FROM_JSON="yq eval -P --output-format=yaml"
					else
						echo "${0##*/}: ERROR: yq(1) is required to use YAML format; install at <https://github.com/mikefarah/yq#install>"
						exit 1
					fi
					;;
				"")
					echo "${0##*/}: ERROR: Must specify configuration format"
					exit 1
					;;
				*)
					echo "${0##*/}: ERROR: Invalid format ($2)"
					exit 1
					;;
			esac
			shift; shift
			;;

		"-H" | "--HELP")
			shift
			;;

		"-L" | "--NOLOG" | "--NO-LOG")
			NOLOG=1
			shift
			;;

		"-Q" | "--QUIET")
			QUIET=1
			shift
			;;

		"GET" | "PUT" | "POST" | "DELETE" | "INSERT" | "EDIT")
			METHOD=$OPTION
			shift
			;;

		"HEAD" | "PATCH" | "PURGE" | "OPTIONS")
			echo "${0##*/}: ERROR: Invalid HTTP method ($OPTION)"
			exit 1
			;;

		*)
			if [ -f $1 ] && [ -r $1 ]; then
				CONF_FILES+=($1)
				if [ "${1##*.}" = "yaml" ]; then
					echo "${0##*/}: INFO: converting $1 to JSON"
					shift; set -- "--format" "yaml" "$@" # Apply the command line option
				else
					shift
				fi
			elif [ "${1:0:1}" = "/" ] || [ "${1:0:4}" = "http" ] && [ "$URI" = "" ]; then
				URI=$1
				shift
			elif [ "${1:0:6}" = "ssh://" ]; then
				UNIT_CTRL=$1
				shift
			else
				echo "${0##*/}: ERROR: Invalid option ($1)"
				exit 1
			fi
			;;
	esac
done

if [ "$URI" = "" ]; then
	cat << __EOF__
${0##*/} - a curl wrapper for managing NGINX Unit configuration

USAGE: ${0##*/} [options] URI

• URI is for Unit's control API target, e.g. /config
• A local Unit control socket is detected unless a remote one is specified.
• Configuration data is read from stdin.
• All options are case-insensitive (excluding filenames and URIs).

General options
  filename …          # Read configuration data from files instead of stdin
  HTTP method         # Default=GET, or PUT when config data is present
  EDIT                # Opens the URI contents in \$EDITOR
  INSERT              # Virtual HTTP method; prepend data to an array
  -f | --format YAML  # Convert configuration data to/from YAML format
  -q | --quiet        # No output to stdout

Local options
  -l | --nolog        # Do not monitor the Unit log file after config changes

Remote options
  ssh://[user@]remote_host[:port]/path/to/control.socket  # Remote Unix socket
  http://remote_host:port/URI                             # Remote TCP socket

 A remote Unit control socket may also be defined with the \$UNIT_CTRL
 environment variable as http://remote_host:port  -OR-  ssh://… (as above)

__EOF__
	exit 1
fi

# Figure out if we're running on the Unit host, or remotely
#
if [ "$UNIT_CTRL" = "" ]; then
	if [ "${URI:0:4}" = "http" ]; then
		REMOTE=1
		UNIT_CTRL=$(echo "$URI" | cut -f1-3 -d/)
		URI=/$(echo "$URI" | cut -f4- -d/)
	fi
elif [ "${UNIT_CTRL:0:6}" = "ssh://" ]; then
	REMOTE=1
	SSH_CMD="ssh $(echo $UNIT_CTRL | cut -f1-3 -d/)"
	UNIT_CTRL="--unix-socket /$(echo $UNIT_CTRL | cut -f4- -d/) _"
elif [ "${URI:0:1}" = "/" ]; then
	REMOTE=1
fi

if [ $REMOTE -eq 0 ]; then
	# Check if Unit is running, find the main process
	#
	PID=($(ps ax | grep unit:\ main | grep -v \ grep | awk '{print $1}'))
	if [ ${#PID[@]} -eq 0 ]; then
		echo "${0##*/}: ERROR: unitd not running (set \$UNIT_CTRL to configure a remote instance)"
		exit 1
	elif [ ${#PID[@]} -gt 1 ]; then
		echo "${0##*/}: ERROR: multiple unitd processes detected (${PID[@]})"
		exit 1
	fi

	# Read the significant unitd conifuration from cache file (or create it)
	#
	if [ -r /tmp/${0##*/}.$PID.env ]; then
		source /tmp/${0##*/}.$PID.env
	else
		# Check we have all the tools we will need (that we didn't already use)
		#
		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
		fi

		# Obtain any optional startup parameters from the 'unitd: main' process
		# so we can get the actual control address and error log location.
		# Command line options and output of ps(1) is notoriously variable across
		# different *nix/BSD platforms so multiple attempts might be needed.
		#
		PARAMS=$((ps -wwo args=COMMAND -p $PID || ps $PID) 2> /dev/null | grep unit | tr '[]' ^ | cut -f2 -d^ | sed -e 's/ --/\n--/g')
		if [ "$PARAMS" = "" ]; 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' ')
		if [ "$CTRL_ADDR" = "" ]; then
			CTRL_ADDR=$($(echo "$PARAMS") --help | grep -A1 '\--control' | 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."
			exit 2
		fi

		# Prepare for network or Unix socket addressing
		#
		if [ $(echo $CTRL_ADDR | grep -c ^unix:) -eq 1 ]; then
			SOCK_FILE=$(echo $CTRL_ADDR | cut -f2- -d:)
			if [ -r $SOCK_FILE ]; then
				UNIT_CTRL="--unix-socket $SOCK_FILE _"
			else
				echo "${0##*/}: ERROR: cannot read unitd control socket: $SOCK_FILE"
				ls -l $SOCK_FILE
				exit 2
			fi
		else
			UNIT_CTRL="http://$CTRL_ADDR"
		fi

		# Get error log filename
		#
		ERROR_LOG=$(echo "$PARAMS" | grep '\--log' | cut -f2 -d' ')
		if [ "$ERROR_LOG" = "" ]; then
			ERROR_LOG=$($(echo "$PARAMS") --help | grep -A1 '\--log' | tail -1 | cut -f2 -d\")
		fi
		if [ "$ERROR_LOG" = "" ]; then
			echo "${0##*/}: WARNING: cannot detect unit log file (will not be monitored). If you started unitd from a relative path then try using the --log option."
			ERROR_LOG=/dev/null
		fi

		# Cache the discovery for this unit PID (and cleanup any old files)
		#
		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
fi

# Choose presentation style
#
if [ $QUIET -eq 1 ]; then
	OUTPUT="tail -c 0" # Equivalent to >/dev/null
elif [ $CONVERT -eq 1 ]; then
	OUTPUT=$CONVERT_FROM_JSON
elif hash jq 2> /dev/null; then
	OUTPUT="jq"
else
	OUTPUT="cat"
fi

# Get current length of error log before we make any changes
#
if [ -f $ERROR_LOG ] && [ -r $ERROR_LOG ]; then
	LOG_LEN=$(wc -l < $ERROR_LOG)
else
	NOLOG=1
fi

# Adjust HTTP method and curl params based on presence of stdin payload
#
if [ -t 0 ] && [ ${#CONF_FILES[@]} -eq 0 ]; then
	if [ "$METHOD" = "DELETE" ]; then
		$SSH_CMD curl -X $METHOD $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ | $OUTPUT
	elif [ "$METHOD" = "EDIT" ]; then
		EDITOR=$(test "$EDITOR" && printf '%s' "$EDITOR" || command -v editor || command -v vim || echo vi)
		EDIT_FILENAME=/tmp/${0##*/}.$$${URI//\//_}
		$SSH_CMD curl -fsS $UNIT_CTRL$URI > $EDIT_FILENAME || exit 2
		if [ "${URI:0:12}" = "/js_modules/" ]; then
			if ! hash jq 2> /dev/null; then
				echo "${0##*/}: ERROR: jq(1) is required to edit JavaScript modules; install at <https://stedolan.github.io/jq/>"
				exit 1
			fi
			jq -r < $EDIT_FILENAME > $EDIT_FILENAME.js # Unescape linebreaks for a better editing experience
			EDIT_FILE=$EDIT_FILENAME.js
			$EDITOR $EDIT_FILENAME.js || exit 2
			# Remove the references, delete old config, push new config+reference
			$SSH_CMD curl -fsS $UNIT_CTRL/config/settings/js_module > /tmp/${0##*/}.$$_js_module && \
			$SSH_CMD curl -X DELETE $UNIT_CTRL/config/settings/js_module && \
			$SSH_CMD curl -fsSX DELETE $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ && \
			printf "%s" "$(< $EDIT_FILENAME.js)" | $SSH_CMD curl -fX PUT --data-binary @- $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ && \
			$SSH_CMD curl -X PUT --data-binary @/tmp/${0##*/}.$$_js_module $UNIT_CTRL/config/settings/js_module 2> /tmp/${0##*/}.$$
		elif [ $CONVERT -eq 1 ]; then
			$CONVERT_FROM_JSON < $EDIT_FILENAME > $EDIT_FILENAME.yaml
			$EDITOR $EDIT_FILENAME.yaml || exit 2
			$CONVERT_TO_JSON < $EDIT_FILENAME.yaml | $SSH_CMD curl -X PUT --data-binary @- $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ | $OUTPUT
		else
			tr -d '\r' < $EDIT_FILENAME > $EDIT_FILENAME.json # Remove carriage-return from newlines
			$EDITOR $EDIT_FILENAME.json || exit 2
			$SSH_CMD curl -X PUT --data-binary @$EDIT_FILENAME.json $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ | $OUTPUT
		fi
	else
		SHOW_LOG=$(echo $URI | grep -c ^/control/)
		$SSH_CMD curl $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ | $OUTPUT
	fi
else
	if [ "$METHOD" = "INSERT" ]; then
		if ! hash jq 2> /dev/null; then
			echo "${0##*/}: ERROR: jq(1) is required to use the INSERT method; install at <https://stedolan.github.io/jq/>"
			exit 1
		fi
		NEW_ELEMENT=$(cat ${CONF_FILES[@]})
		echo $NEW_ELEMENT | jq > /dev/null || exit $? # Test the input is valid JSON before proceeding
		OLD_ARRAY=$($SSH_CMD curl -s $UNIT_CTRL$URI)
		if [ "$(echo $OLD_ARRAY | jq -r type)" = "array" ]; then
			echo $OLD_ARRAY | jq ". |= [$NEW_ELEMENT] + ." | $SSH_CMD curl -X PUT --data-binary @- $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ | $OUTPUT
		else
			echo "${0##*/}: ERROR: the INSERT method expects an array"
			exit 3
		fi
	else
		if [ $CONVERT -eq 1 ]; then
			cat ${CONF_FILES[@]} | $CONVERT_TO_JSON > /tmp/${0##*/}.$$_json
			CONF_FILES=(/tmp/${0##*/}.$$_json)
		fi
		cat ${CONF_FILES[@]} | $SSH_CMD curl -X $METHOD --data-binary @- $UNIT_CTRL$URI 2> /tmp/${0##*/}.$$ | $OUTPUT
	fi
fi

CURL_STATUS=${PIPESTATUS[0]}
if [ $CURL_STATUS -ne 0 ]; then
	echo "${0##*/}: ERROR: curl(1) exited with an error ($CURL_STATUS)"
	if [ $CURL_STATUS -eq 7 ] && [ $REMOTE -eq 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 -f /tmp/${0##*/}.$$
	fi
	exit 4
fi
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..."
	sleep $SHOW_LOG
	echo ""
	sed -n $((LOG_LEN+1)),\$p $ERROR_LOG
fi