@@ -40,6 +40,7 @@ def __init__(self, filename=None, name=None, env_file=None):
4040 self .load ()
4141 self .parse ()
4242 self .env_file = env_file
43+ self .client = get_client ()
4344
4445# Names
4546
@@ -82,6 +83,21 @@ def set_name(self, name):
8283
8384# Listing
8485
86+ def ps (self ):
87+ '''ps will print a table of instances, including pids and names.
88+ '''
89+ instance_names = self .get_instance_names ()
90+ table = []
91+ for instance in self .client .instances (quiet = True , sudo = self .sudo ):
92+ if instance .name in instance_names :
93+ image = os .path .basename (instance ._image )
94+ table .append ([instance .name .rjust (12 ),
95+ instance .pid ,
96+ image ])
97+
98+ bot .custom (prefix = "INSTANCES " , message = "NAME PID IMAGE" ,color = "CYAN" )
99+ bot .table (table )
100+
85101 def iter_instances (self , names ):
86102 '''yield instances one at a time. If an invalid name is given,
87103 exit with error.
@@ -116,21 +132,42 @@ def load(self):
116132
117133 def parse (self ):
118134 '''parse a loaded config'''
135+
136+ # If a port is defined, we need root.
137+ self .sudo = False
138+
119139 if self .config is not None :
120140
141+ # If any of config has ports, must use sudo for networking
142+ for name in self .config .get ('instances' , []):
143+ params = self .config ['instances' ][name ]
144+ if "ports" in params :
145+ self .sudo = True
146+
121147 # Create each instance object
122148 for name in self .config .get ('instances' , []):
123149 params = self .config ['instances' ][name ]
124150
125151 # Validates params
126152 self .instances [name ] = Instance (name = name ,
127153 params = params ,
154+ sudo = self .sudo ,
128155 working_dir = self .working_dir )
129156
130157 # Update volumes with volumes from
131158 for _ , instance in self .instances .items ():
132159 instance .set_volumes_from (self .instances )
133-
160+
161+ # Commands
162+
163+ def shell (self , name ):
164+ '''if an instance exists, shell into it.
165+ '''
166+ if self .instances :
167+ if name in self .instances :
168+ instance = self .instances [name ]
169+ if instance .exists ():
170+ self .client .shell (instance .instance .get_uri ())
134171
135172# Config
136173
@@ -156,18 +193,18 @@ def down(self, names):
156193
157194# Create
158195
159- def create (self , names ):
196+ def create (self , names , writable_tmpfs = False ):
160197 '''call the create function, which defaults to the command instance.create()
161198 '''
162- return self ._create (names )
199+ return self ._create (names , writable_tmpfs = writable_tmpfs )
163200
164- def up (self , names ):
201+ def up (self , names , writable_tmpfs = False ):
165202 '''call the up function, instance.up(), which will build before if
166203 a container binary does not exist.
167204 '''
168- return self ._create (names , command = "up" )
205+ return self ._create (names , command = "up" , writable_tmpfs = writable_tmpfs )
169206
170- def _create (self , names , command = "create" ):
207+ def _create (self , names , command = "create" , writable_tmpfs = False ):
171208 '''create one or more instances. "Command" determines the sub function
172209 to call for the instance, which should be "create" or "up".
173210 If the user provide a list of names, use them, otherwise default
@@ -177,6 +214,7 @@ def _create(self, names, command="create"):
177214 ==========
178215 names: the instance names to create
179216 command: one of "create" or "up"
217+ writable_tmpfs: if the instances should be given writable to tmp
180218 '''
181219 # If no names provided, we create all
182220 if not names :
@@ -198,7 +236,7 @@ def _create(self, names, command="create"):
198236 continue
199237
200238 # If we get here, execute command and add to list
201- getattr (instance , command )(self .working_dir )
239+ getattr (instance , command )(self .working_dir , writable_tmpfs )
202240 created .append (instance .name )
203241 names .remove (instance .name )
204242
0 commit comments