forked from int-brain-lab/IBL-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ingest-entrypoint.sh
executable file
·242 lines (185 loc) · 4.59 KB
/
ingest-entrypoint.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
#! /bin/sh
# ingest entrypoint script
# ========================
# globals
# -------
dbdump="/tmp/dump.sql.gz"
dbcreated="/src/alyx/alyx/db_created"
dbloaded="/src/alyx/alyx/db_loaded"
sucreated="/src/alyx/alyx/superuser_created"
err_exit() { echo "error: $*"; exit 1; }
# checks
# ------
[ -z "$PGUSER" ] && err_exit "PGUSER unset";
[ -z "$PGHOST" ] && err_exit "PGPASSWORD unset";
[ -z "$PGPASSWORD" ] && err_exit "PGPASSWORD unset";
# fetchdump
# ---------
fetchdump() {
echo "=> fetching dbdump for ${ALYX_DL_DATE}";
mv -f ${dbdump} ${dbdump}.prev >/dev/null 2>&1;
wget -q -O ${dbdump} \
--user "$ALYX_DL_USER" \
--password "$ALYX_DL_PASSWORD" \
http://ibl.flatironinstitute.org/json/${ALYX_DL_DATE}_alyxfull.sql.gz
}
# mkdb
# ----
mkdb() {
if [ -f "${dbcreated}" ]; then
echo '# => using existing database';
else
echo '# ==> creating database';
[ ! -f "${dbdump}" ] \
&& err_exit ".. no database dump in $dbdump";
createdb alyx_old;
createdb alyx;
touch ${dbcreated};
fi
}
# loaddb
# ------
loaddb() {
if [ -f "${dbloaded}" ]; then
echo '# => database loaded - skipping load.';
else
echo "# => loading database ${dbdump}"
gzip -dc ${dbdump} |psql -d alyx;
touch ${dbloaded};
fi
}
# configure alyx/django
# ---------------------
alyxcfg() {
echo '# => configuring alyx'
if [ ! -f "$sucreated" ]; then
echo '# ==> configuring settings_secret.py'
# custom settings_secret for multiple DBs
# see also :alyx/alyx/alyx/settings_secret_template.py
cnf="/src/alyx/alyx/alyx/settings_secret.py";
sed \
-e "s/%SECRET_KEY%/0xdeadbeef/" \
-e "s/%DBNAME%/alyx/" \
-e "s/%DBUSER%/$PGUSER/" \
-e "s/%DBPASSWORD%/$PGPASSWORD/" \
-e "s/127.0.0.1/$PGHOST/" \
> $cnf <<-EOF
SECRET_KEY = '%SECRET_KEY%'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '%DBNAME%',
'USER': '%DBUSER%',
'PASSWORD': '%DBPASSWORD%',
'HOST': '127.0.0.1',
'PORT': '5432',
},
'old': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '%DBNAME%_old',
'USER': '%DBUSER%',
'PASSWORD': '%DBPASSWORD%',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
EMAIL_HOST = 'mail.superserver.net'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'UnbreakablePassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EOF
echo '# ==> creating alyx superuser'
/src/alyx/alyx/manage.py createsuperuser \
--no-input \
--username admin \
--email admin@localhost
echo '# ==> setting alyx superuser password'
# note on superuser create:
#
# - no-input 'createsuperuser' creates without password
# - cant set password from cli here or in setpassword command
# - so script reset via manage.py shell
# - see also:
# https://stackoverflow.com/questions/6358030/\
# how-to-reset-django-admin-password
/src/alyx/alyx/manage.py shell <<-EOF
from django.contrib.auth import get_user_model
User = get_user_model()
admin = User.objects.get(username='admin')
admin.set_password('$PGPASSWORD')
admin.save()
exit()
EOF
touch ${sucreated};
fi
}
# alyxprep
# --------
alyxprep() {
echo "# => alyxprep"
/src/alyx/alyx/manage.py makemigrations;
/src/alyx/alyx/manage.py migrate;
}
# alyxstart
# ---------
alyxstart() {
echo '# => starting alyx'
/src/alyx/alyx/manage.py runserver --insecure 0.0.0.0:8888;
}
# renamedb
# --------
renamedb() {
echo "# => renaming databases:";
echo "# ==> ... dropping alyx_old";
dropdb alyx_old || err_exit "couldn't drop alyx_old";
echo "# ==> ... renaming alyx to alyx_old";
psql -c 'alter database alyx rename to alyx_old;' \
> /dev/null \
|| err_exit "couldn't rename alyx to alyx_old";
echo "# ==> ... creating new alyx";
createdb alyx || err_exit "couldn't rename alyx to alyx_old";
rm -f ${dbloaded};
rm -f ${sucreated};
echo "# => ok.";
}
# init
# ----
# perform all initialization steps
init() {
fetchdump;
mkdb;
loaddb;
alyxcfg;
alyxprep;
}
# www
# ---
# initialize environment and run alyx web
www() {
init;
alyxstart;
}
# dev
# ---
# initialize environment and wait indefinitely
dev() {
init;
exec tail -f /dev/null;
}
# _start:
case "$1" in
"fetchdump") fetchdump;;
"mkdb") mkdb;;
"loaddb") loaddb;;
"alyxcfg") alyxcfg;;
"alyxprep") alyxprep;;
"alyxstart") alyxstart;;
"renamedb") renamedb;;
"www") www;;
"dev") dev;;
"sh") exec /bin/sh -c "$*";;
"help") \
echo "usage: `basename $0` [fetchdump|mkdb|loaddb|alyxcfg|alyxprep|alyxstart|renamedb|www|dev|sh]";;
*) ;; # ... sourceable
esac