forked from CJohnsonADAH/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-daemon-do
executable file
·193 lines (149 loc) · 5.98 KB
/
adpn-daemon-do
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
#!/bin/bash
#
# adpn-daemon-do: script to handle adpn daemon commands
#
# @version 2021.0918
SCRIPTPATH="$(which "$0")"
SCRIPTPATH="$( readlink --canonicalize "${SCRIPTPATH}" )"
SCRIPTDIR="$(dirname "${SCRIPTPATH}")"
SCRIPT="$(basename "${SCRIPTPATH}")"
source "${SCRIPTDIR}/adpn-define-aliases"
source "${SCRIPTDIR}/adpn-lockss-daemon-aliases"
__USAGE__="Usage: ${SCRIPT} [--version] [--help] <CMD> [<ARGS>]"
__HELP__="[${SCRIPT}] Try '${SCRIPT} help' for more information."
__DOC__="""${__USAGE__}
--version Display the version of the script
--help Display these usage notes
The most commonly used ${SCRIPT} commands are:
verify Verify that an AU's parameters are complete and correct
and its start URL is accessible to the LOCKSS daemon.
Exit codes:
0 = success (successful operation and expected result)
1-254 = error in executing command
255 = command not supported
"""
##########################################################################################
### COMMAND LINE: loop through switches ##################################################
##########################################################################################
declare -a _CMDLINE ; _CMDLINE=("$0")
shopt -s lastpipe
declare -a SWITCHFILES ; SWITCHFILES=()
if [[ -r "${CONFFILE}" ]] ; then
SWITCHFILES+=(${CONFFILE})
fi
CMDLINETXT=$(mktemp)
until [[ "$#" -lt 1 ]] ; do
_CMDLINE+=("$1")
printf "%s\n" "$1" >> "${CMDLINETXT}"
shift
done
SWITCHFILES+=(${CMDLINETXT})
adpn_command_line "${SWITCHFILES[@]}"
rm "${CMDLINETXT}"
##########################################################################################
### SCRIPT: DETERMINE COMMAND, THEN EXECUTE PIPELINE ####################################
##########################################################################################
EXITCODE=0
CMD="${_ARGV[1]}"
adpn_set_display_settings # V, VV, Q, QQ, DBG, DBGLEVEL, DDBG, SCRIPT_CMD_NAME / @see adpn-define-aliases
adpn_script_handle_version_or_help
if [[ ! -z "${_PARAM[remote]}" ]] ; then
adpn_script_handle_remote_execution -t "daemon" "${_CMDLINE[@]:1}"
EXITCODE="$?"
else
case "${CMD}" in
"verify")
PIPELINE="$( adpn_parameter_from "--pipeline" "staged:verified" )"
if [[ -n "${PIPELINE}" ]] ; then
PIPEDFROM=$( printf "%s" "${PIPELINE}" | cut --field=1 --delimiter=":" )
PIPEDTO=$( printf "%s" "${PIPELINE}" | cut --field=2 --delimiter=":" )
adpn_debug 2 "${LINENO}" "SWITCH='%s', PIPELINE='%s': PIPEDFROM='%s', PIPEDTO='%s'\n" "${_SWITCHES[pipeline]}" "${PIPELINE}" "${PIPEDFROM}" "${PIPEDTO}" 1>&2
fi
declare -a TEMP_CLEANUP=()
AIT_INPUT="$(mktemp)" ; TEMP_CLEANUP+=( "${AIT_INPUT}" )
AIT_OUTPUT="$(mktemp)" ; TEMP_CLEANUP+=( "${AIT_OUTPUT}" )
adpn_read_json_packet_source "${_ARGV[2]}" "${SCRIPT_CMD_NAME}" "${V}" | adpn_get_json_packets | adpn_select_json_packet "${PIPEDFROM}" "-" "head -n 1" "${PIPEDTO}" > "${AIT_INPUT}"
EXITCODE="$?"
if [[ -r "${AIT_INPUT}" ]] ; then
PEER_TO=$( [[ -n "${_PARAM[to]}" ]] && printf "%s" "${_PARAM[to]}" || printf "%s" "${_PARAM[peer]}" )
if [[ -z "${PEER_TO}" ]] ; then
PEER_TO=$( adpnprop "peer" )
fi
if [[ -n "${PEER_TO}" ]] ; then
PEER_TO=$( printf "%s--peer-to=%s" "" "${PEER_TO}" )
fi
declare -a AIT_SW=( "--remote=1" )
[[ -n "${PIPELINE}" ]] && AIT_SW+=( "--pipeline=${PIPELINE}" )
[[ -n "$( adpn_get_user_email )" ]] && AIT_SW+=( "--by=$( adpn_get_user_email )" )
[[ -n "${V}" ]] && AIT_SW+=( "${V}" )
[[ -n "${DDBG}" ]] && AIT_SW+=( "${DDBG}" )
adpn_debug 01 "${LINENO}" '$ adpn-ingest-test "%s" "%s" "%s" > "%s"\n' "${AIT_INPUT}" "${PEER_TO}" "$( join_by '" "' "${AIT_SW[@]}" )" "${AIT_OUTPUT}"
adpn-ingest-test "${AIT_INPUT}" "${PEER_TO}" "${AIT_SW[@]}" > "${AIT_OUTPUT}" ; EXITCODE="$?"
cat "${AIT_OUTPUT}" ; echo
fi
for TMP in "${TEMP_CLEANUP[@]}" ; do
rm "${TMP}"
done
if [[ "${EXITCODE}" -gt 0 ]] ; then
printf "[%s:%d] adpn-ingest-test FAILED.\n" "${SCRIPT_CMD_NAME}" "${LINENO}" 1>&2
fi
;;
"http")
DAEMON_USER="$( adpn_parameter_from ".daemon/user" )"
DAEMON_PASS="$( adpn_parameter_from ".daemon/pass" )"
DAEMON_PASS="$( adpn_getpassword_from "${DAEMON_PASS}" )"
CREDENTIALS="${DAEMON_USER}:${DAEMON_PASS}"
FORMAT="$( adpn_parameter_from "--format" '\text' )"
VERB="${_ARGV[2]}"
[[ -z "${VERB}" ]] && VERB=GET
URL="$( adpn_parameter_from "--url" "${_ARGV[3]}" )"
if [[ "${URL}" =~ ^http(s)?:// ]] ; then
REQ_URL="${URL}${URL_SEP}output=${FORMAT}"
else
LOCKSS_DAEMON_URL_BASE=$( lockss_daemon_url_base )
REQ_URL="${LOCKSS_DAEMON_URL_BASE}/${URL##/}"
[[ "${REQ_URL}" =~ [?] ]] && URL_SEP="&" || URL_SEP="?"
REQ_URL="${REQ_URL}${URL_SEP}output=${FORMAT}"
fi
declare -a _CURLOPT=( "--request" "${VERB}" )
for KEY in "${!_PARAM[@]}" ; do
case "${KEY}" in
"context"|"verbose"|"quiet"|"debug"|"dry-run")
;;
"verb"|"request"|"fail")
;;
"pipe")
PIPED_TEXT="$( cat - )"
PIPED_PAIR="$( printf "%s=%s" "${_PARAM[$KEY]}" "${PIPED_TEXT}" )"
_CURLOPT+=( '--data-urlencode' "${PIPED_PAIR}")
;;
*)
[[ "${KEY}" =~ ^data-(.*) ]] && FIELD="${BASH_REMATCH[1]}" || FIELD="${KEY}"
_CURLOPT+=( "--data-urlencode" "${FIELD}=${_PARAM[$KEY]}" )
;;
esac
done
curl --fail --silent "${REQ_URL}" --user "${CREDENTIALS}" "${_CURLOPT[@]}"
EXITCODE="$?"
;;
"refresh")
HTML_OUT="$( "${ME}" http POST "/DebugPanel" --data-action="Reload Config" --data-url="" --data-msg="" --data-auid="" )"
EXITCODE="$?"
if [[ "${EXITCODE}" -eq 0 ]] ; then
adpn_notice "${LINENO}" "%s" "LOCKSS Daemon Configuration reloaded."
fi
;;
"version"|"help")
EXITCODE=0
;;
*)
echo "[${SCRIPT_CMD_NAME}] '${CMD}' command not understood." 1>&2
echo "${__HELP__}" 1>&2
EXITCODE=255
;;
esac
fi
##########################################################################################
### CLEANUP: exit with settled exit code. ################################################
##########################################################################################
exit ${EXITCODE}