Allow query to fail
[pharos-tools.git] / dashboard / 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 Host, InterfaceProfile
14
15
16 class PDFTemplater:
17     """
18     Utility class to create a full PDF yaml file
19     """
20
21     @classmethod
22     def makePDF(cls, resource):
23         """
24         fills the pod descriptor file template with info about the resource
25         """
26         template = "dashboard/pdf.yaml"
27         info = {}
28         info['details'] = cls.get_pdf_details(resource)
29         info['jumphost'] = cls.get_pdf_jumphost(resource)
30         info['nodes'] = cls.get_pdf_nodes(resource)
31
32         return render_to_string(template, context=info)
33
34     @classmethod
35     def get_pdf_details(cls, resource):
36         """
37         Info for the "details" section
38         """
39         details = {}
40         owner = "Anon"
41         email = "email@mail.com"
42         resource_lab = resource.template.lab
43         lab = resource_lab.name
44         location = resource_lab.location
45         pod_type = "development"
46         link = "https://wiki.opnfv.org/display/INF/Pharos+Laas"
47
48         try:
49             # try to get more specific info that may fail, we dont care if it does
50             booking_owner = booking.models.Booking.objects.get(resource=resource).owner
51             owner = booking_owner.username
52             email = booking_owner.userprofile.email_addr
53         except Exception:
54             pass
55
56         details['contact'] = email
57         details['lab'] = lab
58         details['link'] = link
59         details['owner'] = owner
60         details['location'] = location
61         details['type'] = pod_type
62
63         return details
64
65     @classmethod
66     def get_pdf_jumphost(cls, resource):
67         """
68         returns a dict of all the info for the "jumphost" section
69         """
70         jumphost = Host.objects.get(bundle=resource, config__opnfvRole__name__iexact="jumphost")
71         jumphost_info = cls.get_pdf_host(jumphost)
72         remote_params = jumphost_info['remote_management']  # jumphost has extra block not in normal hosts
73         remote_params.pop("address")
74         remote_params.pop("mac_address")
75         jumphost_info['remote_params'] = remote_params
76         jumphost_info['os'] = jumphost.config.image.os.name
77         return jumphost_info
78
79     @classmethod
80     def get_pdf_nodes(cls, resource):
81         """
82         returns a list of all the "nodes" (every host except jumphost)
83         """
84         pdf_nodes = []
85         nodes = Host.objects.filter(bundle=resource).exclude(config__opnfvRole__name__iexact="jumphost")
86         for node in nodes:
87             pdf_nodes.append(cls.get_pdf_host(node))
88
89         return pdf_nodes
90
91     @classmethod
92     def get_pdf_host(cls, host):
93         """
94         method to gather all needed info about a host
95         returns a dict
96         """
97         host_info = {}
98         host_info['name'] = host.template.resource.name
99         host_info['node'] = cls.get_pdf_host_node(host)
100         host_info['disks'] = []
101         for disk in host.profile.storageprofile.all():
102             host_info['disks'].append(cls.get_pdf_host_disk(disk))
103
104         host_info['interfaces'] = []
105         for interface in host.interfaces.all():
106             host_info['interfaces'].append(cls.get_pdf_host_iface(interface))
107
108         host_info['remote_management'] = cls.get_pdf_host_remote_management(host)
109
110         return host_info
111
112     @classmethod
113     def get_pdf_host_node(cls, host):
114         """
115         returns "node" info for a given host
116         """
117         d = {}
118         d['type'] = "baremetal"
119         d['vendor'] = host.vendor
120         d['model'] = host.model
121         d['memory'] = str(host.profile.ramprofile.first().amount) + "G"
122
123         cpu = host.profile.cpuprofile.first()
124         d['arch'] = cpu.architecture
125         d['cpus'] = cpu.cpus
126         d['cores'] = cpu.cores
127         cflags = cpu.cflags
128         if cflags and cflags.strip():
129             d['cpu_cflags'] = cflags
130         else:
131             d['cpu_cflags'] = "none"
132
133         return d
134
135     @classmethod
136     def get_pdf_host_disk(cls, disk):
137         """
138         returns a dict describing the given disk
139         """
140         disk_info = {}
141         disk_info['name'] = disk.name
142         disk_info['capacity'] = str(disk.size) + "G"
143         disk_info['type'] = disk.media_type
144         disk_info['interface'] = disk.interface
145         disk_info['rotation'] = disk.rotation
146         return disk_info
147
148     @classmethod
149     def get_pdf_host_iface(cls, interface):
150         """
151         returns a dict describing given interface
152         """
153         iface_info = {}
154         iface_info['features'] = "none"
155         iface_info['mac_address'] = interface.mac_address
156         iface_info['name'] = interface.name
157         speed = "unknown"
158         try:
159             profile = InterfaceProfile.objects.get(host=interface.host.profile, name=interface.name)
160             speed = str(int(profile.speed / 1000)) + "gb"
161         except Exception:
162             pass
163         iface_info['speed'] = speed
164         return iface_info
165
166     @classmethod
167     def get_pdf_host_remote_management(cls, host):
168         """
169         gives the remote params of the host
170         """
171         mgmt = {}
172         mgmt['address'] = "I dunno"
173         mgmt['mac_address'] = "I dunno"
174         mgmt['pass'] = "I dunno"
175         mgmt['type'] = "I dunno"
176         mgmt['user'] = "I dunno"
177         mgmt['versions'] = ["I dunno"]
178         return mgmt