forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-do-flag
executable file
·178 lines (134 loc) · 6.37 KB
/
adpn-do-flag
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
#!/bin/bash
#
# adpn-do-flag: script to handle adpn flag commands
#
# @version 2021.0820
SCRIPTPATH="$(which "$0")"
SCRIPTPATH="$( readlink --canonicalize "${SCRIPTPATH}" )"
SCRIPTDIR="$(dirname "${SCRIPTPATH}")"
SCRIPT="$(basename "${SCRIPTPATH}")"
source "${SCRIPTDIR}/adpn-define-aliases"
__USAGE__="Usage: ${SCRIPT} [--version] [--help] <OBJECT> [<ARGS>]"
__HELP__="[${SCRIPT}] Try '${SCRIPT} help' for more information."
__DOC__="""${__USAGE__}
--version Display the version of the script
--help Display these usage notes
--key Provide the name of the flag to set
--value Provide the value of the flag to set
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
declare -a CONTEXT_SW=( "--context=${SCRIPT_CMD_NAME}" )
[[ -n "${DDBG}" ]] && CONTEXT_SW+=( "${DDBG}" )
[[ -n "${V}" ]] && CONTEXT_SW+=( "${V}" )
[[ -n "${Q}" ]] && CONTEXT_SW+=( "${Q}" )
[[ -n "${_SWITCHES[dry-run]}" ]] && CONTEXT_SW+=( "${_SWITCHES[dry-run]}" )
adpn_script_handle_version_or_help
declare -a TEMP_CLEANUP=()
if [[ -n "${_PARAM[remote]}" && -z "${_PARAM[no-remote]}" ]] ; then
adpn_script_handle_remote_execution -tt "flag" "${_CMDLINE[@]:1}"
EXITCODE="$?"
else
adpn_set_pipeline_step "published:flagged" "flag"
AIIT_INPUT="$(mktemp)" ; TEMP_CLEANUP+=( "${AIIT_INPUT}" )
declare -a AGJP_ARGV=( "${_ARGV[1]}" "${SCRIPT_CMD_NAME}" "${V}" )
adpn_debug 10 "${LINENO}" "$( printf "%s " "${AGJP_ARGV[@]}" )"
adpn_read_json_packet_source "${AGJP_ARGV[@]}" | adpn_get_json_packets | adpn_select_json_packet "${PIPEDFROM}" "-" "tail -n 1" "${PIPEDTO}" > "${AIIT_INPUT}" ; EXITCODE="$?"
declare -a AF_PARAMS=( "${_ARGV[@]:2}" )
[[ -n "${_PARAM[key]}" ]] && AIIT_PARAM_KEY="${_PARAM[key]}" || { AIIT_PARAM_KEY="${AF_PARAMS[0]}" ; AF_PARAMS=( "${AF_PARAMS[@]:1}" ) ; }
[[ -n "${_PARAM[value]}" ]] && AIIT_PARAM_VALUE="${_PARAM[value]}" || { AIIT_PARAM_VALUE="${AF_PARAMS[0]}" ; AF_PARAMS=( "${AF_PARAMS[@]:1}" ) ; }
[[ -z "${_PARAM[delete]}" ]] && AIIT_PARAM_OP="+" || AIIT_PARAM_OP="-"
if [[ "${EXITCODE}" -eq 0 && -s "${AIIT_INPUT}" ]] ; then
PEER_TO=$( adpn_parameter_from "--to" "--peer" ".peer" "ALL" )
PEER_TO_SW=$( printf -- "--to=%s" "${PEER_TO}" )
AIIT_SQLOUT="${_PARAM[sqldump]}"
if [[ -z "${AIIT_SQLOUT}" ]] ; then
AIIT_SQLOUT="$( mktemp )" ; TEMP_CLEANUP+=( "${AIIT_SQLOUT}" )
fi
adpn-titlesdb-diff --before "${PEER_TO}" ${V}
declare -a AIIT_CMDLINE=( "adpn-ingest-into-titlesdb.py" "${AIIT_INPUT}" )
AIIT_CMDLINE+=( "${CONTEXT_SW[@]}" )
AIIT_CMDLINE+=( "--parameter=${AIIT_PARAM_OP}${AIIT_PARAM_KEY}:${AIIT_PARAM_VALUE}" )
[[ -n "${_SWITCHES[passthru]}" ]] && AIIT_CMDLINE+=( "${_SWITCHES[passthru]}" )
[[ -n "${PEER_TO_SW}" ]] && AIIT_CMDLINE+=( "${PEER_TO_SW}" )
[[ -n "${PIPELINE_SW}" ]] && AIIT_CMDLINE+=( "${PIPELINE_SW}" )
adpn_debug 20,adpn,flag,ingest,sql "${LINENO}" '$ %s > %s' "$( printf "%q " "${AIIT_CMDLINE[@]}" )" "${AIIT_SQLOUT}"
"${AIIT_CMDLINE[@]}" > "${AIIT_SQLOUT}" ; EXITCODE="$?"
if [[ "${EXITCODE}" -gt 0 ]] ; then
if [[ -z "${Q}" && -r "${AIIT_SQLOUT}" ]] ; then
printf "\n" 1>&2
cat "${AIIT_SQLOUT}" 1>&2
printf "\n" 1>&2
fi
adpn_notice "${LINENO}" "FAILED: adpn-ingest-into-titlesdb.py exited with error code=%d" "${EXITCODE}"
else
adpn_debug 30,adpn,flag,ingest,sql "${LINENO}" 'Diff: compare before/after XML for "%s"' "${PEER_TO}"
[[ -z "${_SWITCHES[dry-run]}" ]] && { adpn-titlesdb-diff --after "${PEER_TO}" "${CONTEXT_SW[@]}" ; EXITCODE="$?" ; }
if [[ "${EXITCODE}" -gt 0 ]] ; then
if [[ -z "${Q}" && -r "${AIIT_SQLOUT}" ]] ; then
printf "\n" 1>&2
cat "${AIIT_SQLOUT}" 1>&2
printf "\n" 1>&2
fi
adpn_notice "${LINENO}" "FAILED: adpn-titlesdb-diff exited with error code=%d." "${EXITCODE}"
else
if [[ -n "${_SWITCHES[dry-run]}" ]] ; then
adpn_debug 30,adpn,flag,ingest,sql "${LINENO}" '$ cat %q' "${AIIT_SQLOUT}"
cat "${AIIT_SQLOUT}"
elif [[ -n "${_PARAM[passthru]}" ]] ; then
adpn_debug 30,adpn,flag,ingest,sql "${LINENO}" '$ cat %q | grep -E "%s"' "${AIIT_SQLOUT}" "${ADPN_JSON_PACKET_REGEX}"
cat "${AIIT_SQLOUT}" | grep -E "${ADPN_JSON_PACKET_REGEX}"
fi
if [[ -z "${QQ}" ]] ; then
printf "\n" 1>&2
adpn_notice "${LINENO}" "DONE: Archival Unit flagged with %s='%s' on %s titlesdb." "${AIIT_PARAM_KEY}" "${AIIT_PARAM_VALUE}" "${PEER_TO}"
fi
if [[ "${PEER_TO}" == "ALL" && -z "${_PARAM[no-commit]}" ]] ; then
JSON_PACKET="$( cat "${AIIT_INPUT}" )"
AU_TITLE="$( adpn_parameter_from "--au_title" ".Ingest Title<${JSON_PACKET}" )"
declare -a AC_SW=()
AC_SW+=( "$( printf -- "--message=Flagged AU (%s) with %s%s%s" "${AU_TITLE}" "${AIIT_PARAM_OP}" "${AIIT_PARAM_KEY}" "${AIIT_PARAM_VALUE+=}${AIIT_PARAM_VALUE}" )" )
[[ -n "${_PARAM[batch]}" ]] && AC_SW+=( "${_SWITCHES[batch]}" )
"${ADPN}" commit "${AC_SW[@]}" "${CONTEXT_SW[@]}"
EXITCODE="$?"
fi
fi
fi
fi
if [[ "${EXITCODE}" -gt 0 ]] ; then
adpn_notice "${LINENO}" "%s FAILED" "${SCRIPT}"
fi
fi
##########################################################################################
### CLEANUP: remove temporary output file. ###############################################
##########################################################################################
for TMP in "${TEMP_CLEANUP[@]}" ; do
rm "${TMP}"
done
exit ${EXITCODE}