@@ -129,6 +129,22 @@ def iter_instances(self, names):
129129 yield self .instances .get (name )
130130
131131
132+ def get_instance (self , name ):
133+ '''get a specifically named instance. We first check that the
134+ client has instances defined, and that the name we are looking
135+ for is also included. If not found, we return None.
136+
137+ Parameters
138+ ==========
139+ names: the name of instances to get. Must be valid
140+ '''
141+ instance = None
142+ if self .instances :
143+ if name in self .instances :
144+ instance = self .instances [name ]
145+ return instance
146+
147+
132148# Loading Functions
133149
134150 def load (self ):
@@ -249,11 +265,34 @@ def shell(self, name):
249265 ==========
250266 name: the name of the instance to shell into
251267 '''
252- if self .instances :
253- if name in self .instances :
254- instance = self .instances [name ]
255- if instance .exists ():
256- self .client .shell (instance .instance .get_uri (), sudo = self .sudo )
268+ instance = self .get_instance (name )
269+ if not instance :
270+ bot .exit ('Cannot find %s, is it up?' % name )
271+
272+ if instance .exists ():
273+ self .client .shell (instance .instance .get_uri (), sudo = self .sudo )
274+
275+
276+ def run (self , name ):
277+ '''if an instance exists, run it.
278+
279+ Parameters
280+ ==========
281+ name: the name of the instance to run
282+ '''
283+ instance = self .get_instance (name )
284+ if not instance :
285+ bot .exit ('Cannot find %s, is it up?' % name )
286+
287+ if instance .exists ():
288+ self .client .quiet = True
289+ result = self .client .run (instance .instance .get_uri (),
290+ sudo = self .sudo ,
291+ return_result = True )
292+
293+ if result ['return_code' ] != 0 :
294+ bot .exit ("Return code %s" % result ['return_code' ])
295+ print ('' .join ([x for x in result ['message' ] if x ]))
257296
258297
259298 def execute (self , name , commands ):
@@ -264,18 +303,20 @@ def execute(self, name, commands):
264303 name: the name of the instance to exec to
265304 commands: a list of commands to issue
266305 '''
267- if self .instances :
268- if name in self .instances :
269- instance = self .instances [name ]
270- if instance .exists ():
271- try :
272- for line in self .client .execute (instance .instance .get_uri (),
273- command = commands ,
274- stream = True ,
275- sudo = self .sudo ):
276- print (line , end = '' )
277- except subprocess .CalledProcessError :
278- bot .exit ('Command had non zero exit status.' )
306+ instance = self .get_instance (name )
307+ if not instance :
308+ bot .exit ('Cannot find %s, is it up?' % name )
309+
310+ if instance .exists ():
311+ try :
312+ for line in self .client .execute (instance .instance .get_uri (),
313+ command = commands ,
314+ stream = True ,
315+ sudo = self .sudo ):
316+ print (line , end = '' )
317+ except subprocess .CalledProcessError :
318+ bot .exit ('Command had non zero exit status.' )
319+
279320
280321# Logs
281322
0 commit comments