blob: b4697ce8f678eee224b0ece0d3f6fadc9b489bab [file] [log] [blame]
Tony Mack89f70f12013-05-10 20:20:03 -04001import os
2os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
Tony Mackc261bd22013-05-10 21:04:42 -04003import string
4import random
Tony Mack89f70f12013-05-10 20:20:03 -04005
Tony Mack585160e2013-05-07 11:15:59 -04006from netaddr import IPAddress, IPNetwork
Siobhan Tully30fd4292013-05-10 08:59:56 -04007from planetstack import settings
Tony Mack735493a2013-04-04 23:54:28 -04008from django.core import management
Siobhan Tully73291342013-05-10 10:50:08 -04009from planetstack.config import Config
Tony Mackd685bfa2013-05-02 10:09:51 -040010try:
Siobhan Tully30fd4292013-05-10 08:59:56 -040011 from openstack.client import OpenStackClient
12 from openstack.driver import OpenStackDriver
Siobhan Tully30fd4292013-05-10 08:59:56 -040013 from core.models import *
Tony Mackd685bfa2013-05-02 10:09:51 -040014 has_openstack = True
15except:
Siobhan Tully30fd4292013-05-10 08:59:56 -040016 has_openstack = False
Tony Mack951dab42013-05-02 19:51:45 -040017
Tony Mack89f70f12013-05-10 20:20:03 -040018manager_enabled = Config().api_nova_enabled
Tony Mack02755d42013-05-02 00:00:10 -040019
Tony Mackc261bd22013-05-10 21:04:42 -040020
21def random_string(size=6):
22 return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(size))
23
Tony Mack02755d42013-05-02 00:00:10 -040024def require_enabled(callable):
Tony Mack02755d42013-05-02 00:00:10 -040025 def wrapper(*args, **kwds):
Tony Mack951dab42013-05-02 19:51:45 -040026 if manager_enabled and has_openstack:
Tony Mack02755d42013-05-02 00:00:10 -040027 return callable(*args, **kwds)
28 else:
29 return None
30 return wrapper
Tony Mack735493a2013-04-04 23:54:28 -040031
32
Tony Mack02755d42013-05-02 00:00:10 -040033class OpenStackManager:
Tony Mack735493a2013-04-04 23:54:28 -040034
Tony Mack02755d42013-05-02 00:00:10 -040035 def __init__(self, auth={}, caller=None):
Tony Mack620f0f32013-05-03 14:18:31 -040036 self.client = None
37 self.driver = None
38 self.caller = None
Tony Mack951dab42013-05-02 19:51:45 -040039 self.has_openstack = has_openstack
Tony Mack620f0f32013-05-03 14:18:31 -040040 self.enabled = manager_enabled
41
42 if has_openstack and manager_enabled:
43 if auth:
Tony Mack41945e02013-05-09 19:25:10 -040044 try:
45 self.init_user(auth, caller)
46 except:
47 # if this fails then it meanse the caller doesn't have a
48 # role at the slice's tenant. if the caller is an admin
49 # just use the admin client/manager.
50 if caller and caller.is_admin:
51 self.init_admin()
52 else: raise
Tony Mack620f0f32013-05-03 14:18:31 -040053 else:
Tony Mack41945e02013-05-09 19:25:10 -040054 self.init_admin()
55
56 @require_enabled
57 def init_user(self, auth, caller):
58 self.client = OpenStackClient(**auth)
59 self.driver = OpenStackDriver(client=self.client)
60 self.caller = caller
61
62 @require_enabled
63 def init_admin(self):
64 # use the admin credentials
65 self.client = OpenStackClient()
66 self.driver = OpenStackDriver(client=self.client)
67 self.caller = self.driver.admin_user
Siobhan Tully73291342013-05-10 10:50:08 -040068 self.caller.kuser_id = self.caller.id
Tony Mack735493a2013-04-04 23:54:28 -040069
Tony Mack02755d42013-05-02 00:00:10 -040070 @require_enabled
71 def save_role(self, role):
Siobhan Tully47ae1b52013-05-10 15:53:14 -040072 if not role.role:
Tony Mack02755d42013-05-02 00:00:10 -040073 keystone_role = self.driver.create_role(role.role_type)
Siobhan Tully47ae1b52013-05-10 15:53:14 -040074 role.role = keystone_role.id
Tony Mack02755d42013-05-02 00:00:10 -040075
76 @require_enabled
77 def delete_role(self, role):
Siobhan Tully47ae1b52013-05-10 15:53:14 -040078 if role.role:
79 self.driver.delete_role({'id': role.role})
Tony Mackd685bfa2013-05-02 10:09:51 -040080
81 @require_enabled
82 def save_key(self, key):
Tony Mack71c685f2013-05-10 20:34:18 -040083 if not key.nkey_id:
Tony Mackc261bd22013-05-10 21:04:42 -040084 key_fields = {'name': random_string(8),
Tony Mackd685bfa2013-05-02 10:09:51 -040085 'key': key.key}
86 nova_key = self.driver.create_keypair(**key_fields)
Tony Mack71c685f2013-05-10 20:34:18 -040087 key.nkey_id = nova_key.id
Tony Mackd685bfa2013-05-02 10:09:51 -040088
89 @require_enabled
90 def delete_key(self, key):
Tony Mack71c685f2013-05-10 20:34:18 -040091 if key.nkey_id:
92 self.driver.delete_keypair(key.nkey_id)
Tony Mackd685bfa2013-05-02 10:09:51 -040093
94 @require_enabled
95 def save_user(self, user):
Siobhan Tully30fd4292013-05-10 08:59:56 -040096 if not user.kuser_id:
Tony Mackd685bfa2013-05-02 10:09:51 -040097 name = user.email[:user.email.find('@')]
98 user_fields = {'name': name,
99 'email': user.email,
100 'password': user.password,
101 'enabled': True}
102 keystone_user = self.driver.create_user(**user_fields)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400103 user.kuser_id = keystone_user.id
Tony Mack386419d2013-05-05 11:48:43 -0400104 if user.site:
Siobhan Tully73291342013-05-10 10:50:08 -0400105 self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'user')
Tony Mack386419d2013-05-05 11:48:43 -0400106 if user.is_admin:
Siobhan Tully73291342013-05-10 10:50:08 -0400107 self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'admin')
Tony Mack386419d2013-05-05 11:48:43 -0400108 else:
Tony Mack85d18832013-05-09 17:02:31 -0400109 # may have admin role so attempt to remove it
Siobhan Tully73291342013-05-10 10:50:08 -0400110 self.driver.delete_user_role(user.kuser_id, user.site.tenant_id, 'admin')
Tony Mack386419d2013-05-05 11:48:43 -0400111
Tony Mackd685bfa2013-05-02 10:09:51 -0400112 @require_enabled
113 def delete_user(self, user):
Siobhan Tully30fd4292013-05-10 08:59:56 -0400114 if user.kuser_id:
115 self.driver.delete_user(user.kuser_id)
Tony Mackd685bfa2013-05-02 10:09:51 -0400116
Tony Mack60722062013-05-02 10:57:04 -0400117 @require_enabled
Tony Macked163d72013-05-02 20:05:42 -0400118 def save_site(self, site, add_role=True):
Tony Mack60722062013-05-02 10:57:04 -0400119 if not site.tenant_id:
120 tenant = self.driver.create_tenant(tenant_name=site.login_base,
121 description=site.name,
122 enabled=site.enabled)
123 site.tenant_id = tenant.id
124 # give caller an admin role at the tenant they've created
Siobhan Tully30fd4292013-05-10 08:59:56 -0400125 self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin')
Tony Mack60722062013-05-02 10:57:04 -0400126
127 # update the record
128 if site.id and site.tenant_id:
129 self.driver.update_tenant(site.tenant_id,
130 description=site.name,
131 enabled=site.enabled)
132
133 @require_enabled
134 def delete_site(self, site):
135 if site.tenant_id:
136 self.driver.delete_tenant(site.tenant_id)
Tony Mackd685bfa2013-05-02 10:09:51 -0400137
Tony Mack93048c22013-05-02 11:20:26 -0400138 @require_enabled
139 def save_slice(self, slice):
140 if not slice.tenant_id:
141 nova_fields = {'tenant_name': slice.name,
142 'description': slice.description,
143 'enabled': slice.enabled}
144 tenant = self.driver.create_tenant(**nova_fields)
145 slice.tenant_id = tenant.id
146
147 # give caller an admin role at the tenant they've created
Siobhan Tully30fd4292013-05-10 08:59:56 -0400148 self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin')
Tony Mack93048c22013-05-02 11:20:26 -0400149
150 # refresh credentials using this tenant
151 self.driver.shell.connect(username=self.driver.shell.keystone.username,
152 password=self.driver.shell.keystone.password,
153 tenant=tenant.name)
154
155 # create network
156 network = self.driver.create_network(slice.name)
157 slice.network_id = network['id']
158
159 # create router
160 router = self.driver.create_router(slice.name)
161 slice.router_id = router['id']
162
Tony Mack585160e2013-05-07 11:15:59 -0400163 # create subnet
164 next_subnet = self.get_next_subnet()
165 cidr = str(next_subnet.cidr)
166 ip_version = next_subnet.version
167 start = str(next_subnet[2])
168 end = str(next_subnet[-2])
169 subnet = self.driver.create_subnet(name=slice.name,
170 network_id = network['id'],
171 cidr_ip = cidr,
172 ip_version = ip_version,
173 start = start,
174 end = end)
175 slice.subnet_id = subnet['id']
176 # add subnet as interface to slice's router
177 self.driver.add_router_interface(router['id'], subnet['id'])
178
179
Tony Mack93048c22013-05-02 11:20:26 -0400180 if slice.id and slice.tenant_id:
181 self.driver.update_tenant(slice.tenant_id,
182 description=slice.description,
183 enabled=slice.enabled)
184
185 @require_enabled
186 def delete_slice(self, slice):
187 if slice.tenant_id:
Tony Mack585160e2013-05-07 11:15:59 -0400188 self.driver.delete_router_interface(slice.router_id, slice.subnet_id)
189 self.driver.delete_subnet(slice.subnet_id)
Tony Mack93048c22013-05-02 11:20:26 -0400190 self.driver.delete_router(slice.router_id)
191 self.driver.delete_network(slice.network_id)
192 self.driver.delete_tenant(slice.tenant_id)
193
Tony Mack585160e2013-05-07 11:15:59 -0400194
195
196 def get_next_subnet(self):
197 # limit ourself to 10.0.x.x for now
198 valid_subnet = lambda net: net.startswith('10.0')
199 subnets = self.driver.shell.quantum.list_subnets()['subnets']
200 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
201 if valid_subnet(subnet['cidr'])]
202 ints.sort()
203 last_ip = IPAddress(ints[-1])
204 last_network = IPNetwork(str(last_ip) + "/24")
205 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
206 return next_network
207
Tony Mack951dab42013-05-02 19:51:45 -0400208 @require_enabled
209 def save_subnet(self, subnet):
210 if not subnet.subnet_id:
211 quantum_subnet = self.driver.create_subnet(name= subnet.slice.name,
212 network_id=subnet.slice.network_id,
213 cidr_ip = subnet.cidr,
214 ip_version=subnet.ip_version,
215 start = subnet.start,
216 end = subnet.end)
217 subnet.subnet_id = quantum_subnet['id']
218 # add subnet as interface to slice's router
219 self.driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id)
220 #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
221 #commands.getstatusoutput(add_route)
222
223
224 @require_enabled
225 def delete_subnet(self, subnet):
226 if subnet.subnet_id:
227 self.driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id)
228 self.driver.delete_subnet(subnet.subnet_id)
229 #del_route = 'route del -net %s' % self.cidr
230 #commands.getstatusoutput(del_route)
smbaker8f5cf5f2013-05-05 13:58:16 -0700231
Tony Mack951dab42013-05-02 19:51:45 -0400232 @require_enabled
233 def save_sliver(self, sliver):
234 if not sliver.instance_id:
235 instance = self.driver.spawn_instance(name=sliver.name,
236 key_name = sliver.key.name,
237 image_id = sliver.image.image_id,
238 hostname = sliver.node.name )
239 sliver.instance_id = instance.id
240 sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
241
Scott Baker13acdd62013-05-08 17:42:56 -0700242 if sliver.instance_id and ("numberCores" in sliver.changed_fields):
smbaker8f5cf5f2013-05-05 13:58:16 -0700243 self.driver.update_instance_metadata(sliver.instance_id, {"cpu_cores": str(sliver.numberCores)})
244
Tony Mack951dab42013-05-02 19:51:45 -0400245 @require_enabled
246 def delete_sliver(self, sliver):
247 if sliver.instance_id:
248 self.driver.destroy_instance(sliver.instance_id)
249
250
Tony Mack735493a2013-04-04 23:54:28 -0400251 def refresh_nodes(self):
252 # collect local nodes
Tony Mack735493a2013-04-04 23:54:28 -0400253 nodes = Node.objects.all()
254 nodes_dict = {}
255 for node in nodes:
Tony Mack48952032013-04-12 11:49:34 -0400256 if 'viccidev10' not in node.name:
257 nodes_dict[node.name] = node
258
Tony Mack51f113d2013-04-13 02:02:22 -0400259 deployment = DeploymentNetwork.objects.filter(name='VICCI')[0]
Tony Mack48952032013-04-12 11:49:34 -0400260 login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws']
261 sites = Site.objects.filter(login_base__in=login_bases)
Tony Mack735493a2013-04-04 23:54:28 -0400262 # collect nova nodes:
Tony Mack48952032013-04-12 11:49:34 -0400263 compute_nodes = self.client.nova.hypervisors.list()
264
Tony Mack735493a2013-04-04 23:54:28 -0400265 compute_nodes_dict = {}
266 for compute_node in compute_nodes:
267 compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node
268
269 # add new nodes:
270 new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys())
Tony Mack51f113d2013-04-13 02:02:22 -0400271 i = 0
272 max = len(sites)
Tony Mack735493a2013-04-04 23:54:28 -0400273 for name in new_node_names:
Tony Mack51f113d2013-04-13 02:02:22 -0400274 if i == max:
275 i = 0
276 site = sites[i]
277 node = Node(name=compute_nodes_dict[name].hypervisor_hostname,
278 site=site,
279 deploymentNetwork=deployment)
Tony Mack735493a2013-04-04 23:54:28 -0400280 node.save()
Tony Mack51f113d2013-04-13 02:02:22 -0400281 i+=1
Tony Mack735493a2013-04-04 23:54:28 -0400282
283 # remove old nodes
284 old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys())
285 Node.objects.filter(name__in=old_node_names).delete()
286
Tony Mack735493a2013-04-04 23:54:28 -0400287 def refresh_images(self):
Tony Mack89f70f12013-05-10 20:20:03 -0400288 from core.models.image import Image
Tony Mack735493a2013-04-04 23:54:28 -0400289 # collect local images
Tony Mack735493a2013-04-04 23:54:28 -0400290 images = Image.objects.all()
291 images_dict = {}
292 for image in images:
293 images_dict[image.name] = image
294
295 # collect glance images
Tony Mack48952032013-04-12 11:49:34 -0400296 glance_images = self.client.glance.get_images()
Tony Mack735493a2013-04-04 23:54:28 -0400297 glance_images_dict = {}
298 for glance_image in glance_images:
299 glance_images_dict[glance_image['name']] = glance_image
300
301 # add new images
302 new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys())
303 for name in new_image_names:
304 image = Image(image_id=glance_images_dict[name]['id'],
305 name=glance_images_dict[name]['name'],
306 disk_format=glance_images_dict[name]['disk_format'],
307 container_format=glance_images_dict[name]['container_format'])
308 image.save()
309
310 # remove old images
311 old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys())
312 Image.objects.filter(name__in=old_image_names).delete()
Tony Mack02755d42013-05-02 00:00:10 -0400313
314