Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 1 | from netaddr import IPAddress, IPNetwork |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 2 | from plstackapi.planetstack import settings |
| 3 | from django.core import management |
| 4 | management.setup_environ(settings) |
Tony Mack | 7856888 | 2013-05-07 15:21:15 -0400 | [diff] [blame^] | 5 | from plstackapi.planetstack.config import Config |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 6 | try: |
| 7 | from plstackapi.openstack.client import OpenStackClient |
| 8 | from plstackapi.openstack.driver import OpenStackDriver |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 9 | from plstackapi.core.models import * |
| 10 | has_openstack = True |
| 11 | except: |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 12 | has_openpstack = False |
| 13 | |
| 14 | manager_enabled = Config().api_nova_enabled |
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: |
| 36 | self.client = OpenStackClient(**auth) |
| 37 | else: |
| 38 | self.client = OpenStackClient() |
| 39 | self.driver = OpenStackDriver(client=self.client) |
| 40 | |
| 41 | if caller: |
Tony Mack | f17fc26 | 2013-05-03 23:30:12 -0400 | [diff] [blame] | 42 | self.caller = caller |
Tony Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 43 | else: |
| 44 | self.caller = self.driver.admin_user |
| 45 | self.caller.user_id = self.caller.id |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 46 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 47 | @require_enabled |
| 48 | def save_role(self, role): |
| 49 | if not role.role_id: |
| 50 | keystone_role = self.driver.create_role(role.role_type) |
| 51 | role.role_id = keystone_role.id |
| 52 | |
| 53 | @require_enabled |
| 54 | def delete_role(self, role): |
| 55 | if role.role_id: |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 56 | self.driver.delete_role({'id': role.role_id}) |
| 57 | |
| 58 | @require_enabled |
| 59 | def save_key(self, key): |
| 60 | if not key.key_id: |
| 61 | key_fields = {'name': key.name, |
| 62 | 'key': key.key} |
| 63 | nova_key = self.driver.create_keypair(**key_fields) |
| 64 | key.key_id = nova_key.id |
| 65 | |
| 66 | @require_enabled |
| 67 | def delete_key(self, key): |
| 68 | if key.key_id: |
| 69 | self.driver.delete_keypair(key.key_id) |
| 70 | |
| 71 | @require_enabled |
| 72 | def save_user(self, user): |
| 73 | if not user.user_id: |
| 74 | name = user.email[:user.email.find('@')] |
| 75 | user_fields = {'name': name, |
| 76 | 'email': user.email, |
| 77 | 'password': user.password, |
| 78 | 'enabled': True} |
| 79 | keystone_user = self.driver.create_user(**user_fields) |
| 80 | user.user_id = keystone_user.id |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 81 | if user.site: |
| 82 | if user.is_admin: |
| 83 | role = 'admin' |
| 84 | else: |
| 85 | role = 'user' |
| 86 | self.driver.add_user_role(user.user_id, user.site.tenant_id, role) |
| 87 | |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 88 | @require_enabled |
| 89 | def delete_user(self, user): |
| 90 | if user.user_id: |
| 91 | self.driver.delete_user(user.user_id) |
| 92 | |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 93 | |
| 94 | |
| 95 | @require_enabled |
Tony Mack | ed163d7 | 2013-05-02 20:05:42 -0400 | [diff] [blame] | 96 | def save_site(self, site, add_role=True): |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 97 | if not site.tenant_id: |
| 98 | tenant = self.driver.create_tenant(tenant_name=site.login_base, |
| 99 | description=site.name, |
| 100 | enabled=site.enabled) |
| 101 | site.tenant_id = tenant.id |
| 102 | # give caller an admin role at the tenant they've created |
| 103 | self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin') |
| 104 | |
| 105 | # update the record |
| 106 | if site.id and site.tenant_id: |
| 107 | self.driver.update_tenant(site.tenant_id, |
| 108 | description=site.name, |
| 109 | enabled=site.enabled) |
| 110 | |
| 111 | @require_enabled |
| 112 | def delete_site(self, site): |
| 113 | if site.tenant_id: |
| 114 | self.driver.delete_tenant(site.tenant_id) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 115 | |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 116 | @require_enabled |
| 117 | def save_slice(self, slice): |
| 118 | if not slice.tenant_id: |
| 119 | nova_fields = {'tenant_name': slice.name, |
| 120 | 'description': slice.description, |
| 121 | 'enabled': slice.enabled} |
| 122 | tenant = self.driver.create_tenant(**nova_fields) |
| 123 | slice.tenant_id = tenant.id |
| 124 | |
| 125 | # give caller an admin role at the tenant they've created |
| 126 | self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin') |
| 127 | |
| 128 | # refresh credentials using this tenant |
| 129 | self.driver.shell.connect(username=self.driver.shell.keystone.username, |
| 130 | password=self.driver.shell.keystone.password, |
| 131 | tenant=tenant.name) |
| 132 | |
| 133 | # create network |
| 134 | network = self.driver.create_network(slice.name) |
| 135 | slice.network_id = network['id'] |
| 136 | |
| 137 | # create router |
| 138 | router = self.driver.create_router(slice.name) |
| 139 | slice.router_id = router['id'] |
| 140 | |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 141 | # create subnet |
| 142 | next_subnet = self.get_next_subnet() |
| 143 | cidr = str(next_subnet.cidr) |
| 144 | ip_version = next_subnet.version |
| 145 | start = str(next_subnet[2]) |
| 146 | end = str(next_subnet[-2]) |
| 147 | subnet = self.driver.create_subnet(name=slice.name, |
| 148 | network_id = network['id'], |
| 149 | cidr_ip = cidr, |
| 150 | ip_version = ip_version, |
| 151 | start = start, |
| 152 | end = end) |
| 153 | slice.subnet_id = subnet['id'] |
| 154 | # add subnet as interface to slice's router |
| 155 | self.driver.add_router_interface(router['id'], subnet['id']) |
| 156 | |
| 157 | |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 158 | if slice.id and slice.tenant_id: |
| 159 | self.driver.update_tenant(slice.tenant_id, |
| 160 | description=slice.description, |
| 161 | enabled=slice.enabled) |
| 162 | |
| 163 | @require_enabled |
| 164 | def delete_slice(self, slice): |
| 165 | if slice.tenant_id: |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 166 | self.driver.delete_router_interface(slice.router_id, slice.subnet_id) |
| 167 | self.driver.delete_subnet(slice.subnet_id) |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 168 | self.driver.delete_router(slice.router_id) |
| 169 | self.driver.delete_network(slice.network_id) |
| 170 | self.driver.delete_tenant(slice.tenant_id) |
| 171 | |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 172 | |
| 173 | |
| 174 | def get_next_subnet(self): |
| 175 | # limit ourself to 10.0.x.x for now |
| 176 | valid_subnet = lambda net: net.startswith('10.0') |
| 177 | subnets = self.driver.shell.quantum.list_subnets()['subnets'] |
| 178 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 179 | if valid_subnet(subnet['cidr'])] |
| 180 | ints.sort() |
| 181 | last_ip = IPAddress(ints[-1]) |
| 182 | last_network = IPNetwork(str(last_ip) + "/24") |
| 183 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
| 184 | return next_network |
| 185 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 186 | @require_enabled |
| 187 | def save_subnet(self, subnet): |
| 188 | if not subnet.subnet_id: |
| 189 | quantum_subnet = self.driver.create_subnet(name= subnet.slice.name, |
| 190 | network_id=subnet.slice.network_id, |
| 191 | cidr_ip = subnet.cidr, |
| 192 | ip_version=subnet.ip_version, |
| 193 | start = subnet.start, |
| 194 | end = subnet.end) |
| 195 | subnet.subnet_id = quantum_subnet['id'] |
| 196 | # add subnet as interface to slice's router |
| 197 | self.driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id) |
| 198 | #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr |
| 199 | #commands.getstatusoutput(add_route) |
| 200 | |
| 201 | |
| 202 | @require_enabled |
| 203 | def delete_subnet(self, subnet): |
| 204 | if subnet.subnet_id: |
| 205 | self.driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id) |
| 206 | self.driver.delete_subnet(subnet.subnet_id) |
| 207 | #del_route = 'route del -net %s' % self.cidr |
| 208 | #commands.getstatusoutput(del_route) |
smbaker | 8f5cf5f | 2013-05-05 13:58:16 -0700 | [diff] [blame] | 209 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 210 | @require_enabled |
| 211 | def save_sliver(self, sliver): |
| 212 | if not sliver.instance_id: |
| 213 | instance = self.driver.spawn_instance(name=sliver.name, |
| 214 | key_name = sliver.key.name, |
| 215 | image_id = sliver.image.image_id, |
| 216 | hostname = sliver.node.name ) |
| 217 | sliver.instance_id = instance.id |
| 218 | sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name') |
| 219 | |
smbaker | 8f5cf5f | 2013-05-05 13:58:16 -0700 | [diff] [blame] | 220 | if sliver.instance_id: |
| 221 | self.driver.update_instance_metadata(sliver.instance_id, {"cpu_cores": str(sliver.numberCores)}) |
| 222 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 223 | @require_enabled |
| 224 | def delete_sliver(self, sliver): |
| 225 | if sliver.instance_id: |
| 226 | self.driver.destroy_instance(sliver.instance_id) |
| 227 | |
| 228 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 229 | def refresh_nodes(self): |
| 230 | # collect local nodes |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 231 | nodes = Node.objects.all() |
| 232 | nodes_dict = {} |
| 233 | for node in nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 234 | if 'viccidev10' not in node.name: |
| 235 | nodes_dict[node.name] = node |
| 236 | |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 237 | deployment = DeploymentNetwork.objects.filter(name='VICCI')[0] |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 238 | login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] |
| 239 | sites = Site.objects.filter(login_base__in=login_bases) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 240 | # collect nova nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 241 | compute_nodes = self.client.nova.hypervisors.list() |
| 242 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 243 | compute_nodes_dict = {} |
| 244 | for compute_node in compute_nodes: |
| 245 | compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node |
| 246 | |
| 247 | # add new nodes: |
| 248 | new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys()) |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 249 | i = 0 |
| 250 | max = len(sites) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 251 | for name in new_node_names: |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 252 | if i == max: |
| 253 | i = 0 |
| 254 | site = sites[i] |
| 255 | node = Node(name=compute_nodes_dict[name].hypervisor_hostname, |
| 256 | site=site, |
| 257 | deploymentNetwork=deployment) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 258 | node.save() |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 259 | i+=1 |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 260 | |
| 261 | # remove old nodes |
| 262 | old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) |
| 263 | Node.objects.filter(name__in=old_node_names).delete() |
| 264 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 265 | def refresh_images(self): |
| 266 | # collect local images |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 267 | images = Image.objects.all() |
| 268 | images_dict = {} |
| 269 | for image in images: |
| 270 | images_dict[image.name] = image |
| 271 | |
| 272 | # collect glance images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 273 | glance_images = self.client.glance.get_images() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 274 | glance_images_dict = {} |
| 275 | for glance_image in glance_images: |
| 276 | glance_images_dict[glance_image['name']] = glance_image |
| 277 | |
| 278 | # add new images |
| 279 | new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) |
| 280 | for name in new_image_names: |
| 281 | image = Image(image_id=glance_images_dict[name]['id'], |
| 282 | name=glance_images_dict[name]['name'], |
| 283 | disk_format=glance_images_dict[name]['disk_format'], |
| 284 | container_format=glance_images_dict[name]['container_format']) |
| 285 | image.save() |
| 286 | |
| 287 | # remove old images |
| 288 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 289 | Image.objects.filter(name__in=old_image_names).delete() |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 290 | |
| 291 | |