From 677649923e407111be95328ffda5af5e14441e0b Mon Sep 17 00:00:00 2001 From: yves Date: Thu, 15 Dec 2011 10:09:13 +0100 Subject: first approch to remove executionPlan class in favor of an execute function, to allow higher flexibility --- shared.py | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/shared.py b/shared.py index 31eaae5..487236f 100644 --- a/shared.py +++ b/shared.py @@ -10,14 +10,35 @@ def logInfo(msg): def logErr(msg): print("\033[1;31mERROR\033[0m " + str(msg)) +def openLogFile(comment) + logFile = open("/tmp/" + comment + ".log","a") + logFile.write("# ### Starting new run \"" + comment + "\" ### #\r\n") + logFile.flush() + return logFile + +def execute(cmd, logFile): + # safty anker subprocess.call(cmd) + try: + subprocess.call(['echo','#>>>'] + cmd,stdout=logFile) + ret = subprocess.call(['echo'] + cmd, stdout=logFile, stderr=logFile) + + if ret != 0: + print() + logErr( "\"" + cmd[0] + "\" returned error code: " + str(ret) + + ". Logged to " + logFile.name) + sys.exit(1) + + except OSError as e: + print() + logErr("failed to execute command \"" + cmd[0] + "\". Logged to " + + logFile.name + ". Error was: " + str(e)) + sys.exit(1) + class ExecutionPlan: def __init__(self, commands, comment = sys.argv[0]): self.commands = commands self.executed = [] - self.logFile = open("/tmp/" + comment + ".log","a") - self.logFile.write("# ### Starting new run \"" + comment + "\" ### #\r\n") - self.logFile.flush() def execute_all(self): for i in range(len(self.executed),len(self.commands)): @@ -28,29 +49,20 @@ class ExecutionPlan: print("\r[" + str(i+1) + "/" + str(total) + "]: excuted " + self.commands[i][0] + " ") - def execute(self, cmd): - # safty anker subprocess.call(cmd) - try: - subprocess.call(['echo','#>>>'] + cmd,stdout=self.logFile) - ret = subprocess.call(['echo'] + cmd, stdout=self.logFile, stderr=self.logFile) - if ret != 0: - print() - logErr( "\"" + cmd[0] + "\" returned error code: " + str(ret) + - ". Logged to " + self.logFile.name) - sys.exit(1) +def parseIp(rawString): + match = re.match('^130\.133\.110\.([0-9]{1,3})$',rawString) + if not match: + logErr('the ip ' + rawString + ' is no valid ip or not a adress from our subnet') + sys.exit(2) + + return rawString, int(match.group(1)) - except OSError as e: - print() - logErr("failed to execute command \"" + cmd[0] + "\". Logged to " - + self.logFile.name + ". Error was: " + str(e)) - sys.exit(1) - self.executed.append(cmd) def parseVmName(rawString): # valdiate vm-name if not re.match('^vm-[a-z]+[0-9]?(-[a-z])?$',rawString): - logErr('the name ' + rawString + ' is no valid vm-name'); + logErr('the name ' + rawString + ' is no valid vm-name') sys.exit(2) return rawString -- cgit v1.2.3-1-g7c22