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): |
| 46 | tenant = self.shell.keystone.tenants.find(id=id) |
| 47 | return self.shell.keystone.tenants.delete(tenant) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 48 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 49 | def create_user(self, name, email, password, enabled): |
| 50 | users = self.shell.keystone.users.findall(email=email) |
| 51 | if not users: |
| 52 | fields = {'name': name, 'email': email, 'password': password, |
| 53 | 'enabled': enabled} |
Tony Mack | fdcbf83 | 2013-04-09 11:47:11 -0400 | [diff] [blame] | 54 | user = self.shell.keystone.users.create(**fields) |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 55 | else: |
| 56 | user = users[0] |
| 57 | return user |
| 58 | |
| 59 | def add_user_role(self, user_id, tenant_id, role_name): |
| 60 | user = self.shell.keystone.users.find(id=user_id) |
| 61 | tenant = self.shell.keystone.tenants.find(id=tenant_id) |
| 62 | role = self.shell.keystone.roles.find(role_name) |
| 63 | return tenant.add_user(user, role) |
| 64 | |
| 65 | def delete_user_role(self, user_id, tenant_id, role_name): |
| 66 | user = self.shell.keystone.users.find(id=user_id) |
| 67 | tenant = self.shell.keystone.tenants.find(id=tenant_id) |
| 68 | role = self.shell.keystone.roles.find(role_name) |
| 69 | return tenant.delete_user(user, role) |
| 70 | |
| 71 | def update_user(self, id, **kwds): |
| 72 | return self.shell.keystone.users.update(id, **kwds) |
| 73 | |
| 74 | def delete_user(self, id): |
| 75 | user = self.shell.keystone.users.find(id=id) |
| 76 | return self.shell.keystone.users.delete(user) |
| 77 | |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 78 | def create_router(self, name, set_gateway=True): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 79 | routers = self.shell.quantum.list_routers(name=name)['routers'] |
| 80 | if routers: |
| 81 | router = routers[0] |
| 82 | else: |
| 83 | router = self.shell.quantum.create_router({'router': {'name': name}}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 84 | if set_gateway: |
Tony Mack | 257bedc | 2013-04-10 08:58:09 -0400 | [diff] [blame] | 85 | nets = self.shell.quantum.list_networks()['networks'] |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 86 | for net in nets: |
| 87 | if net['router:external'] == True: |
Tony Mack | 9dcc050 | 2013-04-10 09:07:45 -0400 | [diff] [blame] | 88 | self.shell.quantum.add_gateway_router(router['id'], |
| 89 | {'network_id': net['id']}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 90 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 91 | return router |
| 92 | |
| 93 | def delete_router(self, name): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 94 | routers = self.shell.quantum.list_routers(name=name)['routers'] |
| 95 | for router in routers: |
| 96 | self.shell.quantum.delete_router(router['id']) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 97 | |
| 98 | def add_router_interface(self, router_id, subnet_id): |
| 99 | router = None |
| 100 | subnet = None |
| 101 | for r in self.shell.quantum.list_routers(): |
| 102 | if r['id'] == router_id: |
| 103 | router = r |
| 104 | break |
| 105 | for s in self.shell.quantum.list_subnets(): |
| 106 | if s['id'] == subnet_id: |
| 107 | subnet = s |
| 108 | break |
| 109 | |
| 110 | if router and subnet: |
| 111 | self.shell.quantum.router_add_interface(router, subnet) |
| 112 | |
| 113 | def delete_router_interface(self, router_id, subnet_id): |
| 114 | router = None |
| 115 | subnet = None |
| 116 | for r in self.shell.quantum.list_routers(): |
| 117 | if r['id'] == router_id: |
| 118 | router = r |
| 119 | break |
| 120 | for s in self.shell.quantum.list_subnets(): |
| 121 | if s['id'] == subnet_id: |
| 122 | subnet = s |
| 123 | break |
| 124 | |
| 125 | if router and subnet: |
| 126 | self.shell.quantum.router_remove_interface(router, subnet) |
| 127 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 128 | def create_network(self, name): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 129 | nets = self.shell.quantum.list_networks(name=name)['networks'] |
| 130 | if nets: |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 131 | net = nets[0] |
Tony Mack | 82a688d | 2013-04-10 08:12:58 -0400 | [diff] [blame] | 132 | else: |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 133 | net = self.shell.quantum.create_network({'network': {'name': name}}) |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 134 | return net |
| 135 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 136 | def delete_network(self, name): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 137 | nets = self.shell.quantum.list_networks(name=name)['networks'] |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 138 | for net in nets: |
| 139 | # delete all subnets: |
| 140 | #subnets = self.api.client_shell.quantum.list_subnets(network_id=net['network_id'])['subnets'] |
| 141 | for subnet_id in net['subnets']: |
| 142 | self.delete_subnet(subnet_id) |
| 143 | self.shell.quantum.delete_network(net['id']) |
| 144 | |
| 145 | def create_subnet(self, network_name, cidr_ip, ip_version, start, end): |
| 146 | nets = self.shell.quantum.list_networks(name=network_name) |
| 147 | if not nets: |
| 148 | raise Exception, "No such network: %s" % network_name |
| 149 | nets = nets[0] |
| 150 | |
| 151 | subnets = self.shell.quantum.list_subnets(name=self.name) |
| 152 | allocation_pools = [{'start': start, 'end': end}] |
| 153 | subnet = self.shell.quantum.create_subnet(network_id=net['id'], |
| 154 | ip_version=ip_version, |
| 155 | cidr=cidr_ip, |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 156 | dns_nameservers=['8.8.8.8', '8.8.8.4'], |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 157 | allocation_pools=allocation_pools) |
| 158 | |
| 159 | # TODO: Add route to external network |
| 160 | # e.g. # route add -net 10.0.3.0/24 dev br-ex gw 10.100.0.5 |
| 161 | return subnet |
| 162 | |
| 163 | def delete_subnet(self, id): |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 164 | return self.shell.quantum.delete_subnet(id=id) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 165 | |
| 166 | |
| 167 | def create_keypair(self, name, key): |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 168 | keys = self.shell.nova.keypairs.findall(name=name) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 169 | if keys: |
Tony Mack | 52cb930 | 2013-04-09 12:25:35 -0400 | [diff] [blame] | 170 | key = keys[0] |
| 171 | else: |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 172 | key = self.shell.nova.keypairs.create(name=name, public_key=key) |
Tony Mack | 52cb930 | 2013-04-09 12:25:35 -0400 | [diff] [blame] | 173 | return key |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 174 | |
| 175 | def delete_keypair(self, name): |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 176 | keys = self.shell.nova.keypairs.findall(name=name) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 177 | for key in keys: |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 178 | self.shell.nova.keypairs.delete(key) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 179 | |
| 180 | def spawn_instance(self, name, key_name=None, hostname=None, flavor=None, image=None, security_group=None, pubkeys=[]): |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 181 | if not flavor: |
| 182 | flavor = self.config.nova_default_flavor |
| 183 | if not image: |
| 184 | image = self.config.nova_default_imave |
| 185 | if not security_group: |
| 186 | security_group = self.config.nova_default_security_group |
| 187 | |
| 188 | authorized_keys = "\n".join(pubkeys) |
| 189 | files = {'/root/.ssh/authorized_keys': authorized_keys} |
| 190 | |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 191 | flavor_id = self.shell.nova.flavors.find(name=flavor) |
| 192 | images = self.shell.glance.get_images(name=image) |
| 193 | if not images: |
| 194 | raise Exception, "Image not found: " + image |
| 195 | image_id = images[0]['id'] |
| 196 | hints = {} |
| 197 | if hostname: |
| 198 | hints['force_hosts']= hostname |
| 199 | server = self.shell.nova.servers.create( |
| 200 | name=name, |
| 201 | key_name = key_name, |
| 202 | flavor=flavor_id, |
| 203 | image=image_id, |
| 204 | security_group = security_group, |
| 205 | files=files, |
| 206 | scheduler_hints=hints) |
| 207 | return server |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 208 | |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 209 | def destroy_instance(self, name, id=None): |
| 210 | args = {'name': name} |
| 211 | if id: |
| 212 | args['id'] = id |
| 213 | servers = self.shell.nova.servers.findall(**args) |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 214 | for server in servers: |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 215 | if name == server.name: |
Tony Mack | 07ece3b | 2013-03-29 20:51:07 -0400 | [diff] [blame] | 216 | if not id or id == server.id: |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 217 | self.shell.nova.servers.delete(server) |