1 ##############################################################################
2 # Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from django.contrib.auth.models import User
12 from django.db import models
13 from django.core.exceptions import PermissionDenied
14 from django.shortcuts import get_object_or_404
19 from booking.models import Booking
20 from resource_inventory.models import (
30 class JobStatus(object):
37 class LabManagerTracker(object):
40 def get(cls, lab_name, token):
42 Takes in a lab name (from a url path)
43 returns a lab manager instance for that lab, if it exists
46 lab = Lab.objects.get(name=lab_name)
48 raise PermissionDenied("Lab not found")
49 if lab.api_token == token:
50 return LabManager(lab)
51 raise PermissionDenied("Lab not authorized")
54 class LabManager(object):
56 This is the class that will ultimately handle all REST calls to
58 handles jobs, inventory, status, etc
59 may need to create helper classes
62 def __init__(self, lab):
65 def update_host_remote_info(self, data, host_id):
66 host = get_object_or_404(Host, labid=host_id, lab=self.lab)
69 info['address'] = data['address']
70 info['mac_address'] = data['mac_address']
71 info['password'] = data['password']
72 info['user'] = data['user']
73 info['type'] = data['type']
74 info['versions'] = json.dumps(data['versions'])
75 except Exception as e:
76 return {"error": "invalid arguement: " + str(e)}
77 remote_info = host.remote_management
78 if "default" in remote_info.mac_address:
79 remote_info = RemoteInfo()
80 remote_info.address = info['address']
81 remote_info.mac_address = info['mac_address']
82 remote_info.password = info['password']
83 remote_info.user = info['user']
84 remote_info.type = info['type']
85 remote_info.versions = info['versions']
87 host.remote_management = remote_info
89 return {"status": "success"}
91 def get_profile(self):
93 prof['name'] = self.lab.name
95 "phone": self.lab.contact_phone,
96 "email": self.lab.contact_email
98 prof['host_count'] = []
99 for host in HostProfile.objects.filter(labs=self.lab):
100 count = Host.objects.filter(profile=host, lab=self.lab).count()
101 prof['host_count'].append(
109 def get_inventory(self):
111 hosts = Host.objects.filter(lab=self.lab)
112 images = Image.objects.filter(from_lab=self.lab)
113 profiles = HostProfile.objects.filter(labs=self.lab)
114 inventory['hosts'] = self.serialize_hosts(hosts)
115 inventory['images'] = self.serialize_images(images)
116 inventory['host_types'] = self.serialize_host_profiles(profiles)
119 def get_status(self):
120 return {"status": self.lab.status}
122 def set_status(self, payload):
125 def get_current_jobs(self):
126 jobs = Job.objects.filter(booking__lab=self.lab)
128 return self.serialize_jobs(jobs, status=JobStatus.CURRENT)
130 def get_new_jobs(self):
131 jobs = Job.objects.filter(booking__lab=self.lab)
133 return self.serialize_jobs(jobs, status=JobStatus.NEW)
135 def get_done_jobs(self):
136 jobs = Job.objects.filter(booking__lab=self.lab)
138 return self.serialize_jobs(jobs, status=JobStatus.DONE)
140 def get_job(self, jobid):
141 return Job.objects.get(pk=jobid).to_dict()
143 def update_job(self, jobid, data):
146 def serialize_jobs(self, jobs, status=JobStatus.NEW):
149 jsonized_job = job.get_delta(status)
150 if len(jsonized_job['payload']) < 1:
152 job_ser.append(jsonized_job)
156 def serialize_hosts(self, hosts):
161 h['hostname'] = host.name
162 h['host_type'] = host.profile.name
163 for iface in host.interfaces.all():
165 eth['mac'] = iface.mac_address
166 eth['busaddr'] = iface.bus_address
167 eth['name'] = iface.name
168 eth['switchport'] = {"switch_name": iface.switch_name, "port_name": iface.port_name}
169 h['interfaces'].append(eth)
172 def serialize_images(self, images):
178 "lab_id": image.lab_id,
179 "dashboard_id": image.id
184 def serialize_host_profiles(self, profiles):
186 for profile in profiles:
189 "cores": profile.cpuprofile.first().cores,
190 "arch": profile.cpuprofile.first().architecture,
191 "cpus": profile.cpuprofile.first().cpus,
194 for disk in profile.storageprofile.all():
197 "type": disk.media_type,
201 p['description'] = profile.description
203 for iface in profile.interfaceprofile.all():
204 p['interfaces'].append(
206 "speed": iface.speed,
211 p['ram'] = {"amount": profile.ramprofile.first().amount}
212 p['name'] = profile.name
213 profile_ser.append(p)
217 class Job(models.Model):
219 This is the class that is serialized and put into the api
221 booking = models.OneToOneField(Booking, on_delete=models.CASCADE, null=True)
222 status = models.IntegerField(default=JobStatus.NEW)
223 complete = models.BooleanField(default=False)
229 for relation in AccessRelation.objects.filter(job=self):
230 if 'access' not in d:
232 d['access'][relation.task_id] = relation.config.to_dict()
233 for relation in SoftwareRelation.objects.filter(job=self):
234 if 'software' not in d:
236 d['software'][relation.task_id] = relation.config.to_dict()
237 for relation in HostHardwareRelation.objects.filter(job=self):
238 if 'hardware' not in d:
240 d['hardware'][relation.task_id] = relation.config.to_dict()
241 for relation in HostNetworkRelation.objects.filter(job=self):
242 if 'network' not in d:
244 d['network'][relation.task_id] = relation.config.to_dict()
245 for relation in SnapshotRelation.objects.filter(job=self):
246 if 'snapshot' not in d:
248 d['snapshot'][relation.task_id] = relation.config.to_dict()
254 def get_tasklist(self, status="all"):
257 HostHardwareRelation,
265 tasklist += list(cls.objects.filter(job=self))
268 tasklist += list(cls.objects.filter(job=self).filter(status=status))
271 def is_fulfilled(self):
273 This method should return true if all of the job's tasks are done,
276 my_tasks = self.get_tasklist()
277 for task in my_tasks:
278 if task.status != JobStatus.DONE:
282 def get_delta(self, status):
286 for relation in AccessRelation.objects.filter(job=self).filter(status=status):
287 if 'access' not in d:
289 d['access'][relation.task_id] = relation.config.get_delta()
290 for relation in SoftwareRelation.objects.filter(job=self).filter(status=status):
291 if 'software' not in d:
293 d['software'][relation.task_id] = relation.config.get_delta()
294 for relation in HostHardwareRelation.objects.filter(job=self).filter(status=status):
295 if 'hardware' not in d:
297 d['hardware'][relation.task_id] = relation.config.get_delta()
298 for relation in HostNetworkRelation.objects.filter(job=self).filter(status=status):
299 if 'network' not in d:
301 d['network'][relation.task_id] = relation.config.get_delta()
302 for relation in SnapshotRelation.objects.filter(job=self).filter(status=status):
303 if 'snapshot' not in d:
305 d['snapshot'][relation.task_id] = relation.config.get_delta()
311 return json.dumps(self.to_dict())
314 class TaskConfig(models.Model):
322 return json.dumps(self.to_dict())
324 def clear_delta(self):
328 class OpnfvApiConfig(models.Model):
330 installer = models.CharField(max_length=100)
331 scenario = models.CharField(max_length=100)
332 roles = models.ManyToManyField(Host)
333 delta = models.TextField()
338 d['installer'] = self.installer
340 d['scenario'] = self.scenario
342 hosts = self.roles.all()
345 for host in self.roles.all():
346 d['roles'].append({host.labid: host.config.opnfvRole.name})
351 return json.dumps(self.to_dict())
353 def set_installer(self, installer):
354 self.installer = installer
355 d = json.loads(self.delta)
356 d['installer'] = installer
357 self.delta = json.dumps(d)
359 def set_scenario(self, scenario):
360 self.scenario = scenario
361 d = json.loads(self.delta)
362 d['scenario'] = scenario
363 self.delta = json.dumps(d)
365 def add_role(self, host):
367 d = json.loads(self.delta)
370 d['roles'].append({host.labid: host.config.opnfvRole.name})
371 self.delta = json.dumps(d)
373 def clear_delta(self):
378 self.delta = self.to_json()
380 return json.loads(self.delta)
383 class AccessConfig(TaskConfig):
384 access_type = models.CharField(max_length=50)
385 user = models.ForeignKey(User, on_delete=models.CASCADE)
386 revoke = models.BooleanField(default=False)
387 context = models.TextField(default="")
388 delta = models.TextField(default="{}")
392 d['access_type'] = self.access_type
393 d['user'] = self.user.id
394 d['revoke'] = self.revoke
396 d['context'] = json.loads(self.context)
403 self.delta = self.to_json()
405 d = json.loads(self.delta)
406 d["lab_token"] = self.accessrelation.lab_token
411 return json.dumps(self.to_dict())
413 def clear_delta(self):
415 d["lab_token"] = self.accessrelation.lab_token
416 self.delta = json.dumps(d)
418 def set_access_type(self, access_type):
419 self.access_type = access_type
420 d = json.loads(self.delta)
421 d['access_type'] = access_type
422 self.delta = json.dumps(d)
424 def set_user(self, user):
426 d = json.loads(self.delta)
427 d['user'] = self.user.id
428 self.delta = json.dumps(d)
430 def set_revoke(self, revoke):
432 d = json.loads(self.delta)
434 self.delta = json.dumps(d)
436 def set_context(self, context):
437 self.context = json.dumps(context)
438 d = json.loads(self.delta)
439 d['context'] = context
440 self.delta = json.dumps(d)
443 class SoftwareConfig(TaskConfig):
445 handled opnfv installations, etc
447 opnfv = models.ForeignKey(OpnfvApiConfig, on_delete=models.CASCADE)
452 d['opnfv'] = self.opnfv.to_dict()
454 d["lab_token"] = self.softwarerelation.lab_token
455 self.delta = json.dumps(d)
461 d['opnfv'] = self.opnfv.get_delta()
462 d['lab_token'] = self.softwarerelation.lab_token
466 def clear_delta(self):
467 self.opnfv.clear_delta()
470 return json.dumps(self.to_dict())
473 class HardwareConfig(TaskConfig):
475 handles imaging, user accounts, etc
477 image = models.CharField(max_length=100, default="defimage")
478 power = models.CharField(max_length=100, default="off")
479 hostname = models.CharField(max_length=100, default="hostname")
480 ipmi_create = models.BooleanField(default=False)
481 delta = models.TextField()
485 d['image'] = self.image
486 d['power'] = self.power
487 d['hostname'] = self.hostname
488 d['ipmi_create'] = str(self.ipmi_create)
489 d['id'] = self.hosthardwarerelation.host.labid
493 return json.dumps(self.to_dict())
497 self.delta = self.to_json()
499 d = json.loads(self.delta)
500 d['lab_token'] = self.hosthardwarerelation.lab_token
503 def clear_delta(self):
505 d["id"] = self.hosthardwarerelation.host.labid
506 d["lab_token"] = self.hosthardwarerelation.lab_token
507 self.delta = json.dumps(d)
509 def set_image(self, image):
511 d = json.loads(self.delta)
512 d['image'] = self.image
513 self.delta = json.dumps(d)
515 def set_power(self, power):
517 d = json.loads(self.delta)
519 self.delta = json.dumps(d)
521 def set_hostname(self, hostname):
522 self.hostname = hostname
523 d = json.loads(self.delta)
524 d['hostname'] = hostname
525 self.delta = json.dumps(d)
527 def set_ipmi_create(self, ipmi_create):
528 self.ipmi_create = ipmi_create
529 d = json.loads(self.delta)
530 d['ipmi_create'] = ipmi_create
531 self.delta = json.dumps(d)
534 class NetworkConfig(TaskConfig):
536 handles network configuration
538 interfaces = models.ManyToManyField(Interface)
539 delta = models.TextField()
543 hid = self.hostnetworkrelation.host.labid
545 for interface in self.interfaces.all():
546 d[hid][interface.mac_address] = []
547 for vlan in interface.config.all():
548 d[hid][interface.mac_address].append({"vlan_id": vlan.vlan_id, "tagged": vlan.tagged})
553 return json.dumps(self.to_dict())
557 self.delta = self.to_json()
559 d = json.loads(self.delta)
560 d['lab_token'] = self.hostnetworkrelation.lab_token
563 def clear_delta(self):
564 self.delta = json.dumps(self.to_dict())
567 def add_interface(self, interface):
568 self.interfaces.add(interface)
569 d = json.loads(self.delta)
570 hid = self.hostnetworkrelation.host.labid
573 d[hid][interface.mac_address] = []
574 for vlan in interface.config.all():
575 d[hid][interface.mac_address].append({"vlan_id": vlan.vlan_id, "tagged": vlan.tagged})
576 self.delta = json.dumps(d)
579 class SnapshotConfig(TaskConfig):
581 host = models.ForeignKey(Host, null=True, on_delete=models.DO_NOTHING)
582 image = models.IntegerField(null=True)
583 dashboard_id = models.IntegerField()
584 delta = models.TextField(default="{}")
589 d['host'] = self.host.labid
591 d['image'] = self.image
592 d['dashboard_id'] = self.dashboard_id
596 return json.dumps(self.to_dict())
600 self.delta = self.to_json()
602 d = json.loads(self.delta)
605 def clear_delta(self):
606 self.delta = json.dumps(self.to_dict())
609 def set_host(self, host):
611 d = json.loads(self.delta)
612 d['host'] = host.labid
613 self.delta = json.dumps(d)
615 def set_image(self, image):
617 d = json.loads(self.delta)
618 d['image'] = self.image
619 self.delta = json.dumps(d)
621 def clear_image(self):
623 d = json.loads(self.delta)
625 self.delta = json.dumps(d)
627 def set_dashboard_id(self, dash):
628 self.dashboard_id = dash
629 d = json.loads(self.delta)
630 d['dashboard_id'] = self.dashboard_id
631 self.delta = json.dumps(d)
634 def get_task(task_id):
635 for taskclass in [AccessRelation, SoftwareRelation, HostHardwareRelation, HostNetworkRelation, SnapshotRelation]:
637 ret = taskclass.objects.get(task_id=task_id)
639 except taskclass.DoesNotExist:
641 from django.core.exceptions import ObjectDoesNotExist
642 raise ObjectDoesNotExist("Could not find matching TaskRelation instance")
646 return str(uuid.uuid4())
649 class TaskRelation(models.Model):
650 status = models.IntegerField(default=JobStatus.NEW)
651 job = models.ForeignKey(Job, on_delete=models.CASCADE)
652 config = models.OneToOneField(TaskConfig, on_delete=models.CASCADE)
653 task_id = models.CharField(default=get_task_uuid, max_length=37)
654 lab_token = models.CharField(default="null", max_length=50)
655 message = models.TextField(default="")
657 def delete(self, *args, **kwargs):
659 return super(self.__class__, self).delete(*args, **kwargs)
662 return "Generic Task"
668 class AccessRelation(TaskRelation):
669 config = models.OneToOneField(AccessConfig, on_delete=models.CASCADE)
674 def delete(self, *args, **kwargs):
676 return super(self.__class__, self).delete(*args, **kwargs)
679 class SoftwareRelation(TaskRelation):
680 config = models.OneToOneField(SoftwareConfig, on_delete=models.CASCADE)
683 return "Software Configuration Task"
685 def delete(self, *args, **kwargs):
687 return super(self.__class__, self).delete(*args, **kwargs)
690 class HostHardwareRelation(TaskRelation):
691 host = models.ForeignKey(Host, on_delete=models.CASCADE)
692 config = models.OneToOneField(HardwareConfig, on_delete=models.CASCADE)
695 return "Hardware Configuration Task"
698 return self.config.to_dict()
700 def delete(self, *args, **kwargs):
702 return super(self.__class__, self).delete(*args, **kwargs)
705 class HostNetworkRelation(TaskRelation):
706 host = models.ForeignKey(Host, on_delete=models.CASCADE)
707 config = models.OneToOneField(NetworkConfig, on_delete=models.CASCADE)
710 return "Network Configuration Task"
712 def delete(self, *args, **kwargs):
714 return super(self.__class__, self).delete(*args, **kwargs)
717 class SnapshotRelation(TaskRelation):
718 snapshot = models.ForeignKey(Image, on_delete=models.CASCADE)
719 config = models.OneToOneField(SnapshotConfig, on_delete=models.CASCADE)
722 return "Snapshot Task"
725 return self.config.to_dict()
727 def delete(self, *args, **kwargs):
729 return super(self.__class__, self).delete(*args, **kwargs)
732 class JobFactory(object):
735 def reimageHost(cls, new_image, booking, host):
737 This method will make all necessary changes to make a lab
740 job = Job.objects.get(booking=booking)
741 # make hardware task new
742 hardware_relation = HostHardwareRelation.objects.get(host=host, job=job)
743 hardware_relation.config.set_image(new_image.lab_id)
744 hardware_relation.config.save()
745 hardware_relation.status = JobStatus.NEW
747 # re-apply networking after host is reset
748 net_relation = HostNetworkRelation.objects.get(host=host, job=job)
749 net_relation.status = JobStatus.NEW
751 # re-apply ssh access after host is reset
752 ssh_relation = AccessRelation.objects.get(job=job, config__access_type="ssh")
753 ssh_relation.status = JobStatus.NEW
755 # save them all at once to reduce the chance
756 # of a lab polling and only seeing partial change
757 hardware_relation.save()
762 def makeSnapshotTask(cls, image, booking, host):
763 relation = SnapshotRelation()
764 job = Job.objects.get(booking=booking)
765 config = SnapshotConfig.objects.create(dashboard_id=image.id)
768 relation.config = config
769 relation.config.save()
770 relation.config = relation.config
771 relation.snapshot = image
775 config.set_host(host)
779 def makeCompleteJob(cls, booking):
780 hosts = Host.objects.filter(bundle=booking.resource)
783 job = Job.objects.get(booking=booking)
785 job = Job.objects.create(status=JobStatus.NEW, booking=booking)
786 cls.makeHardwareConfigs(
790 cls.makeNetworkConfigs(
798 all_users = list(booking.collaborators.all())
799 all_users.append(booking.owner)
800 cls.makeAccessConfig(
806 for user in all_users:
808 cls.makeAccessConfig(
814 "key": user.userprofile.ssh_public_key.open().read().decode(encoding="UTF-8"),
815 "hosts": [host.labid for host in hosts]
822 def makeHardwareConfigs(cls, hosts=[], job=Job()):
824 hardware_config = None
826 hardware_config = HardwareConfig.objects.get(relation__host=host)
828 hardware_config = HardwareConfig()
830 relation = HostHardwareRelation()
833 relation.config = hardware_config
834 relation.config.save()
835 relation.config = relation.config
838 hardware_config.clear_delta()
839 hardware_config.set_image(host.config.image.lab_id)
840 hardware_config.set_hostname(host.template.resource.name)
841 hardware_config.set_power("on")
842 hardware_config.set_ipmi_create(True)
843 hardware_config.save()
846 def makeAccessConfig(cls, users, access_type, revoke=False, job=Job(), context=False):
848 relation = AccessRelation()
850 config = AccessConfig()
851 config.access_type = access_type
854 relation.config = config
858 config.set_context(context)
859 config.set_access_type(access_type)
860 config.set_revoke(revoke)
861 config.set_user(user)
865 def makeNetworkConfigs(cls, hosts=[], job=Job()):
867 network_config = None
869 network_config = NetworkConfig.objects.get(relation__host=host)
871 network_config = NetworkConfig.objects.create()
873 relation = HostNetworkRelation()
876 network_config.save()
877 relation.config = network_config
879 network_config.clear_delta()
881 for interface in host.interfaces.all():
882 network_config.add_interface(interface)
883 network_config.save()
886 def makeSoftware(cls, hosts=[], job=Job()):
887 def init_config(host):
888 opnfv_config = OpnfvApiConfig()
890 opnfv = host.config.bundle.opnfv_config.first()
891 opnfv_config.installer = opnfv.installer.name
892 opnfv_config.scenario = opnfv.scenario.name
900 opnfv_config = init_config(host)
903 opnfv_config.roles.add(host)
904 software_config = SoftwareConfig.objects.create(opnfv=opnfv_config)
905 software_config.save()
906 software_relation = SoftwareRelation.objects.create(job=job, config=software_config)
907 software_relation.save()
908 return software_relation