Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 1 | from plstackapi.planetstack import settings |
| 2 | from django.core import management |
| 3 | management.setup_environ(settings) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 4 | try: |
| 5 | from plstackapi.openstack.client import OpenStackClient |
| 6 | from plstackapi.openstack.driver import OpenStackDriver |
| 7 | from plstackapi.planetstack.config import Config |
| 8 | from plstackapi.core.models import * |
| 9 | has_openstack = True |
| 10 | except: |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame^] | 11 | has_openpstack = False |
| 12 | |
| 13 | manager_enabled = Config().api_nova_enabled |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 14 | |
| 15 | def require_enabled(callable): |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 16 | def wrapper(*args, **kwds): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame^] | 17 | if manager_enabled and has_openstack: |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 18 | return callable(*args, **kwds) |
| 19 | else: |
| 20 | return None |
| 21 | return wrapper |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 22 | |
| 23 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 24 | class OpenStackManager: |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 25 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 26 | def __init__(self, auth={}, caller=None): |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 27 | if auth: |
| 28 | self.client = OpenStackClient(**auth) |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame^] | 29 | else: |
| 30 | self.client = OpenStackClient() |
| 31 | self.has_openstack = has_openstack |
| 32 | self.enabled = manager_enabled |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 33 | self.driver = OpenStackDriver(client=self.client) |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 34 | self.caller=caller |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 35 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 36 | @require_enabled |
| 37 | def save_role(self, role): |
| 38 | if not role.role_id: |
| 39 | keystone_role = self.driver.create_role(role.role_type) |
| 40 | role.role_id = keystone_role.id |
| 41 | |
| 42 | @require_enabled |
| 43 | def delete_role(self, role): |
| 44 | if role.role_id: |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 45 | self.driver.delete_role({'id': role.role_id}) |
| 46 | |
| 47 | @require_enabled |
| 48 | def save_key(self, key): |
| 49 | if not key.key_id: |
| 50 | key_fields = {'name': key.name, |
| 51 | 'key': key.key} |
| 52 | nova_key = self.driver.create_keypair(**key_fields) |
| 53 | key.key_id = nova_key.id |
| 54 | |
| 55 | @require_enabled |
| 56 | def delete_key(self, key): |
| 57 | if key.key_id: |
| 58 | self.driver.delete_keypair(key.key_id) |
| 59 | |
| 60 | @require_enabled |
| 61 | def save_user(self, user): |
| 62 | if not user.user_id: |
| 63 | name = user.email[:user.email.find('@')] |
| 64 | user_fields = {'name': name, |
| 65 | 'email': user.email, |
| 66 | 'password': user.password, |
| 67 | 'enabled': True} |
| 68 | keystone_user = self.driver.create_user(**user_fields) |
| 69 | user.user_id = keystone_user.id |
| 70 | |
| 71 | @require_enabled |
| 72 | def delete_user(self, user): |
| 73 | if user.user_id: |
| 74 | self.driver.delete_user(user.user_id) |
| 75 | |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 76 | |
| 77 | |
| 78 | @require_enabled |
| 79 | def save_site(self, site): |
| 80 | if not site.tenant_id: |
| 81 | tenant = self.driver.create_tenant(tenant_name=site.login_base, |
| 82 | description=site.name, |
| 83 | enabled=site.enabled) |
| 84 | site.tenant_id = tenant.id |
| 85 | # give caller an admin role at the tenant they've created |
| 86 | self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin') |
| 87 | |
| 88 | # update the record |
| 89 | if site.id and site.tenant_id: |
| 90 | self.driver.update_tenant(site.tenant_id, |
| 91 | description=site.name, |
| 92 | enabled=site.enabled) |
| 93 | |
| 94 | @require_enabled |
| 95 | def delete_site(self, site): |
| 96 | if site.tenant_id: |
| 97 | self.driver.delete_tenant(site.tenant_id) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 98 | |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 99 | @require_enabled |
| 100 | def save_slice(self, slice): |
| 101 | if not slice.tenant_id: |
| 102 | nova_fields = {'tenant_name': slice.name, |
| 103 | 'description': slice.description, |
| 104 | 'enabled': slice.enabled} |
| 105 | tenant = self.driver.create_tenant(**nova_fields) |
| 106 | slice.tenant_id = tenant.id |
| 107 | |
| 108 | # give caller an admin role at the tenant they've created |
| 109 | self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin') |
| 110 | |
| 111 | # refresh credentials using this tenant |
| 112 | self.driver.shell.connect(username=self.driver.shell.keystone.username, |
| 113 | password=self.driver.shell.keystone.password, |
| 114 | tenant=tenant.name) |
| 115 | |
| 116 | # create network |
| 117 | network = self.driver.create_network(slice.name) |
| 118 | slice.network_id = network['id'] |
| 119 | |
| 120 | # create router |
| 121 | router = self.driver.create_router(slice.name) |
| 122 | slice.router_id = router['id'] |
| 123 | |
| 124 | if slice.id and slice.tenant_id: |
| 125 | self.driver.update_tenant(slice.tenant_id, |
| 126 | description=slice.description, |
| 127 | enabled=slice.enabled) |
| 128 | |
| 129 | @require_enabled |
| 130 | def delete_slice(self, slice): |
| 131 | if slice.tenant_id: |
| 132 | self.driver.delete_router(slice.router_id) |
| 133 | self.driver.delete_network(slice.network_id) |
| 134 | self.driver.delete_tenant(slice.tenant_id) |
| 135 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame^] | 136 | @require_enabled |
| 137 | def save_subnet(self, subnet): |
| 138 | if not subnet.subnet_id: |
| 139 | quantum_subnet = self.driver.create_subnet(name= subnet.slice.name, |
| 140 | network_id=subnet.slice.network_id, |
| 141 | cidr_ip = subnet.cidr, |
| 142 | ip_version=subnet.ip_version, |
| 143 | start = subnet.start, |
| 144 | end = subnet.end) |
| 145 | subnet.subnet_id = quantum_subnet['id'] |
| 146 | # add subnet as interface to slice's router |
| 147 | self.driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id) |
| 148 | #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr |
| 149 | #commands.getstatusoutput(add_route) |
| 150 | |
| 151 | |
| 152 | @require_enabled |
| 153 | def delete_subnet(self, subnet): |
| 154 | if subnet.subnet_id: |
| 155 | self.driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id) |
| 156 | self.driver.delete_subnet(subnet.subnet_id) |
| 157 | #del_route = 'route del -net %s' % self.cidr |
| 158 | #commands.getstatusoutput(del_route) |
| 159 | |
| 160 | @require_enabled |
| 161 | def save_sliver(self, sliver): |
| 162 | if not sliver.instance_id: |
| 163 | instance = self.driver.spawn_instance(name=sliver.name, |
| 164 | key_name = sliver.key.name, |
| 165 | image_id = sliver.image.image_id, |
| 166 | hostname = sliver.node.name ) |
| 167 | sliver.instance_id = instance.id |
| 168 | sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name') |
| 169 | |
| 170 | @require_enabled |
| 171 | def delete_sliver(self, sliver): |
| 172 | if sliver.instance_id: |
| 173 | self.driver.destroy_instance(sliver.instance_id) |
| 174 | |
| 175 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 176 | def refresh_nodes(self): |
| 177 | # collect local nodes |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 178 | nodes = Node.objects.all() |
| 179 | nodes_dict = {} |
| 180 | for node in nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 181 | if 'viccidev10' not in node.name: |
| 182 | nodes_dict[node.name] = node |
| 183 | |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 184 | deployment = DeploymentNetwork.objects.filter(name='VICCI')[0] |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 185 | login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] |
| 186 | sites = Site.objects.filter(login_base__in=login_bases) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 187 | # collect nova nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 188 | compute_nodes = self.client.nova.hypervisors.list() |
| 189 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 190 | compute_nodes_dict = {} |
| 191 | for compute_node in compute_nodes: |
| 192 | compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node |
| 193 | |
| 194 | # add new nodes: |
| 195 | new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys()) |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 196 | i = 0 |
| 197 | max = len(sites) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 198 | for name in new_node_names: |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 199 | if i == max: |
| 200 | i = 0 |
| 201 | site = sites[i] |
| 202 | node = Node(name=compute_nodes_dict[name].hypervisor_hostname, |
| 203 | site=site, |
| 204 | deploymentNetwork=deployment) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 205 | node.save() |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 206 | i+=1 |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 207 | |
| 208 | # remove old nodes |
| 209 | old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) |
| 210 | Node.objects.filter(name__in=old_node_names).delete() |
| 211 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 212 | def refresh_images(self): |
| 213 | # collect local images |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 214 | images = Image.objects.all() |
| 215 | images_dict = {} |
| 216 | for image in images: |
| 217 | images_dict[image.name] = image |
| 218 | |
| 219 | # collect glance images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 220 | glance_images = self.client.glance.get_images() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 221 | glance_images_dict = {} |
| 222 | for glance_image in glance_images: |
| 223 | glance_images_dict[glance_image['name']] = glance_image |
| 224 | |
| 225 | # add new images |
| 226 | new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) |
| 227 | for name in new_image_names: |
| 228 | image = Image(image_id=glance_images_dict[name]['id'], |
| 229 | name=glance_images_dict[name]['name'], |
| 230 | disk_format=glance_images_dict[name]['disk_format'], |
| 231 | container_format=glance_images_dict[name]['container_format']) |
| 232 | image.save() |
| 233 | |
| 234 | # remove old images |
| 235 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 236 | Image.objects.filter(name__in=old_image_names).delete() |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 237 | |
| 238 | |