diff options
author | yves <steve.harrison@gmx.net> | 2011-12-14 11:28:58 +0100 |
---|---|---|
committer | yves <steve.harrison@gmx.net> | 2011-12-14 11:28:58 +0100 |
commit | f43581c477ddfaf7cb2cb9f9399a6520382899ba (patch) | |
tree | 109783b8292a8e8dab858668eadaf9c6c34f1266 | |
parent | 833de33f1849e5107252324641796e48bc04522f (diff) | |
download | iscsi-helper-f43581c477ddfaf7cb2cb9f9399a6520382899ba.tar.gz iscsi-helper-f43581c477ddfaf7cb2cb9f9399a6520382899ba.tar.bz2 iscsi-helper-f43581c477ddfaf7cb2cb9f9399a6520382899ba.zip |
refactored libary code out
-rwxr-xr-x | createAndExportDisk.py | 92 | ||||
-rw-r--r-- | shared.py | 88 |
2 files changed, 89 insertions, 91 deletions
diff --git a/createAndExportDisk.py b/createAndExportDisk.py index f10a5a8..30093e6 100755 --- a/createAndExportDisk.py +++ b/createAndExportDisk.py @@ -1,97 +1,7 @@ #!/usr/bin/python3 #coding: UTF-8 import optparse -import sys -import re -import subprocess -import random - -def logInfo(msg): - print("\033[1;32mINFO\033[0m " + str(msg)) - -def logErr(msg): - print("\033[1;31mERROR\033[0m " + str(msg)) - -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)): - total = len(self.commands) - print("[" + str(i+1) + "/" + str(total) + "]: excuting " - + self.commands[i][0],end="") - self.execute(self.commands[i]) - 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) - - 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'); - sys.exit(2) - - return rawString - -def parseDiskSize(rawString): - # valdiate disk size - diskSizeUnit = 'G' - diskSize = 0 - - match = re.match('^([0-9]+)([GgMm])?$',rawString) - - if not match: - logErr('the name ' + raw + ' is no a valid disk size <number>(G|M)?'); - sys.exit(2) - - try: - diskSizeUnit = match.group(2) - diskSize = int(match.group(1)) - except ValueError: - logErr('the size ' + options.size + ' is not a valid disk size'); - sys.exit(2) - - if not diskSizeUnit: - diskSizeUnit = 'G' - logInfo("interpreteing disk size in gigbyte magnitude") - - if diskSizeUnit == 'm' or diskSizeUnit == 'M': - if not(1024 < diskSize and diskSize < 500000): - logErr(' Syrsly? ' + str(diskSize) + ' megabytes?') - sys.exit(2) - else: - if not(1 < diskSize and diskSize < 500): - logErr(' Syrsly? ' + str(diskSize) + ' gigabytes?') - sys.exit(2) - return (diskSize, diskSizeUnit) - -# ----------------------------------------------------------------------------- -# Functionality starts here, before only libary code -# ----------------------------------------------------------------------------- +from shared import * # vars volumeGroupName = 'scsiRaid' diff --git a/shared.py b/shared.py new file mode 100644 index 0000000..31eaae5 --- /dev/null +++ b/shared.py @@ -0,0 +1,88 @@ +#!/usr/bin/python3 +#coding: UTF-8 +import subprocess +import sys +import re + +def logInfo(msg): + print("\033[1;32mINFO\033[0m " + str(msg)) + +def logErr(msg): + print("\033[1;31mERROR\033[0m " + str(msg)) + +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)): + total = len(self.commands) + print("[" + str(i+1) + "/" + str(total) + "]: excuting " + + self.commands[i][0],end="") + self.execute(self.commands[i]) + 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) + + 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'); + sys.exit(2) + + return rawString + +def parseDiskSize(rawString): + # valdiate disk size + diskSizeUnit = 'G' + diskSize = 0 + + match = re.match('^([0-9]+)([GgMm])?$',rawString) + + if not match: + logErr('the name ' + raw + ' is no a valid disk size <number>(G|M)?'); + sys.exit(2) + + try: + diskSizeUnit = match.group(2) + diskSize = int(match.group(1)) + except ValueError: + logErr('the size ' + options.size + ' is not a valid disk size'); + sys.exit(2) + + if not diskSizeUnit: + diskSizeUnit = 'G' + logInfo("interpreteing disk size in gigbyte magnitude") + + if diskSizeUnit == 'm' or diskSizeUnit == 'M': + if not(1024 < diskSize and diskSize < 500000): + logErr(' Syrsly? ' + str(diskSize) + ' megabytes?') + sys.exit(2) + else: + if not(1 < diskSize and diskSize < 500): + logErr(' Syrsly? ' + str(diskSize) + ' gigabytes?') + sys.exit(2) + return (diskSize, diskSizeUnit) |