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