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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#coding: UTF-8
import subprocess
import sys
import os
import re
def logInfo(msg):
print("\033[1;32mINFO\033[0m " + str(msg))
def logErr(msg,logFile=None):
print("\033[1;31mERROR\033[0m " + str(msg))
if(logFile):
logFile.write(str(msg))
logFile.flush()
class ExecRecepie:
def __init__(self, desc, command):
self.desc = desc
self.cmd = command
def execute(self, logFile=None, dry=False):
try:
logFile.write('excuting: ' + str(self.cmd))
logFile.flush()
if not dry:
ret = subprocess.call(['echo'] + cmd, stdout=logFile, stderr=logFile)
if ret != 0:
print()
logErr( "\"" + cmd[0] + "\" returned error code: " + str(ret), logFile)
except OSError as e:
print()
logErr("failed to execute command \"" + cmd[0] + "\". Logged to "
+ logFile.name + ". Error was: " + str(e))
sys.exit(1)
class ConfigRecepie:
def __init__(self, desc, directory, contentmap):
self.desc = desc
self.directory = directory
self.contentmap = contentmap
def execute(self, logFile=None, dry=False):
if( dry and not self.directory[:4] is "/tmp"):
self.directory = '/tmp/' + self.directory
try:
os.makedirs(self.directory)
except OSError as e:
logInfo(self.directory + " already there")
for (filename, content) in self.contentmap.items():
try:
fd = open(self.directory + '/' + filename,'w')
fd.write(content)
fd.close()
except IOError:
logErr('Unable to write file: ' + self.directory + filename)
sys.exit(2)
class FstabRecepie:
def __init__(self, desc, fstab, rootDisk):
self.desc = desc
self.fstab = fstab
self.rootDisk = rootDisk
def execute(self, logFile=None, dry=False):
if( dry):
self.fstab = '/tmp/' + self.fstab
with open(self.fstab, 'a') as fstab:
fstab.write(self.rootDisk + ' / ext4 defaults,relatime 0 1\n')
fstab.close()
class Executor:
def __init__(self, steps, comment=sys.argv[0]):
logFile = open("/tmp/" + comment + ".log","a")
logFile.write("# ### Starting new run \"" + comment + "\" ### #\r\n")
logFile.flush()
self.logFile = logFile
self.steps=steps
self.dry=True
def do(self):
totalSteps = len(self.steps)
progress=1
for step in self.steps:
print("[" + str(progress) + "/" + str(totalSteps)
+ "] excuting: " + step.desc, end="")
step.execute(self.logFile, self.dry)
print("\r[" + str(progress) + "/" + str(totalSteps) + "] excuted: "
+ step.desc + " ")
progress = 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)
|