Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 1 | from plstackapi.planetstack.config import Config |
Tony Mack | ad8b093 | 2013-04-07 19:32:07 -0400 | [diff] [blame] | 2 | from plstackapi.openstack.client import OpenStackClient |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 3 | |
Tony Mack | a033ced | 2013-03-29 17:57:06 -0400 | [diff] [blame] | 4 | class OpenStackDriver: |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 5 | |
Tony Mack | ad8b093 | 2013-04-07 19:32:07 -0400 | [diff] [blame] | 6 | def __init__(self, config = None, client=None): |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 7 | if config: |
| 8 | self.config = Config(config) |
| 9 | else: |
| 10 | self.config = Config() |
Tony Mack | ad8b093 | 2013-04-07 19:32:07 -0400 | [diff] [blame] | 11 | |
| 12 | if client: |
| 13 | self.shell = client |
| 14 | else: |
| 15 | self.shell = OpenStackClient() |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 16 | |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 17 | def create_role(self, name): |
| 18 | roles = self.shell.keystone.roles.findall(name=name) |
| 19 | if not roles: |
| 20 | role = self.shell.keystone.roles.create(name) |
| 21 | else: |
| 22 | role = roles[0] |
| 23 | return role |
| 24 | |
Tony Mack | a704053 | 2013-04-08 21:45:43 -0400 | [diff] [blame] | 25 | def delete_role(self, filter): |
| 26 | roles = self.shell.keystone.roles.findall(**filter) |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 27 | for role in roles: |
| 28 | self.shell.keystone.roles.delete(role) |
| 29 | return 1 |
| 30 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 31 | def create_tenant(self, tenant_name, enabled, description): |
Tony Mack | 5ee2190 | 2013-03-29 20:36:42 -0400 | [diff] [blame] | 32 | """Create keystone tenant. Suggested fields: name, description, enabled""" |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 33 | tenants = self.shell.keystone.tenants.findall(name=tenant_name) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 34 | if not tenants: |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 35 | fields = {'tenant_name': tenant_name, 'enabled': enabled, |
Tony Mack | 488950a | 2013-04-07 20:35:50 -0400 | [diff] [blame] | 36 | 'description': description} |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 37 | tenant = self.shell.keystone.tenants.create(**fields) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 38 | else: |
| 39 | tenant = tenants[0] |
| 40 | return tenant |
| 41 | |
| 42 | def update_tenant(self, id, **kwds): |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 43 | return self.shell.keystone.tenants.update(id, **kwds) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 44 | |
| 45 | def delete_tenant(self, id): |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 46 | tenants = self.shell.keystone.tenants.findall(id=id) |
| 47 | for tenant in tenants: |
| 48 | self.shell.keystone.tenants.delete(tenant) |
| 49 | return 1 |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 50 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 51 | def create_user(self, name, email, password, enabled): |
| 52 | users = self.shell.keystone.users.findall(email=email) |
| 53 | if not users: |
| 54 | fields = {'name': name, 'email': email, 'password': password, |
| 55 | 'enabled': enabled} |
Tony Mack | fdcbf83 | 2013-04-09 11:47:11 -0400 | [diff] [blame] | 56 | user = self.shell.keystone.users.create(**fields) |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 57 | else: |
| 58 | user = users[0] |
| 59 | return user |
| 60 | |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 61 | def delete_user(self, id): |
| 62 | users = self.shell.keystone.users.findall(id=id) |
| 63 | for user in users: |
| 64 | self.shell.keystone.users.delete(user) |
| 65 | return 1 |
| 66 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 67 | def add_user_role(self, user_id, tenant_id, role_name): |
| 68 | user = self.shell.keystone.users.find(id=user_id) |
| 69 | tenant = self.shell.keystone.tenants.find(id=tenant_id) |
| 70 | role = self.shell.keystone.roles.find(role_name) |
| 71 | return tenant.add_user(user, role) |
| 72 | |
| 73 | def delete_user_role(self, user_id, tenant_id, role_name): |
| 74 | user = self.shell.keystone.users.find(id=user_id) |
| 75 | tenant = self.shell.keystone.tenants.find(id=tenant_id) |
| 76 | role = self.shell.keystone.roles.find(role_name) |
| 77 | return tenant.delete_user(user, role) |
| 78 | |
| 79 | def update_user(self, id, **kwds): |
| 80 | return self.shell.keystone.users.update(id, **kwds) |
| 81 | |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 82 | def create_router(self, name, set_gateway=True): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 83 | routers = self.shell.quantum.list_routers(name=name)['routers'] |
| 84 | if routers: |
| 85 | router = routers[0] |
| 86 | else: |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 87 | router = self.shell.quantum.create_router({'router': {'name': name}})['router'] |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 88 | if set_gateway: |
Tony Mack | 257bedc | 2013-04-10 08:58:09 -0400 | [diff] [blame] | 89 | nets = self.shell.quantum.list_networks()['networks'] |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 90 | for net in nets: |
| 91 | if net['router:external'] == True: |
Tony Mack | 9dcc050 | 2013-04-10 09:07:45 -0400 | [diff] [blame] | 92 | self.shell.quantum.add_gateway_router(router['id'], |
| 93 | {'network_id': net['id']}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 94 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 95 | return router |
| 96 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 97 | def delete_router(self, id): |
| 98 | routers = self.shell.quantum.list_routers(id=id)['routers'] |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 99 | for router in routers: |
| 100 | self.shell.quantum.delete_router(router['id']) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 101 | |
| 102 | def add_router_interface(self, router_id, subnet_id): |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 103 | router = self.shell.quantum.show_router(router_id)['router'] |
| 104 | subnet = self.shell.quantum.show_subnet(subnet_id)['subnet'] |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 105 | if router and subnet: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 106 | self.shell.quantum.add_interface_router(router_id, {'subnet_id': subnet_id}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 107 | |
| 108 | def delete_router_interface(self, router_id, subnet_id): |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 109 | router = self.shell.quantum.show_router(router_id) |
| 110 | subnet = self.shell.quantum.show_subnet(subnet_id) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 111 | if router and subnet: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 112 | self.shell.quantum.remove_interface_router(router_id, {'subnet_id': subnet_id}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 113 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 114 | def create_network(self, name): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 115 | nets = self.shell.quantum.list_networks(name=name)['networks'] |
| 116 | if nets: |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 117 | net = nets[0] |
Tony Mack | 82a688d | 2013-04-10 08:12:58 -0400 | [diff] [blame] | 118 | else: |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 119 | net = self.shell.quantum.create_network({'network': {'name': name}})['network'] |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 120 | return net |
| 121 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 122 | def delete_network(self, id): |
| 123 | nets = self.shell.quantum.list_networks()['networks'] |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 124 | for net in nets: |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 125 | if net['id'] == id: |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 126 | # delete_all ports |
| 127 | self.delete_network_ports(net['id']) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 128 | # delete all subnets: |
| 129 | for subnet_id in net['subnets']: |
| 130 | self.delete_subnet(subnet_id) |
| 131 | self.shell.quantum.delete_network(net['id']) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 132 | return 1 |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 133 | |
| 134 | def delete_network_ports(self, network_id): |
| 135 | ports = self.shell.quantum.list_ports()['ports'] |
| 136 | for port in ports: |
| 137 | if port['network_id'] == 'network_id': |
| 138 | self.shell.quantum.delete_port(port['id']) |
| 139 | return 1 |
| 140 | |
| 141 | def delete_subnet_ports(self, subnet_id): |
| 142 | ports = self.shell.quantum.list_ports()['ports'] |
| 143 | for port in ports: |
| 144 | delete = False |
| 145 | for fixed_ip in port['fixed_ips']: |
| 146 | if fixed_ip['subnet_id'] == subnet_id: |
| 147 | delete=True |
| 148 | break |
| 149 | if delete: |
| 150 | self.shell.quantum.delete_port(port['id']) |
| 151 | return 1 |
| 152 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 153 | def create_subnet(self, name, network_id, cidr_ip, ip_version, start, end): |
| 154 | #nets = self.shell.quantum.list_networks(name=network_name)['networks'] |
| 155 | #if not nets: |
| 156 | # raise Exception, "No such network: %s" % network_name |
| 157 | #net = nets[0] |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 158 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 159 | subnet = None |
| 160 | subnets = self.shell.quantum.list_subnets()['subnets'] |
| 161 | for snet in subnets: |
| 162 | if snet['cidr'] == cidr_ip and snet['network_id'] == network_id: |
| 163 | subnet = snet |
| 164 | |
| 165 | if not subnet: |
| 166 | allocation_pools = [{'start': start, 'end': end}] |
| 167 | subnet = {'subnet': {'name': name, |
| 168 | 'network_id': network_id, |
| 169 | 'ip_version': ip_version, |
| 170 | 'cidr': cidr_ip, |
| 171 | 'dns_nameservers': ['8.8.8.8', '8.8.8.4'], |
| 172 | 'allocation_pools': allocation_pools}} |
| 173 | subnet = self.shell.quantum.create_subnet(subnet)['subnet'] |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 174 | |
| 175 | # TODO: Add route to external network |
| 176 | # e.g. # route add -net 10.0.3.0/24 dev br-ex gw 10.100.0.5 |
| 177 | return subnet |
| 178 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 179 | def update_subnet(self, id, fields): |
| 180 | return self.shell.quantum.update_subnet(id, fields) |
| 181 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 182 | def delete_subnet(self, id): |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 183 | #return self.shell.quantum.delete_subnet(id=id) |
| 184 | # inefficient but fault tolerant |
| 185 | subnets = self.shell.quantum.list_subnets()['subnets'] |
| 186 | for subnet in subnets: |
| 187 | if subnet['id'] == id: |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 188 | self.delete_subnet_ports(subnet['id']) |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 189 | self.shell.quantum.delete_subnet(id) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 190 | return |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 191 | |
| 192 | def create_keypair(self, name, key): |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 193 | keys = self.shell.nova.keypairs.findall(name=name) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 194 | if keys: |
Tony Mack | 52cb930 | 2013-04-09 12:25:35 -0400 | [diff] [blame] | 195 | key = keys[0] |
| 196 | else: |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 197 | key = self.shell.nova.keypairs.create(name=name, public_key=key) |
Tony Mack | 52cb930 | 2013-04-09 12:25:35 -0400 | [diff] [blame] | 198 | return key |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 199 | |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 200 | def delete_keypair(self, id): |
| 201 | keys = self.shell.nova.keypairs.findall(id=id) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 202 | for key in keys: |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 203 | self.shell.nova.keypairs.delete(key) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 204 | return 1 |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 205 | |
Tony Mack | 221957d | 2013-04-13 19:08:34 -0400 | [diff] [blame] | 206 | def spawn_instance(self, name, key_name=None, hostname=None, flavor_id=None, image_id=None, security_group=None, pubkeys=[]): |
| 207 | #if not flavor_id: |
| 208 | # flavor = self.config.nova_default_flavor |
| 209 | #if not image: |
| 210 | # image = self.config.nova_default_imave |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 211 | if not security_group: |
| 212 | security_group = self.config.nova_default_security_group |
| 213 | |
| 214 | authorized_keys = "\n".join(pubkeys) |
| 215 | files = {'/root/.ssh/authorized_keys': authorized_keys} |
| 216 | |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 217 | hints = {} |
Tony Mack | 79b28ca | 2013-04-15 21:47:45 -0400 | [diff] [blame] | 218 | availability_zone = None |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 219 | if hostname: |
Tony Mack | 79b28ca | 2013-04-15 21:47:45 -0400 | [diff] [blame] | 220 | availability_zone = 'nova:%s' % hostname |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 221 | server = self.shell.nova.servers.create( |
| 222 | name=name, |
| 223 | key_name = key_name, |
| 224 | flavor=flavor_id, |
| 225 | image=image_id, |
| 226 | security_group = security_group, |
| 227 | files=files, |
Tony Mack | 79b28ca | 2013-04-15 21:47:45 -0400 | [diff] [blame] | 228 | scheduler_hints=hints, |
| 229 | availability_zone=availability_zone) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 230 | return server |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 231 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 232 | def destroy_instance(self, id): |
| 233 | servers = self.shell.nova.servers.findall(id=id) |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 234 | for server in servers: |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 235 | self.shell.nova.servers.delete(server) |