Implement OPNFV workflow
[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, booking):
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(booking.resource)
29         info['jumphost'] = cls.get_pdf_jumphost(booking)
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         """
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_jumphost(cls, booking):
67         jumphost = None
68         if booking.opnfv_config:
69             jumphost_opnfv_config = booking.opnfv_config.host_opnfv_config.get(
70                 role__name__iexact="jumphost"
71             )
72             jumphost = booking.resource.hosts.get(config=jumphost_opnfv_config.host_config)
73         else:  # if there is no opnfv config, use headnode
74             jumphost = Host.objects.filter(
75                 bundle=booking.resource,
76                 config__is_head_node=True
77             ).first()
78
79         return jumphost
80
81     @classmethod
82     def get_pdf_jumphost(cls, booking):
83         """
84         returns a dict of all the info for the "jumphost" section
85         """
86         jumphost = cls.get_jumphost(booking)
87         jumphost_info = cls.get_pdf_host(jumphost)
88         jumphost_info['os'] = jumphost.config.image.os.name
89         return jumphost_info
90
91     @classmethod
92     def get_pdf_nodes(cls, booking):
93         """
94         returns a list of all the "nodes" (every host except jumphost)
95         """
96         pdf_nodes = []
97         nodes = set(Host.objects.filter(bundle=booking.resource))
98         nodes.discard(cls.get_jumphost(booking))
99
100         for node in nodes:
101             pdf_nodes.append(cls.get_pdf_host(node))
102
103         return pdf_nodes
104
105     @classmethod
106     def get_pdf_host(cls, host):
107         """
108         method to gather all needed info about a host
109         returns a dict
110         """
111         host_info = {}
112         host_info['name'] = host.template.resource.name
113         host_info['node'] = cls.get_pdf_host_node(host)
114         host_info['disks'] = []
115         for disk in host.profile.storageprofile.all():
116             host_info['disks'].append(cls.get_pdf_host_disk(disk))
117
118         host_info['interfaces'] = []
119         for interface in host.interfaces.all():
120             host_info['interfaces'].append(cls.get_pdf_host_iface(interface))
121
122         host_info['remote'] = cls.get_pdf_host_remote_management(host)
123
124         return host_info
125
126     @classmethod
127     def get_pdf_host_node(cls, host):
128         """
129         returns "node" info for a given host
130         """
131         d = {}
132         d['type'] = "baremetal"
133         d['vendor'] = host.vendor
134         d['model'] = host.model
135         d['memory'] = str(host.profile.ramprofile.first().amount) + "G"
136
137         cpu = host.profile.cpuprofile.first()
138         d['arch'] = cpu.architecture
139         d['cpus'] = cpu.cpus
140         d['cores'] = cpu.cores
141         cflags = cpu.cflags
142         if cflags and cflags.strip():
143             d['cpu_cflags'] = cflags
144         else:
145             d['cpu_cflags'] = "none"
146
147         return d
148
149     @classmethod
150     def get_pdf_host_disk(cls, disk):
151         """
152         returns a dict describing the given disk
153         """
154         disk_info = {}
155         disk_info['name'] = disk.name
156         disk_info['capacity'] = str(disk.size) + "G"
157         disk_info['type'] = disk.media_type
158         disk_info['interface'] = disk.interface
159         disk_info['rotation'] = disk.rotation
160         return disk_info
161
162     @classmethod
163     def get_pdf_host_iface(cls, interface):
164         """
165         returns a dict describing given interface
166         """
167         iface_info = {}
168         iface_info['features'] = "none"
169         iface_info['mac_address'] = interface.mac_address
170         iface_info['name'] = interface.name
171         profile = InterfaceProfile.objects.get(host=interface.host.profile, name=interface.name)
172         iface_info['speed'] = str(int(profile.speed / 1000)) + "gb"
173         return iface_info
174
175     @classmethod
176     def get_pdf_host_remote_management(cls, host):
177         """
178         gives the remote params of the host
179         """
180         man = host.remote_management
181         mgmt = {}
182         mgmt['address'] = man.address
183         mgmt['mac_address'] = man.mac_address
184         mgmt['pass'] = man.password
185         mgmt['type'] = man.management_type
186         mgmt['user'] = man.user
187         mgmt['versions'] = [man.versions]
188         return mgmt