##############################################################################
+from django.contrib.auth.models import User
from django.db import models
from django.core.exceptions import PermissionDenied
import json
import uuid
-from resource_inventory.models import *
from booking.models import Booking
+from resource_inventory.models import (
+ Lab,
+ HostProfile,
+ Host,
+ Image,
+ Interface
+)
class JobStatus(object):
prof = {}
prof['name'] = self.lab.name
prof['contact'] = {
- "phone": self.lab.contact_phone,
- "email": self.lab.contact_email
- }
+ "phone": self.lab.contact_phone,
+ "email": self.lab.contact_email
+ }
prof['host_count'] = []
for host in HostProfile.objects.filter(labs=self.lab):
count = Host.objects.filter(profile=host, lab=self.lab).count()
- prof['host_count'].append({
- "type": host.name,
- "count": count
- })
+ prof['host_count'].append(
+ {
+ "type": host.name,
+ "count": count
+ }
+ )
return prof
def get_inventory(self):
def serialize_images(self, images):
images_ser = []
for image in images:
- images_ser.append({
- "name": image.name,
- "lab_id": image.lab_id,
- "dashboard_id": image.id
- })
+ images_ser.append(
+ {
+ "name": image.name,
+ "lab_id": image.lab_id,
+ "dashboard_id": image.id
+ }
+ )
return images_ser
def serialize_host_profiles(self, profiles):
for profile in profiles:
p = {}
p['cpu'] = {
- "cores": profile.cpuprofile.first().cores,
- "arch": profile.cpuprofile.first().architecture,
- "cpus": profile.cpuprofile.first().cpus,
- }
+ "cores": profile.cpuprofile.first().cores,
+ "arch": profile.cpuprofile.first().architecture,
+ "cpus": profile.cpuprofile.first().cpus,
+ }
p['disks'] = []
for disk in profile.storageprofile.all():
d = {
- "size": disk.size,
- "type": disk.media_type,
- "name": disk.name
- }
+ "size": disk.size,
+ "type": disk.media_type,
+ "name": disk.name
+ }
p['disks'].append(d)
p['description'] = profile.description
p['interfaces'] = []
for iface in profile.interfaceprofile.all():
- p['interfaces'].append({
- "speed": iface.speed,
- "name": iface.name
- })
+ p['interfaces'].append(
+ {
+ "speed": iface.speed,
+ "name": iface.name
+ }
+ )
p['ram'] = {"amount": profile.ramprofile.first().amount}
p['name'] = profile.name
tasklist += list(cls.objects.filter(job=self).filter(status=status))
return tasklist
+ def is_fulfilled(self):
+ """
+ This method should return true if all of the job's tasks are done,
+ and false otherwise
+ """
+ my_tasks = self.get_tasklist()
+ for task in my_tasks:
+ if task.status != JobStatus.DONE:
+ return False
+ return True
+
def get_delta(self, status):
d = {}
j = {}
def clear_delta(self):
self.delta = '{}'
+
class OpnfvApiConfig(models.Model):
installer = models.CharField(max_length=100)
self.save()
return json.loads(self.delta)
+
class AccessConfig(TaskConfig):
access_type = models.CharField(max_length=50)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def to_dict(self):
d = {}
- d['access_type'] = self.access_type
+ d['access_type'] = self.access_type
d['user'] = self.user.id
d['revoke'] = self.revoke
- d['context'] = self.context
+ d['context'] = json.loads(self.context)
return d
def get_delta(self):
self.delta = json.dumps(d)
def set_context(self, context):
- self.context = context
+ self.context = json.dumps(context)
d = json.loads(self.delta)
d['context'] = context
self.delta = json.dumps(d)
+
class SoftwareConfig(TaskConfig):
"""
handled opnfv installations, etc
def to_json(self):
return json.dumps(self.to_dict())
+
class HardwareConfig(TaskConfig):
"""
handles imaging, user accounts, etc
except:
job = Job.objects.create(status=JobStatus.NEW, booking=booking)
cls.makeHardwareConfigs(
- hosts=hosts,
- job=job
- )
+ hosts=hosts,
+ job=job
+ )
cls.makeNetworkConfigs(
- hosts=hosts,
- job=job
- )
+ hosts=hosts,
+ job=job
+ )
cls.makeSoftware(
- hosts=hosts,
- job=job
- )
- cls.makeAccessConfig(
- users=booking.collaborators.all(),
- access_type="vpn",
- revoke=False,
- job=job
- )
+ hosts=hosts,
+ job=job
+ )
+ all_users = list(booking.collaborators.all())
+ all_users.append(booking.owner)
cls.makeAccessConfig(
- users=[booking.owner],
- access_type="vpn",
- revoke=False,
- job=job
+ users=all_users,
+ access_type="vpn",
+ revoke=False,
+ job=job
+ )
+ for user in all_users:
+ try:
+ cls.makeAccessConfig(
+ users=[user],
+ access_type="ssh",
+ revoke=False,
+ job=job,
+ context={
+ "key": user.userprofile.ssh_public_key.open().read().decode(encoding="UTF-8"),
+ "hosts": [host.labid for host in hosts]
+ }
)
+ except Exception:
+ continue
@classmethod
def makeHardwareConfigs(cls, hosts=[], job=Job()):
hardware_config.save()
@classmethod
- def makeAccessConfig(cls, users, access_type, revoke=False, job=Job()):
+ def makeAccessConfig(cls, users, access_type, revoke=False, job=Job(), context=False):
for user in users:
relation = AccessRelation()
relation.job = job
config = AccessConfig()
config.access_type = access_type
config.user = user
+ if context:
+ config.set_context(context)
config.save()
relation.config = config
relation.save()
return software_relation
except:
return None
-
- def makeAccess(cls, user, access_type, revoke):
- pass