summaryrefslogtreecommitdiffstats
path: root/createAndExportDisk.py
blob: ffa71ca5af588a395ddd49719d41156ce8d9a243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python3
#coding: UTF-8
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))

def logErr(msg):
    print("\033[1;31mERROR\033[0m " + str(msg))

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
# -----------------------------------------------------------------------------

# vars
volumeGroupName = 'scsiRaid'
defaultDiskSize = '3G'

# configure known options
usage = "usage: %prog VMNAME [options]"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-s", "--size", dest="size", default=defaultDiskSize,
                    help="SIZE of vm root disk", metavar="SIZE")
parser.add_option("-v", "--verbose", dest="verbose",
                    action="store_true",  default=False,
                    help="should I do output")
(options, args) = parser.parse_args()

# check if required args are given
if len(args) < 1:
    parser.print_usage()
    logErr("a vm-name must be given!")
    sys.exit(2)
elif len(args) > 1:
    parser.print_usage()
    logErr("to many arguments given!")
    sys.exit(2)

vmName = parseVmName(args[0])
diskSize, diskSizeUnit = parseDiskSize(options.size)

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. SSH to (pang|peng).spline.de an install your vm there!")
sys.exit(0)