-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworsica_bash_common.sh
580 lines (561 loc) · 26.8 KB
/
worsica_bash_common.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
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
CREDENTIALS=""
function get_nextcloud_credentials_from_file {
CREDENTIALS_FILE="/usr/local/worsica_web_products/nc-credentials"
if [[ -f ${CREDENTIALS_FILE} ]] ; then
CREDENTIALS=$(cat ${CREDENTIALS_FILE})
echo "[worsica_bash_common.sh] [get_nextcloud_credentials_from_file] [SUCCESS] Found nextcloud credentials (${CREDENTIALS_FILE})"
else
echo "[worsica_bash_common.sh] [get_nextcloud_credentials_from_file] [FAILURE] Error, no credentials found, abort. (${CREDENTIALS_FILE})"
exit 1
fi
}
#affect the ROI_SIMULATION_FOLDER and PATH_TO_PRODUCTS
WORSICA_FOLDER_PATH=$(pwd)
ROI_SIMULATION_FOLDER=""
PATH_TO_PRODUCTS=""
function set_path_to_products {
#$1 SERVICE
#$2 USER_ID
#$3 ROI_ID
#$4 SIMULATION_ID
ROI_SIMULATION_FOLDER="$1/user$2/roi$3/simulation$4"
#echo "[worsica_bash_common.sh] [set_path_to_products] Register ROI_SIMULATION_FOLDER=${ROI_SIMULATION_FOLDER}"
PATH_TO_PRODUCTS="${WORSICA_FOLDER_PATH}/${ROI_SIMULATION_FOLDER}"
#echo "[worsica_bash_common.sh] [set_path_to_products] Register PATH_TO_PRODUCTS=${PATH_TO_PRODUCTS}"
}
#get path, when calling this function from an external script bash
function get_path_to_products {
#$1 SERVICE
#$2 USER_ID
#$3 ROI_ID
#$4 SIMULATION_ID
set_path_to_products $1 $2 $3 $4
echo PATH_TO_PRODUCTS
}
function get_roi_simulation_folder {
#$1 SERVICE
#$2 USER_ID
#$3 ROI_ID
#$4 SIMULATION_ID
set_path_to_products $1 $2 $3 $4
echo ROI_SIMULATION_FOLDER
}
function set_path_to_products_topo {
#$1 SERVICE
#$2 USER_ID
#$3 ROI_ID
#$4 SIMULATION_ID
#$5 GT_ID
ROI_SIMULATION_FOLDER="$1/user$2/roi$3/simulation$4/gt$5"
#echo "[worsica_bash_common.sh] [set_path_to_products] Register ROI_SIMULATION_FOLDER=${ROI_SIMULATION_FOLDER}"
PATH_TO_PRODUCTS="${WORSICA_FOLDER_PATH}/${ROI_SIMULATION_FOLDER}"
#echo "[worsica_bash_common.sh] [set_path_to_products] Register PATH_TO_PRODUCTS=${PATH_TO_PRODUCTS}"
}
#get path, when calling this function from an external script bash
function get_path_to_products_topo {
#$1 SERVICE
#$2 USER_ID
#$3 ROI_ID
#$4 SIMULATION_ID
#$5 GT_ID
set_path_to_products_topo $1 $2 $3 $4 $5
echo PATH_TO_PRODUCTS
}
#affect the NEXTCLOUD_PATH_TO_PRODUCTS
NEXTCLOUD_WEBDAV_PATH="remote.php/dav/files/worsicaAdmin"
NEXTCLOUD_REMOTE_PATH="http://nextcloud/${NEXTCLOUD_WEBDAV_PATH}"
NEXTCLOUD_PATH_TO_REPOSITORY="${NEXTCLOUD_REMOTE_PATH}/repository_images"
NEXTCLOUD_PATH_TO_PRODUCTS=""
function set_nextcloud_path_to_products {
#$1 SERVICE
#$2 USER_ID
#$3 ROI_ID
#$4 SIMULATION_ID
set_path_to_products $1 $2 $3 $4
NEXTCLOUD_PATH_TO_PRODUCTS="${NEXTCLOUD_REMOTE_PATH}/${ROI_SIMULATION_FOLDER}"
#echo "[worsica_bash_common.sh] [set_nextcloud_path_to_products] Register NEXTCLOUD_PATH_TO_PRODUCTS=${NEXTCLOUD_PATH_TO_PRODUCTS}"
}
#get nextcloud path, when calling this function from an external script bash
function get_nextcloud_path_to_products {
#$1 SERVICE
#$2 USER_ID
#$3 ROI_ID
#$4 SIMULATION_ID
set_nextcloud_path_to_products $1 $2 $3 $4
echo PATH_TO_PRODUCTS
}
function set_nextcloud_path_to_products_topo {
#$1 SERVICE
#$2 USER_ID
#$3 ROI_ID
#$4 SIMULATION_ID
#$5 GT_ID
set_path_to_products_topo $1 $2 $3 $4 $5
NEXTCLOUD_PATH_TO_PRODUCTS="${NEXTCLOUD_REMOTE_PATH}/${ROI_SIMULATION_FOLDER}"
#echo "[worsica_bash_common.sh] [set_nextcloud_path_to_products] Register NEXTCLOUD_PATH_TO_PRODUCTS=${NEXTCLOUD_PATH_TO_PRODUCTS}"
}
#get nextcloud path, when calling this function from an external script bash
function get_nextcloud_path_to_products_topo {
#$1 SERVICE
#$2 USER_ID
#$3 ROI_ID
#$4 SIMULATION_ID
#$5 GT_ID
set_nextcloud_path_to_products_topo $1 $2 $3 $4 $5
echo PATH_TO_PRODUCTS
}
function create_folder {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${SIMULATION_STATE}
get_nextcloud_credentials_from_file
set_nextcloud_path_to_products $1 $2 $3 $4
echo "[worsica_bash_common.sh] [create_folder] $1-user$2-roi$3-s$4 state: $5"
echo "============================START============================"
echo "[worsica_bash_common.sh] [create_folder] Check if folder exists"
if cd "${PATH_TO_PRODUCTS}" ; then
echo "[worsica_bash_common.sh] [create_folder] Found folder, no need to create"
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Folder does not exist"
echo "[worsica_bash_common.sh] [create_folder] Create folder ${PATH_TO_PRODUCTS}"
if mkdir -p "${PATH_TO_PRODUCTS}" ; then
echo "[worsica_bash_common.sh] [create_folder] [SUCCESS] Created folder, change directory"
cd "${PATH_TO_PRODUCTS}"
echo $(pwd)
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create folder"
exit 1
fi
fi
echo "[worsica_bash_common.sh] [create_folder] Check if nextcloud folder exists"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X PROPFIND ${NEXTCLOUD_PATH_TO_PRODUCTS} -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
if [[ $CURL_STATUS -eq 200 ]] || [[ $CURL_STATUS -eq 207 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] [SUCCESS] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS} folder exists"
else
#create folders inside nextcloud
#create the workspace folder, then the simulation folder
echo "[worsica_bash_common.sh] [create_folder] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS} folder does not exist"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
while [[ $CURL_STATUS -eq 423 ]] ; do #check for locks
echo "[worsica_bash_common.sh] [create_folder] Locked, try again"
sleep 5
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
done
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 405 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS}/$1 folder does not exist"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
while [[ $CURL_STATUS -eq 423 ]] ; do #check for locks
echo "[worsica_bash_common.sh] [create_folder] Locked, try again"
sleep 5
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
done
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 405 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS}/$1/user$2 folder does not exist"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
while [[ $CURL_STATUS -eq 423 ]] ; do #check for locks
echo "[worsica_bash_common.sh] [create_folder] Locked, try again"
sleep 5
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
done
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 405 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS}/$1/user$2/roi$3 folder does not exist"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3/simulation$4" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
while [[ $CURL_STATUS -eq 423 ]] ; do #check for locks
echo "[worsica_bash_common.sh] [create_folder] Locked, try again"
sleep 5
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3/simulation$4" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
done
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 405 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] [SUCCESS] Created nextcloud folder ${NEXTCLOUD_PATH_TO_PRODUCTS}, change directory"
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create nextcloud folder /simulation$4 in ${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3"
exit 1
fi
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create nextcloud folder /roi$3 in ${NEXTCLOUD_REMOTE_PATH}/$1/user$2"
exit 1
fi
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create nextcloud folder /user$2 in ${NEXTCLOUD_REMOTE_PATH}/$1"
exit 1
fi
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create nextcloud folder /$1 in ${NEXTCLOUD_REMOTE_PATH}"
exit 1
fi
fi
}
function create_folder_topo {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${GENERATE_TOPO_ID}
#$6 is ${SIMULATION_STATE}
get_nextcloud_credentials_from_file
set_nextcloud_path_to_products_topo $1 $2 $3 $4 $5
echo "[worsica_bash_common.sh] [create_folder] $1-user$2-roi$3-s$4-gt$5 state: $6"
echo "============================START============================"
echo "[worsica_bash_common.sh] [create_folder] Check if folder exists"
if cd "${PATH_TO_PRODUCTS}" ; then
echo "[worsica_bash_common.sh] [create_folder] Found folder, no need to create"
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Folder does not exist"
echo "[worsica_bash_common.sh] [create_folder] Create folder ${PATH_TO_PRODUCTS}"
if mkdir -p "${PATH_TO_PRODUCTS}" ; then
echo "[worsica_bash_common.sh] [create_folder] [SUCCESS] Created folder, change directory"
cd "${PATH_TO_PRODUCTS}"
echo $(pwd)
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create folder"
exit 1
fi
fi
echo "[worsica_bash_common.sh] [create_folder] Check if nextcloud folder exists"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X PROPFIND ${NEXTCLOUD_PATH_TO_PRODUCTS} -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
if [[ $CURL_STATUS -eq 200 ]] || [[ $CURL_STATUS -eq 207 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] [SUCCESS] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS} folder exists"
else
#create folders inside nextcloud
#create the workspace folder, then the simulation folder
echo "[worsica_bash_common.sh] [create_folder] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS} folder does not exist"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
while [[ $CURL_STATUS -eq 423 ]] ; do #check for locks
echo "[worsica_bash_common.sh] [create_folder] Locked, try again"
sleep 5
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
done
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 405 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS}/$1 folder does not exist"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
while [[ $CURL_STATUS -eq 423 ]] ; do #check for locks
echo "[worsica_bash_common.sh] [create_folder] Locked, try again"
sleep 5
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
done
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 405 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS}/$1/user$2 folder does not exist"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
while [[ $CURL_STATUS -eq 423 ]] ; do #check for locks
echo "[worsica_bash_common.sh] [create_folder] Locked, try again"
sleep 5
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
done
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 405 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS}/$1/user$2/roi$3 folder does not exist"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3/simulation$4" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
while [[ $CURL_STATUS -eq 423 ]] ; do #check for locks
echo "[worsica_bash_common.sh] [create_folder] Locked, try again"
sleep 5
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3/simulation$4" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
done
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 405 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] Nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS}/$1/user$2/roi$3/simulation$4 folder does not exist"
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3/simulation$4/gt$5" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
while [[ $CURL_STATUS -eq 423 ]] ; do #check for locks
echo "[worsica_bash_common.sh] [create_folder] Locked, try again"
sleep 5
CURL_STATUS=$(curl -u ${CREDENTIALS} -X MKCOL "${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3/simulation$4/gt$5" -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
done
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 405 ]] ; then
echo "[worsica_bash_common.sh] [create_folder] [SUCCESS] Created nextcloud folder ${NEXTCLOUD_PATH_TO_PRODUCTS}, change directory"
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create nextcloud folder /gt$5 in ${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3/simulation$4"
exit 1
fi
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create nextcloud folder /simulation$4 in ${NEXTCLOUD_REMOTE_PATH}/$1/user$2/roi$3"
exit 1
fi
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create nextcloud folder /roi$3 in ${NEXTCLOUD_REMOTE_PATH}/$1/user$2"
exit 1
fi
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create nextcloud folder /user$2 in ${NEXTCLOUD_REMOTE_PATH}/$1"
exit 1
fi
else
echo "[worsica_bash_common.sh] [create_folder] [FAILURE] Unable to create nextcloud folder /$1 in ${NEXTCLOUD_REMOTE_PATH}"
exit 1
fi
fi
}
function upload_file {
#$1 source
#$2 destination (nextcloud)
#upload_file ${IMAGESET_NAME}.zip '${NEXTCLOUD_PATH_TO_REPOSITORY}/${IMAGESET_NAME}.zip'
#states: 201 (CREATED), 204
#(STATUS=$(curl -u ${CREDENTIALS} -T w1a.log 'http://nextcloud/remote.php/dav/files/worsicaAdmin/w1a.log' -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)'); echo $STATUS; if [ $STATUS -eq 201 ]; then echo 'OK'; else echo 'Fail'; fi)
get_nextcloud_credentials_from_file
echo "[worsica_bash_common.sh] [upload_file] Uploading $1 to $2";
CURL_STATUS=$(curl --max-time 60 -u ${CREDENTIALS} -T $1 $2 -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
echo $CURL_STATUS
if [[ $CURL_STATUS -eq 201 ]] || [[ $CURL_STATUS -eq 204 ]]; then
echo "[worsica_bash_common.sh] [upload_file] [SUCCESS] Uploaded to nextcloud $1 to $2"
else
echo "[worsica_bash_common.sh] [upload_file] [FAILURE] Fail upload to nextcloud $1 to $2"
exit 1
fi
}
function download_file {
#$1 source (nextcloud)
#$2 destination
#download_file ${f} ${PATH_TO_PRODUCTS}/${f2}
#states: 200 (OK), 404
#(STATUS=$(curl -u ${CREDENTIALS} -kL 'http://nextcloud/remote.php/dav/files/worsicaAdmin/w2.log' -w '<http_code>%{http_code}</http_code>' -o w2.log | grep -oP '(?<=<http_code>).*?(?=</http_code>)'); echo $STATUS; if [ $STATUS -eq 200 ]; then echo 'OK'; else echo 'Fail'; fi)
get_nextcloud_credentials_from_file
echo "[worsica_bash_common.sh] [download_file] Downloading $1 to $2";
CURL_STATUS=$(curl --max-time 60 -u ${CREDENTIALS} -kL $1 -o $2 -w '<http_code>%{http_code}</http_code>' | grep -oP '(?<=<http_code>).*?(?=</http_code>)')
if [[ $CURL_STATUS -eq 200 ]] ; then
echo "[worsica_bash_common.sh] [download_file] [SUCCESS] Downloaded $1 to $2";
else
echo "[worsica_bash_common.sh] [download_file] [FAILURE] Failed downloading $1 to $2. File does not exist or a problem occured with the download.";
rm -rf $2 #remove the file from host, it will output trash
exit 1
fi
}
#$1 is full nextcloud path ${NEXTCLOUD_PATH_TO_PRODUCTS}
function list_files_nextcloud {
get_nextcloud_credentials_from_file
LF=$(curl -u ${CREDENTIALS} -X PROPFIND $1)
echo $LF | grep -oP '(?<=<d:href>).*?(?=</d:href>)'
}
#load the processing state from nextcloud
function pull_processing {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${IMAGESET_NAME}
TAR_FILE_PREFIX=$1-user$2-roi$3-s$4-$5
set_nextcloud_path_to_products $1 $2 $3 $4
#f="${TAR_FILE_PREFIX}-final-products.zip"
#echo "[worsica_bash_common.sh] [pull_processing] $f found, downloading tar ..."
#download_file ${NEXTCLOUD_PATH_TO_PRODUCTS}/$f ${WORSICA_FOLDER_PATH}/$f
#cd ${WORSICA_FOLDER_PATH}
##tar -xzf ${WORSICA_FOLDER_PATH}/$f2
#unzip ${WORSICA_FOLDER_PATH}/$f
#rm -rf $f
echo "[worsica_bash_common.sh] [pull_processing] Check for final products ${TAR_FILE_PREFIX}"
LIST_FILES=$(list_files_nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS})
#echo ${LIST_FILES}
#for f in ${LIST_FILES}; do
# f2=${f#/${NEXTCLOUD_WEBDAV_PATH}/${ROI_SIMULATION_FOLDER}/}
# echo $f2
f2="${TAR_FILE_PREFIX}-final-products.zip"
if [[ ${LIST_FILES} == *${f2}* ]]; then
echo "[worsica_bash_common.sh] [pull_processing] $f2 found, downloading tar ..."
download_file ${NEXTCLOUD_PATH_TO_PRODUCTS}/$f2 ${WORSICA_FOLDER_PATH}/$f2
cd ${WORSICA_FOLDER_PATH}
unzip -o ${WORSICA_FOLDER_PATH}/$f2
rm -rf $f2
else
echo "[worsica_bash_common.sh] [pull_processing] $f2 not found, skip ..."
fi
#done
}
function pull_processing_binary_topo {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${IMAGESET_NAME}
TAR_FILE_PREFIX=$1-user$2-roi$3-s$4-$5
set_nextcloud_path_to_products $1 $2 $3 $4
#f="${TAR_FILE_PREFIX}-final-products.zip"
#echo "[worsica_bash_common.sh] [pull_processing] $f found, downloading tar ..."
#download_file ${NEXTCLOUD_PATH_TO_PRODUCTS}/$f ${WORSICA_FOLDER_PATH}/$f
#cd ${WORSICA_FOLDER_PATH}
##tar -xzf ${WORSICA_FOLDER_PATH}/$f2
#unzip ${WORSICA_FOLDER_PATH}/$f
#rm -rf $f
echo "[worsica_bash_common.sh] [pull_processing] Check for final products ${TAR_FILE_PREFIX}"
LIST_FILES=$(list_files_nextcloud ${NEXTCLOUD_PATH_TO_PRODUCTS})
#echo ${LIST_FILES}
#for f in ${LIST_FILES}; do
# f2=${f#/${NEXTCLOUD_WEBDAV_PATH}/${ROI_SIMULATION_FOLDER}/}
# echo $f2
f2="${TAR_FILE_PREFIX}-final-products.zip"
if [[ ${LIST_FILES} == *${f2}* ]]; then
echo "[worsica_bash_common.sh] [pull_processing] $f2 found, downloading tar ..."
download_file ${NEXTCLOUD_PATH_TO_PRODUCTS}/$f2 ${WORSICA_FOLDER_PATH}/$f2
cd ${WORSICA_FOLDER_PATH}
#aweish_cleanup
if [[ ${f2} == *'_cleanup'* ]]; then
unzip -o ${WORSICA_FOLDER_PATH}/$f2 "$1/user$2/roi$3/simulation$4/$5/$5_binary.tif"
else
unzip -o ${WORSICA_FOLDER_PATH}/$f2 "$1/user$2/roi$3/simulation$4/$5/$5_*_binary.tif"
fi
rm -rf $f2
else
echo "[worsica_bash_common.sh] [pull_processing] $f2 not found, skip ..."
fi
#done
}
#load the processing state from nextcloud
function remove_multiple_processing {
#$1 is ${IMAGESETS_NAME} string by comma joined
#$2 is ${PATH_TO_PRODUCTS}
echo "[worsica_bash_common.sh] [remove_multiple_processing] Remove multiple imagesets $1"
#IFS=','
for i in $(echo $1 | sed "s/,/ /g") ; do #`echo "$1"`; do
echo "[worsica_bash_common.sh] [remove_multiple_processing] Remove $2/$i"
rm -rf $2/$i
done
}
function pull_multiple_processing {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${IMAGESETS_NAME} string by comma joined
echo "[worsica_bash_common.sh] [pull_multiple_processing] Pull multiple imagesets $5"
#IFS=','
for i in $(echo $5 | sed "s/,/ /g") ; do # `echo "$5"`; do
echo "[worsica_bash_common.sh] [pull_multiple_processing] Pull $i"
pull_processing $1 $2 $3 $4 $i
done
}
function pull_multiple_processing_binary_topo {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${IMAGESETS_NAME} string by comma joined
echo "[worsica_bash_common.sh] [pull_multiple_processing] Pull multiple imagesets $5"
#IFS=','
for i in $(echo $5 | sed "s/,/ /g") ; do # `echo "$5"`; do
echo "[worsica_bash_common.sh] [pull_multiple_processing] Pull $i"
pull_processing_binary_topo $1 $2 $3 $4 $i
done
}
function push_processing {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${IMAGESET_NAME}
#upload workspace to nextcloud as tar gz
#upload_folder ${PATH_TO_PRODUCTS} '${NEXTCLOUD_PATH_TO_PRODUCTS}'
set_nextcloud_path_to_products $1 $2 $3 $4
TAR_FILE_PREFIX=$1-user$2-roi$3-s$4-$5
#cd ${WORSICA_FOLDER_PATH}
#TAR_FILE_RESAMPLED=${TAR_FILE_PREFIX}-resampled.zip
#echo "[worsica_bash_common.sh] [push_processing] Tar ${TAR_FILE_RESAMPLED}"
#if (zip -r ${TAR_FILE_RESAMPLED} ${ROI_SIMULATION_FOLDER}/resampled/$5_* ${ROI_SIMULATION_FOLDER}/auxResample/* ${ROI_SIMULATION_FOLDER}/auxRGB/*); then
# echo "[worsica_bash_common.sh] [push_processing] Upload tar ${TAR_FILE_RESAMPLED} to nextcloud"
# upload_file ${WORSICA_FOLDER_PATH}/${TAR_FILE_RESAMPLED} ${NEXTCLOUD_PATH_TO_PRODUCTS}/${TAR_FILE_RESAMPLED}
# rm -rf ${ROI_SIMULATION_FOLDER}/resampled/$5_* ${ROI_SIMULATION_FOLDER}/auxResample/* ${ROI_SIMULATION_FOLDER}/auxRGB/*
# rm -rf ${TAR_FILE_RESAMPLED}
#else
# echo "[worsica_bash_common.sh] [push_processing] Files/folders not found to tar ${TAR_FILE_RESAMPLED} to nextcloud, moving on"
#fi
#
cd ${WORSICA_FOLDER_PATH}
TAR_FILE_FINAL_PRODUCTS=${TAR_FILE_PREFIX}-final-products.zip
echo "[worsica_bash_common.sh] [push_processing] Tar ${TAR_FILE_FINAL_PRODUCTS}"
if (zip -r ${TAR_FILE_FINAL_PRODUCTS} ${ROI_SIMULATION_FOLDER}/$5); then
echo "[worsica_bash_common.sh] [push_processing] Upload tar ${TAR_FILE_FINAL_PRODUCTS} to nextcloud"
upload_file ${WORSICA_FOLDER_PATH}/${TAR_FILE_FINAL_PRODUCTS} ${NEXTCLOUD_PATH_TO_PRODUCTS}/${TAR_FILE_FINAL_PRODUCTS}
rm -rf ${ROI_SIMULATION_FOLDER}/$5
rm -rf ${TAR_FILE_FINAL_PRODUCTS}
else
echo "[worsica_bash_common.sh] [push_processing] Files/folders not found to tar ${TAR_FILE_FINAL_PRODUCTS} to nextcloud, moving on"
fi
}
function push_processing_topo {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${GT_ID}
#$6 is ${IMAGESET_NAME}
#upload workspace to nextcloud as tar gz
#upload_folder ${PATH_TO_PRODUCTS} '${NEXTCLOUD_PATH_TO_PRODUCTS}'
set_nextcloud_path_to_products_topo $1 $2 $3 $4 $5
TAR_FILE_PREFIX=$1-user$2-roi$3-s$4-gt$5-$6
cd ${WORSICA_FOLDER_PATH}
TAR_FILE_FINAL_PRODUCTS=${TAR_FILE_PREFIX}-final-products.zip
echo "[worsica_bash_common.sh] [push_processing] Tar ${TAR_FILE_FINAL_PRODUCTS}"
if (zip -r ${TAR_FILE_FINAL_PRODUCTS} ${ROI_SIMULATION_FOLDER}); then
echo "[worsica_bash_common.sh] [push_processing] Upload tar ${TAR_FILE_FINAL_PRODUCTS} to nextcloud"
upload_file ${WORSICA_FOLDER_PATH}/${TAR_FILE_FINAL_PRODUCTS} ${NEXTCLOUD_PATH_TO_PRODUCTS}/${TAR_FILE_FINAL_PRODUCTS}
rm -rf ${ROI_SIMULATION_FOLDER}
rm -rf ${TAR_FILE_FINAL_PRODUCTS}
else
echo "[worsica_bash_common.sh] [push_processing] Files/folders not found to tar ${TAR_FILE_FINAL_PRODUCTS} to nextcloud, moving on"
fi
}
function pull_imageset {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${IMAGESET_NAME}
set_path_to_products $1 $2 $3 $4
echo "[worsica_bash_common.sh] [pull_imageset] Download the imageset from the ${NEXTCLOUD_PATH_TO_REPOSITORY}/$5.zip to the ${PATH_TO_PRODUCTS}/$5.zip"
LIST_FILES=$(list_files_nextcloud ${NEXTCLOUD_PATH_TO_REPOSITORY})
#download_file ${NEXTCLOUD_PATH_TO_REPOSITORY}/${f2} ${PATH_TO_PRODUCTS}/${f2}
f2="$5.zip"
#for f in $(list_files_nextcloud ${NEXTCLOUD_PATH_TO_REPOSITORY}); do
# f2=${f#/${NEXTCLOUD_WEBDAV_PATH}/repository_images/}
# echo $f2
if [[ ${LIST_FILES} == *${f2}* ]]; then
echo "[worsica_bash_common.sh] [pull_imageset] $f2 found, downloading zip..."
download_file ${NEXTCLOUD_PATH_TO_REPOSITORY}/$f2 ${PATH_TO_PRODUCTS}/$f2
else
echo "[worsica_bash_common.sh] [pull_imageset] $f2 not found, skip..."
fi
#done
}
function pull_imageset_uploaded {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${IMAGESET_NAME}
set_path_to_products $1 $2 $3 $4
NEXTCLOUD_PATH_TO_REPOSITORY="${NEXTCLOUD_REMOTE_PATH}/user_repository/user${USER_ID}/imagesets"
echo "[worsica_bash_common.sh] [pull_imageset_uploaded] Download the imageset from the ${NEXTCLOUD_PATH_TO_REPOSITORY}/$5.zip to the ${PATH_TO_PRODUCTS}/$5.zip"
LIST_FILES=$(list_files_nextcloud ${NEXTCLOUD_PATH_TO_REPOSITORY})
#download_file ${NEXTCLOUD_PATH_TO_REPOSITORY}/${f2} ${PATH_TO_PRODUCTS}/${f2}
f2="$5.zip"
#for f in $(list_files_nextcloud ${NEXTCLOUD_PATH_TO_REPOSITORY}); do
# f2=${f#/${NEXTCLOUD_WEBDAV_PATH}/repository_images/}
# echo $f2
if [[ ${LIST_FILES} == *${f2}* ]]; then
echo "[worsica_bash_common.sh] [pull_imageset_uploaded] $f2 found, downloading zip..."
download_file ${NEXTCLOUD_PATH_TO_REPOSITORY}/$f2 ${PATH_TO_PRODUCTS}/$f2
else
echo "[worsica_bash_common.sh] [pull_imageset_uploaded] $f2 not found, skip..."
fi
#done
}
#store the processing state to nextcloud
function push_imageset {
#$1 is ${SERVICE}
#$2 is ${USER_ID}
#$3 is ${ROI_ID}
#$4 is ${SIMULATION_ID}
#$5 is ${IMAGESET_NAME}
set_path_to_products $1 $2 $3 $4
echo "[worsica_bash_common.sh] [push_imageset] Upload $5.zip"
upload_file ${PATH_TO_PRODUCTS}/$5.zip ${NEXTCLOUD_PATH_TO_REPOSITORY}/$5.zip
echo "[worsica_bash_common.sh] [push_imageset] Remove zip and sha1 files on host side"
#rm -rf ${PATH_TO_PRODUCTS}/${IMAGESET_NAME}.zip.sha1 ${PATH_TO_PRODUCTS}/${IMAGESET_NAME}.zip
rm -rf ${PATH_TO_PRODUCTS}/$5.zip
}