forked from NMAAHC/nmaahcmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmaahcmmfunctions
executable file
·260 lines (241 loc) · 9.89 KB
/
nmaahcmmfunctions
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
# assign variables
SCRIPTDIR=$(dirname "${0}")
NMAAHCMM_CONFIG_FILE="${SCRIPTDIR}/nmaahcmm.conf"
GM_CONFIG_FILE="${SCRIPTDIR}/gm.conf"
WHAT_IS_THIS="NMAAHC mediamicroservices"
# You can't directly call a function in another shell script. You can move your function definitions into a separate file and then load them into your script using the . command, like this:
# . /path/to/functions.sh
# The script will interpret functions.sh as if its content were actually present in your file at this point. It is a common mechanism for implementing shared libraries of shell functions, and is how each script in nmaahcmm can reference the shared functions in this file.
# Here is an example specific to NMAAHC:
## SCRIPT_PATH="${0%/*}" # SCRIPT_PATH will be the directory path of {0} which is the script being called
## . "${SCRIPT_PATH}/nmaahcmmfunctions" # loads the functions in nmaahcmmfunctions into this script
## [[ -f "${SCRIPT_PATH}/nmaahcmmfunctions" ]] || { echo "Missing '${SCRIPT_PATH}/nmaahcmmfunctions'. Exiting." ; exit 1 ;}; # checks to see that that nmaahcmmfunctions exists; if it doesn't (left side of || exits non-zero) then it will execute the right side of ||
# load configuration file
if [ -f "${NMAAHCMM_CONFIG_FILE}" ] ; then
. "${NMAAHCMM_CONFIG_FILE}"
elif [[ "${CONFIG}" != "Y" ]] && [[ "${REQUIRECONFIG}" != "N" ]] ; then
echo "The NMAAHCmm configuration file is not set. You must first create ${NMAAHCMM_CONFIG_FILE} by running nmaahcmmconfig." 1>&2
exit 1
fi
[[ -n "${LOGDIR}" ]] && [[ ! -d "${LOGDIR}" ]] && mkdir "${LOGDIR}"
# load configuration file
if [ -f "${GM_CONFIG_FILE}" ] ; then
. "${GM_CONFIG_FILE}"
elif [[ "${CONFIG}" != "Y" ]] && [[ "${REQUIRECONFIG}" != "N" ]] && [[ "${CONFIG_TYPE}" = "GM" ]] ; then
echo "The Great Migration configuration file is not set. You must first create ${GM_CONFIG_FILE} by running gmconfig." 1>&2
exit 1
fi
# set text colors; can be used to differentiate statements that display in the terminal window
_setcolors(){
date="$(date +%Y%m%d)"
BIWHITE=$(tput bold)$(tput setaf 7)
BIRED=$(tput bold)$(tput setaf 1)
BIYELLOW=$(tput bold)$(tput setaf 3)
GRAY=$(tput setaf 7)
COLOR_OFF=$(tput sgr0)
}
# this function helps the system gracefully exit the script; it will only run when the operator presses ctrl+C
_initialize_make(){
_cleanup(){
_report -rt "Process cancelled" # tell operator that script is ending
exit 1
}
trap _cleanup SIGHUP SIGINT SIGTERM # "termination signals" that ask the system to clean up and safely kill the ongoing process, with increasing urgency
}
# a substitute for the plain mkdir tool; adds a more explicit warning to the operator if there is already a directory at that location
_mkdir(){
local DIR2MAKE=""
while [ "${*}" != "" ] ; do
DIR2MAKE="${1}"
if [ ! -d "${DIR2MAKE}" ] ; then
mkdir -p "${DIR2MAKE}"
if [ "${?}" -ne 0 ]; then
_report -rt "${0}: Can't create directory at ${DIR2MAKE}"
exit 1
fi
fi
shift
done
}
# removes hidden files (always identified with a period at the beginning of the filename - ".*")
_removehidden(){
if [ -z "${1}" ] ; then
cowsay "no argument provided to remove hidden files. tootles."
else
find "${1}" -name ".*" -exec rm -vfr {} \;
#cowsay "hidden files removed. tootles."
fi
}
_sortk2(){
if [ -z "${1}" ]; then
cowsay "no argument provided to sort. tootles."
else
sort -k 2 -o "${1}" "${1}" # -k 2=sort on the second field, -o=write output to file (instead of standard terminal output)
cowsay "file sorting is done. tootles."
fi
}
_pashua_run(){
# Wrapper function for interfacing to Pashua. Written by Carsten
# Bluem <[email protected]> in 10/2003, modified in 12/2003 (including
# a code snippet contributed by Tor Sigurdsson), 08/2004 and 12/2004.
# Write config file
# Find Pashua binary. We do search both . and dirname "${0}"
# , as in a doubleclickable application, cwd is /
# BTW, all these quotes below are necessary to handle paths
# containing spaces.
BUNDLEPATH="Pashua.app/Contents/MacOS/Pashua"
MYPATH=$(dirname "${0}")
for SEARCHPATH in "${MYPATH}/Pashua" "${MYPATH}/${BUNDLEPATH}" "./${BUNDLEPATH}" \
"/Applications/${BUNDLEPATH}" "${HOME}/Applications/${BUNDLEPATH}"
do
if [ -f "${SEARCHPATH}" -a -x "${SEARCHPATH}" ] ; then
PASHUAPATH=${SEARCHPATH}
break
fi
done
if [ ! "${PASHUAPATH}" ] && [[ "$(uname -s)" = "Darwin" ]] ; then
echo "Error: Pashua is used to edit but is not found."
if [[ "${PASHUAINSTALL}" = "" ]] ; then
echo "Attempting to run: brew cask install pashua"
if [[ "${PASHUAINSTALL}" != "Y" ]] && [[ "${PASHUAFAIL}" != "Y" ]] ; then
brew cask install pashua && PASHUAINSTALL="Y" || PASHUAFAIL=Y
_pashua_run
fi
fi
else
ENCODING=""
# Get result
RESULT=$("${PASHUAPATH}" ${ENCODING} ${PASHUA_CONFIGFILE} | sed 's/ /;;;/g')
# Parse result
for LINE in ${RESULT} ; do
KEY=$(echo ${LINE} | sed 's/^\([^=]*\)=.*$/\1/')
VALUE=$(echo ${LINE} | sed 's/^[^=]*=\(.*\)$/\1/' | sed 's/;;;/ /g')
VARNAME="${KEY}"
VARVALUE="${VALUE}"
eval $VARNAME='$VARVALUE'
done
fi
}
_check_deliverdir(){
if [[ ! -d "${DELIVERDIR}" ]] ; then
_report -rt "The delivery directory, ${DELIVERDIR}, does not exist. Cannot deliver the OUTPUT of $(basename "${0}")."
fi
}
_log(){
if [[ -n "$LOGDIR" ]] ; then
if [[ ! -d "${LOGDIR}" ]] ; then
mkdir "${LOGDIR}"
if [[ "${?}" -ne 0 ]] ; then
_report -rt "${0}: Can't create log directory at ${LOGDIR}, exiting..."
exit 1
fi
fi
MMLOGNAME="nmaahcmm.log"
OPTIND=1
while getopts ":beacw" OPT; do
case "${OPT}" in
b) STATUS="start" ;; # script is beginning
e) STATUS="end" ;; # script is ending
a) STATUS="abort" ;; # script is aborted
c) STATUS="comment" ;; # comment about what script is doing
w) STATUS="warning" ;; # warning information
esac
done
shift $(( ${OPTIND} - 1 ))
NOTE="${1}"
echo "$(date +%FT%T), $(basename "${0}") ${STATUS} ${OP} ${MEDIAID} ${NOTE}" >> "${LOGDIR}/${MMLOGNAME}"
fi
}
_writelog(){
if [[ -n "$LOGDIR" ]] ; then
if [[ -z "${LOG}" ]] ; then
echo "Error, can not write to log, ingest log not yet created"
exit
fi
if [[ ! -f "${LOG}" ]] ; then
mkdir -p "${LOGDIR}"
touch "${LOG}"
fi
KEY="${1}" # key is first string passed to function
VALUE="${2}" # value is variable passed to function
OPTIND=1
while getopts ":t" OPT; do
case "${OPT}" in
t) KEY="${2}" && VALUE="$(date +%FT%T)" ;; # value is day and time in format YYYY-MM-DDTHH:MM:
esac
done
# need to add yaml style escaping
echo "${KEY}: ${VALUE}" >> "${LOG}"
fi
}
_maketemp(){
mktemp -q "/tmp/$(basename "${0}").XXXXXX"
if [ "${?}" -ne 0 ]; then
_report -rt "${0}: Can't create temp file, exiting..."
_writeerrorlog "_maketemp" "was unable to create the temp file, so the script had to exit."
exit 1
fi
}
_check_dependencies(){
DEPS_OK=YES
while [ "${*}" != "" ] ; do
DEPENDENCY="${1}"
if [[ ! $(which "${DEPENDENCY}") ]] ; then
_report -rt "This script requires ${DEPENDENCY} to run but it is not installed."
printf "If you are running ubuntu or debian you might be able to install ${DEPENDENCY} with the following command"
printf "sudo apt-get install ${DEPENDENCY}"
printf "If you are running mac you might be able to install ${DEPENDENCY} with the following command"
printf "brew install ${DEPENDENCY}"
DEPS_OK=NO
fi
shift
done
if [[ "${DEPS_OK}" = "NO" ]]; then
_report -rt "Unmet dependencies. Exiting..."
_log -a "Process terminated by script (unmet dependencies)."
exit 1
else
return 0
fi
}
_report(){
local RED="$(tput setaf 1)" # Red - For Warnings
local GREEN="$(tput setaf 2)" # Green - For Declarations
local BLUE="$(tput setaf 4)" # Blue - For Questions
local NC="$(tput sgr0)" # No Color
local COLOR=""
local STARTMESSAGE=""
local ENDMESSAGE=""
local ECHOOPT=""
local LOG_MESSAGE=""
OPTIND=1
while getopts ":bgrstn" OPT; do
case "${OPT}" in
b) COLOR="${BLUE}" ;; # question mode, use color blue
g) COLOR="${GREEN}" ;; # declaration mode, use color green
r) COLOR="${RED}" ; LOG_MESSAGE="Y" ;; # warning mode, use color red
s) STARTMESSAGE+=([$(basename "${0}")] ) ;; # prepend scriptname to the message
t) STARTMESSAGE+=($(date +%FT%T) '- ' ) ;; # prepend timestamp to the message
n) ECHOOPT="-n" ;; # to avoid line breaks after echo
esac
done
shift $(( ${OPTIND} - 1 ))
MESSAGE="${1}"
echo "${ECHOOPT}" "${COLOR}${STARTMESSAGE[@]} ${MESSAGE}${NC}"
[ "${LOG_MESSAGE}" = "Y" ] && _log -w "${MESSAGE}"
}
_seconds_to_hhmmss(){
num=$1
h=`expr "$num" / 3600`
m=`expr "$num" % 3600 / 60`
s=`expr "$num" % 60`
printf "%02d:%02d:%02d\n" $h $m $s
}
_check_rsync_output(){
RSYNC_ERR="$?"
if [[ "${RSYNC_ERR}" != 0 ]] ; then
((RSYNC_ERROR_COUNT++))
else
:
fi
}