-
Notifications
You must be signed in to change notification settings - Fork 23
/
cqcfg
executable file
·398 lines (355 loc) · 12.8 KB
/
cqcfg
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/bin/sh
# CQ Unix Toolkit
# Copyright (C) 2021 Wunderman Thompson Technology
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
_usage() {
cat <<EOT
Usage: $(basename "${0}") [OPTION...] [configuration-id]
Return details of specified OSGI configuration in pretty-printed form or
machine-friendly form (-m/-n) using instance URL. Allows to modify OSGI
configuration (-s/-v) or even create a new configuration based on factory (-f).
You should rather use already existing configuration-id otherwise configuration
won't be bound. To get list of configuraton IDs, use cqcfgls tool.
Examples:
cqcfg -u admin # Show metadata and properties for CRXDEServlet
com.day.crx.ide.CRXDEServlet # configuration
cqcfg -u admin -j # Return original response for CRXDEServlet
com.day.crx.ide.CRXDEServlet # configuration (for custom processing
# purposes)
cqcfg -u admin -m # Return a parsable list of properties for
com.day.crx.ide.CRXDEServlet # CRXDEServlet configuration
cqcfg -u admin -m -l # Return a parsable list of properties for
com.day.crx.ide.CRXDEServlet # CRXDEServlet configuration with additional
# description column
cqcfg -u admin -m -n # Return a parsable list of config metadata for
com.day.crx.ide.CRXDEServlet # CRXDEServlet configuration
cqcfg -u admin -s prop -v val # Set value 'val' for property 'prop' for
com.day.crx.ide.CRXDEServlet # local instance and CRXDEServlet configuration
cqcfg -u admin -f -s prop # Create a new configuration based on
-v val # factory configuration LogManager for local
org.apache.sling.commons.log # instance with property 'prop' set to 'val'
.LogManager # value.
Options:
-u use specified username for connection
-p use provided password for authentication
-i use specified instance URL to connect
-j returns configuration details in original JSON form
-m returns configuration properties in machine-friendly
form (fields separated by tabs) without header
-l add additional column (description) to output which
often makes output a little less readable (wrapped
lines)
-n in conjuction with -m display configuration general
details (name/description)
-f instead of modification/creation, use factory config
to create new config
-s PROP set property called 'PROP' (multiple times allowed)
-v VAL in conjuction with -s, sets value 'VAL' for property
specified by -s (multiple times allowed)
EOT
exit 1
}
_print_general_info() {
DETAILS=$(echo "${STATUS}" | ${SEDX} "s#${PROP_REGEXP}#\1\3#")
D1='"pid":"([^"]*)"'
D2='"title":"([^"]*)"'
D3='"description":"([^"]*)"'
D4='"bundleLocation":"([^"]*)"'
PID=$(echo "${DETAILS}" | grep -E -o "${D1}" |
${SEDX} "s#.*${D1}.*#\1#")
TITLE=$(echo "${DETAILS}" | grep -E -o "${D2}" |
${SEDX} "s#.*${D2}.*#\1#")
DESCRIPTION=$(echo "${DETAILS}" | grep -E -o "${D3}" |
${SEDX} "s#.*${D3}.*#\1#")
BUNDLE=$(echo "${DETAILS}" | grep -E -o "${D4}" |
${SEDX} "s#.*${D4}.*#\1#")
if [ "${machine_friendly}" -eq 0 ]; then
output="PID\t${PID}\n"
output="${output}TITLE\t${TITLE}\n"
output="${output}DESCRIPTION\t${DESCRIPTION}\n"
output="${output}BUNDLE\t${BUNDLE}\n"
else
output="${PID}\n${TITLE}\n${DESCRIPTION}\n${BUNDLE}"
fi
if [ "${machine_friendly}" -eq 0 -o "${info}" -eq 1 ]; then
printf "%s\n" "${output}" | "${API}" -F "${machine_friendly}"
fi
}
_print_properties() {
T=$(printf '\t')
PROP_ENTRY_REGEXP='("[^"]+":\{"name":)'
PROP_ENTRIES=$(echo "${PROPERTIES}" |
${SEDX} "s#${PROP_ENTRY_REGEXP}#${T}\1#g" |
tr '\t' '\n')
PR12='"([^"]+)":\{"name":"([^"]*)",'
PR3='"type":(.*),'
PR3S='"type":([0-9]+),'
PR3A='"type":(1),'
PR4='"value":"([^"]*)"'
PR4N='"value":([^,"]*)'
PR4A='"values":(\[[^\x5D]*\])'
PR5=',"description":"([^"]*)"}'
VALUES_RG='\{"labels":\[.*\],"values":(\[.*\])\},?'
SWAPRG="(.*)${T}(.*)${T}(.*)${T}(.*)${T}(.*)}?"
TYPE_ID_RG="(.*)${T}(.*)${T}(.*)${T}(.*)${T}([0-9]+,?).*"
PROPS_BASIC_ROWS=$(echo "${PROP_ENTRIES}" |
grep -E "${PR12}" |
${SEDX} "s#${PR12}${PR3S}${PR4}${PR5}#\1${T}\4${T}\2${T}\5${T}\3#" |
${SEDX} "s#${PR12}${PR3}${PR4N}${PR5}#\1${T}\4${T}\2${T}\5${T}\3#" |
${SEDX} "s#${PR12}${PR3A}${PR4A}${PR5}#\1${T}\4${T}\2${T}\5${T}\3#" |
${SEDX} "s#${PR12}${PR3}${PR4A}${PR5}#\1${T}\4${T}\2${T}\5${T}\3#" |
${SEDX} "s#${PR12}${PR3}${PR4}${PR5}#\1${T}\4${T}\2${T}\5${T}\3#" |
${SEDX} "s#${PR12}${PR3S}${PR4}#\1${T}\4${T}\2${T}${T}\3#" |
${SEDX} "s#${PR12}${PR3}${PR4N}#\1${T}\4${T}\2${T}${T}\3#" |
${SEDX} "s#${PR12}${PR3A}${PR4A}#\1${T}\4${T}\2${T}${T}\3#" |
${SEDX} "s#${PR12}${PR3}${PR4A}#\1${T}\4${T}\2${T}${T}\3#" |
${SEDX} "s#${PR12}${PR3}${PR4}#\1${T}\4${T}\2${T}${T}\3#" |
${SEDX} "s#${VALUES_RG}#\1#" |
${SEDX} "s#${TYPE_ID_RG}#\1${T}\2${T}\3${T}\4${T}#" |
${SEDX} "s#${SWAPRG}#\1${T}\2${T}\3${T}\5${T}\4#" |
${SEDX} 's#%22#"#g')
output="${PROPS_BASIC_ROWS}"
if [ "${machine_friendly}" -eq 0 ]; then
output="ID\tVALUE\tNAME\tVALUES\tDESCRIPTION\n${output}"
fi
if [ "${long}" -eq 0 ]; then
output=$(printf "%s\n" "${output}" | cut -f1-4)
fi
if [ "${machine_friendly}" -eq 0 -o "${info}" -eq 0 ]; then
printf "%s\n" "${output}" | "${API}" -F "${machine_friendly}"
fi
}
_print_configuration() {
FILEPATH="/system/console/configMgr/${config_id}"
URL="${instance}${FILEPATH}"
REFERERHEADER="Referer: ${URL}"
STATUS=$(${CURLBIN} \
-s \
--write-out "%{http_code}" \
-u "$AUTH" \
-H "$REFERERHEADER" \
-X POST \
"$instance${FILEPATH}")
EXITCODE=${?}
"${API}" -C "${EXITCODE}"
if [ ${EXITCODE} -ne 0 ]; then
exit ${EXITCODE}
fi
STATUSCODE=$(echo "${STATUS}" | grep -o -E '[0-9]{3}' | tail -n 1)
"${API}" -H "${STATUSCODE}"
EXITCODE2=$?
if [ ${EXITCODE2} -ne 0 ]; then
exit ${EXITCODE2}
fi
STATUS=$(echo "${STATUS}" | ${SEDX} 's#[0-9]{3}$##')
if [ "${original}" -eq 1 ]; then
echo "${STATUS}"
exit
fi
PROP_REGEXP='(.*)"properties":\{(.+})}(.*)'
PROPERTIES=$(echo "${STATUS}" | ${SEDX} 's#\\\"#%22#g' |
grep -E -o "${PROP_REGEXP}" | ${SEDX} "s#${PROP_REGEXP}#\2#")
_print_general_info
if [ "${machine_friendly}" -eq 0 ]; then
printf "\n\n"
fi
_print_properties
exit
}
_modify_configuration() {
CONFIGMGR="/system/console/configMgr/"
original_config_id=$(echo "${config_id}" | sed 's#%20# #g')
if [ "${factory}" -eq 1 ]; then
echo "Using factory: '${original_config_id}'."
temp_pid=$(echo "[Temporary PID replaced by real PID upon save]" |
"${API}" -f)
FACTORY="factoryPid=${config_id}&"
FILEPATH="${CONFIGMGR}${temp_pid}"
else
echo "Modifying '${original_config_id}'."
FACTORY=""
FILEPATH="${CONFIGMGR}${config_id}"
fi
CONST_PARAMS="?apply=true&action=ajaxConfigManager&${FACTORY}"
PROPERTIES="&propertylist="
REFERERHEADER="Referer: ${instance}${FILEPATH}"
first=1
UNIQUE_PROPERTIES=""
for operation in ${operations}; do
propname=$(echo "${operation}" | cut -f1 -d '=')
propvalue=$(echo "${operation}" | cut -f2- -d '=' | "${API}" -f)
prop_original_value=$(echo "${operation}" | cut -f2- -d '=' |
sed 's#%20# #g')
echo " Setting '${propname}' to: '${prop_original_value}' "
[ "${first}" -eq 0 ] && REQ_ACTION="${REQ_ACTION}&"
REQ_ACTION="${REQ_ACTION}${propname}=${propvalue}"
UNIQUE_PROPERTIES=$(printf "%s\n%s\n" "${UNIQUE_PROPERTIES}" \
"${propname}")
first=0
done
UNIQUE_PROPERTIES=$(printf "%s\n" "${UNIQUE_PROPERTIES}" | sort |
uniq)
first=1
for property in ${UNIQUE_PROPERTIES}; do
[ "${first}" -eq 0 ] && PROPERTIES="${PROPERTIES}%2C"
PROPERTIES="${PROPERTIES}${property}"
first=0
done
QUERYSTRING="${CONST_PARAMS}${REQ_ACTION}${PROPERTIES}"
URL="${instance}${FILEPATH}${QUERYSTRING}"
STATUS=$(${CURLBIN} \
-s \
--write-out "%{http_code}\n%{redirect_url}" \
-u "$AUTH" \
-H "$REFERERHEADER" \
-X POST \
"${URL}")
EXITCODE=${?}
"${API}" -C $EXITCODE
if [ ${EXITCODE} -ne 0 ]; then
exit ${EXITCODE}
fi
STATUSCODE=$(echo "${STATUS}" | grep -o -E '[0-9]{3}' | head -n 1)
NEW_CONFIG_ID=$(echo "${STATUS}" | tail -n 1 |
${SEDX} "s#.*(${config_id}.*)#\1#")
TEMPORARY_REDIRECT="302"
if [ ! "${STATUSCODE}" = "${TEMPORARY_REDIRECT}" ]; then
"${API}" -H "${STATUSCODE}"
EXITCODE2=$?
if [ ${EXITCODE2} -ne 0 ]; then
exit ${EXITCODE2}
fi
fi
if [ "${factory}" -eq 1 ]; then
echo "New configuration created: ${NEW_CONFIG_ID}"
fi
exit
}
CWD=$(dirname "${0}")
API="$CWD/cqapi"
"${API}" -P >/dev/null 2>/dev/null
if [ ${?} -ne 0 ]; then
echo "Fatal: cannot find or test cqapi command" >&2
exit 1
fi
CURLBIN=$("${API}" -c)
if [ ${?} -ne 0 ]; then
echo "Fatal: cannot find curl" >&2
exit 1
fi
SEDX=$("${API}" -s)
# API common options
cmdapi=$("${API}" -P "${@}")
username=$(echo "${cmdapi}" | cut -f1)
password=$(echo "${cmdapi}" | cut -f2)
instance=$(echo "${cmdapi}" | cut -f3)
passed=$(echo "${cmdapi}" | cut -f4)
apigetopts=$(echo "${cmdapi}" | cut -f5)
# Custom arguments
machine_friendly=0
operations=""
long=0
original=0
info=0
modify=0
propery_id=
factory=0
v_counter=0
s_counter=0
while getopts ":lfjmns:v:${apigetopts}" opt; do
case "${opt}" in
j)
original=1
;;
m)
machine_friendly=1
;;
n)
info=1
;;
l)
long=1
;;
f)
factory=1
;;
v)
v_counter=$((v_counter + 1))
modify=1
if [ ! -z "${property_id}" ]; then
propvalue=$(echo "${OPTARG}" | sed 's# #%20#g')
operations="${operations} ${property_id}=${propvalue}"
property_id=""
else
echo "For each -v option there must be -s preceeding" \
"option specified" >&2
echo "" >&2
_usage
fi
;;
s)
s_counter=$((s_counter + 1))
modify=1
property_id="${OPTARG}"
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
_usage
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
_usage
;;
esac
done
shift $((OPTIND - 1))
if [ ${#} -ne 1 -o "${passed}" -eq 0 ]; then
_usage
fi
if [ "${s_counter}" -ne "${v_counter}" ]; then
echo "Options: -s and -v are connected together. You need to specify" \
"matching pairs of such options (property/value)." >&2
echo ""
_usage
fi
config_id="${1}"
if [ "${machine_friendly}" -eq 1 -a "${original}" -eq 1 ]; then
echo "Switches: -j and -m cannot be applied together." >&2
echo ""
_usage
fi
if [ "${machine_friendly}" -eq 0 -a "${info}" -eq 1 ]; then
echo "Option: -n is dedicated for -m (machine-friendly output)." >&2
echo ""
_usage
fi
AUTH="${username}:${password}"
config_id=$(echo "${config_id}" | sed 's# #%20#g')
view_options=$((original + info + machine_friendly))
if [ -z "${operations}" -a "${factory}" -eq 1 ]; then
echo "New configuration from factory -f option must be applied" \
"together with modify operations: -s/-v" >&2
echo "See what properties are supported. Call this command with factory" \
" id but without -f/-s/-v options" >&2
echo "" >&2
_usage
exit 2
fi
if [ "${operations}" != "" -a "${view_options}" -ge 1 ]; then
echo "View options -m, -j, -n or -l cannot be applied together " \
"with modify operations: -s/-v. Choose only one functionality." >&2
exit 2
fi
[ "${modify}" -eq 0 ] && _print_configuration
[ "${modify}" -eq 1 ] && _modify_configuration