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: |
| 11 | has_openstack = False |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 12 | |
| 13 | def require_enabled(callable): |
| 14 | enabled = Config().api_nova_enabled |
| 15 | def wrapper(*args, **kwds): |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 16 | if enabled and has_openstack: |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 17 | return callable(*args, **kwds) |
| 18 | else: |
| 19 | return None |
| 20 | return wrapper |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 21 | |
| 22 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 23 | class OpenStackManager: |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 24 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 25 | def __init__(self, auth={}, caller=None): |
| 26 | self.client = None |
| 27 | if auth: |
| 28 | self.client = OpenStackClient(**auth) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 29 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 30 | self.driver = OpenStackDriver(client=self.client) |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 31 | self.caller=caller |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 32 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 33 | @require_enabled |
| 34 | def save_role(self, role): |
| 35 | if not role.role_id: |
| 36 | keystone_role = self.driver.create_role(role.role_type) |
| 37 | role.role_id = keystone_role.id |
| 38 | |
| 39 | @require_enabled |
| 40 | def delete_role(self, role): |
| 41 | if role.role_id: |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 42 | self.driver.delete_role({'id': role.role_id}) |
| 43 | |
| 44 | @require_enabled |
| 45 | def save_key(self, key): |
| 46 | if not key.key_id: |
| 47 | key_fields = {'name': key.name, |
| 48 | 'key': key.key} |
| 49 | nova_key = self.driver.create_keypair(**key_fields) |
| 50 | key.key_id = nova_key.id |
| 51 | |
| 52 | @require_enabled |
| 53 | def delete_key(self, key): |
| 54 | if key.key_id: |
| 55 | self.driver.delete_keypair(key.key_id) |
| 56 | |
| 57 | @require_enabled |
| 58 | def save_user(self, user): |
| 59 | if not user.user_id: |
| 60 | name = user.email[:user.email.find('@')] |
| 61 | user_fields = {'name': name, |
| 62 | 'email': user.email, |
| 63 | 'password': user.password, |
| 64 | 'enabled': True} |
| 65 | keystone_user = self.driver.create_user(**user_fields) |
| 66 | user.user_id = keystone_user.id |
| 67 | |
| 68 | @require_enabled |
| 69 | def delete_user(self, user): |
| 70 | if user.user_id: |
| 71 | self.driver.delete_user(user.user_id) |
| 72 | |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 73 | |
| 74 | |
| 75 | @require_enabled |
| 76 | def save_site(self, site): |
| 77 | if not site.tenant_id: |
| 78 | tenant = self.driver.create_tenant(tenant_name=site.login_base, |
| 79 | description=site.name, |
| 80 | enabled=site.enabled) |
| 81 | site.tenant_id = tenant.id |
| 82 | # give caller an admin role at the tenant they've created |
| 83 | self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin') |
| 84 | |
| 85 | # update the record |
| 86 | if site.id and site.tenant_id: |
| 87 | self.driver.update_tenant(site.tenant_id, |
| 88 | description=site.name, |
| 89 | enabled=site.enabled) |
| 90 | |
| 91 | @require_enabled |
| 92 | def delete_site(self, site): |
| 93 | if site.tenant_id: |
| 94 | self.driver.delete_tenant(site.tenant_id) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 95 | |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 96 | @require_enabled |
| 97 | def save_slice(self, slice): |
| 98 | if not slice.tenant_id: |
| 99 | nova_fields = {'tenant_name': slice.name, |
| 100 | 'description': slice.description, |
| 101 | 'enabled': slice.enabled} |
| 102 | tenant = self.driver.create_tenant(**nova_fields) |
| 103 | slice.tenant_id = tenant.id |
| 104 | |
| 105 | # give caller an admin role at the tenant they've created |
| 106 | self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin') |
| 107 | |
| 108 | # refresh credentials using this tenant |
| 109 | self.driver.shell.connect(username=self.driver.shell.keystone.username, |
| 110 | password=self.driver.shell.keystone.password, |
| 111 | tenant=tenant.name) |
| 112 | |
| 113 | # create network |
| 114 | network = self.driver.create_network(slice.name) |
| 115 | slice.network_id = network['id'] |
| 116 | |
| 117 | # create router |
| 118 | router = self.driver.create_router(slice.name) |
| 119 | slice.router_id = router['id'] |
| 120 | |
| 121 | if slice.id and slice.tenant_id: |
| 122 | self.driver.update_tenant(slice.tenant_id, |
| 123 | description=slice.description, |
| 124 | enabled=slice.enabled) |
| 125 | |
| 126 | @require_enabled |
| 127 | def delete_slice(self, slice): |
| 128 | if slice.tenant_id: |
| 129 | self.driver.delete_router(slice.router_id) |
| 130 | self.driver.delete_network(slice.network_id) |
| 131 | self.driver.delete_tenant(slice.tenant_id) |
| 132 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 133 | def refresh_nodes(self): |
| 134 | # collect local nodes |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 135 | nodes = Node.objects.all() |
| 136 | nodes_dict = {} |
| 137 | for node in nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 138 | if 'viccidev10' not in node.name: |
| 139 | nodes_dict[node.name] = node |
| 140 | |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 141 | deployment = DeploymentNetwork.objects.filter(name='VICCI')[0] |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 142 | login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] |
| 143 | sites = Site.objects.filter(login_base__in=login_bases) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 144 | # collect nova nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 145 | compute_nodes = self.client.nova.hypervisors.list() |
| 146 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 147 | compute_nodes_dict = {} |
| 148 | for compute_node in compute_nodes: |
| 149 | compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node |
| 150 | |
| 151 | # add new nodes: |
| 152 | new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys()) |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 153 | i = 0 |
| 154 | max = len(sites) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 155 | for name in new_node_names: |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 156 | if i == max: |
| 157 | i = 0 |
| 158 | site = sites[i] |
| 159 | node = Node(name=compute_nodes_dict[name].hypervisor_hostname, |
| 160 | site=site, |
| 161 | deploymentNetwork=deployment) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 162 | node.save() |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 163 | i+=1 |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 164 | |
| 165 | # remove old nodes |
| 166 | old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) |
| 167 | Node.objects.filter(name__in=old_node_names).delete() |
| 168 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 169 | def refresh_images(self): |
| 170 | # collect local images |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 171 | images = Image.objects.all() |
| 172 | images_dict = {} |
| 173 | for image in images: |
| 174 | images_dict[image.name] = image |
| 175 | |
| 176 | # collect glance images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 177 | glance_images = self.client.glance.get_images() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 178 | glance_images_dict = {} |
| 179 | for glance_image in glance_images: |
| 180 | glance_images_dict[glance_image['name']] = glance_image |
| 181 | |
| 182 | # add new images |
| 183 | new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) |
| 184 | for name in new_image_names: |
| 185 | image = Image(image_id=glance_images_dict[name]['id'], |
| 186 | name=glance_images_dict[name]['name'], |
| 187 | disk_format=glance_images_dict[name]['disk_format'], |
| 188 | container_format=glance_images_dict[name]['container_format']) |
| 189 | image.save() |
| 190 | |
| 191 | # remove old images |
| 192 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 193 | Image.objects.filter(name__in=old_image_names).delete() |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 194 | |
| 195 | |