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