-
Notifications
You must be signed in to change notification settings - Fork 0
/
singularity_local.sh
executable file
·314 lines (273 loc) · 10.7 KB
/
singularity_local.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
306
307
308
309
310
311
312
313
314
#!/bin/bash
# Function to check service readiness
check_service_ready() {
local service_name=$1
local check_command=$2
local counter=0
echo "Waiting for $service_name to start..."
while ! eval $check_command; do
if [ "$counter" -lt "$timeout_seconds" ]; then
sleep 1
((counter++))
echo "Checking $service_name... ($counter seconds)"
else
echo "Timeout reached. $service_name did not start within $timeout_minutes minutes."
exit 1
fi
done
echo "$service_name is up and running."
}
# Function to start each service
start_service() {
case $1 in
postgres)
singularity run --bind $postgres_data:/var/lib/postgresql/data \
--bind $postgres_run:/var/run/postgresql \
--bind $postgres_backup:/backups \
--env-file ./.envs/.local/.postgres $postgres_sif \
&>./postgres_log.txt &
check_service_ready "PostgreSQL" "pg_isready -h localhost -p 5432"
;;
redis)
singularity exec $redis_sif redis-server &> redis_log.txt &
check_service_ready "Redis" "redis-cli ping > /dev/null 2>&1"
;;
django)
singularity exec --bind .:/app \
--env-file ./.envs/.local/.concat_env_files \
$django_sif bash -c 'cd /app && /entrypoint /start' &> django_log.txt &
check_service_ready "Django app" "curl -s http://localhost:8000 > /dev/null"
;;
docs)
singularity exec --bind .:/app \
--env-file ./.envs/.local/.django \
$docs_sif /start-docs &>docs_log.txt &
check_service_ready "Docs" "echo 'Docs is ready'"
;;
celeryworker)
singularity exec --bind .:/app \
--env-file ./.envs/.local/.django \
--env-file ./.envs/.local/.postgres \
--env-file ./.envs/.local/.regulatory_data \
$django_sif /start-celeryworker &> celeryworker_log.txt &
check_service_ready "Celery worker" "echo 'Celery worker is ready'"
;;
celerybeat)
singularity exec --bind .:/app \
--env-file ./.envs/.local/.django \
--env-file ./.envs/.local/.postgres \
--env-file ./.envs/.local/.regulatory_data \
$django_sif /start-celerybeat &> celerybeat_log.txt &
check_service_ready "Celery beat" "echo 'Celery beat is ready'"
;;
celeryflower)
singularity exec --bind .:/app \
--env-file ./.envs/.local/.django \
--env-file ./.envs/.local/.postgres \
--env-file ./.envs/.local/.regulatory_data \
$django_sif /start-flower &> celeryflower_log.txt &
check_service_ready "Celery flower" "echo 'Celery flower is ready'"
;;
*)
echo "Unknown service: $1"
;;
esac
}
# Start specified services
for service in "${services_to_start[@]}"; do
start_service $service
done
show_help() {
cat << EOF
Usage: ${0##*/} [OPTIONS]...
Launch Singularity containers for a Django application with optional services.
-h, --help display this help and exit
-p, --postgres_sif PATH path to the PostgreSQL Singularity image file
-r, --redis_sif PATH path to the Redis Singularity image file
-d, --django_sif PATH path to the Django Singularity image file
-o, --docs_sif PATH path to the Docs Singularity image file
-g, --postgres_data PATH path to the PostgreSQL data directory, eg postgres_data
-b, --postgres_backup PATH path to the PostgreSQL data backups directory, eg postgres_backup
-v, --postgres_run PATH path to the PostgresSQL run directory, eg postgres_run
-t, --timeout MINUTES optional timeout in minutes for service readiness checks (default: 3)
-s, --services "SERVICE1 SERVICE2" optional comma separated, no spaces, list of services to start (e.g., postgres,redis,django)
Examples:
${0##*/} -p /path/to/postgres.sif -r /path/to/redis.sif -d /path/to/django.sif -o /path/to/docs.sif \\
-g /path/to/postgres_data -b /path/to/postgres_backup -v postgres_run /path/to/postgres_run \\
-t 3 -s "postgres redis django"
EOF
}
# Initialize variables
postgres_sif=""
redis_sif=""
django_sif=""
docs_sif=""
postgres_data=postgres_data
postgres_backup=postgres_backup
postgres_run=postgres_run
timeout_minutes=3
services_to_start=()
# Parse options
OPTS=$(getopt -o hp:r:d:o:g:b:v:t:s: --long help,postgres_sif:,redis_sif:,django_sif:,docs_sif:,postgres_data:,postgres_backup:,postgres_run:,timeout:,services: -n 'parse-options' -- "$@")
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
eval set -- "$OPTS"
while true; do
case "$1" in
-h | --help ) show_help; exit 0 ;;
-p | --postgres_sif ) postgres_sif="$2"; shift 2 ;;
-r | --redis_sif ) redis_sif="$2"; shift 2 ;;
-d | --django_sif ) django_sif="$2"; shift 2 ;;
-o | --docs_sif ) docs_sif="$2"; shift 2 ;;
-g | --postgres_data ) postgres_data="$2"; shift 2 ;;
-b | --postgres_backup ) postgres_backup="$2"; shift 2 ;;
-v | --postgres_run ) postgres_run="$2"; shift 2 ;;
-t | --timeout ) timeout_minutes="$2"; shift 2 ;;
-s | --services )
IFS=',' read -r -a services_to_start <<< "$2"
shift 2 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
main() {
# Loop over the array of packages to load them
spack_packages=("postgresql" "singularityce" "redis")
for pkg in "${spack_packages[@]}"; do
if ! eval $(spack load --sh $pkg); then
echo "Error loading $pkg package with spack. Ensure the package exists and is available."
exit 1
fi
echo "$pkg loaded successfully."
done
let "timeout_seconds=timeout_minutes * 60"
# Start specified services
for service in "${services_to_start[@]}"; do
start_service $service
done
}
# Call main function
main "$@"
# let "timeout_seconds=timeout_minutes * 60"
# # Implement the rest of your script here, including loading spack packages,
# # starting the services as specified in `services_to_start`, and checking for their readiness.
# # You'll replace or modify the previous loop and checks according to the new variables used.
# # Define an array of Spack packages to load
# spack_packages=("postgresql" "singularityce" "redis")
# # Loop over the array of packages
# for pkg in "${spack_packages[@]}"; do
# if ! eval $(spack load --sh $pkg); then
# echo "Error loading $pkg package with spack. Ensure the package exists and is available."
# exit 1
# fi
# echo "$pkg loaded successfully."
# done
# # pass in the path to where you want postgres data to be
# # stored on your local machine
# yeastregulatorydb_local_postgres_data=$1
# yeastregulatorydb_local_postgres_data_backups=$2
# # paths to sif files
# postgres_sif=$3
# redis_sif=$4
# django_sif=$5
# docs_sif=$6
# # optional arguments
# TIMEOUT_MINUTES="${7:-3}"
# # Convert minutes to seconds for the timeout
# let "TIMEOUT_SECONDS=TIMEOUT_MINUTES * 60"
# # start the postgres container
# singularity exec $postgres_sif \
# --bind $yeastregulatorydb_local_postgres_data:/var/lib/postgresql/data \
# --bind $yeastregulatorydb_local_postgres_data_backups:/backups \
# --env-file ./.envs/.local/.postgres &
# # Counter to keep track of the time elapsed
# counter=0
# # check that the postgres container is up and running
# # before starting the next container. Timeout and issue exit
# # error if it takes too long
# while ! pg_isready -h localhost -p 5432; do
# if [ "$counter" -lt "$TIMEOUT_SECONDS" ]; then
# echo "Waiting for PostgreSQL to start... $((counter++))s"
# sleep 1
# else
# echo "Timeout reached. PostgreSQL did not start within $TIMEOUT_MINUTES minutes."
# exit 1
# fi
# done
# echo "PostgreSQL is up and running."
# # Start the redis container
# singularity exec $redis_sif redis-server &
# counter=0
# # Check if Redis is ready
# echo "Waiting for Redis to start..."
# while ! redis-cli ping > /dev/null 2>&1; do
# if [ "$counter" -lt "$TIMEOUT_SECONDS" ]; then
# sleep 1
# ((counter++))
# echo "Checking Redis... ($counter seconds)"
# else
# echo "Timeout reached. Redis did not start within $(($TIMEOUT_SECONDS / 60)) minutes."
# exit 1
# fi
# done
# echo "Redis is up and running."
# singularity exec $django_sif \
# --bind .:/app \
# --env-file ./.envs/.local/.django \
# --env-file ./.envs/.local/.postgres \
# --env-file ./.envs/.local/.regulatory_data \
# --port 8000:8000 \
# /start &
# # Check if the Django app is ready
# counter=0
# echo "Waiting for Django app to start..."
# while ! curl -s http://localhost:8000 > /dev/null; do
# if [ "$counter" -lt "$TIMEOUT_SECONDS" ]; then
# sleep 1
# ((counter++))
# echo "Checking Django app... ($counter seconds)"
# else
# echo "Timeout reached. Django did not start within $(($TIMEOUT_SECONDS / 60)) minutes."
# exit 1
# fi
# done
# echo "Django app is up and running."
# # docs:
# # image: yeastregulatorydb_local_docs
# # container_name: yeastregulatorydb_local_docs
# # build:
# # context: .
# # dockerfile: ./compose/local/docs/Dockerfile
# # env_file:
# # - ./.envs/.local/.django
# # volumes:
# # - ./docs:/docs:z
# # - ./config:/app/config:z
# # - ./yeastregulatorydb:/app/yeastregulatorydb:z
# # ports:
# # - '9000:9000'
# # command: /start-docs
# # celeryworker:
# # <<: *django
# # image: yeastregulatorydb_local_celeryworker
# # container_name: yeastregulatorydb_local_celeryworker
# # depends_on:
# # - redis
# # - postgres
# # ports: []
# # command: /start-celeryworker
# # celerybeat:
# # <<: *django
# # image: yeastregulatorydb_local_celerybeat
# # container_name: yeastregulatorydb_local_celerybeat
# # depends_on:
# # - redis
# # - postgres
# # ports: []
# # command: /start-celerybeat
# # flower:
# <<: *django
# image: yeastregulatorydb_local_flower
# container_name: yeastregulatorydb_local_flower
# ports:
# - '5555:5555'
# command: /start-flower