diff options
Diffstat (limited to 'shared.py')
-rw-r--r-- | shared.py | 52 |
1 files changed, 32 insertions, 20 deletions
@@ -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 |