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