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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
from resource_inventory.models import (
ResourceTemplate,
Image,
Server,
ResourceBundle,
ResourceProfile,
InterfaceProfile,
PhysicalNetwork,
ResourceConfiguration,
NetworkConnection,
InterfaceConfiguration,
Network,
DiskProfile,
CpuProfile,
RamProfile,
Interface
)
import json
from django.contrib.auth.models import User
from account.models import (
Lab,
PublicNetwork
)
from resource_inventory.resource_manager import ResourceManager
from resource_inventory.pdf_templater import PDFTemplater
from booking.quick_deployer import update_template
from datetime import timedelta
from django.utils import timezone
from booking.models import Booking
from notifier.manager import NotificationHandler
from api.models import JobFactory
from api.models import JobStatus
def print_div():
print("====================================================================")
def book_host(owner_username, host_labid, lab_username, hostname, image_id, template_name, length_days=21, collaborator_usernames=[], purpose="internal", project="LaaS"):
"""
creates a quick booking using the given host
"""
lab = Lab.objects.get(lab_user__username=lab_username)
host = Server.objects.filter(lab=lab).get(labid=host_labid)
if host.booked:
print("Can't book host, already marked as booked")
return
else:
host.booked = True
host.save()
template = ResourceTemplate.objects.filter(public=True).get(name=template_name)
image = Image.objects.get(id=image_id)
owner = User.objects.get(username=owner_username)
new_template = update_template(template, image, hostname, owner)
rmanager = ResourceManager.getInstance()
vlan_map = rmanager.get_vlans(new_template)
# only a single host so can reuse var for iter here
resource_bundle = ResourceBundle.objects.create(template=new_template)
res_configs = new_template.getConfigs()
for config in res_configs:
try:
host.bundle = resource_bundle
host.config = config
rmanager.configureNetworking(resource_bundle, host, vlan_map)
host.save()
except Exception:
host.booked = False
host.save()
print("Failed to book host due to error configuring it")
return
new_template.save()
booking = Booking.objects.create(
purpose=purpose,
project=project,
lab=lab,
owner=owner,
start=timezone.now(),
end=timezone.now() + timedelta(days=int(length_days)),
resource=resource_bundle,
opnfv_config=None
)
booking.pdf = PDFTemplater.makePDF(booking)
booking.save()
for collaborator_username in collaborator_usernames:
try:
user = User.objects.get(username=collaborator_username)
booking.collaborators.add(user)
except Exception:
print("couldn't add user with username ", collaborator_username)
booking.save()
JobFactory.makeCompleteJob(booking)
NotificationHandler.notify_new_booking(booking)
def mark_working(host_labid, lab_username, working=True):
lab = Lab.objects.get(lab_user__username=lab_username)
server = Server.objects.filter(lab=lab).get(labid=host_labid)
print("changing server working status from ", server.working, "to", working)
server.working = working
server.save()
def mark_booked(host_labid, lab_username, booked=True):
lab = Lab.objects.get(lab_user__username=lab_username)
server = Server.objects.filter(lab=lab).get(labid=host_labid)
print("changing server booked status from ", server.booked, "to", booked)
server.booked = booked
server.save()
# returns host filtered by lab and then unique id within lab
def get_host(host_labid, lab_username):
lab = Lab.objects.get(lab_user__username=lab_username)
return Server.objects.filter(lab=lab).get(labid=host_labid)
def get_info(host_labid, lab_username):
info = {}
host = get_host(host_labid, lab_username)
info['host_labid'] = host_labid
info['booked'] = host.booked
info['working'] = host.working
info['profile'] = str(host.profile)
if host.bundle:
binfo = {}
info['bundle'] = binfo
if host.config:
cinfo = {}
info['config'] = cinfo
return info
def map_cntt_interfaces(labid: str):
"""
Use this during cntt migrations, call it with a host labid and it will change profiles for this host
as well as mapping its interfaces across. interface ens1f2 should have the mac address of interface eno50
as an invariant before calling this function
"""
host = get_host(labid, "unh_iol")
host.profile = ResourceProfile.objects.get(name="HPE x86 CNTT")
host.save()
host = get_host(labid, "unh_iol")
for iface in host.interfaces.all():
new_ifprofile = None
if iface.profile.name == "ens1f2":
new_ifprofile = InterfaceProfile.objects.get(host=host.profile, name="eno50")
else:
new_ifprofile = InterfaceProfile.objects.get(host=host.profile, name=iface.profile.name)
iface.profile = new_ifprofile
iface.save()
def detect_leaked_hosts(labid="unh_iol"):
"""
Use this to try to detect leaked hosts.
These hosts may still be in the process of unprovisioning,
but if they are not (or unprovisioning is frozen) then
these hosts are instead leaked
"""
working_servers = Server.objects.filter(working=True, lab__lab_user__username=labid)
booked = working_servers.filter(booked=True)
filtered = booked
print_div()
print("In use now:")
for booking in Booking.objects.filter(end__gte=timezone.now()):
res_for_booking = booking.resource.get_resources()
print(res_for_booking)
for resource in res_for_booking:
filtered = filtered.exclude(id=resource.id)
print_div()
print("Possibly leaked:")
for host in filtered:
print(host)
print_div()
return filtered
def booking_for_host(host_labid: str, labid="unh_iol"):
server = Server.objects.get(lab__lab_user__username=labid, labid=host_labid)
booking = server.bundle.booking_set.first()
print_div()
print(booking)
print("id:", booking.id)
print("owner:", booking.owner)
print("job (id):", booking.job, "(" + str(booking.job.id) + ")")
print_div()
return booking
def force_release_booking(booking_id):
booking = Booking.objects.get(id=booking_id)
job = booking.job
tasks = job.get_tasklist()
for task in tasks:
task.status = JobStatus.DONE
task.save()
def free_leaked_public_vlans(safety_buffer_days=2):
for lab in Lab.objects.all():
current_booking_set = Booking.objects.filter(end__gte=timezone.now() + timedelta(days=safety_buffer_days))
marked_nets = set()
for booking in current_booking_set:
for network in get_network_metadata(booking.id):
marked_nets.add(network["vlan_id"])
for net in PublicNetwork.objects.filter(lab=lab).filter(in_use=True):
if net.vlan not in marked_nets:
lab.vlan_manager.release_public_vlan(net.vlan)
def get_network_metadata(booking_id: int):
booking = Booking.objects.get(id=booking_id)
bundle = booking.resource
pnets = PhysicalNetwork.objects.filter(bundle=bundle).all()
metadata = {}
for pnet in pnets:
net = pnet.generic_network
mdata = {"vlan_id": pnet.vlan_id, "netname": net.name, "public": net.is_public}
metadata[net.name] = mdata
return metadata
def print_dict_pretty(a_dict):
print(json.dumps(a_dict, sort_keys=True, indent=4))
"""
schema:
{
"name": str
"description": str
"labs": [
str (lab username)
]
"disks": {
<diskname> : {
capacity: int (GiB)
media_type: str ("SSD" or "HDD")
interface: str ("sata", "sas", "ssd", "nvme", "scsi", or "iscsi")
}
}
interfaces: {
<intname>: {
"speed": int (mbit)
"nic_type": str ("onboard" or "pcie")
"order": int (compared to the other interfaces, indicates the "order" that the ports are laid out)
}
}
cpus: {
cores: int (hardware threads count)
architecture: str (x86_64" or "aarch64")
cpus: int (number of sockets)
cflags: str
}
ram: {
amount: int (GiB)
channels: int
}
}
"""
def add_profile(data):
base_profile = ResourceProfile.objects.create(name=data['name'], description=data['description'])
base_profile.save()
for lab_username in data['labs']:
lab = Lab.objects.get(lab_user__username=lab_username)
base_profile.labs.add(lab)
base_profile.save()
for diskname in data['disks'].keys():
disk = data['disks'][diskname]
disk_profile = DiskProfile.objects.create(name=diskname, size=disk['capacity'], media_type=disk['media_type'], interface=disk['interface'], host=base_profile)
disk_profile.save()
for ifacename in data['interfaces'].keys():
iface = data['interfaces'][ifacename]
iface_profile = InterfaceProfile.objects.create(name=ifacename, speed=iface['speed'], nic_type=iface['nic_type'], order=iface['order'], host=base_profile)
iface_profile.save()
cpu = data['cpus']
cpu_prof = CpuProfile.objects.create(cores=cpu['cores'], architecture=cpu['architecture'], cpus=cpu['cpus'], cflags=cpu['cflags'], host=base_profile)
cpu_prof.save()
ram_prof = RamProfile.objects.create(amount=data['ram']['amount'], channels=data['ram']['channels'], host=base_profile)
ram_prof.save()
def make_default_template(resource_profile, image_id=None, template_name=None, connected_interface_names=None, interfaces_tagged=False, connected_interface_tagged=False, owner_username="root", lab_username="unh_iol", public=True, temporary=False, description=""):
if not resource_profile:
raise Exception("No viable continuation from none resource_profile")
if not template_name:
template_name = resource_profile.name
if not connected_interface_names:
connected_interface_names = [InterfaceProfile.objects.filter(host=resource_profile).first().name]
print("setting connected interface names to", connected_interface_names)
if not image_id:
image_id = Image.objects.filter(host_type=resource_profile).first().id
image = Image.objects.get(id=image_id)
base = ResourceTemplate.objects.create(
name=template_name,
xml="",
owner=User.objects.get(username=owner_username),
lab=Lab.objects.get(lab_user__username=lab_username), description=description,
public=public, temporary=temporary, copy_of=None)
rconf = ResourceConfiguration.objects.create(profile=resource_profile, image=image, template=base, is_head_node=True, name="opnfv_host")
rconf.save()
connected_interfaces = []
for iface_prof in InterfaceProfile.objects.filter(host=resource_profile).all():
iface_conf = InterfaceConfiguration.objects.create(profile=iface_prof, resource_config=rconf)
if iface_prof.name in connected_interface_names:
connected_interfaces.append(iface_conf)
network = Network.objects.create(name="public", bundle=base, is_public=True)
for iface in connected_interfaces:
connection = NetworkConnection.objects.create(network=network, vlan_is_tagged=interfaces_tagged)
connection.save()
iface.connections.add(connection)
print("adding connection to iface ", iface)
iface.save()
connection.save()
"""
Note: interfaces should be dict from interface name (eg ens1f0) to dict of schema:
{
mac_address: <mac addr>,
bus_addr: <bus addr>, //this field is optional, "" is default
}
"""
def add_server(profile, uname, interfaces, lab_username="unh_iol", vendor="unknown", model="unknown"):
server = Server.objects.create(
bundle=None,
profile=profile,
config=None,
working=True,
vendor=vendor,
model=model,
labid=uname,
lab=Lab.objects.get(lab_user__username=lab_username),
name=uname,
booked=False)
for iface_prof in InterfaceProfile.objects.filter(host=profile).all():
mac_addr = interfaces[iface_prof.name]["mac_address"]
bus_addr = "unknown"
if "bus_addr" in interfaces[iface_prof.name].keys():
bus_addr = interfaces[iface_prof.name]["bus_addr"]
iface = Interface.objects.create(acts_as=None, profile=iface_prof, mac_address=mac_addr, bus_address=bus_addr)
iface.save()
server.interfaces.add(iface)
server.save()
def extend_booking(booking_id, days=0, hours=0, minutes=0, weeks=0):
booking = Booking.objects.get(id=booking_id)
booking.end = booking.end + timedelta(days=days, hours=hours, minutes=minutes, weeks=weeks)
booking.save()
|