1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, 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.template.loader import render_to_string
13 from resource_inventory.models import Server
17 """Utility class to create a full PDF yaml file."""
20 def makePDF(cls, booking):
21 """Fill the pod descriptor file template with info about the resource."""
22 template = "dashboard/pdf.yaml"
24 info['details'] = cls.get_pdf_details(booking.resource)
26 info['jumphost'] = cls.get_pdf_jumphost(booking)
28 # filling in jumphost info can be optional in some cases, this shouldn't be a hard error
30 info['nodes'] = cls.get_pdf_nodes(booking)
32 return render_to_string(template, context=info)
35 def get_pdf_details(cls, resource):
36 """Info for the "details" section."""
39 email = "email@mail.com"
40 resource_lab = resource.template.lab
41 lab = resource_lab.name
42 location = resource_lab.location
43 pod_type = "development"
44 link = resource_lab.lab_info_link
47 # try to get more specific info that may fail, we dont care if it does
48 booking_owner = booking.models.Booking.objects.get(resource=resource).owner
49 owner = booking_owner.username
50 email = booking_owner.userprofile.email_addr
54 details['contact'] = email
56 details['link'] = link
57 details['owner'] = owner
58 details['location'] = location
59 details['type'] = pod_type
64 def get_jumphost(cls, booking):
65 """Return the host designated as the Jumphost for the booking."""
67 if booking.opnfv_config:
68 jumphost_opnfv_config = booking.opnfv_config.host_opnfv_config.get(
69 role__name__iexact="jumphost"
71 jumphost = booking.resource.hosts.get(config=jumphost_opnfv_config.host_config)
72 else: # if there is no opnfv config, use headnode
73 jumphost = Server.objects.filter(
74 bundle=booking.resource,
75 config__is_head_node=True
81 def get_pdf_jumphost(cls, booking):
82 """Return a dict of all the info for the "jumphost" section."""
83 jumphost = cls.get_jumphost(booking)
84 jumphost_info = cls.get_pdf_host(jumphost)
85 jumphost_info['os'] = jumphost.config.image.os.name
89 def get_pdf_nodes(cls, booking):
90 """Return a list of all the "nodes" (every host except jumphost)."""
92 nodes = set(Server.objects.filter(bundle=booking.resource))
93 nodes.discard(cls.get_jumphost(booking))
96 pdf_nodes.append(cls.get_pdf_host(node))
101 def get_pdf_host(cls, host):
103 Gather all needed info about a host.
108 host_info['name'] = host.name
109 host_info['node'] = cls.get_pdf_host_node(host)
110 host_info['disks'] = []
111 for disk in host.profile.storageprofile.all():
112 host_info['disks'].append(cls.get_pdf_host_disk(disk))
114 host_info['interfaces'] = []
115 for interface in host.interfaces.all():
116 host_info['interfaces'].append(cls.get_pdf_host_iface(interface))
118 host_info['remote'] = cls.get_pdf_host_remote_management(host)
123 def get_pdf_host_node(cls, host):
124 """Return "node" info for a given host."""
126 d['type'] = "baremetal"
127 d['vendor'] = host.vendor
128 d['model'] = host.model
129 d['memory'] = str(host.profile.ramprofile.first().amount) + "G"
131 cpu = host.profile.cpuprofile.first()
132 d['arch'] = cpu.architecture
134 d['cores'] = cpu.cores
136 if cflags and cflags.strip():
137 d['cpu_cflags'] = cflags
139 d['cpu_cflags'] = "none"
144 def get_pdf_host_disk(cls, disk):
145 """Return a dict describing the given disk."""
147 disk_info['name'] = disk.name
148 disk_info['capacity'] = str(disk.size) + "G"
149 disk_info['type'] = disk.media_type
150 disk_info['interface'] = disk.interface
151 disk_info['rotation'] = disk.rotation
155 def get_pdf_host_iface(cls, interface):
156 """Return a dict describing given interface."""
158 iface_info['features'] = "none"
159 iface_info['mac_address'] = interface.mac_address
160 iface_info['name'] = interface.profile.name
161 speed = str(int(interface.profile.speed / 1000)) + "gb"
162 iface_info['speed'] = speed
166 def get_pdf_host_remote_management(cls, host):
167 """Get the remote params of the host."""
168 man = host.remote_management
170 mgmt['address'] = man.address
171 mgmt['mac_address'] = man.mac_address
172 mgmt['pass'] = man.password
173 mgmt['type'] = man.management_type
174 mgmt['user'] = man.user
175 mgmt['versions'] = [man.versions]