2
2
3
3
from __future__ import print_function
4
4
import argparse
5
- from argparse import ArgumentParser
6
5
from time import time , sleep
7
6
from datetime import datetime as dt
8
7
from io import open
23
22
import requests
24
23
from requests .exceptions import RequestException
25
24
from pkg_resources import resource_string , parse_version , require
25
+ from alidock .argumentparser import AliDockArgumentParser
26
26
from alidock .log import Log
27
27
from alidock .util import splitEsc , getUserId , getUserName , execReturn , deactivateVenv , checkRocm
28
28
@@ -42,7 +42,15 @@ def __init__(self, overrideConf=None):
42
42
self .cli = docker .from_env ()
43
43
self .dirInside = "/home/alidock"
44
44
self .userName = getUserName ()
45
- self .conf = {
45
+ self .conf = self .getDefaultConf ()
46
+ self .parseConfig ()
47
+ self .overrideConfig (overrideConf )
48
+ self .conf ["dockName" ] = "{dockName}-{userId}" .format (dockName = self .conf ["dockName" ],
49
+ userId = getUserId ())
50
+
51
+ @staticmethod
52
+ def getDefaultConf ():
53
+ return {
46
54
"dockName" : "alidock" ,
47
55
"imageName" : "alipier/alidock:latest" ,
48
56
"dirOutside" : os .path .join ("~" , "alidock" ),
@@ -56,10 +64,6 @@ def __init__(self, overrideConf=None):
56
64
"web" : False ,
57
65
"debug" : False
58
66
}
59
- self .parseConfig ()
60
- self .overrideConfig (overrideConf )
61
- self .conf ["dockName" ] = "{dockName}-{userId}" .format (dockName = self .conf ["dockName" ],
62
- userId = getUserId ())
63
67
64
68
def parseConfig (self ):
65
69
confFile = os .path .join (os .path .expanduser ("~" ), ".alidock-config.yaml" )
@@ -425,11 +429,12 @@ def updateFunc():
425
429
updateFunc = updateFunc )
426
430
427
431
def entrypoint ():
428
- argp = ArgumentParser ()
429
- argp .add_argument ("--quiet" , "-q" , dest = "quiet" , default = False , action = "store_true" ,
430
- help = "Do not print any message" )
431
- argp .add_argument ("--version" , "-v" , dest = "version" , default = False , action = "store_true" ,
432
- help = "Print current alidock version on stdout" )
432
+ argp = AliDockArgumentParser (atStartTitle = "only valid if container is not running, "
433
+ "not effective otherwise" )
434
+ argp .addArgument ("--quiet" , "-q" , dest = "quiet" , default = False , action = "store_true" ,
435
+ help = "Do not print any message" )
436
+ argp .addArgument ("--version" , "-v" , dest = "version" , default = False , action = "store_true" ,
437
+ help = "Print current alidock version on stdout" )
433
438
434
439
# tmux: both normal and terminal integration ("control mode")
435
440
tmuxArgs = argp .add_mutually_exclusive_group ()
@@ -441,38 +446,40 @@ def entrypoint():
441
446
"(integration with your terminal)" )
442
447
443
448
# The following switches can be set in a configuration file
444
- argp .add_argument ("--name" , dest = "dockName" , default = None ,
445
- help = "Override default container name [dockName]" )
446
- argp .add_argument ("--image" , dest = "imageName" , default = None ,
447
- help = "Override default image name [imageName]" )
448
- argp .add_argument ("--shared" , dest = "dirOutside" , default = None ,
449
- help = "Override host path of persistent home [dirOutside]" )
450
- argp .add_argument ("--mount" , dest = "mount" , default = None , nargs = "+" ,
451
- help = "Host directories to mount under /mnt inside alidock, in the format "
452
- "/external/path[:label[:[rw|ro]]] [mount]" )
453
- argp .add_argument ("--update-period" , dest = "updatePeriod" , default = None ,
454
- help = "Override update check period [updatePeriod]" )
455
- argp .add_argument ("--no-update-image" , dest = "dontUpdateImage" , default = None ,
456
- action = "store_true" ,
457
- help = "Do not update the Docker image [dontUpdateImage]" )
458
- argp .add_argument ("--no-update-alidock" , dest = "dontUpdateAlidock" , default = None ,
459
- action = "store_true" ,
460
- help = "Do not update alidock automatically [dontUpdateAlidock]" )
461
- argp .add_argument ("--nvidia" , dest = "useNvidiaRuntime" , default = None ,
462
- action = "store_true" ,
463
- help = "Launch container using the NVIDIA Docker runtime [useNvidiaRuntime]" )
464
- argp .add_argument ("--rocm" , dest = "enableRocmDevices" , default = None ,
465
- action = "store_true" ,
466
- help = "Expose devices needed by ROCm [enableRocmDevices]" )
467
- argp .add_argument ("--cvmfs" , dest = "cvmfs" , default = None ,
468
- action = "store_true" ,
469
- help = "Mount CVMFS inside the container [cvmfs]" )
470
- argp .add_argument ("--web" , dest = "web" , default = None ,
471
- action = "store_true" ,
472
- help = "Make X11 available from a web browser [web]" )
473
- argp .add_argument ("--debug" , dest = "debug" , default = None ,
474
- action = "store_true" ,
475
- help = "Increase verbosity [debug]" )
449
+ argp .addArgument ("--name" , dest = "dockName" , default = None , config = True ,
450
+ help = "Override default container name" )
451
+ argp .addArgument ("--update-period" , dest = "updatePeriod" , default = None , config = True ,
452
+ help = "Override update check period" )
453
+ argp .addArgument ("--no-update-alidock" , dest = "dontUpdateAlidock" , default = None , config = True ,
454
+ action = "store_true" ,
455
+ help = "Do not update alidock automatically" )
456
+ argp .addArgument ("--debug" , dest = "debug" , default = None , config = True ,
457
+ action = "store_true" ,
458
+ help = "Increase verbosity" )
459
+
460
+ # Args valid only when starting the container; they can be set in a config file
461
+ argp .addArgumentStart ("--image" , dest = "imageName" , default = None , config = True ,
462
+ help = "Override default image name" )
463
+ argp .addArgumentStart ("--shared" , dest = "dirOutside" , default = None , config = True ,
464
+ help = "Override host path of persistent home" )
465
+ argp .addArgumentStart ("--mount" , dest = "mount" , default = None , nargs = "+" , config = True ,
466
+ help = "Host dirs to mount under /mnt inside alidock, in the format "
467
+ "/external/path[:label[:[rw|ro]]]" )
468
+ argp .addArgumentStart ("--no-update-image" , dest = "dontUpdateImage" , default = None , config = True ,
469
+ action = "store_true" ,
470
+ help = "Do not update the Docker image" )
471
+ argp .addArgumentStart ("--nvidia" , dest = "useNvidiaRuntime" , default = None , config = True ,
472
+ action = "store_true" ,
473
+ help = "Use the NVIDIA Docker runtime" )
474
+ argp .addArgumentStart ("--rocm" , dest = "enableRocmDevices" , default = None , config = True ,
475
+ action = "store_true" ,
476
+ help = "Expose devices needed by ROCm" )
477
+ argp .addArgumentStart ("--cvmfs" , dest = "cvmfs" , default = None , config = True ,
478
+ action = "store_true" ,
479
+ help = "Mount CVMFS inside the container" )
480
+ argp .addArgumentStart ("--web" , dest = "web" , default = None , config = True ,
481
+ action = "store_true" ,
482
+ help = "Make X11 available from a web browser" )
476
483
477
484
argp .add_argument ("action" , default = "enter" , nargs = "?" ,
478
485
choices = ["enter" , "root" , "exec" , "start" , "status" , "stop" ],
@@ -481,13 +488,13 @@ def entrypoint():
481
488
argp .add_argument ("shellCmd" , nargs = argparse .REMAINDER ,
482
489
help = "Command to execute in the container (works with exec)" )
483
490
484
-
491
+ argp . genConfigHelp ( AliDock . getDefaultConf ())
485
492
args = argp .parse_args ()
486
493
487
494
LOG .setQuiet (args .quiet )
488
495
489
496
try :
490
- processActions (args )
497
+ processActions (args , argp . argsAtStart )
491
498
except AliDockError as exc :
492
499
LOG .error ("Cannot continue: {msg}" .format (msg = exc ))
493
500
exit (10 )
@@ -498,7 +505,22 @@ def entrypoint():
498
505
LOG .error ("Cannot communicate to Docker, is it running? Full error: {msg}" .format (msg = exc ))
499
506
exit (12 )
500
507
501
- def processEnterStart (aliDock , args ):
508
+ def checkArgsAtStart (args , argsAtStart ):
509
+ ignoredArgs = []
510
+ for sta in argsAtStart :
511
+ if args .__dict__ [sta .config ] is not None :
512
+ ignoredArgs .append (sta .option )
513
+ if ignoredArgs :
514
+ LOG .warning ("The following options are being ignored:" )
515
+ for ign in ignoredArgs :
516
+ LOG .warning (" " + ign )
517
+ LOG .warning ("This is because alidock is already running and they are only valid when a "
518
+ "new container is started." )
519
+ LOG .warning ("You may want to stop alidock first with:" )
520
+ LOG .warning (" alidock stop" )
521
+ LOG .warning ("and try again. Check `alidock --help` for more information" )
522
+
523
+ def processEnterStart (aliDock , args , argsAtStart ):
502
524
created = False
503
525
if not aliDock .isRunning ():
504
526
created = True
@@ -514,6 +536,10 @@ def processEnterStart(aliDock, args):
514
536
515
537
LOG .info ("Creating container, hold on" )
516
538
aliDock .run ()
539
+ else :
540
+ # Container is running. Check if user has specified parameters that will be ignored and warn
541
+ checkArgsAtStart (args , argsAtStart )
542
+
517
543
if args .action == "enter" :
518
544
if (args .tmux or args .tmuxControl ) and os .environ .get ("TMUX" ) is None :
519
545
LOG .info ("Resuming tmux session in the container" )
@@ -550,7 +576,7 @@ def processStop(aliDock):
550
576
LOG .info ("Shutting down the container" )
551
577
aliDock .stop ()
552
578
553
- def processActions (args ):
579
+ def processActions (args , argsAtStart ):
554
580
555
581
if args .version :
556
582
ver = str (require (__package__ )[0 ].version )
@@ -578,7 +604,7 @@ def processActions(args):
578
604
LOG .warning ("Cannot check for alidock updates this time" )
579
605
580
606
if args .action in ["enter" , "exec" , "root" , "start" ]:
581
- processEnterStart (aliDock , args )
607
+ processEnterStart (aliDock , args , argsAtStart )
582
608
elif args .action == "status" :
583
609
processStatus (aliDock )
584
610
elif args .action == "stop" :
0 commit comments