Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 1 | from netaddr import IPAddress, IPNetwork |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 2 | from planetstack import settings |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 3 | from django.core import management |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 5 | try: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 6 | from openstack.client import OpenStackClient |
| 7 | from openstack.driver import OpenStackDriver |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 8 | from core.models import * |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 9 | has_openstack = True |
| 10 | except: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 11 | has_openstack = False |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 12 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 13 | #manager_enabled = Config().api_nova_enabled |
| 14 | manager_enabled = False |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 15 | |
| 16 | def require_enabled(callable): |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 17 | def wrapper(*args, **kwds): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 18 | if manager_enabled and has_openstack: |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 19 | return callable(*args, **kwds) |
| 20 | else: |
| 21 | return None |
| 22 | return wrapper |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 23 | |
| 24 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 25 | class OpenStackManager: |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 26 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 27 | def __init__(self, auth={}, caller=None): |
Tony Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 28 | self.client = None |
| 29 | self.driver = None |
| 30 | self.caller = None |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 31 | self.has_openstack = has_openstack |
Tony Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 32 | self.enabled = manager_enabled |
| 33 | |
| 34 | if has_openstack and manager_enabled: |
| 35 | if auth: |
Tony Mack | 41945e0 | 2013-05-09 19:25:10 -0400 | [diff] [blame] | 36 | 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 Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 45 | else: |
Tony Mack | 41945e0 | 2013-05-09 19:25:10 -0400 | [diff] [blame] | 46 | 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 |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 60 | self.caller.kuser_id = self.caller.id |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 61 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 62 | @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 Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 71 | 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 Mack | 85d1883 | 2013-05-09 17:02:31 -0400 | [diff] [blame] | 76 | key_fields = {'name': key.user.email[:key.user.email.find('@')], |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 77 | '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): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 88 | if not user.kuser_id: |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 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) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 95 | user.kuser_id = keystone_user.id |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 96 | if user.site: |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 97 | self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'user') |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 98 | if user.is_admin: |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 99 | self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'admin') |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 100 | else: |
Tony Mack | 85d1883 | 2013-05-09 17:02:31 -0400 | [diff] [blame] | 101 | # may have admin role so attempt to remove it |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 102 | self.driver.delete_user_role(user.kuser_id, user.site.tenant_id, 'admin') |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 103 | |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 104 | @require_enabled |
| 105 | def delete_user(self, user): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 106 | if user.kuser_id: |
| 107 | self.driver.delete_user(user.kuser_id) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 108 | |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 109 | @require_enabled |
Tony Mack | ed163d7 | 2013-05-02 20:05:42 -0400 | [diff] [blame] | 110 | def save_site(self, site, add_role=True): |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 111 | 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 |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 117 | self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin') |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 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 Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 129 | |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 130 | @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 |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 140 | self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin') |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 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 Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 155 | # 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 Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 172 | 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 Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 180 | self.driver.delete_router_interface(slice.router_id, slice.subnet_id) |
| 181 | self.driver.delete_subnet(slice.subnet_id) |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 182 | 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 Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 186 | |
| 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 Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 200 | @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) |
smbaker | 8f5cf5f | 2013-05-05 13:58:16 -0700 | [diff] [blame] | 223 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 224 | @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 Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 234 | if sliver.instance_id and ("numberCores" in sliver.changed_fields): |
smbaker | 8f5cf5f | 2013-05-05 13:58:16 -0700 | [diff] [blame] | 235 | self.driver.update_instance_metadata(sliver.instance_id, {"cpu_cores": str(sliver.numberCores)}) |
| 236 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 237 | @require_enabled |
| 238 | def delete_sliver(self, sliver): |
| 239 | if sliver.instance_id: |
| 240 | self.driver.destroy_instance(sliver.instance_id) |
| 241 | |
| 242 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 243 | def refresh_nodes(self): |
| 244 | # collect local nodes |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 245 | nodes = Node.objects.all() |
| 246 | nodes_dict = {} |
| 247 | for node in nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 248 | if 'viccidev10' not in node.name: |
| 249 | nodes_dict[node.name] = node |
| 250 | |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 251 | deployment = DeploymentNetwork.objects.filter(name='VICCI')[0] |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 252 | login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] |
| 253 | sites = Site.objects.filter(login_base__in=login_bases) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 254 | # collect nova nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 255 | compute_nodes = self.client.nova.hypervisors.list() |
| 256 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 257 | 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 Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 263 | i = 0 |
| 264 | max = len(sites) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 265 | for name in new_node_names: |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 266 | 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 Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 272 | node.save() |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 273 | i+=1 |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 274 | |
| 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 Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 279 | def refresh_images(self): |
| 280 | # collect local images |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 281 | images = Image.objects.all() |
| 282 | images_dict = {} |
| 283 | for image in images: |
| 284 | images_dict[image.name] = image |
| 285 | |
| 286 | # collect glance images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 287 | glance_images = self.client.glance.get_images() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 288 | 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 Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 304 | |
| 305 | |