From aeb4f9729b8936405a5435119b9a5a13f5a03922 Mon Sep 17 00:00:00 2001 From: yves Date: Wed, 14 Dec 2011 00:06:44 +0100 Subject: added first executor version, error handling must be added --- createAndExportDisk.py | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/createAndExportDisk.py b/createAndExportDisk.py index 535300e..ffa71ca 100755 --- a/createAndExportDisk.py +++ b/createAndExportDisk.py @@ -3,6 +3,28 @@ import optparse import sys import re +import subprocess + +class ExecutionPlan: + + def __init__(self, commands): + self.commands = commands + self.executed = [] + self.logFile = open("/tmp/log","w") + self.todo = len(self.commands) + + def execute_all(self): + for i in range(len(self.executed),self.todo): + print("[" + str(i+1) + "/" + str(self.todo) + "]: excuting ",end="") + self.execute_next() + print("\r[" + str(i+1) + "/" + str(self.todo) + "]: excuted ") + + def execute_next(self): + cmd = self.commands.pop(0) + # safty anker subprocess.call(cmd) + subprocess.call(['echo','>>>'] + cmd,stdout=self.logFile) + subprocess.call(['echo'] + cmd,stdout=self.logFile,stderr=self.logFile) + self.executed.append(cmd) def logInfo(msg): print("\033[1;32mINFO\033[0m " + str(msg)) @@ -10,9 +32,6 @@ def logInfo(msg): def logErr(msg): print("\033[1;31mERROR\033[0m " + str(msg)) -def execute(command): - logInfo('would execute: ' + str(command)) - def parseVmName(rawString): # valdiate vm-name if not re.match('^vm-[a-z]+[0-9]?(-[a-z])?$',rawString): @@ -84,10 +103,15 @@ elif len(args) > 1: vmName = parseVmName(args[0]) diskSize, diskSizeUnit = parseDiskSize(options.size) -execute(["lvcreate", "-n", vmName, "-L", str(diskSize) + str(diskSizeUnit)]) -execute(["mkfs", "/dev/" + volumeGroupName + "/" + vmName, "-l",vmName]) -execute(["addIscsiDisk", vmName]) +cmds = [ + ["lvcreate", "-n", vmName, "-L", str(diskSize) + str(diskSizeUnit)], + ["mkfs", "/dev/" + volumeGroupName + "/" + vmName, "-l",vmName], + ["addIscsiDisk", vmName] +] + +execPlan = ExecutionPlan(cmds) +execPlan.execute_all() -logInfo("all done. Proceed to {pang,peng} an install your vm there!") +logInfo("all done. SSH to (pang|peng).spline.de an install your vm there!") sys.exit(0) -- cgit v1.2.3-1-g7c22