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