blob: e310883fe4b3f0590ffd1b19f0fddeb5f670fc17 [file] [log] [blame]
Tony Mack89f70f12013-05-10 20:20:03 -04001import os
Siobhan Tullybf1153a2013-05-27 20:53:48 -04002#os.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
Tony Mackb0d97422013-06-10 09:57:45 -04009from core.models import *
Siobhan Tully73291342013-05-10 10:50:08 -040010from planetstack.config import Config
Tony Mackd685bfa2013-05-02 10:09:51 -040011try:
Siobhan Tully30fd4292013-05-10 08:59:56 -040012 from openstack.client import OpenStackClient
13 from openstack.driver import OpenStackDriver
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
Tony Mackc59fcaf2013-06-10 11:14:04 -040057 def init_caller(self, caller, tenant):
58 auth = {'username': caller.email,
59 'password': '',
60 'tenant': tenant}
Tony Mack41945e02013-05-09 19:25:10 -040061 self.client = OpenStackClient(**auth)
62 self.driver = OpenStackDriver(client=self.client)
63 self.caller = caller
64
65 @require_enabled
Tony Mackc59fcaf2013-06-10 11:14:04 -040066 def init_admin(self, tenant=None):
Tony Mack41945e02013-05-09 19:25:10 -040067 # use the admin credentials
Tony Mackc59fcaf2013-06-10 11:14:04 -040068 self.client = OpenStackClient(tenant=tenant)
Tony Mack41945e02013-05-09 19:25:10 -040069 self.driver = OpenStackDriver(client=self.client)
70 self.caller = self.driver.admin_user
Siobhan Tully73291342013-05-10 10:50:08 -040071 self.caller.kuser_id = self.caller.id
Tony Mack735493a2013-04-04 23:54:28 -040072
Tony Mack02755d42013-05-02 00:00:10 -040073 @require_enabled
74 def save_role(self, role):
Siobhan Tully47ae1b52013-05-10 15:53:14 -040075 if not role.role:
Tony Mack02755d42013-05-02 00:00:10 -040076 keystone_role = self.driver.create_role(role.role_type)
Siobhan Tully47ae1b52013-05-10 15:53:14 -040077 role.role = keystone_role.id
Tony Mack02755d42013-05-02 00:00:10 -040078
79 @require_enabled
80 def delete_role(self, role):
Siobhan Tully47ae1b52013-05-10 15:53:14 -040081 if role.role:
82 self.driver.delete_role({'id': role.role})
Tony Mackd685bfa2013-05-02 10:09:51 -040083
84 @require_enabled
Tony Mackb0d97422013-06-10 09:57:45 -040085 def save_key(self, key, name):
86 key_fields = {'name': name,
87 'public_key': key}
88 nova_key = self.driver.create_keypair(**key_fields)
Tony Mackd685bfa2013-05-02 10:09:51 -040089
90 @require_enabled
91 def delete_key(self, key):
Tony Mack71c685f2013-05-10 20:34:18 -040092 if key.nkey_id:
93 self.driver.delete_keypair(key.nkey_id)
Tony Mackd685bfa2013-05-02 10:09:51 -040094
95 @require_enabled
96 def save_user(self, user):
Siobhan Tully30fd4292013-05-10 08:59:56 -040097 if not user.kuser_id:
Tony Mackd685bfa2013-05-02 10:09:51 -040098 name = user.email[:user.email.find('@')]
99 user_fields = {'name': name,
100 'email': user.email,
101 'password': user.password,
102 'enabled': True}
103 keystone_user = self.driver.create_user(**user_fields)
Siobhan Tully30fd4292013-05-10 08:59:56 -0400104 user.kuser_id = keystone_user.id
Tony Mackb0d97422013-06-10 09:57:45 -0400105
106 if user.public_key:
107 self.save_key(user.public_key, user.keyname)
108
Tony Mack386419d2013-05-05 11:48:43 -0400109 if user.site:
Siobhan Tully73291342013-05-10 10:50:08 -0400110 self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'user')
Tony Mack386419d2013-05-05 11:48:43 -0400111 if user.is_admin:
Siobhan Tully73291342013-05-10 10:50:08 -0400112 self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'admin')
Tony Mack386419d2013-05-05 11:48:43 -0400113 else:
Tony Mack85d18832013-05-09 17:02:31 -0400114 # may have admin role so attempt to remove it
Siobhan Tully73291342013-05-10 10:50:08 -0400115 self.driver.delete_user_role(user.kuser_id, user.site.tenant_id, 'admin')
Tony Mack386419d2013-05-05 11:48:43 -0400116
Tony Mackd685bfa2013-05-02 10:09:51 -0400117 @require_enabled
118 def delete_user(self, user):
Siobhan Tully30fd4292013-05-10 08:59:56 -0400119 if user.kuser_id:
120 self.driver.delete_user(user.kuser_id)
Tony Mackd685bfa2013-05-02 10:09:51 -0400121
Tony Mack60722062013-05-02 10:57:04 -0400122 @require_enabled
Tony Macked163d72013-05-02 20:05:42 -0400123 def save_site(self, site, add_role=True):
Tony Mack60722062013-05-02 10:57:04 -0400124 if not site.tenant_id:
125 tenant = self.driver.create_tenant(tenant_name=site.login_base,
126 description=site.name,
127 enabled=site.enabled)
128 site.tenant_id = tenant.id
129 # give caller an admin role at the tenant they've created
Siobhan Tully30fd4292013-05-10 08:59:56 -0400130 self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin')
Tony Mack60722062013-05-02 10:57:04 -0400131
132 # update the record
133 if site.id and site.tenant_id:
134 self.driver.update_tenant(site.tenant_id,
135 description=site.name,
136 enabled=site.enabled)
137
138 @require_enabled
139 def delete_site(self, site):
140 if site.tenant_id:
141 self.driver.delete_tenant(site.tenant_id)
Tony Mackd685bfa2013-05-02 10:09:51 -0400142
Tony Mack93048c22013-05-02 11:20:26 -0400143 @require_enabled
144 def save_slice(self, slice):
145 if not slice.tenant_id:
146 nova_fields = {'tenant_name': slice.name,
147 'description': slice.description,
148 'enabled': slice.enabled}
149 tenant = self.driver.create_tenant(**nova_fields)
150 slice.tenant_id = tenant.id
151
152 # give caller an admin role at the tenant they've created
Siobhan Tully30fd4292013-05-10 08:59:56 -0400153 self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin')
Tony Mack93048c22013-05-02 11:20:26 -0400154
155 # refresh credentials using this tenant
156 self.driver.shell.connect(username=self.driver.shell.keystone.username,
157 password=self.driver.shell.keystone.password,
158 tenant=tenant.name)
159
160 # create network
161 network = self.driver.create_network(slice.name)
162 slice.network_id = network['id']
163
164 # create router
165 router = self.driver.create_router(slice.name)
166 slice.router_id = router['id']
167
Tony Mack585160e2013-05-07 11:15:59 -0400168 # create subnet
169 next_subnet = self.get_next_subnet()
170 cidr = str(next_subnet.cidr)
171 ip_version = next_subnet.version
172 start = str(next_subnet[2])
173 end = str(next_subnet[-2])
174 subnet = self.driver.create_subnet(name=slice.name,
175 network_id = network['id'],
176 cidr_ip = cidr,
177 ip_version = ip_version,
178 start = start,
179 end = end)
180 slice.subnet_id = subnet['id']
181 # add subnet as interface to slice's router
182 self.driver.add_router_interface(router['id'], subnet['id'])
Tony Mackf180f212013-05-28 09:19:01 -0400183 # add external route
184 self.driver.add_external_route(subnet)
Tony Mack585160e2013-05-07 11:15:59 -0400185
186
Tony Mack93048c22013-05-02 11:20:26 -0400187 if slice.id and slice.tenant_id:
188 self.driver.update_tenant(slice.tenant_id,
189 description=slice.description,
190 enabled=slice.enabled)
191
192 @require_enabled
193 def delete_slice(self, slice):
194 if slice.tenant_id:
Tony Mack585160e2013-05-07 11:15:59 -0400195 self.driver.delete_router_interface(slice.router_id, slice.subnet_id)
196 self.driver.delete_subnet(slice.subnet_id)
Tony Mack93048c22013-05-02 11:20:26 -0400197 self.driver.delete_router(slice.router_id)
198 self.driver.delete_network(slice.network_id)
199 self.driver.delete_tenant(slice.tenant_id)
Tony Mackf180f212013-05-28 09:19:01 -0400200 # delete external route
201 subnet = None
202 subnets = self.driver.shell.quantum.list_subnets()['subnets']
203 for snet in subnets:
204 if snet['id'] == slice.subnet_id:
205 subnet = snet
206 if subnet:
207 self.driver.delete_external_route(subnet)
Tony Mack93048c22013-05-02 11:20:26 -0400208
Tony Mack585160e2013-05-07 11:15:59 -0400209
Tony Mackc59fcaf2013-06-10 11:14:04 -0400210 @require_enabled
Tony Mack585160e2013-05-07 11:15:59 -0400211 def get_next_subnet(self):
212 # limit ourself to 10.0.x.x for now
213 valid_subnet = lambda net: net.startswith('10.0')
214 subnets = self.driver.shell.quantum.list_subnets()['subnets']
215 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
216 if valid_subnet(subnet['cidr'])]
217 ints.sort()
218 last_ip = IPAddress(ints[-1])
219 last_network = IPNetwork(str(last_ip) + "/24")
220 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
221 return next_network
222
Tony Mack951dab42013-05-02 19:51:45 -0400223 @require_enabled
224 def save_subnet(self, subnet):
225 if not subnet.subnet_id:
226 quantum_subnet = self.driver.create_subnet(name= subnet.slice.name,
227 network_id=subnet.slice.network_id,
228 cidr_ip = subnet.cidr,
229 ip_version=subnet.ip_version,
230 start = subnet.start,
231 end = subnet.end)
232 subnet.subnet_id = quantum_subnet['id']
233 # add subnet as interface to slice's router
234 self.driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id)
235 #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
236 #commands.getstatusoutput(add_route)
237
238
239 @require_enabled
240 def delete_subnet(self, subnet):
241 if subnet.subnet_id:
242 self.driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id)
243 self.driver.delete_subnet(subnet.subnet_id)
244 #del_route = 'route del -net %s' % self.cidr
245 #commands.getstatusoutput(del_route)
smbaker8f5cf5f2013-05-05 13:58:16 -0700246
Tony Mack951dab42013-05-02 19:51:45 -0400247 @require_enabled
248 def save_sliver(self, sliver):
249 if not sliver.instance_id:
250 instance = self.driver.spawn_instance(name=sliver.name,
Tony Mackb0d97422013-06-10 09:57:45 -0400251 key_name = sliver.creator.keyname,
Tony Mack951dab42013-05-02 19:51:45 -0400252 image_id = sliver.image.image_id,
253 hostname = sliver.node.name )
254 sliver.instance_id = instance.id
255 sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
256
Scott Baker13acdd62013-05-08 17:42:56 -0700257 if sliver.instance_id and ("numberCores" in sliver.changed_fields):
smbaker8f5cf5f2013-05-05 13:58:16 -0700258 self.driver.update_instance_metadata(sliver.instance_id, {"cpu_cores": str(sliver.numberCores)})
259
Tony Mack951dab42013-05-02 19:51:45 -0400260 @require_enabled
261 def delete_sliver(self, sliver):
262 if sliver.instance_id:
263 self.driver.destroy_instance(sliver.instance_id)
264
265
Tony Mack735493a2013-04-04 23:54:28 -0400266 def refresh_nodes(self):
267 # collect local nodes
Tony Mack735493a2013-04-04 23:54:28 -0400268 nodes = Node.objects.all()
269 nodes_dict = {}
270 for node in nodes:
Tony Mack48952032013-04-12 11:49:34 -0400271 if 'viccidev10' not in node.name:
272 nodes_dict[node.name] = node
273
Siobhan Tullybf1153a2013-05-27 20:53:48 -0400274 deployment = Deployment.objects.filter(name='VICCI')[0]
Tony Mack48952032013-04-12 11:49:34 -0400275 login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws']
276 sites = Site.objects.filter(login_base__in=login_bases)
Tony Mack735493a2013-04-04 23:54:28 -0400277 # collect nova nodes:
Tony Mack48952032013-04-12 11:49:34 -0400278 compute_nodes = self.client.nova.hypervisors.list()
279
Tony Mack735493a2013-04-04 23:54:28 -0400280 compute_nodes_dict = {}
281 for compute_node in compute_nodes:
282 compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node
283
284 # add new nodes:
285 new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys())
Tony Mack51f113d2013-04-13 02:02:22 -0400286 i = 0
287 max = len(sites)
Tony Mack735493a2013-04-04 23:54:28 -0400288 for name in new_node_names:
Tony Mack51f113d2013-04-13 02:02:22 -0400289 if i == max:
290 i = 0
291 site = sites[i]
292 node = Node(name=compute_nodes_dict[name].hypervisor_hostname,
293 site=site,
Tony Mackb0d97422013-06-10 09:57:45 -0400294 deployment=deployment)
Tony Mack735493a2013-04-04 23:54:28 -0400295 node.save()
Tony Mack51f113d2013-04-13 02:02:22 -0400296 i+=1
Tony Mack735493a2013-04-04 23:54:28 -0400297
298 # remove old nodes
299 old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys())
300 Node.objects.filter(name__in=old_node_names).delete()
301
Tony Mack735493a2013-04-04 23:54:28 -0400302 def refresh_images(self):
Tony Mack89f70f12013-05-10 20:20:03 -0400303 from core.models.image import Image
Tony Mack735493a2013-04-04 23:54:28 -0400304 # collect local images
Tony Mack735493a2013-04-04 23:54:28 -0400305 images = Image.objects.all()
306 images_dict = {}
307 for image in images:
308 images_dict[image.name] = image
309
310 # collect glance images
Tony Mack48952032013-04-12 11:49:34 -0400311 glance_images = self.client.glance.get_images()
Tony Mack735493a2013-04-04 23:54:28 -0400312 glance_images_dict = {}
313 for glance_image in glance_images:
314 glance_images_dict[glance_image['name']] = glance_image
315
316 # add new images
317 new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys())
318 for name in new_image_names:
319 image = Image(image_id=glance_images_dict[name]['id'],
320 name=glance_images_dict[name]['name'],
321 disk_format=glance_images_dict[name]['disk_format'],
322 container_format=glance_images_dict[name]['container_format'])
323 image.save()
324
325 # remove old images
326 old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys())
327 Image.objects.filter(name__in=old_image_names).delete()
Tony Mack02755d42013-05-02 00:00:10 -0400328
329