-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmanage
More file actions
executable file
·599 lines (503 loc) · 19 KB
/
manage
File metadata and controls
executable file
·599 lines (503 loc) · 19 KB
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/bin/bash
# Helioviewer.org Docker Management Script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Show usage information
usage() {
echo "Usage: $0 <command>"
echo ""
echo "Available commands:"
echo " init Initialize settings for Docker environment"
echo " preup Run init, npm_install, build_js_css, and create data directories. Do this before docker compose up"
echo " postup Populates database. Do this after docker compose up"
echo " up Shorthand for preup, docker compose up, postup"
echo " composer Run composer commands in the API container"
echo " npm_install Install npm dependencies for web client"
echo " build_js_css Build JavaScript and CSS for web client"
echo " watch_js Watch and rebuild JavaScript on changes"
echo " watch_3d Watch and rebuild 3D JavaScript on changes"
echo " downloader Run the Helioviewer data downloader"
echo " download_test_data Download test data for development"
echo " pytest Run pytest tests in the Python container"
echo " init_superset Initialize Superset database and default roles/permissions"
echo " prepare-dashboard Prepare a dashboard export zip file for import"
echo " gen-superset-pk Generate RSA private/public key pair for Superset guest tokens"
echo ""
}
# Load environment variables from .env file
load_env() {
local env_file="${SCRIPT_DIR}/.env"
if [ ! -f "${env_file}" ]; then
echo "Error: ${env_file} not found"
exit 1
fi
# Source .env file but filter out UID and GID to avoid readonly variable errors
# UID and GID are readonly in bash but needed in .env for docker-compose
grep -v '^UID=' "${env_file}" | grep -v '^GID=' > ..env
source ..env
rm ..env
}
# Check if file should be overwritten
# Returns 0 (true) if file should be overwritten, 1 (false) otherwise
# Returns 0 (true) if the file doesn't exist.
should_overwrite() {
local fname="$1"
if [ ! -f "${fname}" ]; then
return 0
fi
echo "Warning: ${fname} already exists"
read -p "Overwrite existing $(basename "${fname}")? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
else
return 1
fi
}
# Initialize settings.cfg for Docker environment
init_settings() {
echo "Initializing settings.cfg for Docker environment..."
local settings_example="${SCRIPT_DIR}/api/install/settings/settings.example.cfg"
local settings_file="${SCRIPT_DIR}/api/install/settings/settings.cfg"
if [ ! -f "${settings_example}" ]; then
echo "Error: ${settings_example} not found"
exit 1
fi
if should_overwrite "${settings_file}"; then
# Load environment variables from .env file
load_env
# Copy example file
cp "${settings_example}" "${settings_file}"
# Replace localhost with Docker service names
sed -i.bak 's/^dbhost = localhost$/dbhost = database/' "${settings_file}"
# Replace database configuration with environment variables
sed -i.bak "s/^dbname = helioviewer$/dbname = ${HV_DB_NAME}/" "${settings_file}"
sed -i.bak "s/^dbuser = helioviewer$/dbuser = ${HV_DB_USER}/" "${settings_file}"
sed -i.bak "s/^dbpass = helioviewer$/dbpass = ${HV_DB_PASS}/" "${settings_file}"
# Replace /mnt/data paths with /tmp paths for Docker volumes
sed -i.bak 's|/mnt/data/hvpull|/tmp/hvpull|g' "${settings_file}"
sed -i.bak 's|/mnt/data/jp2|/tmp/jp2|g' "${settings_file}"
# Disable email notifications by setting server to empty string
sed -i.bak 's/^server = localhost$/server = /' "${settings_file}"
# Clean up backup file
rm -f "${settings_file}.bak"
echo "Settings initialized successfully at ${settings_file}"
else
echo "Skipping settings.cfg initialization"
fi
}
# Initialize Config.ini and Private.php for Docker environment
init_config() {
echo "Initializing Config.ini for Docker environment..."
local config_example="${SCRIPT_DIR}/api/settings/Config.Example.ini"
local config_file="${SCRIPT_DIR}/api/settings/Config.ini"
if [ ! -f "${config_example}" ]; then
echo "Error: ${config_example} not found"
exit 1
fi
if should_overwrite "${config_file}"; then
# Load environment variables from .env file
load_env
# Copy example file
cp "${config_example}" "${config_file}"
# Replace paths and URLs with Docker-appropriate values
sed -i.bak 's|jp2_dir = /var/www-api/docroot/jp2|jp2_dir = /tmp/jp2|' "${config_file}"
sed -i.bak "s|web_root_url = http://localhost|web_root_url = ${API_URL}|" "${config_file}"
sed -i.bak "s|client_url = http://helioviewer.org|client_url = ${CLIENT_URL}|" "${config_file}"
sed -i.bak "s|coordinator_url = 'http://coordinator'|coordinator_url = 'http://coordinator'|" "${config_file}"
sed -i.bak 's|/var/www-api/docroot|/var/www/api.helioviewer.org/docroot|g' "${config_file}"
# Add CORS allowed origins
sed -i.bak "s|;acao_url\[\] = ''|acao_url[] = 'http://localhost:8080'\nacao_url[] = 'http://127.0.0.1:8080'\nacao_url[] = '${CLIENT_URL}'|" "${config_file}"
# Clean up backup file
rm -f "${config_file}.bak"
echo "Config.ini initialized successfully at ${config_file}"
else
echo "Skipping Config.ini initialization"
fi
# Initialize Private.php
echo "Initializing Private.php..."
local private_example="${SCRIPT_DIR}/api/settings/Private.Example.php"
local private_file="${SCRIPT_DIR}/api/settings/Private.php"
if [ ! -f "${private_example}" ]; then
echo "Error: ${private_example} not found"
exit 1
fi
if should_overwrite "${private_file}"; then
cp "${private_example}" "${private_file}"
echo "Private.php initialized successfully at ${private_file}"
else
echo "Skipping Private.php initialization"
fi
}
# Initialize web client Config.js for Docker environment
init_web_config() {
echo "Initializing web client Config.js for Docker environment..."
local config_file="${SCRIPT_DIR}/helioviewer.org/resources/js/Utility/Config.js"
if [ ! -f "${config_file}" ]; then
echo "Error: ${config_file} not found"
exit 1
fi
# Load environment variables from .env file
load_env
# Replace API and client URLs with Docker-appropriate values
sed -i.bak "s|'back_end' : \"https://api.helioviewer.org/\"|'back_end' : \"${API_URL}/\"|" "${config_file}"
sed -i.bak "s|'web_root_url' : \"https://helioviewer.org\"|'web_root_url' : \"${CLIENT_URL}\"|" "${config_file}"
sed -i.bak "s|'user_video_feed' : \"https://api.helioviewer.org/\"|'user_video_feed' : \"${API_URL}/\"|" "${config_file}"
sed -i.bak "s|'coordinator_url' : 'https://api.helioviewer.org/coordinate'|'coordinator_url' : '${COORDINATOR_URL}'|" "${config_file}"
# Clean up backup file
rm -f "${config_file}.bak"
echo "Web client Config.js initialized successfully at ${config_file}"
}
# Install PHP dependencies via Composer
init_composer() {
echo "Installing PHP dependencies..."
docker run --rm \
--user "${UID}:$(id -g)" \
-v "${SCRIPT_DIR}/api:/app" \
-w /app \
composer:latest \
composer install
}
# Initialize all settings for Docker environment
init() {
echo "Initializing Helioviewer settings for Docker environment..."
echo "-------------------------------------------"
init_settings
echo "-------------------------------------------"
init_config
echo "-------------------------------------------"
init_web_config
echo "-------------------------------------------"
init_composer
# Files required for API tests
load_env
cp compose/*.jp2 $HOST_JPEG2000_PATH
}
# Initialize everything: run init, npm_install, build_js_css, and create data directories
preup() {
echo "Running pre initialization..."
echo "-------------------------------------------"
# Load environment variables
load_env
# Create necessary data directories
echo "Creating data directories..."
mkdir -p "${HOST_JPEG2000_PATH}" "${HOST_CACHE_PATH}" "${HOST_LOG_PATH}"
echo "Data directories created: ${HOST_JPEG2000_PATH}, ${HOST_CACHE_PATH}, ${HOST_LOG_PATH}"
echo "-------------------------------------------"
# Run init
init
echo "-------------------------------------------"
# Install npm dependencies
npm_install
echo "-------------------------------------------"
# Build JavaScript and CSS
build_js_css
echo "-------------------------------------------"
# Generate superset private key
gen_superset_pk
echo "-------------------------------------------"
}
postup() {
init_superset
}
up() {
set -e
preup
docker compose up --wait
postup
}
# Run composer in the API container
composer() {
docker compose exec api /usr/bin/composer "$@"
}
# Install npm dependencies for web client
npm_install() {
echo "Installing npm dependencies..."
docker run --rm \
--user "${UID}:$(id -g)" \
-e HOME=/tmp \
-e NODE_OPTIONS='--localstorage-file=/tmp/helioviewer_localstorage' \
-e npm_config_cache=/tmp/.npm \
-v "${SCRIPT_DIR}/helioviewer.org:/app" \
-w /app \
node:lts \
npm ci
}
# Build JavaScript and CSS for web client
build_js_css() {
echo "Building JavaScript and CSS..."
docker run --rm \
--user "${UID}:$(id -g)" \
-e HOME=/tmp \
-v "${SCRIPT_DIR}/helioviewer.org:/app/helioviewer.org" \
-w /app/helioviewer.org/resources/build \
ghcr.io/helioviewer-project/node \
ant
}
# Watch and rebuild JavaScript on changes
watch_js() {
echo "Watching JavaScript for changes..."
cd "${SCRIPT_DIR}/helioviewer.org/resources/build" && npx webpack watch
}
# Watch and rebuild 3D JavaScript on changes
watch_3d() {
echo "Watching 3D JavaScript for changes..."
cd "${SCRIPT_DIR}/helioviewer.org/resources/build" && npx webpack --config webpack3d.config.js watch
}
# Run the downloader
downloader() {
echo "Starting Helioviewer downloader..."
# Load environment variables from .env file
load_env
# Run in detached mode and trap SIGINT to send SIGKILL for immediate stop
local container_id
container_id=$(docker run -d \
--init \
--stop-timeout 0 \
--network helioviewer_default \
--user "${UID}:$(id -g)" \
--env-file "${SCRIPT_DIR}/.env" \
--platform=linux/x86_64 \
-e HOME=/tmp \
-e HV_DATA_PATH="${HV_DATA_PATH}" \
-v "${SCRIPT_DIR}/api/install:/app" \
-v "${HOST_JPEG2000_PATH}:/tmp/jp2" \
ghcr.io/helioviewer-project/python \
/app/downloader.py "$@")
# Trap Ctrl+C to kill container immediately
trap "docker kill ${container_id} 2>/dev/null; exit 0" SIGINT SIGTERM
# Follow logs and wait for container to finish
docker logs -f "${container_id}"
docker wait "${container_id}" >/dev/null 2>&1
}
# Download test data
download_test_data() {
echo "Downloading test data..."
# Load environment variables from .env file
load_env
# Create push directory if it doesn't exist
mkdir -p "${HOST_JPEG2000_PATH}/push"
# Download and extract the test data tarball
echo "Downloading test data tarball from https://helioviewer.org/jp2/sample.tar.gz..."
curl -L -o /tmp/sample.tar.gz https://helioviewer.org/jp2/sample.tar.gz
echo "Extracting test data to ${HOST_JPEG2000_PATH}/push/..."
tar -xzf /tmp/sample.tar.gz -C "${HOST_JPEG2000_PATH}/push/"
rm -f /tmp/sample.tar.gz
echo "Processing test data with downloader..."
# Run the downloader to process the extracted files
docker run --rm \
--init \
--stop-timeout 0 \
--network helioviewer_default \
--user "${UID}:$(id -g)" \
--env-file "${SCRIPT_DIR}/.env" \
--platform=linux/x86_64 \
--entrypoint /bin/sh \
-e HOME=/tmp \
-e HV_DATA_PATH="${HV_DATA_PATH}" \
-v "${SCRIPT_DIR}/api/install:/app" \
-v "${HOST_JPEG2000_PATH}:/tmp/jp2" \
ghcr.io/helioviewer-project/python \
-c 'expect -c "
set timeout -1
spawn python3 /app/downloader.py -d local -b local -m localmove -s \"1900-01-01 00:00:00\" -e \"2100-01-01 00:00:00\"
expect \"Sleeping for 30 minutes\"
exit 0
"'
echo "Test data downloaded and processed successfully"
}
# Run pytest tests
pytest() {
echo "Running pytest tests..."
load_env
docker run --rm \
--user "${UID}:$(id -g)" \
--network helioviewer_default \
-e HOME=/tmp \
-e PYTEST_API_HOST=api \
-v "${SCRIPT_DIR}/api:/app" \
-v "${HOST_JPEG2000_PATH}:/tmp/jp2" \
-w /app/install \
ghcr.io/helioviewer-project/python \
-m pytest
}
# Initialize Superset
init_superset() {
echo "Initializing Superset database and default roles/permissions..."
load_env
docker compose exec superset superset db upgrade && \
docker compose exec superset superset init && \
docker compose exec superset superset fab create-admin \
--username "${SUPERSET_ADMIN_USER}" \
--firstname Admin \
--lastname User \
--email admin@localhost \
--password "${SUPERSET_ADMIN_PASS}" && \
docker compose cp superset/dashboards_prepared.zip superset:/tmp/dashboards.zip && \
docker compose exec superset superset import-dashboards -p /tmp/dashboards.zip -u admin
docker compose down superset-guest && docker compose up superset-guest -d
}
# Generate RSA private/public key pair for Superset guest tokens
gen_superset_pk() {
echo "Generating RSA private/public key pair for Superset guest tokens..."
# Load environment variables
load_env
local private_key="${SUPERSET_GUEST_PK}"
local public_key="${SUPERSET_GUEST_PK}.pub"
local config_file="${SUPERSET_CONFIG_FILE}"
# Check if private key already exists
if [ -f "${private_key}" ]; then
echo "Warning: Private key already exists at ${private_key}"
read -p "Overwrite existing key pair? This will invalidate existing tokens. (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Skipping key generation. Existing keys were not modified."
return 0
fi
fi
# Generate RSA private key (4096 bits)
echo "Generating RSA private key (4096 bits)..."
openssl genrsa -out "${private_key}" 4096
if [ $? -ne 0 ]; then
echo "Error: Failed to generate private key"
exit 1
fi
# Extract public key from private key
echo "Extracting public key..."
openssl rsa -in "${private_key}" -pubout -out "${public_key}"
if [ $? -ne 0 ]; then
echo "Error: Failed to extract public key"
exit 1
fi
# Update superset_config.py with the public key
echo "Updating ${config_file} with public key..."
# Use awk to replace the GUEST_TOKEN_JWT_SECRET line with the public key
# Pass public key as ARGV to handle multiline content on macOS
awk 'BEGIN {
pubkey=ARGV[1]
delete ARGV[1]
}
/^# GUEST_TOKEN_JWT_SECRET =/ {
print "GUEST_TOKEN_JWT_SECRET = \"\"\"" pubkey "\"\"\""
next
}
{ print }
' "$(cat "${public_key}")" "${config_file}" > "${config_file}.tmp" && mv "${config_file}.tmp" "${config_file}"
if [ $? -ne 0 ]; then
echo "Error: Failed to update superset_config.py"
exit 1
fi
echo "-------------------------------------------"
echo "RSA key pair generated successfully!"
echo "Private key: ${private_key}"
echo "Public key: ${public_key}"
echo ""
echo "The public key has been added to ${config_file}"
echo "Make sure to restart Superset for the changes to take effect."
echo "-------------------------------------------"
}
# Prepare dashboard export zip file for import
prepare_dashboard() {
local zip_file="$1"
if [ -z "${zip_file}" ]; then
echo "Error: No zip file specified"
echo "Usage: $0 prepare-dashboard <path-to-dashboard.zip>"
exit 1
fi
if [ ! -f "${zip_file}" ]; then
echo "Error: File '${zip_file}' not found"
exit 1
fi
echo "Preparing dashboard export from ${zip_file}..."
load_env
# Create temporary directory
local temp_dir=$(mktemp -d)
echo "Using temporary directory: ${temp_dir}"
# Unzip the file
echo "Extracting dashboard export..."
unzip -q "${zip_file}" -d "${temp_dir}"
# Find and edit the databases YAML file
local db_yaml="${temp_dir}/dashboard_export_"*"/databases/Helioviewer_Daily.yaml"
if [ -f ${db_yaml} ]; then
echo "Updating database configuration in ${db_yaml}..."
sed -i.bak "s|^sqlalchemy_uri:.*|sqlalchemy_uri: mysql://${SUPERSET_READ_USER}:${SUPERSET_READ_PASS}@${HV_DB_HOST}:3306/${HV_DB_NAME}|" ${db_yaml}
rm -f "${db_yaml}.bak"
else
echo "Warning: Database YAML file not found at expected path"
fi
# Find and edit the datasets data.yaml file
local dataset_yaml="${temp_dir}/dashboard_export_"*"/datasets/Helioviewer_Daily/data.yaml"
if [ -f ${dataset_yaml} ]; then
echo "Updating dataset schema in ${dataset_yaml}..."
sed -i.bak "s|^schema: helioviewer|schema: ${HV_DB_NAME}|" ${dataset_yaml}
rm -f "${dataset_yaml}.bak"
else
echo "Warning: Dataset YAML file not found at expected path"
fi
# Create output zip file
local output_file="${SCRIPT_DIR}/superset/dashboards_prepared.zip"
echo "Creating prepared dashboard export at ${output_file}..."
cd "${temp_dir}" && zip -q -r "${output_file}" .
# Clean up temporary directory
echo "Cleaning up temporary directory..."
rm -rf "${temp_dir}"
echo "Dashboard export prepared successfully at ${output_file}"
echo "You can now import this file into Superset"
}
# Main command dispatcher
case "$1" in
init)
init
;;
preup)
preup
;;
postup)
postup
;;
up)
up
;;
composer)
shift
composer "$@"
;;
npm_install)
npm_install
;;
build_js_css)
build_js_css
;;
watch_js)
watch_js
;;
watch_3d)
watch_3d
;;
downloader)
shift
downloader "$@"
;;
download_test_data)
download_test_data
;;
pytest)
shift
pytest "$@"
;;
init_superset)
init_superset
;;
prepare-dashboard)
shift
prepare_dashboard "$@"
;;
gen-superset-pk)
gen_superset_pk
;;
*)
echo "Error: Unknown command '$1'"
echo ""
usage
exit 1
;;
esac