summaryrefslogtreecommitdiffstats
path: root/shared.py
blob: 31eaae58e8d3ed0f5a19ca643588937e01897171 (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
#!/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)