forked from CJohnsonADAH/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-titlesdb-diff
executable file
·155 lines (125 loc) · 5.84 KB
/
adpn-titlesdb-diff
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
#!/bin/bash
#
# adpn-titlesdb-diff: Take a before/after diff of titlesdb XML listing for given peer node
# from the ADPNet configuration server, to confirm that an AU has been correctly added to
# the ingest list, or correctly published to the network.
#
# If taking a "before" snapshot, returns 0 provided the HTTP GET comes off successfully
# and provides identical results one after the other.
#
# If taking an "after" snapshot, returns 0 provided the HTTP GET comes off successfully
# and *differs* from the "before" snapshot.
#
# Default values for commonly-reused configuration switches can be specified in plaintext
# file named `adpn-titlesdb-diff.defaults.conf`, with one switch per line, in the same
# directory as the script.
#
# @version 2021.0330
__DOC__="""
Usage: adpn-titlesdb-diff [<OPTION>]... [<PEER>|ALL]
--help Display these usage notes
--verbose[=<LEVEL>] Display diagnostic messages. (Higher levels = more messages.)
--props-server=<URL> URL for ADPNet config/props server
e.g. http://configuration.adpn.org/titlelist/
--xmldir=<DIR> file system path for before/after snapshots
default: user home dir (${HOME})
--wget=<CMD> command for HTTP GET request; should print to stdout
e.g. 'curl', 'wget -O -', 'curl --silent', etc.
--before save a 'Before' snapshot
--after save an 'After' snapshot and diff against 'Before'
<PEER>|ALL code for node to check (e.g.: ADAH, AUB, UAT, etc.)
or ALL for titles published to all nodes on the network
Exit codes:
0 = success (successful retrieval and expected result)
1 = diff failure (no difference detected between 'Before' and 'After' snapshots)
2-255 = HTTP request failure (usually a curl or wget error code)
"""
SCRIPTPATH=$(which $0)
SCRIPTDIR=$(dirname "${SCRIPTPATH}")
SCRIPTNAME=$(basename "${SCRIPTPATH}")
source "${SCRIPTDIR}/adpn-define-aliases"
##########################################################################################
### COMMAND LINE: loop through switches ##################################################
##########################################################################################
_CMDLINE="$0 $*"
declare -a _ARGV ; _ARGV=("$0")
declare -A _PARAM ; _PARAM=()
shopt -s lastpipe
declare -a SWITCHFILES ; SWITCHFILES=()
if [[ -r "${CONFFILE}" ]] ; then
SWITCHFILES+=(${CONFFILE})
fi
CMDLINETXT=`mktemp`
until [[ "$#" -lt 1 ]] ; do
printf "%s\n" "$1" >> "${CMDLINETXT}"
shift
done
SWITCHFILES+=(${CMDLINETXT})
adpn_command_line "${SWITCHFILES[@]}"
##########################################################################################
### SCRIPT: Download XML from props server, then diff against old. #######################
##########################################################################################
PROPS=$(adpn_parameter_from "--props-server" ".props-server" "" )
XMLDIR=$(adpn_parameter_from "--xmldir" ".xmldir" ~ )
WGET=$(adpn_parameter_from "--wget" ".wget" "" )
if [[ -n "${PROPS}" ]] ; then
TITLELIST="${PROPS%%+(/)}/titlelist/"
fi
[[ -z "${_PARAM[verbose]}" ]] && V=0 || V="${_PARAM[verbose]}"
[[ "${V}" =~ ^[0-9]+$ ]] && V="${V}" || V=1
[[ $V -ge 1 ]] && echo "[${SCRIPTNAME}] command line: ${_CMDLINE}" >&2
[[ ! -z "${XMLDIR}" ]] || XMLDIR=~
if [[ -z "${WGET}" ]] ; then
WGET=$(which curl) && [[ ! -z "${WGET}" ]] && WGET="${WGET} --fail --silent"
[[ $V -ge 2 ]] && HEADS=$(mktemp) || HEADS=""
[[ -n "${HEADS}" ]] && WGET="${WGET} --dump-header ${HEADS}"
fi
if [[ -z "${WGET}" ]] ; then
WGET=$(which wget) && [[ ! -z "${WGET}" ]] && WGET="${WGET} --quiet --output-document=-"
fi
# curl, wget, or similar; should print to stdout
if [[ ! -z "${_PARAM[help]}" ]] ; then
echo "${__DOC__}"
echo ""
echo "props-server: ${PROPS}"
echo "xmldir: ${XMLDIR}"
echo "curl/wget: ${WGET}"
[[ -n "${HEADS}" ]] && rm "${HEADS}"
exit 0
fi
PEER="$( [[ -z "${_ARGV[1]}" ]] && printf "%s" "ALL" || printf "%s" "${_ARGV[1]}" )"
P_STYPE="$( [[ -z "${_PARAM[stype]}" ]] && printf "%s" "0" || printf "%s" "${_PARAM[stype]}" )"
P_EXT="$( [[ -z "${_PARAM[ext]}" ]] && printf "%s" ".xml" || printf "%s" "${_PARAM[ext]}" )"
OLDXML="$( [[ -z "${_PARAM[before]}" || "${_PARAM[before]}" == "before" ]] && printf "%s" "${XMLDIR}/titlesdb.${PEER}-${P_STYPE}.0${P_EXT}" || printf "%s" "${_PARAM[before]}" )"
NEWXML="$( [[ -z "${_PARAM[after]}" || "${_PARAM[after]}" == "after" ]] && printf "%s" "${XMLDIR}/titlesdb.${PEER}-${P_STYPE}.1${P_EXT}" || printf "%s" "${_PARAM[after]}" )"
EXITCODE=0
TDB_URL="${TITLELIST}index?peer=${PEER}&stype=${P_STYPE}&ext=${P_EXT}"
if [[ ! -z "${_PARAM[before]}" || ! -r "${OLDXML}" ]] ; then
[[ $V -ge 2 ]] && echo "${WGET} '${TDB_URL}' > '${OLDXML}' || EXITCODE=\$?" >&2
${WGET} "${TDB_URL}" > "${OLDXML}" || EXITCODE=$?
[[ -n "${HEADS}" ]] && cat "${HEADS}" >& 2
[[ -n "${HEADS}" ]] && rm "${HEADS}"
fi
[[ $V -ge 2 ]] && echo "${WGET} '${TDB_URL}' > '${NEWXML}' || EXITCODE=\$?" >&2
${WGET} "${TDB_URL}" > "${NEWXML}" || EXITCODE=$?
[[ -n "${HEADS}" ]] && cat "${HEADS}" >& 2
[[ -n "${HEADS}" ]] && rm "${HEADS}"
if [[ "${EXITCODE}" -gt 0 ]] ; then
echo "[${SCRIPTNAME}] HTTP GET (${WGET}:${TDB_URL}) failed, exit code: ${EXITCODE}" 1>&2
exit "${EXITCODE}"
fi
[[ $V -ge 2 ]] && echo "diff -u '${OLDXML}' '${NEWXML}' ; EXITCODE=\$?" >&2
diff -u "${OLDXML}" "${NEWXML}" ; EXITCODE=$?
if [[ -z "${_PARAM[before]}" ]] ; then
if [[ "${EXITCODE}" -eq 0 ]] ; then
echo "[${SCRIPTNAME}] No difference found on ${PEER} @" $(date) >&2
EXITCODE=1
elif [[ "${EXITCODE}" -eq 1 ]] ; then
EXITCODE=0
fi
fi
##########################################################################################
### CLEANUP: remove temporary output file. ###############################################
##########################################################################################
rm "${CMDLINETXT}"
exit ${EXITCODE}