-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.py
executable file
·349 lines (306 loc) · 10.5 KB
/
init.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/python3
#
#
import os
import sys
import glob
import json
import copy
import importlib
from systems import ubuntu
from systems import centos
from systems import opensuse
from systems import freebsd
from systems import openbsd
from systems import openwrt
from systems import windows
from systems import osx
from targets import libvirt
from targets import docker
from targets import qemu
servicenames = []
for servicefile in glob.glob("services/*.py"):
servicename = servicefile.split("/")[1].split(".")[0]
print(f"importing: {servicename}")
exec(f"from services import {servicename}")
servicenames.append(servicename)
bootserver = "192.168.122.1"
def deepupdate(target, src):
for k, v in src.items():
if k in target:
for k2, v2 in src[k].items():
if k2 in target[k]:
target[k][k2]+=v2
else:
target[k][k2] = v2
else:
target[k] = copy.deepcopy(v)
def load_hostfile(hostfile):
hostdata = {}
with open(hostfile) as json_file:
hostdata_tmp = json.load(json_file)
hostdata_tmp["hostid"] = hostfile.split("/")[-1].split(".")[0]
if "includes" in hostdata_tmp:
for include in hostdata_tmp["includes"]:
with open(include) as json_inc:
hostdata_inc = json.load(json_inc)
deepupdate(hostdata, hostdata_inc)
deepupdate(hostdata, hostdata_tmp)
print (hostdata)
return hostdata
force = False
test = False
csv = False
cmkbulk = False
inventory = False
info = False
menu = False
hostfiles = []
if len(sys.argv) == 1:
print("USAGE: init.py [-f|-i|-t|-c|-m] JSONFILE [JSONFILE ...]")
exit(1)
else:
for argv in sys.argv[1:]:
if argv == "-t":
test = True
elif argv == "-f":
force = True
elif argv == "-c":
csv = True
elif argv == "-m":
menu = True
elif argv == "--cmkbulk":
cmkbulk = True
elif argv == "--inventory":
inventory = True
elif argv == "-i":
info = True
elif argv == "-p":
## copy tftp-files ##
os.system("mkdir -p /var/lib/tftpboot/")
os.system("cp -a files/pxe/* /var/lib/tftpboot/")
isolinuxtxtnet = ""
isolinuxtxtnet += "UI vesamenu.c32\n"
isolinuxtxtnet += "MENU INCLUDE graphics.conf\n"
isolinuxtxtnet += "MENU TITLE Installer\n"
isolinuxtxtnet += "PROMPT 0\n"
isolinuxtxtnet += "TIMEOUT 0\n"
isolinuxtxtnet += "\n"
isolinuxtxtnet += "DEFAULT main\n"
isolinuxtxtnet += "\n"
isolinuxtxtnet += "LABEL main\n"
isolinuxtxtnet += " MENU LABEL Return to Main Menu...\n"
isolinuxtxtnet += " KERNEL vesamenu.c32\n"
isolinuxtxtnet += " APPEND pxelinux.cfg/default\n"
isolinuxtxtnet += "\n"
isolinuxtxtnet += "MENU SEPARATOR\n"
isolinuxtxtnet += "\n"
isolinuxtxtnet += ubuntu.pxe(bootserver)
isolinuxtxtnet += centos.pxe(bootserver)
isolinuxtxtnet += opensuse.pxe(bootserver)
isolinuxtxtnet += windows.pxe(bootserver)
isolinuxtxtnet += freebsd.pxe(bootserver)
isolinuxtxtnet += openbsd.pxe(bootserver)
isolinuxtxtnet += openwrt.pxe(bootserver)
print(isolinuxtxtnet)
with open("/var/lib/tftpboot/install.conf", "w") as ofile:
ofile.write(isolinuxtxtnet)
exit(0)
else:
hostfiles.append(argv)
if not os.path.exists("temp"):
os.mkdir("temp")
if menu == True:
import whiptail
whip = whiptail.Whiptail("Testnetz", "Host-Select", 20, 80)
menu = []
mn = 1
for hostfile in glob.glob("hosts/*.json"):
hostdata = load_hostfile(hostfile)
menu.append([str(mn), hostdata["hostname"]])
mn = mn + 1
ret = whip.menu("Init Host", menu)
mn = 1
for hostfile in glob.glob("hosts/*.json"):
if int(ret) == int(mn):
print (hostfile)
force = True
hostfiles.append(hostfile)
mn = mn + 1
if csv == True:
for hostfile in hostfiles:
hostdata = load_hostfile(hostfile)
for dev in hostdata["network"]["interfaces"]:
for ipv4 in hostdata["network"]["interfaces"][dev]["ipv4"]:
print(hostfile + ";" + hostdata["hostname"] + ";" + hostdata["os"] + ";" + dev + ";" + ipv4["address"] + ";" + ipv4["netmask"])
elif cmkbulk == True:
print("hostname;ip address;agent")
for hostfile in hostfiles:
hostdata = load_hostfile(hostfile)
exist = False
for dev in hostdata["network"]["interfaces"]:
for ipv4 in hostdata["network"]["interfaces"][dev]["ipv4"]:
if exist == False:
print(hostdata["hostname"] + ";" + ipv4["address"] + ";cmk-agent")
exist = True
elif inventory == True:
oss = {}
for hostfile in hostfiles:
hostdata = load_hostfile(hostfile)
oss[hostdata["os"]] = hostdata["os"]
for os in oss:
print("[" + os + "]")
for hostfile in hostfiles:
hostdata = load_hostfile(hostfile)
exist = False
if hostdata["os"] == os:
for dev in hostdata["network"]["interfaces"]:
for ipv4 in hostdata["network"]["interfaces"][dev]["ipv4"]:
if exist == False:
if hostdata["os"] == "centos" or hostdata["os"] == "ubuntu" or hostdata["os"] == "debian" or hostdata["os"] == "opensuse":
print(hostdata["hostname"] + " ansible_host=" + ipv4["address"] + " ansible_python_interpreter=/usr/bin/python3")
elif hostdata["os"] == "windows":
print(hostdata["hostname"] + " ansible_host=" + ipv4["address"] + " ansible_user=administrator ansible_password=admin ansible_port=5986 ansible_connection=winrm ansible_winrm_server_cert_validation=ignore")
elif hostdata["os"] == "osx":
print(hostdata["hostname"] + " ansible_host=" + ipv4["address"] + " ansible_python_interpreter=/usr/bin/python ansible_user=oliver")
elif hostdata["os"] == "openbsd" or hostdata["os"] == "freebsd":
print(hostdata["hostname"] + " ansible_host=" + ipv4["address"] + " ansible_python_interpreter=/usr/local/bin/python3")
else:
print(hostdata["hostname"] + " ansible_host=" + ipv4["address"] + "")
exist = True
print("")
elif test == True:
ips = {}
macs = {}
names = {}
test = True
for hostfile in hostfiles:
print(hostfile)
hostdata = load_hostfile(hostfile)
if "hostname" not in hostdata:
print("Hostname not found")
test = False
else:
print(" Hostname: " + hostdata["hostname"])
if hostdata["hostname"] in names:
print(" # Hostname allready exist: " + names[hostdata["hostname"]])
test = False
else:
names[hostdata["hostname"]] = hostfile
if "network" not in hostdata:
print(" # Network section not found")
test = False
else:
if "interfaces" not in hostdata["network"]:
print(" # Interfaces section not found")
test = False
else:
for dev in hostdata["network"]["interfaces"]:
print(" network-interface: " + dev)
if "hwaddr" not in hostdata["network"]["interfaces"][dev]:
print(" # hwaddr not found in interface " + dev)
#test = False
else:
print(" hwaddr: " + hostdata["network"]["interfaces"][dev]["hwaddr"])
if hostdata["network"]["interfaces"][dev]["hwaddr"] in macs:
print(" # hwaddr allready exist: " + macs[hostdata["network"]["interfaces"][dev]["hwaddr"]])
test = False
else:
macs[hostdata["network"]["interfaces"][dev]["hwaddr"]] = hostfile
if "ipv4" not in hostdata["network"]["interfaces"][dev]:
print(" # ipv4 section not found")
test = False
else:
for ipv4 in hostdata["network"]["interfaces"][dev]["ipv4"]:
if "address" not in ipv4:
print(" # no address in ipv4")
test = False
else:
print(" ipv4: " + ipv4["address"])
if ipv4["address"] in ips:
print(" # ip allready exist: " + ips[ipv4["address"]])
test = False
else:
ips[ipv4["address"]] = hostfile
print("")
if test == True:
print("All Right")
else:
print("Found errors")
print("")
else:
for hostfile in hostfiles:
hostdata = load_hostfile(hostfile)
tempdir = "temp/" + hostdata["hostname"]
if "bootserver" not in hostdata:
hostdata["bootserver"] = bootserver
## set diskimage names ##
if hostdata["target"] == "libvirt" or hostdata["target"] == "pxe":
diskimages = libvirt.diskimages_get(hostdata, tempdir)
elif hostdata["target"] == "qemu":
diskimages = qemu.diskimages_get(hostdata, tempdir)
elif hostdata["target"] == "docker":
diskimages = docker.diskimages_get(hostdata, tempdir)
## show info ##
if info == True:
if hostdata["target"] == "libvirt" or hostdata["target"] == "pxe":
libvirt.info(hostdata, tempdir)
elif hostdata["target"] == "docker":
docker.info(hostdata, tempdir)
continue
print("hostfile: " + hostfile)
print(" hostname: " + hostdata["hostname"])
## remove old temp files and images ##
if force == True:
print(" remove old temp files and images")
if os.path.exists(tempdir):
print(" rm -rf 'temp/" + hostdata["hostname"] + "'")
os.system("rm -rf 'temp/" + hostdata["hostname"] + "'")
for disk in diskimages:
if os.path.exists(diskimages[disk]):
print(" rm -rf '" + diskimages[disk] + "'")
os.system("rm -rf '" + diskimages[disk] + "'")
## create temp dir ##
if not os.path.exists("isoimages"):
os.mkdir("isoimages")
if not os.path.exists("libvirt"):
os.mkdir("libvirt")
if not os.path.exists("libvirt/images"):
os.mkdir("libvirt/images")
if not os.path.exists(tempdir):
os.mkdir(tempdir)
## create diskimages ##
if hostdata["target"] == "libvirt" or hostdata["target"] == "pxe":
diskimages = libvirt.diskimages_create(hostdata, tempdir)
elif hostdata["target"] == "qemu":
diskimages = qemu.diskimages_create(hostdata, tempdir)
## create service installer scripts ##
services = []
if "services" in hostdata:
if not os.path.exists(tempdir + "/services"):
os.mkdir(tempdir + "/services")
for servicename in servicenames:
exec(f"services.append({servicename}.setup(hostdata, '{tempdir}/services'))")
## create autoinstaller ##
print(" create autoinstaller for " + hostdata["os"] + "")
if hostdata["os"] == "ubuntu" or hostdata["os"] == "debian":
ubuntu.autoseed(hostdata, tempdir, services)
elif hostdata["os"] == "centos" or hostdata["os"] == "fedora":
centos.autoseed(hostdata, tempdir, services)
elif hostdata["os"] == "opensuse":
opensuse.autoseed(hostdata, tempdir, services)
elif hostdata["os"] == "windows":
windows.autoseed(hostdata, tempdir, services)
elif hostdata["os"] == "freebsd":
freebsd.autoseed(hostdata, tempdir, services)
elif hostdata["os"] == "openbsd":
openbsd.autoseed(hostdata, tempdir, services)
elif hostdata["os"] == "openwrt":
openwrt.autoseed(hostdata, tempdir, services)
## boot guest system ##
if hostdata["target"] == "libvirt" or hostdata["target"] == "pxe":
libvirt.boot(hostdata, tempdir, force)
elif hostdata["target"] == "docker":
docker.boot(hostdata, tempdir, force)
elif hostdata["target"] == "qemu":
qemu.boot(hostdata, tempdir, force)