-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyraxctl
executable file
·543 lines (466 loc) · 17.6 KB
/
hyraxctl
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/bin/bash
#
# hyraxctl
# Can be used to start and stop the docker containers running Hyrax and the
# # Apache http services, either individually or together.
# In this implementation Apache httpd connects to Hyrax using AJP on port 8009.
#
# The functions are simple. Read the usage statement, and then read the code.
#
#
##########################################################################
# loggy()
#
function loggy() {
echo "# $*" >&2
}
export H1="#######################################################################"
export H2="# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
export H3="# - - - - - - - - - - - - - - - - - - - - - - -"
export HYRAX_NAME=${HYRAX_NAME:-"hyrax"}
export HTTPD_NAME=${HTTPD_NAME:-"httpd"}
export HYRAX_HOME=${HYRAX_HOME:-"/home/ubuntu/hyrax"}
export DATA_MOUNT=${DATA_MOUNT:-"/usr/share/hyrax"}
export HYRAX_IMAGE=${HYRAX_IMAGE:-"opendap/hyrax:snapshot"}
export HTTPD_IMAGE_DEFAULT="httpd:latest"
# As of 11/16/203 we rolled back to this because of some issue we don't
# understand in httpd:latest - ndp 11/16/2023
export HTTPD_IMAGE_DEFAULT="httpd:2.4.56-bullseye"
export HTTPD_IMAGE=${HTTPD_IMAGE:-"$HTTPD_IMAGE_DEFAULT"}
export APACHE_HTTPD_HOME=${APACHE_HTTPD_HOME:-"/usr/local/apache2"}
export HYRAX_NETWORK=${HYRAX_NETWORK:-"hyrax-net"}
export HYRAX_DEPLOYMENT="http://test.opendap.org"
export THREDDS_CATALOG_ROOT=${THREDDS_CATALOG_ROOT:-""}
export HYRAX_MEMORY=${HYRAX_MEMORY:-"24g"}
################################################################################
#
# show_config()
#
function show_config() {
echo "${H2}" >&2
echo "#" >&2
echo "# show_config()" >&2
echo "#" >&2
echo "# PRIMARY: " >&2
echo "#" >&2
echo "# HYRAX_NAME: ${HYRAX_NAME}" >&2
echo "# HTTPD_NAME: ${HTTPD_NAME}" >&2
echo "# HYRAX_HOME: ${HYRAX_HOME}" >&2
echo "# DATA_MOUNT: ${DATA_MOUNT}" >&2
echo "# HYRAX_IMAGE: ${HYRAX_IMAGE}" >&2
echo "# HTTPD_IMAGE: ${HTTPD_IMAGE}" >&2
echo "# APACHE_HTTPD_HOME: ${APACHE_HTTPD_HOME}" >&2
echo "# HYRAX_NETWORK: ${HYRAX_NETWORK}" >&2
echo "# HYRAX_DEPLOYMENT: ${HYRAX_DEPLOYMENT}" >&2
echo "# HYRAX_MEMORY: ${HYRAX_MEMORY}" >&2
echo "#" >&2
echo "# DERIVED: " >&2
echo "#" >&2
echo "# log_dir: ${log_dir}" >&2
echo "# data_dir: ${data_dir}" >&2
echo "# bes_site_conf_file: ${bes_site_conf_file}" >&2
echo "# bes_log_dir: ${bes_log_dir}" >&2
echo "# olfs_conf_dir: ${olfs_conf_dir}" >&2
echo "# tomcat_server_xml: ${tomcat_server_xml}" >&2
echo "# tomcat_server_xml_volume: ${tomcat_server_xml_volume}" >&2
echo "# httpd_conf: ${httpd_conf}" >&2
echo "# htdocs_content: ${htdocs_content}" >&2
echo "# expires_sh: ${expires_sh}" >&2
echo "# ncwms: ${ncwms}" >&2
echo "#" >&2
}
################################################################################
#
# config_test_opendap_org()
#
# shellcheck disable=SC2120
function config_test_opendap_org(){
local hyrax_home=${1:-"${HYRAX_HOME}"}
local data_dir_in=${2:-"${DATA_MOUNT}"}
echo "${H1}" >&2
echo "#" >&2
echo "# config_test_opendap_org()" >&2
echo "#" >&2
echo "# Using hyrax_home: ${hyrax_home}" >&2
echo "#" >&2
# Location of the log directories for bes, olfs**, and tomcat
export log_dir="${hyrax_home}/log"
# Set up the location of the data directory for Hyrax.
# If one is not provided the default ${hyrax_home}/data
# will be used.
if test -n "${data_dir_in}"
then
export data_dir="${data_dir_in}"
else
export data_dir="${hyrax_home}/data"
echo "# Using default data_dir value of: ${data_dir}" >&2
fi
# The location of the OLFS persistent configuration
# to be injected into the Hyrax container at /etc/olfs
#
# **If this is used then the OLFS logs will be written
# to ${hyrax_home}/olfs and the log directory
# ${hyrax_home}/log/olfs will be ignored.
export olfs_conf_dir="${hyrax_home}/olfs"
export bes_site_conf_file="${hyrax_home}/bes/site.conf"
export bes_log_dir="${log_dir}/bes"
# A Tomcat server.xml file that enables AJP pn 8009
export tomcat_server_xml="${hyrax_home}/tomcat/server.xml"
# Creates the tomcat server.xml volume mount key pair for the Docker run command.
export tomcat_server_xml_volume=""
if test -n "${tomcat_server_xml}"; then
export tomcat_server_xml_volume="--volume ${tomcat_server_xml}:/tomcat/conf/server.xml"
fi
#
# Variables for configuring Docker launch of httpd
#
# We inject a modified httpd.conf file
export httpd_conf="${hyrax_home}/httpd/httpd.conf"
# Our own collection of things that need to be served via http,
# possibly utilizing range get capabilities
export htdocs_content="${hyrax_home}/httpd/htdocs"
# Q: Is expires.sh still in use? Can we drop it?
# A: Yes it's still in use by libdap4 tests.
# Q: Can we drop it?
# A: No we can't without fixing various things
export expires_sh="${hyrax_home}/httpd/cgi-bin/expires.sh"
# ncWMS host name to be injected into the ncWMS configuration.
export ncwms="${HYRAX_DEPLOYMENT}"
show_config
}
################################################################################
#
# config_ndp_local()
#
function config_ndp_local(){
echo "${H1}" >&2
echo "#" >&2
echo "# config_ndp_local()" >&2
echo "#" >&2
prefix=${prefix:-"/Users/ndp/OPeNDAP/hyrax/build"}
export hyrax_home="/Users/ndp/OPeNDAP/test.opendap.org"
export ncwms="http://localhost:8080"
export log_dir="${hyrax_home}/log"
export bes_log_dir="${log_dir}/bes"
export data_dir="${prefix}/share/hyrax"
export olfs_conf_dir="${hyrax_home}/etc/olfs"
export bes_site_conf_file="${hyrax_home}/bes/site.conf"
# export tomcat_server_xml="${hyrax_home}/tomcat/server.xml"
# export tomcat_server_xml_volume="--volume ${tomcat_server_xml}:/tomcat/conf/server.xml"
show_config
}
################################################################################
#
# make_network_as_needed()
#
function make_network_as_needed(){
#
# Check for the network that will be shared with Apache httpd
# If it's not found, create it.
#
local status=
docker network inspect ${HYRAX_NETWORK} > /dev/null 2>&1
status=$?
if test $status -ne 0 ;
then
echo "# No Docker network by the name of ${HYRAX_NETWORK} was found. Creating..." >&2
docker network create hyrax-net
echo "# Created Docker network '${HYRAX_NETWORK}' for Hyrax and Apache httpd." >&2
echo "#" >&2
else
echo "# Docker network '${HYRAX_NETWORK}' exists, using." >&2
echo "#" >&2
fi
}
################################################################################
#
# delete_network()
#
function delete_network(){
# Dump Hyrax Docker Network
docker network rm ${HYRAX_NETWORK}
}
################################################################################
#
# start_httpd()
#
function start_httpd(){
echo "# Starting Docker container for Apache httpd: ${HTTPD_IMAGE}" >&2
docker run -d \
--name ${HTTPD_NAME} \
--net=${HYRAX_NETWORK} \
--publish 80:80 \
--publish 443:443 \
--volume ${httpd_conf}:${APACHE_HTTPD_HOME}/conf/httpd.conf \
--volume ${htdocs_content}:${APACHE_HTTPD_HOME}/htdocs \
--volume ${expires_sh}:${APACHE_HTTPD_HOME}/cgi-bin/expires.sh \
"${HTTPD_IMAGE}"
echo "#" >&2
echo "# Docker container: ${HTTPD_IMAGE} STARTED." >&2
echo "#" >&2
}
################################################################################
#
# stop_httpd()
#
function stop_httpd(){
# Dump httpd container
docker rm -f "${HTTPD_NAME}"
}
################################################################################
#
# update_httpd()
#
function update_httpd() {
docker pull "${HTTPD_IMAGE}"
stop_httpd
start_httpd
}
function get_platform(){
local sys_arch=$(arch)
echo "# sys_arch: sys_arch" >&2
local platform=""
if test $sys_arch = "arm64"; then platform="--platform linux/amd64"; fi
echo "$platform"
}
function set_file_permissions() {
echo "# set_file_permissions()" >&2
local vers_doc=$( which sw_vers )
if test $? -ne 0
then
local name="testy_besty"
local platform=""
platform=$( get_platform )
echo "# platform: $platform" >&2
local use_disconnect="-d" # ${disconnect:-"-d"}
echo "# use_disconnect: $use_disconnect" >&2
echo "# Checking bes user info in Hyrax container: ${HYRAX_IMAGE}" >&2
docker run $use_disconnect $platform --name "${name}" ${HYRAX_IMAGE}
sleep_time=10
echo "# Waiting $sleep_time seconds for hyrax to get going..."
sleep $sleep_time
local bes_uid
bes_uid=$(docker logs "${name}" 2>&1 | grep bes_uid | awk '{print $2;}')
echo "# bes_uid: ${bes_uid}" >&2
local bes_gid
bes_gid=$(docker logs "${name}" 2>&1 | grep bes_gid | awk '{print $2;}')
echo "# bes_gid: ${bes_gid}" >&2
echo "# Stopping Hyrax container: ${HYRAX_IMAGE}" >&2
docker container rm -f "${name}"
if test -n "$bes_uid" && test -n "$bes_gid"
then
echo "# Setting ownership of ${bes_log_dir}" >&2
sudo chown -R ${bes_uid}:${bes_gid} "${bes_log_dir}"
else
echo "ERROR - Failed to determine one or both of bes user uid or gid."
return 1
fi
else
echo "# Detected OSX, no permissions were changed." >&2
sw_vers | awk '{print "# "$0;}' >&2
fi
return 0
}
################################################################################
# save_logs()
# Saves the bes log file. The tomcat logs and OLFS logs are already being
# saved/rolled like a server should.
function save_logs() {
cp "${HYRAX_HOME}/log/bes/bes.log" "${HYRAX_HOME}/log/bes"/$(date +"%Y-%m-%d-%H:%M:%S")"-bes.log"
}
################################################################################
#
# start_hyrax()
#
function start_hyrax(){
echo "# start_hyrax()" >&2
save_logs
set_file_permissions
if test $? -ne 0
then
echo "ERROR - Unable to set host file permissions. Hyrax was not started."
return 1;
fi
local use_disconnect=${disconnect:-"-d"}
echo "# use_disconnect: $use_disconnect" >&2
local platform=""
platform=$( get_platform )
echo "# platform: $platform" >&2
echo "# Starting Hyrax container: ${HYRAX_IMAGE}" >&2
docker run $use_disconnect $platform \
--name ${HYRAX_NAME} \
--net=${HYRAX_NETWORK} \
--publish 8009:8009 \
--publish 8080:8080 \
--publish 8443:8443 \
--volume ${data_dir}:/usr/share/hyrax:ro \
--volume ${bes_log_dir}:/var/log/bes \
--volume ${log_dir}/tomcat/console.log:/var/log/tomcat/console.log \
--volume ${log_dir}/tomcat:/usr/share/tomcat/logs \
--volume ${olfs_conf_dir}:/etc/olfs \
--volume ${bes_site_conf_file}:/etc/bes/site.conf \
--memory=${HYRAX_MEMORY} \
${tomcat_server_xml_volume} \
"${HYRAX_IMAGE}" \
-e [email protected] \
-s \
-n ${ncwms}
echo "#" >&2
echo "# Docker container: ${HYRAX_IMAGE} STARTED." >&2
echo "#" >&2
}
################################################################################
#
# stop_hyrax()
#
function stop_hyrax(){
# Dump Hyrax container
docker rm -f "${HYRAX_NAME}"
}
################################################################################
#
# update_hyrax()
#
function update_hyrax() {
docker pull "${HYRAX_IMAGE}"
stop_hyrax
start_hyrax
}
################################################################################
#
# start_all()
#
function start_all(){
make_network_as_needed
start_hyrax
start_httpd
}
################################################################################
#
# stop_all()
#
function stop_all(){
# Dump all containers
docker rm -f $(docker ps -aq)
# Dump the network.
docker network rm ${HYRAX_NETWORK}
}
################################################################################
#
# clean_and_update()
#
function clean_and_update_all(){
stop_all
# Dump ALL the images
docker rmi -f $(docker images -q)
# Pull our stuff
docker pull ${HYRAX_IMAGE}
docker pull ${HTTPD_IMAGE}
}
################################################################################
#
# usage()
#
function usage(){
echo "${H1}" >&2
echo "# NAME" >&2
echo "# hyraxctl - hyrax control" >&2
echo "#" >&2
echo "# SYNOPSIS" >&2
echo "# hyraxctl cmd1 [cmd2 ... cmdN]" >&2
echo "#" >&2
echo "# DESCRIPTION" >&2
echo "# The hyraxctl utility implements a collection of commands that " >&2
echo "# allow one to start, stop, and update Hyrax and Apache httpd docker" >&2
echo "# images with all of the configuration and resources needed to" >&2
echo "# operate test.opendap.org." >&2
echo "# The commands can be invoked by using one or more in the hyraxctl" >&2
echo "# commandline line invocation:" >&2
echo "# " >&2
echo "# ./hyraxctl show_config" >&2
echo "# " >&2
echo "# Or, for two or more commands:" >&2
echo "# " >&2
echo "# ./hyraxctl config_test_opendap_org start_all" >&2
echo "# " >&2
echo "# Primary configuration values can be exported in the parent shell" >&2
echo "# in order to inject different. Additionally, the file can be" >&2
echo "# sourced:" >&2
echo "# source ./hyraxctl" >&2
echo "# . ./hyraxctl" >&2
echo "# " >&2
echo "# After which all of the commands will be directly available in the current" >&2
echo "# shell:" >&2
echo "# # . ./hyraxctl" >&2
echo "# # show_config" >&2
echo "# # start_all" >&2
echo "# # stop_all" >&2
echo "#" >&2
echo "# The available commands follow:" >&2
echo "#" >&2
echo "# show_config - Sends the configuration state to stderr." >&2
echo "# config_test_opendap_org - Configures the shell for deployment on" >&2
echo "# test.opendap.org (ubuntu Docker host)" >&2
echo "# config_ndp_local - Configures the shell for dev deployment" >&2
echo "# on ndp's system." >&2
echo "#" >&2
echo "# start_all - Makes the docker network, starts Hyrax," >&2
echo "# starts httpd." >&2
echo "# stop_all - Stops and deletes all containers, deletes" >&2
echo "# docker network." >&2
echo "# start_hyrax - Starts Hyrax in the docker engine using" >&2
echo "# configuration values in the env." >&2
echo "# stop_hyrax - Deletes the running (or not) hyrax " >&2
echo "# container from the docker engine." >&2
echo "# update_hyrax - Pulls the latest HYRAX_IMAGE tag (currently: " >&2
echo "# ${HYRAX_IMAGE}), stops the Hyrax container," >&2
echo "# and then starts the (possibly updated) image." >&2
echo "# start_httpd - Starts httpd in the docker engine using" >&2
echo "# configuration values in the env." >&2
echo "# stop_httpd - Deletes the running (or not) httpd " >&2
echo "# container from the docker engine." >&2
echo "# update_httpd - Pulls the latest HYRAX_IMAGE tag (currently: " >&2
echo "# ${HTTPD_IMAGE}), stops the Hyrax container," >&2
echo "# and then starts the (possibly updated) image." >&2
echo "# clean_and_update_all - Stops and deletes all containers, deletes" >&2
echo "# docker network, deletes all images, pulls" >&2
echo "# Hyrax and httpd by tag" >&2
echo "#" >&2
echo "# make_network_as_needed - Creates docker network for Hyrax and" >&2
echo "# httpd, as needed." >&2
echo "# delete_network - Deletes the docker network for Hyrax" >&2
echo "# and httpd." >&2
echo "#" >&2
echo "${H3}" >&2
}
function run_cmd(){
local cmd="${1}"
if test "${cmd}" == "show_config"; then show_config;
elif test "${cmd}" == "config_test_opendap_org"; then config_test_opendap_org;
elif test "${cmd}" == "config_ndp_local"; then config_ndp_local;
elif test "${cmd}" == "start_all"; then start_all;
elif test "${cmd}" == "stop_all"; then stop_all;
elif test "${cmd}" == "start_hyrax"; then start_hyrax;
elif test "${cmd}" == "stop_hyrax"; then stop_hyrax;
elif test "${cmd}" == "start_httpd"; then start_httpd;
elif test "${cmd}" == "stop_httpd"; then stop_httpd;
elif test "${cmd}" == "clean_and_update"; then clean_and_update;
elif test "${cmd}" == "make_network_as_needed"; then make_network_as_needed;
elif test "${cmd}" == "delete_network"; then delete_network;
else
echo "ERROR Unrecognized command: ${cmd}" >&2
usage
fi
}
echo "ArgCount: $#" >&2
if test "$#" -gt 0
then
for a_cmd in "$@"
do
echo "Running command: ${a_cmd}" >&2
run_cmd "${a_cmd}"
done
else
config_test_opendap_org
usage
fi