Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 1 | import os |
| 2 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
Tony Mack | c261bd2 | 2013-05-10 21:04:42 -0400 | [diff] [blame] | 3 | import string |
| 4 | import random |
Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 5 | |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 6 | from netaddr import IPAddress, IPNetwork |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 7 | from planetstack import settings |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 8 | from django.core import management |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 9 | from planetstack.config import Config |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 10 | try: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 11 | from openstack.client import OpenStackClient |
| 12 | from openstack.driver import OpenStackDriver |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 13 | from core.models import * |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 14 | has_openstack = True |
| 15 | except: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 16 | has_openstack = False |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 17 | |
Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 18 | manager_enabled = Config().api_nova_enabled |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 19 | |
Tony Mack | c261bd2 | 2013-05-10 21:04:42 -0400 | [diff] [blame] | 20 | |
| 21 | def random_string(size=6): |
| 22 | return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(size)) |
| 23 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 24 | def require_enabled(callable): |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 25 | def wrapper(*args, **kwds): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 26 | if manager_enabled and has_openstack: |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 27 | return callable(*args, **kwds) |
| 28 | else: |
| 29 | return None |
| 30 | return wrapper |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 31 | |
| 32 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 33 | class OpenStackManager: |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 34 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 35 | def __init__(self, auth={}, caller=None): |
Tony Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 36 | self.client = None |
| 37 | self.driver = None |
| 38 | self.caller = None |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 39 | self.has_openstack = has_openstack |
Tony Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 40 | self.enabled = manager_enabled |
| 41 | |
| 42 | if has_openstack and manager_enabled: |
| 43 | if auth: |
Tony Mack | 41945e0 | 2013-05-09 19:25:10 -0400 | [diff] [blame] | 44 | 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 Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 53 | else: |
Tony Mack | 41945e0 | 2013-05-09 19:25:10 -0400 | [diff] [blame] | 54 | 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 Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 68 | self.caller.kuser_id = self.caller.id |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 69 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 70 | @require_enabled |
| 71 | def save_role(self, role): |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 72 | if not role.role: |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 73 | keystone_role = self.driver.create_role(role.role_type) |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 74 | role.role = keystone_role.id |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 75 | |
| 76 | @require_enabled |
| 77 | def delete_role(self, role): |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 78 | if role.role: |
| 79 | self.driver.delete_role({'id': role.role}) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 80 | |
| 81 | @require_enabled |
| 82 | def save_key(self, key): |
Tony Mack | 71c685f | 2013-05-10 20:34:18 -0400 | [diff] [blame] | 83 | if not key.nkey_id: |
Tony Mack | c261bd2 | 2013-05-10 21:04:42 -0400 | [diff] [blame] | 84 | key_fields = {'name': random_string(8), |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 85 | 'key': key.key} |
| 86 | nova_key = self.driver.create_keypair(**key_fields) |
Tony Mack | 71c685f | 2013-05-10 20:34:18 -0400 | [diff] [blame] | 87 | key.nkey_id = nova_key.id |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 88 | |
| 89 | @require_enabled |
| 90 | def delete_key(self, key): |
Tony Mack | 71c685f | 2013-05-10 20:34:18 -0400 | [diff] [blame] | 91 | if key.nkey_id: |
| 92 | self.driver.delete_keypair(key.nkey_id) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 93 | |
| 94 | @require_enabled |
| 95 | def save_user(self, user): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 96 | if not user.kuser_id: |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 97 | 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 Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 103 | user.kuser_id = keystone_user.id |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 104 | if user.site: |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 105 | 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] | 106 | if user.is_admin: |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 107 | 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] | 108 | else: |
Tony Mack | 85d1883 | 2013-05-09 17:02:31 -0400 | [diff] [blame] | 109 | # may have admin role so attempt to remove it |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 110 | 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] | 111 | |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 112 | @require_enabled |
| 113 | def delete_user(self, user): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 114 | if user.kuser_id: |
| 115 | self.driver.delete_user(user.kuser_id) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 116 | |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 117 | @require_enabled |
Tony Mack | ed163d7 | 2013-05-02 20:05:42 -0400 | [diff] [blame] | 118 | def save_site(self, site, add_role=True): |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 119 | 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 Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 125 | self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin') |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 126 | |
| 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 Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 137 | |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 138 | @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 Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 148 | self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin') |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 149 | |
| 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 Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 163 | # 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 Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 180 | 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 Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 188 | self.driver.delete_router_interface(slice.router_id, slice.subnet_id) |
| 189 | self.driver.delete_subnet(slice.subnet_id) |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 190 | 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 Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 194 | |
| 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 Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 208 | @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) |
smbaker | 8f5cf5f | 2013-05-05 13:58:16 -0700 | [diff] [blame] | 231 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 232 | @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 Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 242 | if sliver.instance_id and ("numberCores" in sliver.changed_fields): |
smbaker | 8f5cf5f | 2013-05-05 13:58:16 -0700 | [diff] [blame] | 243 | self.driver.update_instance_metadata(sliver.instance_id, {"cpu_cores": str(sliver.numberCores)}) |
| 244 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 245 | @require_enabled |
| 246 | def delete_sliver(self, sliver): |
| 247 | if sliver.instance_id: |
| 248 | self.driver.destroy_instance(sliver.instance_id) |
| 249 | |
| 250 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 251 | def refresh_nodes(self): |
| 252 | # collect local nodes |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 253 | nodes = Node.objects.all() |
| 254 | nodes_dict = {} |
| 255 | for node in nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 256 | if 'viccidev10' not in node.name: |
| 257 | nodes_dict[node.name] = node |
| 258 | |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 259 | deployment = DeploymentNetwork.objects.filter(name='VICCI')[0] |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 260 | login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] |
| 261 | sites = Site.objects.filter(login_base__in=login_bases) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 262 | # collect nova nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 263 | compute_nodes = self.client.nova.hypervisors.list() |
| 264 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 265 | 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 Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 271 | i = 0 |
| 272 | max = len(sites) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 273 | for name in new_node_names: |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 274 | 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 Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 280 | node.save() |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 281 | i+=1 |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 282 | |
| 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 Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 287 | def refresh_images(self): |
Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 288 | from core.models.image import Image |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 289 | # collect local images |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 290 | images = Image.objects.all() |
| 291 | images_dict = {} |
| 292 | for image in images: |
| 293 | images_dict[image.name] = image |
| 294 | |
| 295 | # collect glance images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 296 | glance_images = self.client.glance.get_images() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 297 | 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 Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 313 | |
| 314 | |