Tony Mack | 9b5dd75 | 2013-05-09 20:33:21 -0400 | [diff] [blame] | 1 | import commands |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 2 | import hashlib |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 3 | from planetstack.config import Config |
Tony Mack | 9446692 | 2014-06-19 20:09:40 -0400 | [diff] [blame^] | 4 | from core.models import Deployment |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 5 | |
| 6 | try: |
| 7 | from openstack.client import OpenStackClient |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 8 | has_openstack = True |
| 9 | except: |
| 10 | has_openstack = False |
| 11 | |
| 12 | manager_enabled = Config().api_nova_enabled |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 13 | |
Tony Mack | a033ced | 2013-03-29 17:57:06 -0400 | [diff] [blame] | 14 | class OpenStackDriver: |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 15 | |
Tony Mack | 9446692 | 2014-06-19 20:09:40 -0400 | [diff] [blame^] | 16 | def __init__(self, config = None, client=None): |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 17 | if config: |
| 18 | self.config = Config(config) |
| 19 | else: |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 20 | self.config = Config() |
Tony Mack | ad8b093 | 2013-04-07 19:32:07 -0400 | [diff] [blame] | 21 | |
| 22 | if client: |
| 23 | self.shell = client |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 24 | |
Tony Mack | c12d5ca | 2013-09-24 10:30:39 -0400 | [diff] [blame] | 25 | self.enabled = manager_enabled |
| 26 | self.has_openstack = has_openstack |
| 27 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 28 | def client_driver(self, caller=None, tenant=None, deployment=None): |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 29 | if caller: |
| 30 | auth = {'username': caller.email, |
| 31 | 'password': hashlib.md5(caller.password).hexdigest()[:6], |
| 32 | 'tenant': tenant} |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 33 | client = OpenStackClient(deployment=deployment, **auth) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 34 | else: |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 35 | client = OpenStackClient(tenant=tenant, deployment=deployment) |
| 36 | |
Tony Mack | 9446692 | 2014-06-19 20:09:40 -0400 | [diff] [blame^] | 37 | driver = OpenStackDriver(client=client) |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 38 | return driver |
| 39 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 40 | def admin_driver(self, tenant=None, deployment=None): |
Tony Mack | 9446692 | 2014-06-19 20:09:40 -0400 | [diff] [blame^] | 41 | deployment = Deployment.objects.get(name=deployment) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 42 | client = OpenStackClient(tenant=tenant, deployment=deployment) |
Tony Mack | 9446692 | 2014-06-19 20:09:40 -0400 | [diff] [blame^] | 43 | driver = OpenStackDriver(client=client) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 44 | return driver |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 45 | |
Scott Baker | 8a6d512 | 2013-08-07 18:57:15 -0700 | [diff] [blame] | 46 | def create_role(self, name): |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 47 | roles = self.shell.keystone.roles.findall(name=name) |
Tony Mack | 0f26b27 | 2014-04-15 12:17:15 -0400 | [diff] [blame] | 48 | roles_title = self.shell.keystone.roles.findall(name=name.title()) |
| 49 | roles_found = roles + roles_title |
| 50 | if not roles_found: |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 51 | role = self.shell.keystone.roles.create(name) |
| 52 | else: |
Tony Mack | 0f26b27 | 2014-04-15 12:17:15 -0400 | [diff] [blame] | 53 | role = roles_found[0] |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 54 | return role |
| 55 | |
Tony Mack | a704053 | 2013-04-08 21:45:43 -0400 | [diff] [blame] | 56 | def delete_role(self, filter): |
| 57 | roles = self.shell.keystone.roles.findall(**filter) |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 58 | for role in roles: |
| 59 | self.shell.keystone.roles.delete(role) |
| 60 | return 1 |
| 61 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 62 | def create_tenant(self, tenant_name, enabled, description): |
Tony Mack | 5ee2190 | 2013-03-29 20:36:42 -0400 | [diff] [blame] | 63 | """Create keystone tenant. Suggested fields: name, description, enabled""" |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 64 | tenants = self.shell.keystone.tenants.findall(name=tenant_name) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 65 | if not tenants: |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 66 | fields = {'tenant_name': tenant_name, 'enabled': enabled, |
Tony Mack | 488950a | 2013-04-07 20:35:50 -0400 | [diff] [blame] | 67 | 'description': description} |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 68 | tenant = self.shell.keystone.tenants.create(**fields) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 69 | else: |
| 70 | tenant = tenants[0] |
Tony Mack | 0dc2f56 | 2013-04-29 19:41:39 -0400 | [diff] [blame] | 71 | |
| 72 | # always give the admin user the admin role to any tenant created |
| 73 | # by the driver. |
| 74 | self.add_user_role(self.admin_user.id, tenant.id, 'admin') |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 75 | return tenant |
| 76 | |
| 77 | def update_tenant(self, id, **kwds): |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 78 | return self.shell.keystone.tenants.update(id, **kwds) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 79 | |
| 80 | def delete_tenant(self, id): |
Tony Mack | ac3e014 | 2013-06-26 11:28:57 -0400 | [diff] [blame] | 81 | ctx = self.shell.nova_db.ctx |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 82 | tenants = self.shell.keystone.tenants.findall(id=id) |
| 83 | for tenant in tenants: |
Tony Mack | ac3e014 | 2013-06-26 11:28:57 -0400 | [diff] [blame] | 84 | # nova does not automatically delete the tenant's instances |
| 85 | # so we manually delete instances before deleteing the tenant |
Scott Baker | 8a6d512 | 2013-08-07 18:57:15 -0700 | [diff] [blame] | 86 | instances = self.shell.nova_db.instance_get_all_by_filters(ctx, |
Tony Mack | ac3e014 | 2013-06-26 11:28:57 -0400 | [diff] [blame] | 87 | {'project_id': tenant.id}, 'id', 'asc') |
Tony Mack | 28897e1 | 2013-07-09 07:25:38 -0400 | [diff] [blame] | 88 | client = OpenStackClient(tenant=tenant.name) |
Tony Mack | ac3e014 | 2013-06-26 11:28:57 -0400 | [diff] [blame] | 89 | driver = OpenStackDriver(client=client) |
| 90 | for instance in instances: |
| 91 | driver.destroy_instance(instance.id) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 92 | self.shell.keystone.tenants.delete(tenant) |
| 93 | return 1 |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 94 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 95 | def create_user(self, name, email, password, enabled): |
| 96 | users = self.shell.keystone.users.findall(email=email) |
| 97 | if not users: |
| 98 | fields = {'name': name, 'email': email, 'password': password, |
| 99 | 'enabled': enabled} |
Tony Mack | fdcbf83 | 2013-04-09 11:47:11 -0400 | [diff] [blame] | 100 | user = self.shell.keystone.users.create(**fields) |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 101 | else: |
| 102 | user = users[0] |
| 103 | return user |
| 104 | |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 105 | def delete_user(self, id): |
| 106 | users = self.shell.keystone.users.findall(id=id) |
| 107 | for user in users: |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 108 | # delete users keys |
| 109 | keys = self.shell.nova.keypairs.findall() |
| 110 | for key in keys: |
| 111 | self.shell.nova.keypairs.delete(key) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 112 | self.shell.keystone.users.delete(user) |
Tony Mack | ac2ebaa | 2014-04-10 12:26:18 -0400 | [diff] [blame] | 113 | return 1 |
| 114 | |
| 115 | def get_admin_role(self): |
| 116 | role = None |
| 117 | for admin_role_name in ['admin', 'Admin']: |
| 118 | roles = self.shell.keystone.roles.findall(name=admin_role_name) |
| 119 | if roles: |
| 120 | role = roles[0] |
| 121 | break |
| 122 | return role |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 123 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 124 | def add_user_role(self, kuser_id, tenant_id, role_name): |
| 125 | user = self.shell.keystone.users.find(id=kuser_id) |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 126 | tenant = self.shell.keystone.tenants.find(id=tenant_id) |
Tony Mack | a4f1feb | 2014-03-28 20:09:21 -0400 | [diff] [blame] | 127 | # admin role can be lowercase or title. Look for both |
| 128 | role = None |
| 129 | if role_name.lower() == 'admin': |
Tony Mack | ac2ebaa | 2014-04-10 12:26:18 -0400 | [diff] [blame] | 130 | role = self.get_admin_role() |
| 131 | else: |
Tony Mack | a4f1feb | 2014-03-28 20:09:21 -0400 | [diff] [blame] | 132 | # look up non admin role or force exception when admin role isnt found |
| 133 | role = self.shell.keystone.roles.find(name=role_name) |
Tony Mack | 54304c6 | 2013-04-28 11:27:07 -0400 | [diff] [blame] | 134 | |
| 135 | role_found = False |
| 136 | user_roles = user.list_roles(tenant.id) |
| 137 | for user_role in user_roles: |
| 138 | if user_role.name == role.name: |
| 139 | role_found = True |
| 140 | if not role_found: |
| 141 | tenant.add_user(user, role) |
| 142 | |
| 143 | return 1 |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 144 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 145 | def delete_user_role(self, kuser_id, tenant_id, role_name): |
| 146 | user = self.shell.keystone.users.find(id=kuser_id) |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 147 | tenant = self.shell.keystone.tenants.find(id=tenant_id) |
Tony Mack | ac2ebaa | 2014-04-10 12:26:18 -0400 | [diff] [blame] | 148 | # admin role can be lowercase or title. Look for both |
| 149 | role = None |
| 150 | if role_name.lower() == 'admin': |
| 151 | role = self.get_admin_role() |
| 152 | else: |
| 153 | # look up non admin role or force exception when admin role isnt found |
| 154 | role = self.shell.keystone.roles.find(name=role_name) |
Tony Mack | 54304c6 | 2013-04-28 11:27:07 -0400 | [diff] [blame] | 155 | |
| 156 | role_found = False |
| 157 | user_roles = user.list_roles(tenant.id) |
| 158 | for user_role in user_roles: |
| 159 | if user_role.name == role.name: |
| 160 | role_found = True |
| 161 | if role_found: |
| 162 | tenant.remove_user(user, role) |
| 163 | |
| 164 | return 1 |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 165 | |
Tony Mack | 6795fbe | 2013-06-11 10:10:24 -0400 | [diff] [blame] | 166 | def update_user(self, id, fields): |
| 167 | if 'password' in fields: |
| 168 | self.shell.keystone.users.update_password(id, fields['password']) |
| 169 | if 'enabled' in fields: |
| 170 | self.shell.keystone.users.update_enabled(id, fields['enabled']) |
| 171 | return 1 |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 172 | |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 173 | def create_router(self, name, set_gateway=True): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 174 | routers = self.shell.quantum.list_routers(name=name)['routers'] |
| 175 | if routers: |
| 176 | router = routers[0] |
| 177 | else: |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 178 | router = self.shell.quantum.create_router({'router': {'name': name}})['router'] |
Tony Mack | c1665b1 | 2013-04-29 16:45:14 -0400 | [diff] [blame] | 179 | # add router to external network |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 180 | if set_gateway: |
Tony Mack | 257bedc | 2013-04-10 08:58:09 -0400 | [diff] [blame] | 181 | nets = self.shell.quantum.list_networks()['networks'] |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 182 | for net in nets: |
| 183 | if net['router:external'] == True: |
Tony Mack | 9dcc050 | 2013-04-10 09:07:45 -0400 | [diff] [blame] | 184 | self.shell.quantum.add_gateway_router(router['id'], |
| 185 | {'network_id': net['id']}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 186 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 187 | return router |
| 188 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 189 | def delete_router(self, id): |
| 190 | routers = self.shell.quantum.list_routers(id=id)['routers'] |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 191 | for router in routers: |
| 192 | self.shell.quantum.delete_router(router['id']) |
Tony Mack | c1665b1 | 2013-04-29 16:45:14 -0400 | [diff] [blame] | 193 | # remove router form external network |
Tony Mack | 0c0e05d | 2013-04-29 17:52:37 -0400 | [diff] [blame] | 194 | #nets = self.shell.quantum.list_networks()['networks'] |
| 195 | #for net in nets: |
| 196 | # if net['router:external'] == True: |
| 197 | # self.shell.quantum.remove_gateway_router(router['id']) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 198 | |
| 199 | def add_router_interface(self, router_id, subnet_id): |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 200 | router = self.shell.quantum.show_router(router_id)['router'] |
| 201 | subnet = self.shell.quantum.show_subnet(subnet_id)['subnet'] |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 202 | if router and subnet: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 203 | self.shell.quantum.add_interface_router(router_id, {'subnet_id': subnet_id}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 204 | |
| 205 | def delete_router_interface(self, router_id, subnet_id): |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 206 | router = self.shell.quantum.show_router(router_id) |
| 207 | subnet = self.shell.quantum.show_subnet(subnet_id) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 208 | if router and subnet: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 209 | self.shell.quantum.remove_interface_router(router_id, {'subnet_id': subnet_id}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 210 | |
Scott Baker | 1ac5ddb | 2013-08-14 10:50:48 -0700 | [diff] [blame] | 211 | def create_network(self, name, shared=False): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 212 | nets = self.shell.quantum.list_networks(name=name)['networks'] |
| 213 | if nets: |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 214 | net = nets[0] |
Tony Mack | 82a688d | 2013-04-10 08:12:58 -0400 | [diff] [blame] | 215 | else: |
Scott Baker | 1ac5ddb | 2013-08-14 10:50:48 -0700 | [diff] [blame] | 216 | net = self.shell.quantum.create_network({'network': {'name': name, 'shared': shared}})['network'] |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 217 | return net |
| 218 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 219 | def delete_network(self, id): |
| 220 | nets = self.shell.quantum.list_networks()['networks'] |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 221 | for net in nets: |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 222 | if net['id'] == id: |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 223 | # delete_all ports |
| 224 | self.delete_network_ports(net['id']) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 225 | # delete all subnets: |
| 226 | for subnet_id in net['subnets']: |
| 227 | self.delete_subnet(subnet_id) |
| 228 | self.shell.quantum.delete_network(net['id']) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 229 | return 1 |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 230 | |
| 231 | def delete_network_ports(self, network_id): |
| 232 | ports = self.shell.quantum.list_ports()['ports'] |
| 233 | for port in ports: |
Tony Mack | c1665b1 | 2013-04-29 16:45:14 -0400 | [diff] [blame] | 234 | if port['network_id'] == network_id: |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 235 | self.shell.quantum.delete_port(port['id']) |
| 236 | return 1 |
| 237 | |
| 238 | def delete_subnet_ports(self, subnet_id): |
| 239 | ports = self.shell.quantum.list_ports()['ports'] |
| 240 | for port in ports: |
| 241 | delete = False |
| 242 | for fixed_ip in port['fixed_ips']: |
| 243 | if fixed_ip['subnet_id'] == subnet_id: |
| 244 | delete=True |
| 245 | break |
| 246 | if delete: |
| 247 | self.shell.quantum.delete_port(port['id']) |
| 248 | return 1 |
| 249 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 250 | def create_subnet(self, name, network_id, cidr_ip, ip_version, start, end): |
| 251 | #nets = self.shell.quantum.list_networks(name=network_name)['networks'] |
| 252 | #if not nets: |
| 253 | # raise Exception, "No such network: %s" % network_name |
| 254 | #net = nets[0] |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 255 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 256 | subnet = None |
| 257 | subnets = self.shell.quantum.list_subnets()['subnets'] |
| 258 | for snet in subnets: |
| 259 | if snet['cidr'] == cidr_ip and snet['network_id'] == network_id: |
| 260 | subnet = snet |
Scott Baker | 8a6d512 | 2013-08-07 18:57:15 -0700 | [diff] [blame] | 261 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 262 | if not subnet: |
Andy Bavier | d2b0c6d | 2014-04-25 11:44:15 -0400 | [diff] [blame] | 263 | # HACK: Add metadata route -- Neutron does not reliably supply this |
| 264 | metadata_ip = cidr_ip.replace("0/24", "3") |
| 265 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 266 | allocation_pools = [{'start': start, 'end': end}] |
| 267 | subnet = {'subnet': {'name': name, |
| 268 | 'network_id': network_id, |
| 269 | 'ip_version': ip_version, |
| 270 | 'cidr': cidr_ip, |
Tony Mack | f549eb4 | 2014-04-18 15:33:24 -0400 | [diff] [blame] | 271 | #'dns_nameservers': ['8.8.8.8', '8.8.4.4'], |
Tony Mack | 8b04773 | 2014-04-25 12:42:21 -0400 | [diff] [blame] | 272 | 'host_routes': [{'destination':'169.254.169.254/32','nexthop':metadata_ip}], |
Andy Bavier | d2b0c6d | 2014-04-25 11:44:15 -0400 | [diff] [blame] | 273 | 'gateway_ip': None, |
Scott Baker | 8a6d512 | 2013-08-07 18:57:15 -0700 | [diff] [blame] | 274 | 'allocation_pools': allocation_pools}} |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 275 | subnet = self.shell.quantum.create_subnet(subnet)['subnet'] |
Andy Bavier | d2b0c6d | 2014-04-25 11:44:15 -0400 | [diff] [blame] | 276 | # self.add_external_route(subnet) |
| 277 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 278 | return subnet |
| 279 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 280 | def update_subnet(self, id, fields): |
| 281 | return self.shell.quantum.update_subnet(id, fields) |
| 282 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 283 | def delete_subnet(self, id): |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 284 | #return self.shell.quantum.delete_subnet(id=id) |
| 285 | # inefficient but fault tolerant |
| 286 | subnets = self.shell.quantum.list_subnets()['subnets'] |
| 287 | for subnet in subnets: |
| 288 | if subnet['id'] == id: |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 289 | self.delete_subnet_ports(subnet['id']) |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 290 | self.shell.quantum.delete_subnet(id) |
Tony Mack | 9b5dd75 | 2013-05-09 20:33:21 -0400 | [diff] [blame] | 291 | self.delete_external_route(subnet) |
| 292 | return 1 |
| 293 | |
Tony Mack | 70780e9 | 2013-06-30 12:46:16 -0400 | [diff] [blame] | 294 | def get_external_routes(self): |
| 295 | status, output = commands.getstatusoutput('route') |
| 296 | routes = output.split('\n')[3:] |
| 297 | return routes |
| 298 | |
| 299 | def add_external_route(self, subnet, routes=[]): |
| 300 | if not routes: |
| 301 | routes = self.get_external_routes() |
| 302 | |
Tony Mack | 9b5dd75 | 2013-05-09 20:33:21 -0400 | [diff] [blame] | 303 | ports = self.shell.quantum.list_ports()['ports'] |
| 304 | |
| 305 | gw_ip = subnet['gateway_ip'] |
| 306 | subnet_id = subnet['id'] |
| 307 | |
| 308 | # 1. Find the port associated with the subnet's gateway |
| 309 | # 2. Find the router associated with that port |
| 310 | # 3. Find the port associated with this router and on the external net |
| 311 | # 4. Set up route to the subnet through the port from step 3 |
| 312 | ip_address = None |
| 313 | for port in ports: |
| 314 | for fixed_ip in port['fixed_ips']: |
| 315 | if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip: |
| 316 | gw_port = port |
| 317 | router_id = gw_port['device_id'] |
| 318 | router = self.shell.quantum.show_router(router_id)['router'] |
Tony Mack | 4418701 | 2013-07-09 07:15:17 -0400 | [diff] [blame] | 319 | if router and router.get('external_gateway_info'): |
Tony Mack | 16f51e0 | 2013-07-01 10:33:29 -0400 | [diff] [blame] | 320 | ext_net = router['external_gateway_info']['network_id'] |
| 321 | for port in ports: |
| 322 | if port['device_id'] == router_id and port['network_id'] == ext_net: |
| 323 | ip_address = port['fixed_ips'][0]['ip_address'] |
Tony Mack | 9b5dd75 | 2013-05-09 20:33:21 -0400 | [diff] [blame] | 324 | |
| 325 | if ip_address: |
Tony Mack | 70780e9 | 2013-06-30 12:46:16 -0400 | [diff] [blame] | 326 | # check if external route already exists |
| 327 | route_exists = False |
| 328 | if routes: |
| 329 | for route in routes: |
| 330 | if subnet['cidr'] in route and ip_address in route: |
| 331 | route_exists = True |
| 332 | if not route_exists: |
| 333 | cmd = "route add -net %s dev br-ex gw %s" % (subnet['cidr'], ip_address) |
Tony Mack | 16f51e0 | 2013-07-01 10:33:29 -0400 | [diff] [blame] | 334 | s, o = commands.getstatusoutput(cmd) |
| 335 | #print cmd, "\n", s, o |
Tony Mack | 9b5dd75 | 2013-05-09 20:33:21 -0400 | [diff] [blame] | 336 | |
| 337 | return 1 |
| 338 | |
| 339 | def delete_external_route(self, subnet): |
| 340 | ports = self.shell.quantum.list_ports()['ports'] |
| 341 | |
| 342 | gw_ip = subnet['gateway_ip'] |
| 343 | subnet_id = subnet['id'] |
| 344 | |
| 345 | # 1. Find the port associated with the subnet's gateway |
| 346 | # 2. Find the router associated with that port |
| 347 | # 3. Find the port associated with this router and on the external net |
| 348 | # 4. Set up route to the subnet through the port from step 3 |
| 349 | ip_address = None |
| 350 | for port in ports: |
| 351 | for fixed_ip in port['fixed_ips']: |
| 352 | if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip: |
| 353 | gw_port = port |
| 354 | router_id = gw_port['device_id'] |
| 355 | router = self.shell.quantum.show_router(router_id)['router'] |
| 356 | ext_net = router['external_gateway_info']['network_id'] |
| 357 | for port in ports: |
| 358 | if port['device_id'] == router_id and port['network_id'] == ext_net: |
| 359 | ip_address = port['fixed_ips'][0]['ip_address'] |
| 360 | |
| 361 | if ip_address: |
| 362 | cmd = "route delete -net %s" % (subnet['cidr']) |
| 363 | commands.getstatusoutput(cmd) |
| 364 | |
| 365 | return 1 |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 366 | |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 367 | def create_keypair(self, name, public_key): |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 368 | keys = self.shell.nova.keypairs.findall(name=name) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 369 | if keys: |
Tony Mack | 52cb930 | 2013-04-09 12:25:35 -0400 | [diff] [blame] | 370 | key = keys[0] |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 371 | # update key |
| 372 | if key.public_key != public_key: |
| 373 | self.delete_keypair(key.id) |
| 374 | key = self.shell.nova.keypairs.create(name=name, public_key=public_key) |
Tony Mack | 52cb930 | 2013-04-09 12:25:35 -0400 | [diff] [blame] | 375 | else: |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 376 | key = self.shell.nova.keypairs.create(name=name, public_key=public_key) |
Tony Mack | 52cb930 | 2013-04-09 12:25:35 -0400 | [diff] [blame] | 377 | return key |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 378 | |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 379 | def delete_keypair(self, id): |
| 380 | keys = self.shell.nova.keypairs.findall(id=id) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 381 | for key in keys: |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 382 | self.shell.nova.keypairs.delete(key) |
Scott Baker | 8a6d512 | 2013-08-07 18:57:15 -0700 | [diff] [blame] | 383 | return 1 |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 384 | |
Scott Baker | 8a6d512 | 2013-08-07 18:57:15 -0700 | [diff] [blame] | 385 | def get_private_networks(self, tenant=None): |
| 386 | if not tenant: |
| 387 | tenant = self.shell.nova.tenant |
| 388 | tenant = self.shell.keystone.tenants.find(name=tenant) |
| 389 | search_opts = {"tenant_id": tenant.id, "shared": False} |
| 390 | private_networks = self.shell.quantum.list_networks(**search_opts) |
| 391 | return private_networks |
| 392 | |
| 393 | def get_shared_networks(self): |
| 394 | search_opts = {"shared": True} |
| 395 | shared_networks = self.shell.quantum.list_networks(**search_opts) |
| 396 | return shared_networks |
| 397 | |
Scott Baker | 0d5ea5c | 2013-08-09 14:49:56 -0700 | [diff] [blame] | 398 | def get_network_subnet(self, network_id): |
| 399 | subnet_id = None |
| 400 | subnet = None |
| 401 | if network_id: |
| 402 | os_networks = self.shell.quantum.list_networks(id=network_id)["networks"] |
| 403 | if os_networks: |
| 404 | os_network = os_networks[0] |
| 405 | if os_network['subnets']: |
| 406 | subnet_id = os_network['subnets'][0] |
| 407 | os_subnets = self.shell.quantum.list_subnets(id=subnet_id)['subnets'] |
| 408 | if os_subnets: |
| 409 | subnet = os_subnets[0]['cidr'] |
| 410 | |
| 411 | return (subnet_id, subnet) |
| 412 | |
Scott Baker | 9c7a1e1 | 2014-05-30 14:42:42 -0700 | [diff] [blame] | 413 | def spawn_instance(self, name, key_name=None, hostname=None, image_id=None, security_group=None, pubkeys=[], nics=None, metadata=None, userdata=None): |
Tony Mack | e7e304f | 2013-04-30 18:18:03 -0400 | [diff] [blame] | 414 | flavor_name = self.config.nova_default_flavor |
| 415 | flavor = self.shell.nova.flavors.find(name=flavor_name) |
Tony Mack | 221957d | 2013-04-13 19:08:34 -0400 | [diff] [blame] | 416 | #if not image: |
| 417 | # image = self.config.nova_default_imave |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 418 | if not security_group: |
Scott Baker | 3a0e66b | 2013-09-12 11:21:34 -0700 | [diff] [blame] | 419 | security_group = self.config.nova_default_security_group |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 420 | |
Tony Mack | 8538b44 | 2014-04-16 07:49:41 -0400 | [diff] [blame] | 421 | files = {} |
Tony Mack | 38c158b | 2014-04-16 20:15:55 -0400 | [diff] [blame] | 422 | #if pubkeys: |
| 423 | # files["/root/.ssh/authorized_keys"] = "\n".join(pubkeys).encode('base64') |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 424 | hints = {} |
Tony Mack | 79b28ca | 2013-04-15 21:47:45 -0400 | [diff] [blame] | 425 | availability_zone = None |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 426 | if hostname: |
Tony Mack | 6719811 | 2014-04-09 01:46:03 -0400 | [diff] [blame] | 427 | availability_zone = 'nova:%s' % hostname.split('.')[0] |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 428 | server = self.shell.nova.servers.create( |
| 429 | name=name, |
| 430 | key_name = key_name, |
Tony Mack | e7e304f | 2013-04-30 18:18:03 -0400 | [diff] [blame] | 431 | flavor=flavor.id, |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 432 | image=image_id, |
| 433 | security_group = security_group, |
Tony Mack | 38c158b | 2014-04-16 20:15:55 -0400 | [diff] [blame] | 434 | #files = files, |
Tony Mack | 79b28ca | 2013-04-15 21:47:45 -0400 | [diff] [blame] | 435 | scheduler_hints=hints, |
Scott Baker | 8a6d512 | 2013-08-07 18:57:15 -0700 | [diff] [blame] | 436 | availability_zone=availability_zone, |
Scott Baker | 3a0e66b | 2013-09-12 11:21:34 -0700 | [diff] [blame] | 437 | nics=nics, |
Tony Mack | 6719811 | 2014-04-09 01:46:03 -0400 | [diff] [blame] | 438 | networks=nics, |
Scott Baker | 9c7a1e1 | 2014-05-30 14:42:42 -0700 | [diff] [blame] | 439 | meta=metadata, |
| 440 | userdata=userdata) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 441 | return server |
Scott Baker | 6a50b02 | 2013-08-26 08:25:37 -0700 | [diff] [blame] | 442 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 443 | def destroy_instance(self, id): |
Scott Baker | 6a50b02 | 2013-08-26 08:25:37 -0700 | [diff] [blame] | 444 | if (self.shell.nova.tenant=="admin"): |
| 445 | # findall() is implemented as a list() followed by a python search of the |
| 446 | # list. Since findall() doesn't accept "all_tenants", we do this using |
| 447 | # list() ourselves. This allows us to delete an instance as admin. |
| 448 | servers = self.shell.nova.servers.list(search_opts={"all_tenants": True}) |
| 449 | else: |
| 450 | servers = self.shell.nova.servers.list() |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 451 | for server in servers: |
Scott Baker | 6a50b02 | 2013-08-26 08:25:37 -0700 | [diff] [blame] | 452 | if server.id == id: |
| 453 | result=self.shell.nova.servers.delete(server) |
smbaker | 6ada7bf | 2013-04-30 18:15:48 -0700 | [diff] [blame] | 454 | |
| 455 | def update_instance_metadata(self, id, metadata): |
| 456 | servers = self.shell.nova.servers.findall(id=id) |
| 457 | for server in servers: |
| 458 | self.shell.nova.servers.set_meta(server, metadata) |
| 459 | # note: set_meta() returns a broken Server() object. Don't try to |
| 460 | # print it in the shell or it will fail in __repr__. |
| 461 | |
| 462 | def delete_instance_metadata(self, id, metadata): |
| 463 | # note: metadata is a dict. Only the keys matter, not the values. |
| 464 | servers = self.shell.nova.servers.findall(id=id) |
| 465 | for server in servers: |
| 466 | self.shell.nova.servers.delete_meta(server, metadata) |
| 467 | |