MCPcopy Create free account
hub / github.com/apache/cloudstack / bash

Class bash

python/lib/cloudutils/utilities.py:23–78  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21import sys
22import os
23class bash:
24 def __init__(self, args, timeout=600):
25 self.args = args
26 logging.debug("execute:%s"%args)
27 self.timeout = timeout
28 self.process = None
29 self.success = False
30 self.run()
31
32 def run(self):
33 class Alarm(Exception):
34 pass
35 def alarm_handler(signum, frame):
36 raise Alarm
37
38 try:
39 self.process = Popen(self.args, shell=True, stdout=PIPE, stderr=PIPE)
40 if self.timeout != -1:
41 signal(SIGALRM, alarm_handler)
42 alarm(self.timeout)
43
44 try:
45 self.stdout, self.stderr = self.process.communicate()
46 if self.timeout != -1:
47 alarm(0)
48 except Alarm:
49 os.kill(self.process.pid, SIGKILL)
50 raise CloudRuntimeException("Timeout during command execution")
51
52 self.success = self.process.returncode == 0
53 except:
54 raise CloudRuntimeException(formatExceptionInfo())
55
56 if not self.success:
57 logging.debug("Failed to execute:" + self.getErrMsg())
58
59 def isSuccess(self):
60 return self.success
61
62 def getStdout(self):
63 return self.stdout.decode('utf-8').strip('\n')
64
65 def getLines(self):
66 return self.stdout.decode('utf-8').strip('\n').split('\n')
67
68 def getStderr(self):
69 return self.stderr.decode('utf-8').strip('\n')
70
71 def getErrMsg(self):
72 if self.isSuccess():
73 return ""
74
75 if self.getStderr() is None or self.getStderr() == "":
76 return self.getStdout()
77 else:
78 return self.getStderr()
79
80def initLoging(logFile=None):

Callers 15

getDefaultNetworkMethod · 0.85
createBridgeMethod · 0.85
isBridgeSupportedMethod · 0.85
isOvsBridgeMethod · 0.85
getDevInfoMethod · 0.85
executeFromFileMethod · 0.85
checkHostNameMethod · 0.85
configMethod · 0.85
__init__Method · 0.85
isServiceRunningMethod · 0.85
stopServiceMethod · 0.85
disableServiceMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected