add nick
[laas.git] / src / resource_inventory / pdf_templater.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
3 #
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 ##############################################################################
9
10
11 from django.template.loader import render_to_string
12 import booking
13 from resource_inventory.models import Server
14
15
16 class PDFTemplater:
17     """Utility class to create a full PDF yaml file."""
18
19     @classmethod
20     def makePDF(cls, booking):
21         """Fill the pod descriptor file template with info about the resource."""
22         template = "dashboard/pdf.yaml"
23         info = {}
24         info['details'] = cls.get_pdf_details(booking.resource)
25         try:
26             info['jumphost'] = cls.get_pdf_jumphost(booking)
27         except Exception:
28             # filling in jumphost info can be optional in some cases, this shouldn't be a hard error
29             info['jumphost'] = {}
30         info['nodes'] = cls.get_pdf_nodes(booking)
31
32         return render_to_string(template, context=info)
33
34     @classmethod
35     def get_pdf_details(cls, resource):
36         """Info for the "details" section."""
37         details = {}
38         owner = "Anon"
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
45
46         try:
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
51         except Exception:
52             pass
53
54         details['contact'] = email
55         details['lab'] = lab
56         details['link'] = link
57         details['owner'] = owner
58         details['location'] = location
59         details['type'] = pod_type
60
61         return details
62
63     @classmethod
64     def get_jumphost(cls, booking):
65         """Return the host designated as the Jumphost for the booking."""
66         jumphost = None
67         if booking.opnfv_config:
68             jumphost_opnfv_config = booking.opnfv_config.host_opnfv_config.get(
69                 role__name__iexact="jumphost"
70             )
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
76             ).first()
77
78         return jumphost
79
80     @classmethod
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
86         return jumphost_info
87
88     @classmethod
89     def get_pdf_nodes(cls, booking):
90         """Return a list of all the "nodes" (every host except jumphost)."""
91         pdf_nodes = []
92         nodes = set(Server.objects.filter(bundle=booking.resource))
93         nodes.discard(cls.get_jumphost(booking))
94
95         for node in nodes:
96             pdf_nodes.append(cls.get_pdf_host(node))
97
98         return pdf_nodes
99
100     @classmethod
101     def get_pdf_host(cls, host):
102         """
103         Gather all needed info about a host.
104
105         returns a dictionary
106         """
107         host_info = {}
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))
113
114         host_info['interfaces'] = []
115         for interface in host.interfaces.all():
116             host_info['interfaces'].append(cls.get_pdf_host_iface(interface))
117
118         host_info['remote'] = cls.get_pdf_host_remote_management(host)
119
120         return host_info
121
122     @classmethod
123     def get_pdf_host_node(cls, host):
124         """Return "node" info for a given host."""
125         d = {}
126         d['type'] = "baremetal"
127         d['vendor'] = host.vendor
128         d['model'] = host.model
129         d['memory'] = str(host.profile.ramprofile.first().amount) + "G"
130
131         cpu = host.profile.cpuprofile.first()
132         d['arch'] = cpu.architecture
133         d['cpus'] = cpu.cpus
134         d['cores'] = cpu.cores
135         cflags = cpu.cflags
136         if cflags and cflags.strip():
137             d['cpu_cflags'] = cflags
138         else:
139             d['cpu_cflags'] = "none"
140
141         return d
142
143     @classmethod
144     def get_pdf_host_disk(cls, disk):
145         """Return a dict describing the given disk."""
146         disk_info = {}
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
152         return disk_info
153
154     @classmethod
155     def get_pdf_host_iface(cls, interface):
156         """Return a dict describing given interface."""
157         iface_info = {}
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
163         return iface_info
164
165     @classmethod
166     def get_pdf_host_remote_management(cls, host):
167         """Get the remote params of the host."""
168         man = host.remote_management
169         mgmt = {}
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]
176         return mgmt