forked from NationalSecurityAgency/datawave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
306 lines (259 loc) · 9.13 KB
/
common.sh
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
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${BIN_DIR}/logging.sh"
source "${BIN_DIR}/query.sh"
# Upon uninstall, tarballs will be preserved in place by default.
# To remove them, use DW_UNINSTALL_RM_BINARIES_FLAG_*
DW_UNINSTALL_RM_BINARIES_FLAG_LONG="--remove-binaries"
DW_UNINSTALL_RM_BINARIES_FLAG_SHORT="-rb"
function register() {
local servicename="$1"
local servicescript="${DW_CLOUD_HOME}/bin/services/${servicename}/bootstrap.sh"
if [ -z "${servicename}" ] ; then
error "Registration failed: service name was null"
return 1
fi
if [ ! -f "${servicescript}" ] ; then
error "Registration failed: ${servicescript} doesn't exist"
return 1
fi
if [ ! -z "$( echo "${DW_CLOUD_SERVICES}" | grep "${servicename}" )" ] ; then
return 1 # servicename is already registered
fi
# Source the service script
source "${servicescript}"
# TODO: should probably verify/assert that all required functions have been implemented
# Finally, add service name to our list
DW_CLOUD_SERVICES="${DW_CLOUD_SERVICES} ${servicename}"
}
function resetQuickstartEnvironment() {
if [ "${1}" == "--hard" ] ; then
# The nuclear option...
# kill -9 all running services
allStop ${1}
# Uninstall. All will be erased including downloaded tarballs, although you'll
# still be able to opt out before the trigger is pulled here...
allUninstall ${DW_UNINSTALL_RM_BINARIES_FLAG_LONG}
fi
# Now we can re-source all bootstraps and thus re-register all configured services.
# To accomplish that, all we need to do is unset DW_CLOUD_SERVICES and then source env.sh
# Any missing tarballs will be re-downloaded (and/or re-copied if local) and, wrt DataWave tarballs, rebuilt
DW_CLOUD_SERVICES=""
source "${DW_CLOUD_HOME}/bin/env.sh"
}
function askYesNo() {
# First, check the status of DEFAULT_YN_RESPONSE to see if the
# user wants to avoid prompts of any kind. The only valid values
# are either Y or N, case-insensitive...
if [ -n "${DEFAULT_YN_RESPONSE}" ] ; then
local defaultResponse="$( echo "$DEFAULT_YN_RESPONSE" | tr '[:upper:]' '[:lower:]' )"
[ "${defaultResponse}" == "y" ] && return 0
[ "${defaultResponse}" == "n" ] && return 1
fi
# Get the user's 'yes|y' / 'no|n' reply to the given question
# (Keeps asking until a valid response is given)
echo
YN_MSG="$1"
while true; do
read -r -p "${YN_MSG} [y/n]: " USER_REPLY
case "$( echo "$USER_REPLY" | tr '[:upper:]' '[:lower:]' )" in
y|yes) echo && return 0 ;;
n|no) echo && return 1 ;;
esac
echo " - Invalid response"
done
}
function downloadTarball() {
# Downloads the specified tarball, if it doesn't already exist.
# If you want to utilize a tarball from the local file system, simply use
# "file:///absolute/path/to/filename" as the $1 (uri) arg
local uri="$1"
local tarballdir="$2"
tarball="$( basename ${uri} )"
if [ ! -f "${tarballdir}/${tarball}" ] ; then
if [[ ${uri} == file://* ]] ; then
$( cd "${tarballdir}" && curl -o "./${tarball}" "${uri}" )
else
$( cd "${tarballdir}" && wget ${DW_WGET_OPTS} "${uri}" )
fi
fi
}
function writeSiteXml() {
# Writes *-site.xml files, such as hdfs-site.xml, accumulo-site.xml, etc...
local sitefile="$1" # The file name to write
local siteconf="$2" # The property name/value pairs to write
# read the "name value" siteconf properties, one line at a time
printf '%s\n' "$siteconf" | ( while IFS= read -r nameval ; do
# parse the name and value from the line
local name=${nameval% *}
local value=${nameval##* }
# concatenate the xml into a big blob
local xml=${xml}$(printf "\n <property>\n <name>$name</name>\n <value>$value</value>\n </property>\n")
done
# write the blob to file...
printf "<configuration>${xml}\n</configuration>" > ${sitefile} )
}
function allStatus() {
# Gets the status of all registered services
local services=(${DW_CLOUD_SERVICES})
for servicename in "${services[@]}" ; do
${servicename}Status
done
}
function allStart() {
# Starts all registered services
local services=(${DW_CLOUD_SERVICES})
for servicename in "${services[@]}" ; do
if ${servicename}IsInstalled ; then
${servicename}IsRunning || ${servicename}Start
else
info "${servicename} service is not installed"
fi
done
}
function allStop() {
[[ "${1}" == "--hard" ]] && local kill9=true
# Stops all registered services
if ! servicesAreRunning ; then
echo "No services are currently running"
return 1
fi
local services=(${DW_CLOUD_SERVICES})
# Loop in reverse order for stopping services
# In other words, order of registration matters.
# e.g., we don't want to stop Hadoop *before* Accumulo.
for (( idx=${#services[@]}-1 ; idx>=0 ; idx-- )) ; do
if [ "${kill9}" == true ] ; then
local pidList="$( ${services[idx]}PidList )"
if [ ! -z "${pidList}" ] ; then
info "Killing ${services[idx]} services: ${pidList}"
kill -9 ${pidList}
fi
else
${services[idx]}Stop
fi
done
}
function allServicesAreInstalled() {
local services=(${DW_CLOUD_SERVICES})
local -i numServices=${#services[@]}
local -i servicesNotInstalled=0
for servicename in "${services[@]}" ; do
if ! ${servicename}IsInstalled ; then
servicesNotInstalled=$(( servicesNotInstalled + 1 ))
DW_CLOUD_SERVICES_NOT_INSTALLED="${DW_CLOUD_SERVICES_NOT_INSTALLED} ${servicename}"
fi
done
[ ${servicesNotInstalled} -gt 0 ] && return 1 || return 0
}
function isInteractiveShell() {
[ -z "$PS1" ] && return 1 || return 0
}
function isLoginShell() {
local shell="$0"
[[ "${shell:0:1}" != "-" ]] && return 1 || return 0
}
function promptForInstallation() {
installInProgressFlag="${DW_CLOUD_HOME}/.install-in-progress"
[ -f "${installInProgressFlag}" ] && return
if ! allServicesAreInstalled ; then
if [ "${DW_REDEPLOY_IN_PROGRESS}" == true ] ; then
# Auto-install on redeploy
installMissingServices
else
if askYesNo "Install the following services: ${DW_CLOUD_SERVICES_NOT_INSTALLED} ?" ; then
installMissingServices
fi
fi
fi
}
function installMissingServices() {
if ! allServicesAreInstalled ; then
date > "${installInProgressFlag}"
local services=(${DW_CLOUD_SERVICES_NOT_INSTALLED})
for servicename in "${services[@]}" ; do
${servicename}IsInstalled || ${servicename}Install
done
rm -f "${installInProgressFlag}"
fi
}
function allInstall() {
# Installs all registered services
if servicesAreRunning ; then
echo "Services are currently running"
allStatus
return 1
fi
local services=(${DW_CLOUD_SERVICES})
for servicename in "${services[@]}" ; do
${servicename}Install
done
}
function allUninstall() {
# ${DW_CLOUD_DATA} will be removed by default. To keep it, use '--keep-data' flag
# Uninstalls all registered services.
if servicesAreRunning ; then
echo "Stop running services before uninstalling!"
allStatus
return 1
fi
askYesNo "Uninstalling everything under '${DW_CLOUD_HOME}'. This can not be undone.
Continue?" || return 1
local removeBinaries=false
local keepData=false
while [ "${1}" != "" ]; do
case "${1}" in
--keep-data)
keepData=true
;;
${DW_UNINSTALL_RM_BINARIES_FLAG_LONG}|${DW_UNINSTALL_RM_BINARIES_FLAG_SHORT})
removeBinaries=true
;;
*)
error "Invalid argument passed: ${1}" && return 1
esac
shift
done
local services=(${DW_CLOUD_SERVICES})
for servicename in "${services[@]}" ; do
if [ "${removeBinaries}" == true ] ; then
${servicename}Uninstall ${DW_UNINSTALL_RM_BINARIES_FLAG_LONG}
else
${servicename}Uninstall
fi
done
if [[ "${keepData}" == false ]] ; then
# Remove data
[ -d "${DW_CLOUD_DATA}" ] && rm -rf "${DW_CLOUD_DATA}" && info "Removed ${DW_CLOUD_DATA}"
fi
}
function allPrintenv() {
echo
echo "DW Cloud Environment"
echo
( set -o posix ; set ) | grep "DW_CLOUD"
echo
local services=(${DW_CLOUD_SERVICES})
for servicename in "${services[@]}" ; do
${servicename}Printenv
done
}
function assertCreateDir() {
[[ $# -eq 0 || -z "$1" ]] && fatal "[${FUNCNAME[0]}] Directory parameter cannot be empty"
[ -d $1 ] && warn "[${FUNCNAME[0]}] already exists!" && return
mkdir -p "$1" && info "Created directory: $1"
[ ! -d "$1" ] && fatal "[${FUNCNAME[0]}] configured base directory $1 does not exist"
}
function sshIsInstalled() {
[ -z "$( which ssh )" ] && return 1
[ -z "$( which sshd )" ] && return 1
return 0
}
function servicesAreRunning() {
# Returns 0, if any registered services are running.
# Returns non-zero, if no registered services are running
local services=(${DW_CLOUD_SERVICES})
for servicename in "${services[@]}" ; do
${servicename}IsRunning && return 0
done
return 1 # Nothing running
}