diff options
author | yves <steve.harrison@gmx.net> | 2011-12-15 10:09:13 +0100 |
---|---|---|
committer | yves <steve.harrison@gmx.net> | 2011-12-15 10:09:13 +0100 |
commit | 677649923e407111be95328ffda5af5e14441e0b (patch) | |
tree | 6826b271609c99d6426a4e26a61cc90fee5e6119 | |
parent | 34b1f974c2e6a134efc95c400e45307fbb600612 (diff) | |
download | iscsi-helper-677649923e407111be95328ffda5af5e14441e0b.tar.gz iscsi-helper-677649923e407111be95328ffda5af5e14441e0b.tar.bz2 iscsi-helper-677649923e407111be95328ffda5af5e14441e0b.zip |
first approch to remove executionPlan class in favor of an execute function, to allow higher flexibility
-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 |