1818
1919
2020from spython .logger import bot
21- from spython .utils import run_command
21+ from spython .utils import run_command , check_install
2222
23-
24- def instances (self ):
23+ def instances (self , name = None , return_json = False , quiet = False ):
2524 '''list instances. For Singularity, this is provided as a command sub
2625 group.
2726
@@ -30,33 +29,92 @@ def instances(self):
3029 Return codes provided are different from standard linux:
3130 see https://github.com/singularityware/singularity/issues/1706
3231
32+ Parameters
33+ ==========
34+ return_json: return a json list of instances instead of objects (False)
35+ name: if defined, return the list for just one instance (used to ged pid)
36+
3337 Return Code -- Reason
3438 0 -- Instances Found
3539 1 -- No Instances, libexecdir value not found, functions file not found
3640 255 -- Couldn't get UID
3741
3842 '''
43+ from spython .instance .cmd .iutils import parse_table
44+ self ._check_install ()
45+ cmd = self ._init_command ('instance.list' )
3946
40- self .check_install ()
47+ # If the user has provided a name, we want to see a particular instance
48+ if name is not None :
49+ cmd .append (name )
4150
42- cmd = self ._init_command ('instance.list' )
4351 output = run_command (cmd , quiet = True )
4452 instances = None
4553
4654 # Success, we have instances
4755
4856 if output ['return_code' ] == 0 :
49- print ('' .join (output ['message' ]))
50- instances = output ['message' ][1 :]
57+
58+ # Only print the table if we are returning json
59+ if quiet is False :
60+ print ('' .join (output ['message' ]))
61+
62+ # Prepare json result from table
63+
64+ header = ['daemon_name' ,'pid' ,'container_image' ]
65+ instances = parse_table (output ['message' ][0 ], header )
66+
67+ # Does the user want instance objects instead?
68+ listing = []
69+ if return_json is False :
70+ for i in instances :
71+
72+ new_instance = self .instance (pid = i ['pid' ],
73+ name = i ['daemon_name' ],
74+ image = i ['container_image' ],
75+ start = False )
76+
77+ listing .append (new_instance )
78+ instances = listing
5179
5280 # Couldn't get UID
5381
5482 elif output ['return_code' ] == 255 :
5583 bot .error ("Couldn't get UID" )
5684
57-
5885 # Return code of 0
5986 else :
6087 bot .info ('No instances found.' )
6188
89+ # If we are given a name, return just one
90+ if name is not None and instances is not None :
91+ if len (instances ) == 1 :
92+ instances = instances [0 ]
93+
6294 return instances
95+
96+
97+ def stopall (self , sudo = False , quiet = True ):
98+ '''stop ALL instances. This command is only added to the command group
99+ as it doesn't make sense to call from a single instance
100+
101+ Parameters
102+ ==========
103+ sudo: if the command should be done with sudo (exposes different set of
104+ instances)
105+
106+ '''
107+ from spython .utils import run_command , check_install
108+ check_install ()
109+
110+ cmd = self ._init_command ('instance.stop' )
111+ cmd = cmd + ['--all' ]
112+ output = run_command (cmd , sudo = sudo , quiet = quiet )
113+
114+ if output ['return_code' ] != 0 :
115+ message = '%s : return code %s' % (output ['message' ],
116+ output ['return_code' ])
117+ bot .error (message )
118+ return output ['return_code' ]
119+
120+ return output ['return_code' ]
0 commit comments