-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-atlas_container.sh-dockerhub
executable file
·895 lines (771 loc) · 29.9 KB
/
run-atlas_container.sh-dockerhub
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#!/bin/bash
# coding: utf-8
# version=2024-04-12-r01
# author: Shuwei Ye <[email protected]>
"true" '''\'
myScript="${BASH_SOURCE:-$0}"
ret=0
sourced=0
if [ -n "$ZSH_VERSION" ]; then
case $ZSH_EVAL_CONTEXT in *:file) sourced=1; esac
else
case ${0##*/} in bash|-bash|zsh|-zsh) sourced=1; esac
fi
mySetup=runMe-here.sh
if [[ -e $mySetup && ( $# -eq 0 || "$*" =~ "--rerun" ) ]]; then
source $mySetup
ret=$?
elif [[ $# -eq 1 && "$1" =~ ^[Jj]upyter$ ]]; then
# source $mySetup jupyter
echo "Jupyter is not ready yet"
ret=$?
else
if [ "X" != "X$BASH_SOURCE" ]; then
shopt -s expand_aliases
fi
myDir=$(dirname $myScript)
myDir=$(readlink -f $myDir)
# myDir=$(py_readlink $myDir)
if [[ $# -eq 0 ]]; then
python3 -B -I "$myScript" --help
ret=$?
else
now=$(date +"%s")
python3 -B -I -S "$myScript" --shellFilename $mySetup "$@"
ret=$?
if [ -e $mySetup ]; then
# check if the setup script is newly created
# mtime_setup=$(py_stat $mySetup)
mtime_setup=$(stat -c %Y $mySetup 2>/dev/null || stat -f %m $mySetup)
if [ "$(( $mtime_setup - $now ))" -ge 0 ]; then
echo -e "\nTo reuse the same container next time, just run"
echo -e "\n\t source $mySetup \n or \n\t source $myScript"
sleep 2
source $mySetup
fi
fi
fi
fi
[[ $sourced == 1 ]] && return $ret || exit $ret
'''
import getpass
import os
import sys
import io
from datetime import datetime, timezone
# from time import sleep
import argparse
import json
import pprint
import re
import subprocess
import fnmatch
import difflib
from shutil import which, rmtree
from subprocess import getstatusoutput
from urllib.request import urlopen
from urllib.error import URLError, HTTPError
import ssl
GITHUB_REPO = "usatlas/ML-Containers"
GITHUB_PATH = "run-atlas_container.sh-dockerhub"
URL_SELF = "https://raw.githubusercontent.com/%s/main/%s" % (
GITHUB_REPO, GITHUB_PATH)
URL_API_SELF = "https://api.github.com/repos/%s/commits?path=%s&per_page=100" % (
GITHUB_REPO, GITHUB_PATH)
CONTAINER_CMDS = ["podman", "docker", "nerdctl", "apptainer", "singularity"]
ContCmds_available = []
ATLAS_PROJECTS = [
"athena",
"analysisbase",
"athanalysis",
"analysistop",
"athsimulation",
"rucio-clients"]
DOCKERHUB_REPO = "https://hub.docker.com/v2/repositories/atlas"
class Version(str):
def __lt__(self, other):
va = re.split(r"[.-]", self)
vb = re.split(r"[.-]", other)
# print("va=",va, "; vb=",vb)
for i in range(min(len(va), len(vb))):
ai = va[i]
bi = vb[i]
if ai.isdigit() and bi.isdigit():
if ai != bi:
return int(ai) < int(bi)
elif ai != bi:
return ai < bi
return len(va) < len(vb)
def set_default_subparser(parser, default_subparser, index_action=1):
"""default subparser selection. Call after setup, just before parse_args()
parser: the name of the parser you're making changes to
default_subparser: the name of the subparser to call by default"""
if len(sys.argv) <= index_action:
parser.print_help()
sys.exit(1)
subparser_found = False
for arg in sys.argv[1:]:
if arg in ["-h", "--help",
"-V", "--version"]: # global help or version, no default subparser
break
else:
for x in parser._subparsers._actions:
if not isinstance(x, argparse._SubParsersAction):
continue
for sp_name in x._name_parser_map.keys():
if sp_name in sys.argv[1:]:
subparser_found = True
if not subparser_found:
# insert default in first position before all other arguments
sys.argv.insert(index_action, default_subparser)
def getUrlContent(url):
try:
response = urlopen(url)
except (URLError, HTTPError) as e:
print("Error fetching URL:", e)
ssl_context = ssl._create_unverified_context()
response = urlopen(url, context=ssl_context)
return response.read().decode("utf-8")
def getLastCommit():
json_obj = json.loads(getUrlContent(URL_API_SELF))
recentCommit = json_obj[0]["commit"]["committer"]["date"]
myScript = os.path.abspath(sys.argv[0])
myMTime = datetime.fromtimestamp(
os.path.getmtime(myScript), tz=timezone.utc)
myDate = myMTime.strftime("%Y-%m-%dT%H:%M:%SZ")
return myDate, recentCommit
def getVersion(myFile=None):
if myFile is None:
myScript = os.path.abspath(sys.argv[0])
myFile = open(myScript, "r")
else:
if isinstance(myFile, str):
myFile = myFile.split("\n")
no = 0
version = ""
for line in myFile:
no += 1
if no > 10:
break
if re.search(r"^#.* version", line):
version = line.split("=")[1]
break
if isinstance(myFile, io.IOBase):
myFile.close()
return version
def parseArgTags(inputArgs, requireRelease=False):
argTags = []
releaseTags = {}
for arg in inputArgs:
argTags += re.split(r"[,:]", arg)
for tag in argTags:
if len(tag) == 0:
continue
if tag.lower() in ATLAS_PROJECTS:
releaseTags["project"] = tag.lower()
# elif tag == "latest" or tag[0].isdigit():
else:
if "releases" not in releaseTags:
releaseTags["releases"] = [tag]
else:
releaseTags["releases"] += [tag]
# print("!!Warning!! Unrecognized input arg=", tag)
if "project" not in releaseTags:
print("The input arg tags=", argTags)
print("!!Warning!! No project is given from the available choices:",
ATLAS_PROJECTS)
for argTag in argTags:
closeProject = None
argTag_1 = re.split(r"[-.=;]", argTag, 1)[0].lower()
if argTag_1 in ATLAS_PROJECTS:
closeProject = argTag_1
if not closeProject:
closeMatch = difflib.get_close_matches(
argTag.lower(), ATLAS_PROJECTS, n=1, cutoff=0.8)
if closeMatch:
closeProject = closeMatch[0]
if closeProject:
print()
if isinstance(closeProject, list):
closeProject = closeProject[0]
print("The closest matched project=", closeProject)
print(" against the input arg tag=", argTag)
print("\nPlease correct it, then rerun\n")
break
sys.exit(1)
if requireRelease and "releases" not in releaseTags:
print("!!Warning!! No release is given")
sys.exit(1)
return releaseTags
def selfUpdate(args):
currentVersion = getVersion()
myDate, recentCommit = getLastCommit()
print("The most recent GitHub commit's UTC timestamp is", recentCommit)
content = getUrlContent(URL_SELF)
latestVersion = getVersion(getUrlContent(URL_SELF))
if latestVersion > currentVersion or (
latestVersion == currentVersion and recentCommit > myDate):
print("Update available, updating the script itself")
myScript = os.path.abspath(sys.argv[0])
os.rename(myScript, myScript + ".old")
try:
myfile = open(myScript, "w")
myfile.write(content)
myfile.close()
print("Update finished")
except Exception:
err = sys.exc_info()[1]
print("Failed to write out the latest version of this script\n", err)
print("Keep the current version")
os.rename(myScript + ".old", myScript)
return
print("Already up-to-date, no update needed")
def run_shellCmd(shellCmd, exitOnFailure=True):
retCode, out = getstatusoutput(shellCmd)
if retCode != 0 and exitOnFailure:
print("!!Error!! Failed in running the following command")
print("\t", shellCmd)
sys.exit(1)
return retCode, out
def listImageTags(project):
url_tags = DOCKERHUB_REPO + "/" + project + "/tags?page_size=5000"
json_obj = json.loads(getUrlContent(url_tags))
json_tags = json_obj["results"]
imageTags = {}
latest_digest = ""
latest_name = ""
for tagObj in json_tags:
name = tagObj["name"]
digest = tagObj["images"][0]["digest"]
imageSize = tagObj["full_size"]
lastUpdate = tagObj["tag_last_pushed"]
imageTags[name] = {
"imageCompressedSize": imageSize,
"lastUpdate": lastUpdate,
"releaseName": name,
"digest": digest}
if name == "latest":
latest_digest = digest
elif latest_digest != "":
if digest == latest_digest:
latest_name = name
if latest_name != "":
imageTags["latest"]["releaseName"] = latest_name
return imageTags
def listReleases(args):
inputTags = args.tags
if len(inputTags) == 0:
print("Please specify a project name. The available projects are:\n\n",
ATLAS_PROJECTS)
sys.exit(1)
releaseTags = parseArgTags(inputTags, requireRelease=False)
project = releaseTags["project"]
if "releases" in releaseTags:
releases = releaseTags["releases"]
else:
releases = None
imageTags = listImageTags(project)
releasePrint = ""
if releases is None:
tags = list(imageTags.keys())
else:
tags = []
releasesWild = []
releasesNoWild = []
for release in releases:
if "*" in release or "?" in release:
releasesWild += [release]
else:
releasesNoWild += [release]
releasePrint = "matching release tag(s)=%s" % releases
for tagName in imageTags:
matchTag = True
if len(releases) == 1 and tagName == releases[0]:
tags = [tagName]
break
for releaseWild in releasesWild:
if not fnmatch.fnmatch(tagName, releaseWild):
matchTag = False
break
if not matchTag:
continue
for releaseNoWild in releasesNoWild:
item_1 = releaseNoWild.split(r".")[0]
if item_1.isdigit() and len(item_1) < 4:
if not tagName.startswith(releaseNoWild):
matchTag = False
break
else:
if (releaseNoWild != tagName and
releaseNoWild not in re.split(r"[-.]", tagName)):
matchTag = False
break
if matchTag:
# if fnmatch.fnmatch(tagName, release) or release in
# re.split(r"[-.]", tagName):
tags += [tagName]
tags.sort(key=Version)
if len(tags) > 0:
columns = os.get_terminal_size().columns
pp = pprint.PrettyPrinter(indent=4, compact=True, width=columns)
print("Found the following release containers for the project= %s, %s\n" %
(project, releasePrint))
pp.pprint(tags)
if len(tags) == 1:
print()
getImageInfo(project, tags[0])
print("\nTo run the above image, just run")
print("\tsource %s %s,%s" % (sys.argv[0], project, tags[0]))
else:
print("No release container found for the project=%s, and %s" %
(project, releasePrint))
def getImageInfo(project, release, printOut=True):
imageInfo = {}
imageTags = listImageTags(project)
if release in imageTags:
imageInfo = imageTags[release].copy()
releaseName = imageInfo.pop("releaseName")
imageInfo["dockerPath"] = "docker.io/atlas/%s:%s" % (
project, releaseName)
else:
print("!!Warning!! release=%s is NOT available" % release)
sys.exit(1)
if len(imageInfo) > 0 and printOut:
print("The matched image info:")
print("\tdockerPath=", imageInfo["dockerPath"],
"\n\timage compressed size=", imageInfo["imageCompressedSize"],
"\n\tlast update time=", imageInfo["lastUpdate"],
"\n\tdigest=", imageInfo["digest"])
return imageInfo
def printImageInfo(args):
releaseTags = parseArgTags(args.tags, requireRelease=True)
project = releaseTags["project"]
releases = releaseTags["releases"]
if len(releases) > 1:
print("Only one release tag is allowed, but multiple are given \n\t",
releases)
sys.exit(1)
getImageInfo(project, releases[0])
# build Singularity sandbox
def build_sandbox(contCmd, sandboxPath, dockerPath, force=False):
if os.path.exists(sandboxPath):
if not force and os.path.exists(sandboxPath + "/entrypoint.sh"):
print("%s already, and would not override it." % sandboxPath)
print("\nTo override the existing sandbox, please add the option '-f'")
print("Quit now")
sys.exit(1)
os.system("chmod -R +w %s; rm -rf %s" % (sandboxPath, sandboxPath))
buildCmd = "%s build --sandbox --fix-perms -F %s docker://%s" % (
contCmd, sandboxPath, dockerPath)
print("\nBuilding Singularity sandbox\n")
retCode = subprocess.call(buildCmd.split())
if retCode != 0:
print("!!Warning!! Building the Singularity sandbox failed. Exit now")
sys.exit(1)
# write setup for Singularity sandbox
def write_sandboxSetup(filename, inputArgs, imageInfo, contCmd, sandboxPath,
bindOpt):
imageSize = imageInfo["imageCompressedSize"]
dockerPath = imageInfo["dockerPath"]
lastUpdate = imageInfo["lastUpdate"]
myScript = os.path.abspath(sys.argv[0])
shellFile = open(filename, "w")
shellFile.write("""
inputArgs="%s"
contCmd=%s
dockerPath=%s
imageCompressedSize=%s
imageLastUpdate=%s
sandboxPath=%s
bindOpt="%s"
releaseSetup1="/release_setup.sh"
releaseSetup2="/home/atlas/release_setup.sh"
if [ -e $sandboxPath$releaseSetup1 -o $sandboxPath$releaseSetup2 ]; then
if [[ $# -eq 1 && "$1" =~ ^[Jj]upyter$ ]]; then
runCmd="echo Jupyter is not ready yet"
# runCmd=("$contCmd exec $bindOpt $sandboxPath /bin/bash -c "
# '"source $releaseSetup; jupyter lab"')
else
if [ -e $sandboxPath$releaseSetup1 ]; then
releaseSetup=$releaseSetup1
else
releaseSetup=$releaseSetup2
fi
runCmd="$contCmd run $bindOpt $sandboxPath /bin/bash --init-file $releaseSetup"
fi
echo -e "\\n$runCmd\\n"
eval $runCmd
else
echo "The Singularity sandbox=$sandboxPath does not exist or is invalid"
echo "Please rebuild the Singularity sandbox by running the following"
echo -e "\n\t source %s $imageName"
fi
""" % (" ".join(inputArgs), contCmd, dockerPath, imageSize, lastUpdate,
sandboxPath, bindOpt, myScript))
shellFile.close()
# Failover another container command "apptainer" or "singularity"
def failOver_Singularity(args, imageInfo, oldContCmd):
if "apptainer" in ContCmds_available or "singularity" in ContCmds_available:
if args.contCmd != oldContCmd:
if "apptainer" in ContCmds_available:
newContCmd = "apptainer"
else:
newContCmd = "singularity"
print("\t Next trying another container command %s again" % newContCmd)
setup(args, imageInfo, newContCmd)
sys.exit(0)
else:
print("\nYou may retry with the option --app or --sing")
sys.exit(1)
else:
sys.exit(1)
# create docker/podman container
def create_container(contCmd, contName, imageInfo, bindOpt, args):
force = args.force
dockerPath = imageInfo["dockerPath"]
pullCmd = "%s pull %s" % (contCmd, dockerPath)
retCode = subprocess.call(pullCmd.split())
# username = getpass.getuser()
# home = os.path.expanduser("~")
jupyterOpt = ""
# jupyterOpt = "-p 8888:8888 -e NB_USER=%s -e HOME=%s -v %s:%s" % (username, home, home, home)
# jupyterOpt += " -v /etc/passwd:/etc/passwd:ro -v /etc/group:/etc/group:ro"
if retCode != 0:
print("!!Warning!! Pulling the image %s failed, exit now" % dockerPath)
failOver_Singularity(args, imageInfo, contCmd)
_, out = run_shellCmd("%s ps -a -f name='%s' " % (contCmd, contName))
if out.find(contName) > 0:
if force:
print("\nThe container=%s already exists, removing it now" %
contName)
run_shellCmd("%s rm -f %s" % (contCmd, contName))
else:
print("\nThe container=%s already exists, "
"\n\tplease rerun the command with the option '-f' to remove it" %
contName)
print("\nQuit now")
sys.exit(1)
pwd = os.getcwd()
createOpt = "-it -v %s:%s -w %s %s %s" % (pwd,
pwd, pwd, jupyterOpt, bindOpt)
createCmd = "%s create %s --name %s %s" % \
(contCmd, createOpt, contName, dockerPath)
ret, _ = run_shellCmd(createCmd, False)
if ret != 0:
print("!!Error!! Failed in running the following command")
print("\t", createCmd)
failOver_Singularity(args, imageInfo, contCmd)
startCmd = "%s start %s" % (contCmd, contName)
ret, _ = run_shellCmd(startCmd, False)
if ret != 0:
print("!!Error!! Failed in running the following command")
print("\t", startCmd)
rmCmd = "%s rm -f %s" % (contCmd, contName)
run_shellCmd(rmCmd, exitOnFailure=False)
failOver_Singularity(args, imageInfo, contCmd)
# write setup for Docker/Podman container
def write_dockerSetup(filename, inputArgs, contCmd, contName, imageInfo,
bindOpt, override=False):
imageSize = imageInfo["imageCompressedSize"]
dockerPath = imageInfo["dockerPath"]
lastUpdate = imageInfo["lastUpdate"]
wcCmd = ("%s exec %s wc -l /release_setup.sh "
"/home/atlas/release_setup.sh 2>/dev/null" % (contCmd, contName))
_, releaseSetup = run_shellCmd(wcCmd, exitOnFailure=False)
if len(releaseSetup.split("\n")) == 1:
print("!!Error!! No 'release_setup.sh' is found in the image, exit now")
sys.exit(1)
else:
items = releaseSetup.split()
if "/release_setup.sh" in items:
releaseSetup = "/release_setup.sh"
else:
releaseSetup = "/home/atlas/release_setup.sh"
shellFile = open(filename, "w")
shellFile.write("""
inputArgs="%s"
contCmd=%s
dockerPath=%s
imageCompressedSize=%s
imageLastUpdate=%s
contName=%s
bindOpt="%s"
releaseSetup=%s
jupyterOpt="-p 8888:8888 -e NB_USER=$USER -e HOME=$HOME -v ${HOME}:${HOME}"
re_exited="ago[\ ]+Exited"
re_up="ago[\ ]+Up"
listOut=$($contCmd ps -a -f name="$contName" 2>/dev/null | tail -1)
if [[ "$listOut" =~ $re_exited ]]; then
startCmd="$contCmd start $contName"
echo -e "\\n$startCmd"
eval $startCmd >/dev/null
elif [[ "$listOut" =~ $re_up ]]; then
if [[ "$listOut" =~ "(Paused)" ]]; then
unpauseCmd="$contCmd unpause $contName"
echo -e "\\n$unpauseCmd"
eval $unpauseCmd >/dev/null
fi
else
createCmd=("$contCmd create -it $bindOpt -v $PWD:$PWD -w $PWD "
"$jupyterOpt --name $contName $dockerPath")
echo -e "\\n$createCmd"
eval $createCmd >/dev/null
startCmd="$contCmd start $contName"
echo -e "\\n$startCmd"
eval $startCmd >/dev/null
fi
if [[ $# -eq 1 && "$1" =~ ^[Jj]upyter$ ]]; then
uid=$(id -u)
gid=$(id -g)
runCmd="echo Jupyter is not ready yet"
# $contCmd exec -u ${uid}:${gid} -e USER=$USER $contName ls $releaseSetup >/dev/null
# if [ $? -eq 0 ]; then
# runCmd=("$contCmd exec -it -u ${uid}:${gid} -e USER=$USER $contName /bin/bash -c "
# '"source $releaseSetup; jupyter lab --ip 0.0.0.0"')
# else
# echo "This release container can NOT run JupyterLab in non-root mode; exit now"
# runCmd=""
# fi
else
runCmd="$contCmd exec -it $contName /bin/bash --init-file $releaseSetup"
fi
if [[ "$runCmd" != "" ]]; then
echo -e "\\n$runCmd\\n"
eval $runCmd
fi
stopCmd="$contCmd stop $contName"
echo -e "\\n$stopCmd"
eval $stopCmd
""" % (" ".join(inputArgs), contCmd, dockerPath, imageSize, lastUpdate,
contName, bindOpt, releaseSetup))
shellFile.close()
def getMyImageInfo(filename):
shellFile = open(filename, "r")
myImageInfo = {}
for line in shellFile:
if re.search(r"^(cont|image|docker|sand|runOpt|lines).*=", line):
key, value = line.strip().split("=")
myImageInfo[key] = value
return myImageInfo
def isLastRelease(filename, project, release, contCmd):
if not os.path.exists(filename):
return False
myImageInfo = getMyImageInfo(filename)
if "dockerPath" not in myImageInfo:
return False
dockerPath = myImageInfo["dockerPath"]
if dockerPath.endswith("/%s:%s" % (project, release)):
if contCmd is None or contCmd == myImageInfo["contCmd"]:
print("The release was previously used in the current directory.\n\t Reuse it now")
# sleep(1)
os.utime(filename, None)
return True
return False
def printMe(args):
if not os.path.exists(args.shellFilename):
print("No previous container/sandbox setup is found")
return None
myImageInfo = getMyImageInfo(args.shellFilename)
if "runOpt" in myImageInfo:
myImageInfo.pop("runOpt")
pp = pprint.PrettyPrinter(indent=4)
print("The image/container used in the current work directory:")
pp.pprint(myImageInfo)
def cleanLast(filename):
if not os.path.exists(filename):
return
myImageInfo = getMyImageInfo(filename)
if len(myImageInfo) > 0:
contCmd = myImageInfo["contCmd"]
if contCmd == "singularity" or contCmd == "apptainer":
sandboxPath = myImageInfo["sandboxPath"]
print("\nRemoving the last sandbox=%s\n" % sandboxPath)
try:
rmtree(sandboxPath)
except BaseException:
pass
os.rename(filename, filename + ".last")
else:
contName = myImageInfo["contName"]
print("\nRemoving the last container=%s\n" % contName)
rmCmd = "%s rm -f %s" % (contCmd, contName)
run_shellCmd(rmCmd, exitOnFailure=False)
os.rename(filename, filename + ".last")
def prepare_setup(args):
global ContCmds_available
releaseTags = parseArgTags(args.tags, requireRelease=True)
project = releaseTags["project"]
releases = releaseTags["releases"]
if len(releases) > 1:
print("Only one release tag is allowed, but multiple are given \n\t",
releases)
release = releases[0]
if not args.force:
if isLastRelease(args.shellFilename, project, release, args.contCmd):
sys.exit(0)
imageInfo = getImageInfo(project, release)
# dockerPath = imageInfo["dockerPath"]
for cmd in CONTAINER_CMDS:
cmdFound = which(cmd)
if cmdFound is not None:
ContCmds_available += [cmd]
if len(ContCmds_available) == 0:
print("Found none of container running commands:",
CONTAINER_CMDS, "; exit now")
print("Please install one of the above tool first")
sys.exit(1)
contCmd = ContCmds_available[0]
if args.contCmd is not None:
if args.contCmd in ContCmds_available:
contCmd = args.contCmd
else:
print("The specified command=%s to run containers is NOT found" %
args.contCmd)
print("Please choose the available command(s) on the machine to run containers")
print("\t", ContCmds_available)
sys.exit(1)
cleanLast(args.shellFilename)
return imageInfo, contCmd
def setup(args, imageInfo=None, contCmd=None):
if imageInfo is None:
imageInfo, contCmd = prepare_setup(args)
dockerPath = imageInfo["dockerPath"]
project, release = dockerPath.split("/")[-1].split(":")
paths = args.volume
volumes = []
if paths is not None:
pwd = os.getcwd()
home = os.path.expanduser("~")
for path in paths.split(","):
localPath = path.split(":")[0]
if os.path.samefile(pwd, localPath):
continue
elif (contCmd == "singularity" or
contCmd == "apptainer") and os.path.samefile(home, localPath):
continue
volumes += [path]
if contCmd == "singularity" or contCmd == "apptainer":
if not os.path.exists("singularity"):
os.mkdir("singularity")
sandboxPath = "singularity/%s-%s" % (project, release)
build_sandbox(contCmd, sandboxPath, dockerPath, args.force)
bindOpt = ""
for path in volumes:
bindOpt += " -B %s" % path
write_sandboxSetup(args.shellFilename, args.tags, imageInfo, contCmd,
sandboxPath, bindOpt)
elif contCmd == "podman" or contCmd == "docker" or contCmd == "nerdctl":
testCmd = "%s info" % contCmd
# run_shellCmd(testCmd)
ret, _ = run_shellCmd(testCmd, False)
if ret != 0:
print("!!Error!! Failed in running the following command")
print("\t", testCmd)
failOver_Singularity(args, imageInfo, contCmd)
contName = "_".join([getpass.getuser(), project, release])
bindOpt = ""
for path in volumes:
if path.find(":") > 0:
bindOpt += " -v %s" % path
else:
bindOpt += " -v %s:%s" % (path, path)
create_container(contCmd, contName, imageInfo, bindOpt, args)
write_dockerSetup(args.shellFilename, args.tags, contCmd, contName,
imageInfo, bindOpt, args.force)
# sleep(1)
def jupyter(args):
if not os.path.exists(args.shellFilename):
print("No previous container/sandbox setup is found")
myScript = os.path.abspath(sys.argv[0])
print("Please run 'source %s setup ImageName' first" % myScript)
return None
def main():
myScript = os.path.basename(os.path.abspath(sys.argv[0]))
example_global = """Examples:
./%s list AthAnalysis
./%s list analysisbase,"21.2.2*"
./%s list analysisbase,22.2
./%s AnalysisBase:latest
./%s # Empty arg to rerun the already setup container
./%s setup AnalysisBase,21.2.132""" % ((myScript,) * 6)
example_setup = """Examples:
./%s AnalysisBase,21.2.132
./%s --sing AnalysisBase,21.2.132""" % (myScript, myScript)
parser = argparse.ArgumentParser(
epilog=example_global,
usage="%(prog)s [options]",
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
"--shellFilename",
action="store",
help=argparse.SUPPRESS)
parser.add_argument(
"--rerun",
action="store_true",
help="rerun the already setup container")
parser.add_argument(
"-V", "--version",
action="store_true",
help="print out the script version")
sp = parser.add_subparsers(dest="command", help="Default=setup")
sp_listReleases = sp.add_parser(
"listReleases", aliases=["list"],
help="list container releases",
description="list all available ATLAS releases of a given project")
# sp_listReleases.add_argument("projectName", metavar="<ProjectName>", help="Project name to list releases")
sp_listReleases.add_argument(
"tags", nargs="*", metavar="<ReleaseTags>",
help="Project name to list releases, and release number with wildcard *")
sp_listReleases.set_defaults(func=listReleases)
sp_printImageInfo = sp.add_parser(
"printImageInfo", aliases=["getImageInfo"],
help="print Info of a container release",
description="print the image size and last update date of the given image")
sp_printImageInfo.add_argument("tags", nargs="+", metavar="<ReleaseTags>")
sp_printImageInfo.set_defaults(func=printImageInfo)
sp_printMe = sp.add_parser(
"printMe",
help="print info of the setup container",
description="print the container/sandbox set up for the work directory")
sp_printMe.set_defaults(func=printMe)
desc = "update the script itself"
sp_update = sp.add_parser("selfUpdate", help=desc, description=desc)
sp_update.set_defaults(func=selfUpdate)
sp_setup = sp.add_parser(
"setup",
help="set up a container release",
description="create a container/sandbox for the given image",
epilog=example_setup,
formatter_class=argparse.RawDescriptionHelpFormatter)
group_cmd = sp_setup.add_mutually_exclusive_group()
for cmd in CONTAINER_CMDS:
group_cmd.add_argument("--%s" % cmd, dest="contCmd",
action="store_const", const="%s" % cmd,
help="Use %s to the container" % cmd)
sp_setup.add_argument(
"-f", "--force",
action="store_true", default=False,
help="Force to override the existing container/sandbox")
sp_setup.add_argument(
"-B", "--volume",
nargs="?", metavar="path[,srcPath:targePath]",
help="Additional path(s) delimited by comma, to be mounted into the container")
sp_setup.add_argument(
"tags", nargs="+", metavar="<ReleaseTags>",
help="A release to run")
sp_setup.set_defaults(func=setup)
set_default_subparser(parser, "setup", 3)
sp_jupyter = sp.add_parser(
"jupyter",
help="(not ready) run Jupyter with the container",
description="(not ready yet)run JupyterLab on the already created container/sandbox")
sp_jupyter.set_defaults(func=jupyter)
args, _ = parser.parse_known_args()
if args.version:
version = getVersion()
if version is not None:
print("Version=", version)
sys.exit(0)
args.func(args)
if __name__ == "__main__":
main()