-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpsij-ci-setup
executable file
·376 lines (311 loc) · 10.8 KB
/
psij-ci-setup
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
#!/bin/bash
if pip --version 2>&1 | egrep -q 'python 3\..*' >/dev/null 2>&1 ; then
PIP="pip"
else
PIP="pip3"
fi
existing_error_trailer() {
echo "If you are certain that you want to install multiple entries, "
echo "you can re-run this script with the \"-f\" flag. "
echo "================================================================"
}
cron_check() {
ps -eo command | awk '{print $1}' | grep cron >/dev/null 2>&1
S1=$?
crontab -l 2>&1 | grep "not allowed" >/dev/null 2>&1
S2=$?
echo "$(($S1 || ! $S2))"
}
crontab_list() {
# wraps crontab -l such that an empty string is returned for a missing
# crontab rather than an error
if crontab -l 2>&1 | grep "no crontab for" >/dev/null 2>&1 ; then
echo ""
else
crontab -l
fi
}
cron_check_existing() {
crontab_list | grep "psij-ci-run" >/dev/null 2>&1
}
cron_existing_error() {
EXISTING=`crontab_list | grep "psij-ci-run"`
echo
echo "================================================================"
echo "Error: a crontab for PSI/J tests already exists: "
echo ">>> $EXISTING"
echo
echo "You can edit your crontab with \"crontab -e\" and remove the "
echo "existing entry, then re-run this tool. "
existing_error_trailer
exit 2
}
cron_install() {
CMD="$CMD >> testing.log 2>&1"
LINE="$MINUTE $HOUR * * * cd \"$MYPATH\" && $CMD"
echo
echo "================================================================"
echo "The following line will be installed in your crontab:"
echo "$LINE"
echo "================================================================"
CRT_CRONTAB=`crontab -l 2>&1`
if [ "$?" != "0" ]; then
if echo "$CRT_CRONTAB" | grep "no crontab for" >/dev/null 2>&1 ; then
CRT_CRONTAB=""
else
echo "Error updating crontab: $CRT_CRONTAB"
exit 2
fi
fi
{ echo "$CRT_CRONTAB"; echo "$LINE"; } | crontab -
declare -x | egrep -v "^declare -x (BASH_VERSINFO|DISPLAY|EUID|GROUPS|SHELLOPTS|TERM|UID|_)=" >psij-ci-env
}
at_check() {
FLAG_FILE=`mktemp`
echo "rm $FLAG_FILE" | at now >/dev/null 2>&1
if [ -f "$FLAG_FILE" ]; then
echo 0
rm $FLAG_FILE
else
echo 1
fi
}
at_check_existing() {
for JOB in `atq`; do
JOB_NO=`echo $JOB | awk '{print $1}'`
if at -c $JOB_NO | grep psij-ci-run >/dev/null 2>&1; then
EXISTING_AT_JOB_NUM=$JOB_NO
EXISTING_AT_JOB="$JOB `at -c $JOB_NO | grep psij-ci-run`"
return 0
fi
done
return 1
}
at_existing_error() {
echo
echo "================================================================"
echo "Error: an AT job for PSI/J tests already exists: "
echo ">>> $EXISTING_AT_JOB"
echo
echo "You can remove this job by running "
echo "> atrm $EXISTING_AT_JOB_NUM"
echo "then re-run this tool. "
existing_error_trailer
exit 2
}
at_install() {
CMD="$CMD --reschedule $HOUR:$MINUTE >> testing.log 2>&1"
echo
echo "================================================================"
echo "The following will be executed:"
echo "> echo \"$CMD\" | at $HOUR:$MINUTE"
echo "================================================================"
echo "$CMD" | at $HOUR:$MINUTE >/dev/null 2>&1
}
screen_check() {
which screen >/dev/null 2>&1
echo $?
}
screen_check_existing() {
EXISTING_SCREEN_JOB=`screen -list | grep psij-ci-run`
}
screen_existing_error() {
echo
echo "================================================================"
echo "Error: a Screen job for PSI/J tests already exists: "
echo ">>> $EXISTING_SCREEN_JOB"
echo
echo "You can remove this job by running attaching to the screen "
echo "session and exiting it with CTRL+C and then re-run this tool. "
exitsing_error_trailer
exit 2
}
screen_install() {
CMD="$CMD --repeat >> testing.log 2>&1"
echo
echo "================================================================"
echo "WARNING: Screen sessions do not persist across reboots. Please "
echo "check regularly and re-run this script if necessary. "
echo " "
echo "The following will be executed:"
echo "> screen -d -m bash -c \"$CMD\""
echo "================================================================"
screen -d -m bash -c "$CMD"
}
manual_check() {
echo 0
}
manual_check_existing() {
false
}
manual_existing_error() {
true
}
manual_install() {
CMD="$CMD --repeat >> testing.log 2>&1"
echo
echo "================================================================"
echo "Please run the following command in the background: "
echo "> $CMD"
echo "================================================================"
}
completed_msg() {
echo
echo "================================================================"
echo "Setup complete. If you have not already done so, please take "
echo "some time to customize testing.conf. "
echo "================================================================"
}
continue_or_exit() {
RESPONSE=""
while [ "$RESPONSE" != "C" ] && [ "$RESPONSE" != "X" ]; do
echo -n "Would you like to (C)ontinue or E(x)it? "
read -n1 RESPONSE
echo
RESPONSE=${RESPONSE^}
if [ "$RESPONSE" == "X" ]; then
echo "Operation canceled"
exit 1
fi
done
}
check_email() {
if ! cat testing.conf | egrep '^maintainer_email\s*=.*@.*$' >/dev/null 2>&1 ; then
echo
echo "================================================================"
echo "Warning: there appears to be no maintainer email specified. "
echo "A maintainer email can help the PSI/J team contact you when "
echo "problems arise with the test suite. It is highly recommended "
echo "that you provide one. "
echo "If you would like to do that, please exit this script and add "
echo "your email to the relevant line in testing.conf. "
echo "================================================================"
echo
continue_or_exit
fi
}
check_key() {
if [ ! -f ~/.psij/key ]; then
BASE_URL=`egrep '^[^#]*server_url.*' ./testing.conf |awk '{split($0, a, "="); print a[2]}'|xargs`
AUTH_URL="${BASE_URL}/auth.html"
echo
echo "================================================================"
echo "There is no upload key (~/.psij/key). An upload key is needed "
echo "to upload results to the results site. To obtain a key, please "
echo "go to ${AUTH_URL}/auth.html in a browser and follow "
echo "the instructions on that page. "
echo "================================================================"
echo
RESPONSE=""
DONE="0"
while [ "$DONE" == "0" ]; do
echo "1. (C)ontinue without a key "
echo "2. E(x)it? "
echo "3. (E)nter a key here to be placed in ~/.psij/key "
if [ "$XDG_CURRENT_DESKTOP" != "" ]; then
echo "4. (O)pen $AUTH_URL in a browser "
fi
read -n1 RESPONSE
echo
RESPONSE=${RESPONSE^}
if [ "$RESPONSE" == "C" ] || [ "$RESPONSE" == "1" ]; then
DONE=1
elif [ "$RESPONSE" == "X" ] || [ "$RESPONSE" == "2" ]; then
echo "Operation canceled"
exit 1
elif [ "$RESPONSE" == "E" ] || [ "$RESPONSE" == "3" ]; then
RESPONSE=""
while [ "$RESPONSE" != "Y" ]; do
echo -n "Enter key: "
read KEY
echo "You entered '$KEY'."
echo "Is this correct (Y/N)?"
read -n1 RESPONSE
RESPONSE=${RESPONSE^}
done
mkdir -p ~/.psij
echo "$KEY" > ~/.psij/key
chmod 600 ~/.psij/key
DONE=1
elif [ "$RESPONSE" == "O" ] || [ "$RESPONSE" == "4" ]; then
xdg-open "$AUTH_URL"
fi
done
fi
}
FORCE=0
if [ "$1" == "-f" ]; then
FORCE=1
shift
fi
MYPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
echo
echo "================================================================"
echo "This script will install requirements for the PSI/J CI tests and"
echo "add a cron job to run the tests once a day at some random time. "
echo " "
echo "If you need to set up a special environment, such as loading an "
echo "environment module please do so and then re-run this script. "
echo "================================================================"
echo
continue_or_exit
check_email
check_key
cd "$MYPATH"
echo -n "Installing dependencies..."
OUT=`$PIP install --target .packages --upgrade -r requirements-tests.txt 2>&1`
if [ "$?" != "0" ]; then
echo "FAILED"
echo $OUT
exit 2
else
echo "Done"
fi
export PYTHONPATH=`pwd`/.packages:$PYTHONPATH
HOUR=`echo $(($RANDOM % 24))`
MINUTE=`echo $(($RANDOM % 60))`
MYPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
CMD="./psij-ci-run"
METHODS=(cron at screen manual)
METHOD_NAMES=(Cron AT Screen manual)
echo
echo "================================================================"
echo "Please choose a method to run the tests: "
SEQ=1
FIRST_AVAILABLE=1
DEFAULT_SEQ=1
for METHOD in "${METHODS[@]}"; do
AVAILABLE=`${METHOD}_check`
AVTEXT=""
RECTEXT=""
if [ "$AVAILABLE" != "0" ]; then
AVTEXT="(not detected)"
else
if [ "$FIRST_AVAILABLE" == "1" ]; then
FIRST_AVAILABLE=0
RECTEXT="(recommended)"
DEFAULT_SEQ=$SEQ
fi
fi
echo "$SEQ) ${METHOD_NAMES[$SEQ - 1]} $AVTEXT $RECTEXT"
SEQ=$((SEQ + 1))
done
echo "================================================================"
while echo "1234X" | grep -v "$RESPONSE" >/dev/null 2>&1 ; do
echo -n "Method 1-4 ($DEFAULT_SEQ) or E(x)it? "
read -n1 RESPONSE
RESPONSE=${RESPONSE^}
if [ "$RESPONSE" == "" ]; then
RESPONSE=$DEFAULT_SEQ
fi
if [ "$RESPONSE" == "X" ]; then
echo "Operation canceled"
exit 1
fi
done
METHOD="${METHODS[$RESPONSE - 1]}"
if [ "$FORCE" != "1" ] && ${METHOD}_check_existing ; then
${METHOD}_existing_error
else
${METHOD}_install
fi