blob: 669a05f63f26ee422fd7fd7c0f1d896d88778436 [file] [log] [blame]
Tony Mack585160e2013-05-07 11:15:59 -04001from netaddr import IPAddress, IPNetwork
Tony Mack735493a2013-04-04 23:54:28 -04002from plstackapi.planetstack import settings
3from django.core import management
4management.setup_environ(settings)
Tony Mack78568882013-05-07 15:21:15 -04005from plstackapi.planetstack.config import Config
Tony Mackd685bfa2013-05-02 10:09:51 -04006try:
7 from plstackapi.openstack.client import OpenStackClient
8 from plstackapi.openstack.driver import OpenStackDriver
Tony Mackd685bfa2013-05-02 10:09:51 -04009 from plstackapi.core.models import *
10 has_openstack = True
11except:
Tony Mack4df0eaa2013-05-07 15:23:26 -040012 has_openstack = False
Tony Mack951dab42013-05-02 19:51:45 -040013
14manager_enabled = Config().api_nova_enabled
Tony Mack02755d42013-05-02 00:00:10 -040015
16def require_enabled(callable):
Tony Mack02755d42013-05-02 00:00:10 -040017 def wrapper(*args, **kwds):
Tony Mack951dab42013-05-02 19:51:45 -040018 if manager_enabled and has_openstack:
Tony Mack02755d42013-05-02 00:00:10 -040019 return callable(*args, **kwds)
20 else:
21 return None
22 return wrapper
Tony Mack735493a2013-04-04 23:54:28 -040023
24
Tony Mack02755d42013-05-02 00:00:10 -040025class OpenStackManager:
Tony Mack735493a2013-04-04 23:54:28 -040026
Tony Mack02755d42013-05-02 00:00:10 -040027 def __init__(self, auth={}, caller=None):
Tony Mack620f0f32013-05-03 14:18:31 -040028 self.client = None
29 self.driver = None
30 self.caller = None
Tony Mack951dab42013-05-02 19:51:45 -040031 self.has_openstack = has_openstack
Tony Mack620f0f32013-05-03 14:18:31 -040032 self.enabled = manager_enabled
33
34 if has_openstack and manager_enabled:
35 if auth:
Tony Mack41945e02013-05-09 19:25:10 -040036 try:
37 self.init_user(auth, caller)
38 except:
39 # if this fails then it meanse the caller doesn't have a
40 # role at the slice's tenant. if the caller is an admin
41 # just use the admin client/manager.
42 if caller and caller.is_admin:
43 self.init_admin()
44 else: raise
Tony Mack620f0f32013-05-03 14:18:31 -040045 else:
Tony Mack41945e02013-05-09 19:25:10 -040046 self.init_admin()
47
48 @require_enabled
49 def init_user(self, auth, caller):
50 self.client = OpenStackClient(**auth)
51 self.driver = OpenStackDriver(client=self.client)
52 self.caller = caller
53
54 @require_enabled
55 def init_admin(self):
56 # use the admin credentials
57 self.client = OpenStackClient()
58 self.driver = OpenStackDriver(client=self.client)
59 self.caller = self.driver.admin_user
60 self.caller.user_id = self.caller.id
Tony Mack735493a2013-04-04 23:54:28 -040061
Tony Mack02755d42013-05-02 00:00:10 -040062 @require_enabled
63 def save_role(self, role):
64 if not role.role_id:
65 keystone_role = self.driver.create_role(role.role_type)
66 role.role_id = keystone_role.id
67
68 @require_enabled
69 def delete_role(self, role):
70 if role.role_id:
Tony Mackd685bfa2013-05-02 10:09:51 -040071 self.driver.delete_role({'id': role.role_id})
72
73 @require_enabled
74 def save_key(self, key):
75 if not key.key_id:
Tony Mack85d18832013-05-09 17:02:31 -040076 key_fields = {'name': key.user.email[:key.user.email.find('@')],
Tony Mackd685bfa2013-05-02 10:09:51 -040077 'key': key.key}
78 nova_key = self.driver.create_keypair(**key_fields)
79 key.key_id = nova_key.id
80
81 @require_enabled
82 def delete_key(self, key):
83 if key.key_id:
84 self.driver.delete_keypair(key.key_id)
85
86 @require_enabled
87 def save_user(self, user):
88 if not user.user_id:
89 name = user.email[:user.email.find('@')]
90 user_fields = {'name': name,
91 'email': user.email,
92 'password': user.password,
93 'enabled': True}
94 keystone_user = self.driver.create_user(**user_fields)
95 user.user_id = keystone_user.id
Tony Mack386419d2013-05-05 11:48:43 -040096 if user.site:
Tony Mackd84caef2013-05-09 17:22:31 -040097 self.driver.add_user_role(user.user_id, user.site.tenant_id, 'user')
Tony Mack386419d2013-05-05 11:48:43 -040098 if user.is_admin:
Tony Mack85d18832013-05-09 17:02:31 -040099 self.driver.add_user_role(user.user_id, user.site.tenant_id, 'admin')
Tony Mack386419d2013-05-05 11:48:43 -0400100 else:
Tony Mack85d18832013-05-09 17:02:31 -0400101 # may have admin role so attempt to remove it
Tony Mackd84caef2013-05-09 17:22:31 -0400102 self.driver.delete_user_role(user.user_id, user.site.tenant_id, 'admin')
Tony Mack386419d2013-05-05 11:48:43 -0400103
Tony Mackd685bfa2013-05-02 10:09:51 -0400104 @require_enabled
105 def delete_user(self, user):
106 if user.user_id:
107 self.driver.delete_user(user.user_id)
108
Tony Mack60722062013-05-02 10:57:04 -0400109 @require_enabled
Tony Macked163d72013-05-02 20:05:42 -0400110 def save_site(self, site, add_role=True):
Tony Mack60722062013-05-02 10:57:04 -0400111 if not site.tenant_id:
112 tenant = self.driver.create_tenant(tenant_name=site.login_base,
113 description=site.name,
114 enabled=site.enabled)
115 site.tenant_id = tenant.id
116 # give caller an admin role at the tenant they've created
117 self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin')
118
119 # update the record
120 if site.id and site.tenant_id:
121 self.driver.update_tenant(site.tenant_id,
122 description=site.name,
123 enabled=site.enabled)
124
125 @require_enabled
126 def delete_site(self, site):
127 if site.tenant_id:
128 self.driver.delete_tenant(site.tenant_id)
Tony Mackd685bfa2013-05-02 10:09:51 -0400129
Tony Mack93048c22013-05-02 11:20:26 -0400130 @require_enabled
131 def save_slice(self, slice):
132 if not slice.tenant_id:
133 nova_fields = {'tenant_name': slice.name,
134 'description': slice.description,
135 'enabled': slice.enabled}
136 tenant = self.driver.create_tenant(**nova_fields)
137 slice.tenant_id = tenant.id
138
139 # give caller an admin role at the tenant they've created
140 self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin')
141
142 # refresh credentials using this tenant
143 self.driver.shell.connect(username=self.driver.shell.keystone.username,
144 password=self.driver.shell.keystone.password,
145 tenant=tenant.name)
146
147 # create network
148 network = self.driver.create_network(slice.name)
149 slice.network_id = network['id']
150
151 # create router
152 router = self.driver.create_router(slice.name)
153 slice.router_id = router['id']
154
Tony Mack585160e2013-05-07 11:15:59 -0400155 # create subnet
156 next_subnet = self.get_next_subnet()
157 cidr = str(next_subnet.cidr)
158 ip_version = next_subnet.version
159 start = str(next_subnet[2])
160 end = str(next_subnet[-2])
161 subnet = self.driver.create_subnet(name=slice.name,
162 network_id = network['id'],
163 cidr_ip = cidr,
164 ip_version = ip_version,
165 start = start,
166 end = end)
167 slice.subnet_id = subnet['id']
168 # add subnet as interface to slice's router
169 self.driver.add_router_interface(router['id'], subnet['id'])
170
171
Tony Mack93048c22013-05-02 11:20:26 -0400172 if slice.id and slice.tenant_id:
173 self.driver.update_tenant(slice.tenant_id,
174 description=slice.description,
175 enabled=slice.enabled)
176
177 @require_enabled
178 def delete_slice(self, slice):
179 if slice.tenant_id:
Tony Mack585160e2013-05-07 11:15:59 -0400180 self.driver.delete_router_interface(slice.router_id, slice.subnet_id)
181 self.driver.delete_subnet(slice.subnet_id)
Tony Mack93048c22013-05-02 11:20:26 -0400182 self.driver.delete_router(slice.router_id)
183 self.driver.delete_network(slice.network_id)
184 self.driver.delete_tenant(slice.tenant_id)
185
Tony Mack585160e2013-05-07 11:15:59 -0400186
187
188 def get_next_subnet(self):
189 # limit ourself to 10.0.x.x for now
190 valid_subnet = lambda net: net.startswith('10.0')
191 subnets = self.driver.shell.quantum.list_subnets()['subnets']
192 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
193 if valid_subnet(subnet['cidr'])]
194 ints.sort()
195 last_ip = IPAddress(ints[-1])
196 last_network = IPNetwork(str(last_ip) + "/24")
197 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
198 return next_network
199
Tony Mack951dab42013-05-02 19:51:45 -0400200 @require_enabled
201 def save_subnet(self, subnet):
202 if not subnet.subnet_id:
203 quantum_subnet = self.driver.create_subnet(name= subnet.slice.name,
204 network_id=subnet.slice.network_id,
205 cidr_ip = subnet.cidr,
206 ip_version=subnet.ip_version,
207 start = subnet.start,
208 end = subnet.end)
209 subnet.subnet_id = quantum_subnet['id']
210 # add subnet as interface to slice's router
211 self.driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id)
212 #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
213 #commands.getstatusoutput(add_route)
214
215
216 @require_enabled
217 def delete_subnet(self, subnet):
218 if subnet.subnet_id:
219 self.driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id)
220 self.driver.delete_subnet(subnet.subnet_id)
221 #del_route = 'route del -net %s' % self.cidr
222 #commands.getstatusoutput(del_route)
smbaker8f5cf5f2013-05-05 13:58:16 -0700223
Tony Mack951dab42013-05-02 19:51:45 -0400224 @require_enabled
225 def save_sliver(self, sliver):
226 if not sliver.instance_id:
227 instance = self.driver.spawn_instance(name=sliver.name,
228 key_name = sliver.key.name,
229 image_id = sliver.image.image_id,
230 hostname = sliver.node.name )
231 sliver.instance_id = instance.id
232 sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
233
Scott Baker13acdd62013-05-08 17:42:56 -0700234 if sliver.instance_id and ("numberCores" in sliver.changed_fields):
smbaker8f5cf5f2013-05-05 13:58:16 -0700235 self.driver.update_instance_metadata(sliver.instance_id, {"cpu_cores": str(sliver.numberCores)})
236
Tony Mack951dab42013-05-02 19:51:45 -0400237 @require_enabled
238 def delete_sliver(self, sliver):
239 if sliver.instance_id:
240 self.driver.destroy_instance(sliver.instance_id)
241
242
Tony Mack735493a2013-04-04 23:54:28 -0400243 def refresh_nodes(self):
244 # collect local nodes
Tony Mack735493a2013-04-04 23:54:28 -0400245 nodes = Node.objects.all()
246 nodes_dict = {}
247 for node in nodes:
Tony Mack48952032013-04-12 11:49:34 -0400248 if 'viccidev10' not in node.name:
249 nodes_dict[node.name] = node
250
Tony Mack51f113d2013-04-13 02:02:22 -0400251 deployment = DeploymentNetwork.objects.filter(name='VICCI')[0]
Tony Mack48952032013-04-12 11:49:34 -0400252 login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws']
253 sites = Site.objects.filter(login_base__in=login_bases)
Tony Mack735493a2013-04-04 23:54:28 -0400254 # collect nova nodes:
Tony Mack48952032013-04-12 11:49:34 -0400255 compute_nodes = self.client.nova.hypervisors.list()
256
Tony Mack735493a2013-04-04 23:54:28 -0400257 compute_nodes_dict = {}
258 for compute_node in compute_nodes:
259 compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node
260
261 # add new nodes:
262 new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys())
Tony Mack51f113d2013-04-13 02:02:22 -0400263 i = 0
264 max = len(sites)
Tony Mack735493a2013-04-04 23:54:28 -0400265 for name in new_node_names:
Tony Mack51f113d2013-04-13 02:02:22 -0400266 if i == max:
267 i = 0
268 site = sites[i]
269 node = Node(name=compute_nodes_dict[name].hypervisor_hostname,
270 site=site,
271 deploymentNetwork=deployment)
Tony Mack735493a2013-04-04 23:54:28 -0400272 node.save()
Tony Mack51f113d2013-04-13 02:02:22 -0400273 i+=1
Tony Mack735493a2013-04-04 23:54:28 -0400274
275 # remove old nodes
276 old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys())
277 Node.objects.filter(name__in=old_node_names).delete()
278
Tony Mack735493a2013-04-04 23:54:28 -0400279 def refresh_images(self):
280 # collect local images
Tony Mack735493a2013-04-04 23:54:28 -0400281 images = Image.objects.all()
282 images_dict = {}
283 for image in images:
284 images_dict[image.name] = image
285
286 # collect glance images
Tony Mack48952032013-04-12 11:49:34 -0400287 glance_images = self.client.glance.get_images()
Tony Mack735493a2013-04-04 23:54:28 -0400288 glance_images_dict = {}
289 for glance_image in glance_images:
290 glance_images_dict[glance_image['name']] = glance_image
291
292 # add new images
293 new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys())
294 for name in new_image_names:
295 image = Image(image_id=glance_images_dict[name]['id'],
296 name=glance_images_dict[name]['name'],
297 disk_format=glance_images_dict[name]['disk_format'],
298 container_format=glance_images_dict[name]['container_format'])
299 image.save()
300
301 # remove old images
302 old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys())
303 Image.objects.filter(name__in=old_image_names).delete()
Tony Mack02755d42013-05-02 00:00:10 -0400304
305