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

def openLogFile(comment=sys.argv[0]):
    logFile = open("/tmp/" + comment + ".log","a")
    logFile.write("# ### Starting new run \"" + comment + "\" ### #\r\n")
    logFile.flush()
    return logFile

def execute(cmd, logFile=None):
    # 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 ProgressLogger:

    def __init__(self, totalSteps, steps=[]):
        self.totalSteps = totalSteps
        self.progress=1
        self.steps=steps

    def start(self, step=None):
        if(step == None):
            step = self.steps.pop()

        print("[" + str(self.progress) + "/" + str(self.totalSteps)
                + "] excuting: " + step, end="")

        self.current=step

    def done(self):
        print("\r[" + str(self.progress) + "/" + str(self.totalSteps) + "] excuted: "
                + self.current + "  ")
        self.progress = self.progress + 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))


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)