add nick
[laas.git] / src / dashboard / populate_db_iol.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 import json
11 import yaml
12
13 from account.models import Lab, UserProfile
14 from django.contrib.auth.models import User
15 from resource_inventory.models import (
16     HostProfile,
17     InterfaceProfile,
18     DiskProfile,
19     CpuProfile,
20     RamProfile,
21     VlanManager,
22     Scenario,
23     Installer,
24     Opsys,
25     OPNFVRole,
26     Image,
27     Interface,
28     Host
29 )
30
31
32 class Populator:
33
34     def __init__(self):
35         self.host_profile_count = 0
36         self.generic_host_count = 0
37         self.host_profiles = []
38         self.generic_bundle_count = 0
39         self.booking_count = 0
40
41     def make_host_profile(self, lab, data):
42         hostProfile = HostProfile.objects.create(
43             host_type=data['host']['type'],
44             name=data['host']['name'],
45             description=data['host']['description']
46         )
47         hostProfile.save()
48
49         for iface_data in data['interfaces']:
50
51             interfaceProfile = InterfaceProfile.objects.create(
52                 speed=iface_data['speed'],
53                 name=iface_data['name'],
54                 host=hostProfile
55             )
56             interfaceProfile.save()
57
58         for disk_data in data['disks']:
59
60             diskProfile = DiskProfile.objects.create(
61                 size=disk_data['size'],
62                 media_type=disk_data['type'],
63                 name=disk_data['name'],
64                 host=hostProfile
65             )
66             diskProfile.save()
67
68         cpuProfile = CpuProfile.objects.create(
69             cores=data['cpu']['cores'],
70             architecture=data['cpu']['arch'],
71             cpus=data['cpu']['cpus'],
72             host=hostProfile
73         )
74         cpuProfile.save()
75         ramProfile = RamProfile.objects.create(
76             amount=data['ram']['amount'],
77             channels=data['ram']['channels'],
78             host=hostProfile
79         )
80         ramProfile.save()
81         hostProfile.labs.add(lab)
82         return hostProfile
83
84     def make_users(self):
85         user_pberberian = User.objects.create(username="pberberian")
86         user_pberberian.save()
87         user_pberberian_prof = UserProfile.objects.create(user=user_pberberian)
88         user_pberberian_prof.save()
89
90         user_sbergeron = User.objects.create(username="sbergeron")
91         user_sbergeron.save()
92         user_sbergeron_prof = UserProfile.objects.create(user=user_sbergeron)
93         user_sbergeron_prof.save()
94         return [user_sbergeron, user_pberberian]
95
96     def make_labs(self):
97         unh_iol = User.objects.create(username="unh_iol")
98         unh_iol.save()
99         vlans = []
100         reserved = []
101         for i in range(1, 4096):
102             vlans.append(1)
103             reserved.append(0)
104         iol = Lab.objects.create(
105             lab_user=unh_iol,
106             name="UNH_IOL",
107             vlan_manager=VlanManager.objects.create(
108                 vlans=json.dumps(vlans),
109                 reserved_vlans=json.dumps(reserved),
110                 allow_overlapping=False,
111                 block_size=20,
112             ),
113             api_token=Lab.make_api_token(),
114             contact_email="nfv-lab@iol.unh.edu",
115             location="University of New Hampshire, Durham NH, 03824 USA"
116         )
117         return [iol]
118
119     def make_configurations(self):
120         # scenarios
121         scen1 = Scenario.objects.create(name="os-nosdn-nofeature-noha")
122         scen2 = Scenario.objects.create(name="os-odl-kvm-ha")
123         scen3 = Scenario.objects.create(name="os-nosdn-nofeature-ha")
124
125         # installers
126         fuel = Installer.objects.create(name="Fuel")
127         fuel.sup_scenarios.add(scen1)
128         fuel.sup_scenarios.add(scen3)
129         fuel.save()
130         joid = Installer.objects.create(name="Joid")
131         joid.sup_scenarios.add(scen1)
132         joid.sup_scenarios.add(scen2)
133         joid.save()
134         apex = Installer.objects.create(name="Apex")
135         apex.sup_scenarios.add(scen2)
136         apex.sup_scenarios.add(scen3)
137         apex.save()
138         daisy = Installer.objects.create(name="Daisy")
139         daisy.sup_scenarios.add(scen1)
140         daisy.sup_scenarios.add(scen2)
141         daisy.sup_scenarios.add(scen3)
142         daisy.save()
143         compass = Installer.objects.create(name="Compass")
144         compass.sup_scenarios.add(scen1)
145         compass.sup_scenarios.add(scen3)
146         compass.save()
147
148         # operating systems
149         ubuntu = Opsys.objects.create(name="Ubuntu")
150         ubuntu.sup_installers.add(compass)
151         ubuntu.sup_installers.add(joid)
152         ubuntu.save()
153         centos = Opsys.objects.create(name="CentOs")
154         centos.sup_installers.add(apex)
155         centos.sup_installers.add(fuel)
156         centos.save()
157         suse = Opsys.objects.create(name="Suse")
158         suse.sup_installers.add(fuel)
159         suse.save()
160
161         # opnfv roles
162         OPNFVRole.objects.create(name="Compute", description="Does the heavy lifting")
163         OPNFVRole.objects.create(name="Controller", description="Controls everything")
164         OPNFVRole.objects.create(name="Jumphost", description="Entry Point")
165
166         lab = Lab.objects.first()
167         user = UserProfile.objects.first().user
168         Image.objects.create(
169             lab_id=23,
170             name="hpe centos",
171             from_lab=lab,
172             owner=user,
173             host_type=HostProfile.objects.get(name="hpe")
174         )
175         Image.objects.create(
176             lab_id=25,
177             name="hpe ubuntu",
178             from_lab=lab,
179             owner=user,
180             host_type=HostProfile.objects.get(name="hpe")
181         )
182
183         Image.objects.create(
184             lab_id=26,
185             name="hpe suse",
186             from_lab=lab,
187             owner=user,
188             host_type=HostProfile.objects.get(name="hpe")
189         )
190
191         Image.objects.create(
192             lab_id=27,
193             name="arm ubuntu",
194             from_lab=lab,
195             owner=user,
196             host_type=HostProfile.objects.get(name="arm")
197         )
198
199     def make_lab_hosts(self, hostcount, profile, lab, data, offset=1):
200         for i in range(hostcount):
201             name = "Host_" + lab.name + "_" + profile.name + "_" + str(i + offset)
202             host = Host.objects.create(
203                 name=name,
204                 lab=lab,
205                 profile=profile,
206                 labid=data[i]['labid']
207             )
208             for iface_profile in profile.interfaceprofile.all():
209                 iface_data = data[i]['interfaces'][iface_profile.name]
210                 Interface.objects.create(
211                     mac_address=iface_data['mac'],
212                     bus_address=iface_data['bus'],
213                     name=iface_profile.name,
214                     host=host
215                 )
216
217     def make_profile_data(self):
218         """
219         Create Profile Data.
220
221         returns a dictionary of data from the yaml files
222         created by inspection scripts
223         """
224         data = []
225         for prof in ["hpe", "arm"]:  # TODO
226             profile_dict = {}
227             host = {
228                 "name": prof,
229                 "type": 0,
230                 "description": "some LaaS servers"
231             }
232             profile_dict['host'] = host
233             profile_dict['interfaces'] = []
234             for interface in [{"name": "eno1", "speed": 1000}, {"name": "eno2", "speed": 10000}]:  # TODO
235                 iface_dict = {}
236                 iface_dict["name"] = interface['name']
237                 iface_dict['speed'] = interface['speed']
238                 profile_dict['interfaces'].append(iface_dict)
239
240             profile_dict['disks'] = []
241             for disk in [{"size": 1000, "type": "ssd", "name": "sda"}]:  # TODO
242                 disk_dict = {}
243                 disk_dict['size'] = disk['size']
244                 disk_dict['type'] = disk['type']
245                 disk_dict['name'] = disk['name']
246                 profile_dict['disks'].append(disk_dict)
247
248             # cpu
249             cpu = {}
250             cpu['cores'] = 4
251             cpu['arch'] = "x86"
252             cpu['cpus'] = 2
253             profile_dict['cpu'] = cpu
254
255             # ram
256             ram = {}
257             ram['amount'] = 256
258             ram['channels'] = 4
259             profile_dict['ram'] = ram
260
261             data.append(profile_dict)
262
263         return data
264
265     def get_lab_data(self, lab):
266         data = {}
267         path = "/laas_dashboard/data/" + lab.name + "/"
268         host_file = open(path + "hostlist.json")
269         host_structure = json.loads(host_file.read())
270         host_file.close()
271         for profile in host_structure['profiles'].keys():
272             data[profile] = {}
273             prof_path = path + profile
274             for host in host_structure['profiles'][profile]:
275                 host_file = open(prof_path + "/" + host + ".yaml")
276                 host_data = yaml.load(host_file.read())
277                 host_file.close()
278                 data[profile][host] = host_data
279         return data
280
281     def make_profiles_and_hosts(self, lab, lab_data):
282         for host_profile_name, host_data_dict in lab_data.items():
283             if len(host_data_dict) < 1:
284                 continue
285             host_profile = HostProfile.objects.create(
286                 name=host_profile_name,
287                 description=""
288             )
289             host_profile.labs.add(lab)
290             example_host_data = list(host_data_dict.values())[0]
291
292             cpu_data = example_host_data['cpu']
293             CpuProfile.objects.create(
294                 cores=cpu_data['cores'],
295                 architecture=cpu_data['arch'],
296                 cpus=cpu_data['cpus'],
297                 host=host_profile
298             )
299
300             ram_data = example_host_data['memory']
301             RamProfile.objects.create(
302                 amount=int(ram_data[:-1]),
303                 channels=1,
304                 host=host_profile
305             )
306
307             disks_data = example_host_data['disk']
308             for disk_data in disks_data:
309                 size = 0
310                 try:
311                     size = int(disk_data['size'].split('.')[0])
312                 except Exception:
313                     size = int(disk_data['size'].split('.')[0][:-1])
314                 DiskProfile.objects.create(
315                     size=size,
316                     media_type="SSD",
317                     name=disk_data['name'],
318                     host=host_profile
319                 )
320
321             ifaces_data = example_host_data['interface']
322             for iface_data in ifaces_data:
323                 InterfaceProfile.objects.create(
324                     speed=iface_data['speed'],
325                     name=iface_data['name'],
326                     host=host_profile
327                 )
328
329             # all profiles created
330             for hostname, host_data in host_data_dict.items():
331                 host = Host.objects.create(
332                     name=hostname,
333                     labid=hostname,
334                     profile=host_profile,
335                     lab=lab
336                 )
337                 for iface_data in host_data['interface']:
338                     Interface.objects.create(
339                         mac_address=iface_data['mac'],
340                         bus_address=iface_data['busaddr'],
341                         name=iface_data['name'],
342                         host=host
343                     )
344
345     def populate(self):
346         self.labs = self.make_labs()
347         # We should use the existing users, not creating our own
348         for lab in self.labs:
349             lab_data = self.get_lab_data(lab)
350             self.make_profiles_and_hosts(lab, lab_data)
351
352         # We will add opnfv info and images as they are created and supported