blob: 5bb1eb2537dc1432ea2c73a9e14f6814b9208529 [file] [log] [blame]
Tony Mack9b5dd752013-05-09 20:33:21 -04001import commands
Siobhan Tully30fd4292013-05-10 08:59:56 -04002from planetstack.config import Config
3from openstack.client import OpenStackClient
Tony Mack01942f52013-03-26 14:26:40 -04004
Tony Macka033ced2013-03-29 17:57:06 -04005class OpenStackDriver:
Tony Mack01942f52013-03-26 14:26:40 -04006
Tony Mackad8b0932013-04-07 19:32:07 -04007 def __init__(self, config = None, client=None):
Tony Mack01942f52013-03-26 14:26:40 -04008 if config:
9 self.config = Config(config)
10 else:
11 self.config = Config()
Tony Mackad8b0932013-04-07 19:32:07 -040012
Tony Mack0dc2f562013-04-29 19:41:39 -040013 self.admin_client = OpenStackClient()
Tony Mack89f70f12013-05-10 20:20:03 -040014 self.admin_user = self.admin_client.keystone.users.find(name=self.admin_client.keystone.username)
Tony Mack0dc2f562013-04-29 19:41:39 -040015
Tony Mackad8b0932013-04-07 19:32:07 -040016 if client:
17 self.shell = client
18 else:
19 self.shell = OpenStackClient()
Tony Mack01942f52013-03-26 14:26:40 -040020
Scott Baker8a6d5122013-08-07 18:57:15 -070021 def create_role(self, name):
Tony Mackb5f339f2013-04-08 16:29:38 -040022 roles = self.shell.keystone.roles.findall(name=name)
23 if not roles:
24 role = self.shell.keystone.roles.create(name)
25 else:
Scott Baker8a6d5122013-08-07 18:57:15 -070026 role = roles[0]
Tony Mackb5f339f2013-04-08 16:29:38 -040027 return role
28
Tony Macka7040532013-04-08 21:45:43 -040029 def delete_role(self, filter):
30 roles = self.shell.keystone.roles.findall(**filter)
Tony Mackb5f339f2013-04-08 16:29:38 -040031 for role in roles:
32 self.shell.keystone.roles.delete(role)
33 return 1
34
Tony Mack1fbdeca2013-04-06 00:10:36 -040035 def create_tenant(self, tenant_name, enabled, description):
Tony Mack5ee21902013-03-29 20:36:42 -040036 """Create keystone tenant. Suggested fields: name, description, enabled"""
Tony Mack1fbdeca2013-04-06 00:10:36 -040037 tenants = self.shell.keystone.tenants.findall(name=tenant_name)
Tony Mack7823b892013-03-29 20:35:26 -040038 if not tenants:
Tony Mack1fbdeca2013-04-06 00:10:36 -040039 fields = {'tenant_name': tenant_name, 'enabled': enabled,
Tony Mack488950a2013-04-07 20:35:50 -040040 'description': description}
Tony Mack1fbdeca2013-04-06 00:10:36 -040041 tenant = self.shell.keystone.tenants.create(**fields)
Tony Mack7823b892013-03-29 20:35:26 -040042 else:
43 tenant = tenants[0]
Tony Mack0dc2f562013-04-29 19:41:39 -040044
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 Mack7823b892013-03-29 20:35:26 -040048 return tenant
49
50 def update_tenant(self, id, **kwds):
Tony Mack1fbdeca2013-04-06 00:10:36 -040051 return self.shell.keystone.tenants.update(id, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -040052
53 def delete_tenant(self, id):
Tony Mackac3e0142013-06-26 11:28:57 -040054 ctx = self.shell.nova_db.ctx
Tony Mackfd24d0d2013-04-14 00:59:17 -040055 tenants = self.shell.keystone.tenants.findall(id=id)
56 for tenant in tenants:
Tony Mackac3e0142013-06-26 11:28:57 -040057 # nova does not automatically delete the tenant's instances
58 # so we manually delete instances before deleteing the tenant
Scott Baker8a6d5122013-08-07 18:57:15 -070059 instances = self.shell.nova_db.instance_get_all_by_filters(ctx,
Tony Mackac3e0142013-06-26 11:28:57 -040060 {'project_id': tenant.id}, 'id', 'asc')
Tony Mack28897e12013-07-09 07:25:38 -040061 client = OpenStackClient(tenant=tenant.name)
Tony Mackac3e0142013-06-26 11:28:57 -040062 driver = OpenStackDriver(client=client)
63 for instance in instances:
64 driver.destroy_instance(instance.id)
Tony Mackfd24d0d2013-04-14 00:59:17 -040065 self.shell.keystone.tenants.delete(tenant)
66 return 1
Tony Mack0211f1b2013-04-04 23:56:24 -040067
Tony Mack1fbdeca2013-04-06 00:10:36 -040068 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 Mackfdcbf832013-04-09 11:47:11 -040073 user = self.shell.keystone.users.create(**fields)
Tony Mack1fbdeca2013-04-06 00:10:36 -040074 else:
75 user = users[0]
76 return user
77
Tony Mackfd24d0d2013-04-14 00:59:17 -040078 def delete_user(self, id):
79 users = self.shell.keystone.users.findall(id=id)
80 for user in users:
Tony Mackc59fcaf2013-06-10 11:14:04 -040081 # delete users keys
82 keys = self.shell.nova.keypairs.findall()
83 for key in keys:
84 self.shell.nova.keypairs.delete(key)
Tony Mackfd24d0d2013-04-14 00:59:17 -040085 self.shell.keystone.users.delete(user)
86 return 1
87
Siobhan Tully30fd4292013-05-10 08:59:56 -040088 def add_user_role(self, kuser_id, tenant_id, role_name):
89 user = self.shell.keystone.users.find(id=kuser_id)
Tony Mack1fbdeca2013-04-06 00:10:36 -040090 tenant = self.shell.keystone.tenants.find(id=tenant_id)
Tony Mack54304c62013-04-28 11:27:07 -040091 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 Mack1fbdeca2013-04-06 00:10:36 -0400102
Siobhan Tully30fd4292013-05-10 08:59:56 -0400103 def delete_user_role(self, kuser_id, tenant_id, role_name):
104 user = self.shell.keystone.users.find(id=kuser_id)
Tony Mack1fbdeca2013-04-06 00:10:36 -0400105 tenant = self.shell.keystone.tenants.find(id=tenant_id)
Tony Mack54304c62013-04-28 11:27:07 -0400106 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 Mack1fbdeca2013-04-06 00:10:36 -0400117
Tony Mack6795fbe2013-06-11 10:10:24 -0400118 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 Mack1fbdeca2013-04-06 00:10:36 -0400124
Tony Mack240903f2013-04-06 01:31:37 -0400125 def create_router(self, name, set_gateway=True):
Tony Mack28be7562013-04-10 08:45:55 -0400126 routers = self.shell.quantum.list_routers(name=name)['routers']
127 if routers:
128 router = routers[0]
129 else:
Tony Mack62bc59a2013-04-14 23:27:12 -0400130 router = self.shell.quantum.create_router({'router': {'name': name}})['router']
Tony Mackc1665b12013-04-29 16:45:14 -0400131 # add router to external network
Tony Mack240903f2013-04-06 01:31:37 -0400132 if set_gateway:
Tony Mack257bedc2013-04-10 08:58:09 -0400133 nets = self.shell.quantum.list_networks()['networks']
Tony Mack240903f2013-04-06 01:31:37 -0400134 for net in nets:
135 if net['router:external'] == True:
Tony Mack9dcc0502013-04-10 09:07:45 -0400136 self.shell.quantum.add_gateway_router(router['id'],
137 {'network_id': net['id']})
Tony Mack240903f2013-04-06 01:31:37 -0400138
Tony Mack0211f1b2013-04-04 23:56:24 -0400139 return router
140
Tony Mack62bc59a2013-04-14 23:27:12 -0400141 def delete_router(self, id):
142 routers = self.shell.quantum.list_routers(id=id)['routers']
Tony Mack28be7562013-04-10 08:45:55 -0400143 for router in routers:
144 self.shell.quantum.delete_router(router['id'])
Tony Mackc1665b12013-04-29 16:45:14 -0400145 # remove router form external network
Tony Mack0c0e05d2013-04-29 17:52:37 -0400146 #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 Mack240903f2013-04-06 01:31:37 -0400150
151 def add_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400152 router = self.shell.quantum.show_router(router_id)['router']
153 subnet = self.shell.quantum.show_subnet(subnet_id)['subnet']
Tony Mack240903f2013-04-06 01:31:37 -0400154 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400155 self.shell.quantum.add_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400156
157 def delete_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400158 router = self.shell.quantum.show_router(router_id)
159 subnet = self.shell.quantum.show_subnet(subnet_id)
Tony Mack240903f2013-04-06 01:31:37 -0400160 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400161 self.shell.quantum.remove_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400162
Scott Baker1ac5ddb2013-08-14 10:50:48 -0700163 def create_network(self, name, shared=False):
Tony Mack28be7562013-04-10 08:45:55 -0400164 nets = self.shell.quantum.list_networks(name=name)['networks']
165 if nets:
Tony Mack53c63342013-04-10 08:11:45 -0400166 net = nets[0]
Tony Mack82a688d2013-04-10 08:12:58 -0400167 else:
Scott Baker1ac5ddb2013-08-14 10:50:48 -0700168 net = self.shell.quantum.create_network({'network': {'name': name, 'shared': shared}})['network']
Tony Mack53c63342013-04-10 08:11:45 -0400169 return net
170
Tony Mack62bc59a2013-04-14 23:27:12 -0400171 def delete_network(self, id):
172 nets = self.shell.quantum.list_networks()['networks']
Tony Mack0211f1b2013-04-04 23:56:24 -0400173 for net in nets:
Tony Mack62bc59a2013-04-14 23:27:12 -0400174 if net['id'] == id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400175 # delete_all ports
176 self.delete_network_ports(net['id'])
Tony Mack62bc59a2013-04-14 23:27:12 -0400177 # 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 Mackfd24d0d2013-04-14 00:59:17 -0400181 return 1
Tony Mack94c6ea02013-04-16 21:41:50 -0400182
183 def delete_network_ports(self, network_id):
184 ports = self.shell.quantum.list_ports()['ports']
185 for port in ports:
Tony Mackc1665b12013-04-29 16:45:14 -0400186 if port['network_id'] == network_id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400187 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 Mack48952032013-04-12 11:49:34 -0400202 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 Mack0211f1b2013-04-04 23:56:24 -0400207
Tony Mack48952032013-04-12 11:49:34 -0400208 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
Scott Baker8a6d5122013-08-07 18:57:15 -0700213
Tony Mack48952032013-04-12 11:49:34 -0400214 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 Mackcdb85aa2013-05-02 15:09:45 -0400220 'dns_nameservers': ['8.8.8.8', '8.8.4.4'],
Scott Baker8a6d5122013-08-07 18:57:15 -0700221 'allocation_pools': allocation_pools}}
Tony Mack48952032013-04-12 11:49:34 -0400222 subnet = self.shell.quantum.create_subnet(subnet)['subnet']
Tony Mack9b5dd752013-05-09 20:33:21 -0400223 self.add_external_route(subnet)
Tony Mack0211f1b2013-04-04 23:56:24 -0400224 # 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 Mack48952032013-04-12 11:49:34 -0400228 def update_subnet(self, id, fields):
229 return self.shell.quantum.update_subnet(id, fields)
230
Tony Mack0211f1b2013-04-04 23:56:24 -0400231 def delete_subnet(self, id):
Tony Mackfd24d0d2013-04-14 00:59:17 -0400232 #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 Mack94c6ea02013-04-16 21:41:50 -0400237 self.delete_subnet_ports(subnet['id'])
Tony Mack31d34132013-04-15 00:02:06 -0400238 self.shell.quantum.delete_subnet(id)
Tony Mack9b5dd752013-05-09 20:33:21 -0400239 self.delete_external_route(subnet)
240 return 1
241
Tony Mack70780e92013-06-30 12:46:16 -0400242 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 Mack9b5dd752013-05-09 20:33:21 -0400251 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 Mack44187012013-07-09 07:15:17 -0400267 if router and router.get('external_gateway_info'):
Tony Mack16f51e02013-07-01 10:33:29 -0400268 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 Mack9b5dd752013-05-09 20:33:21 -0400272
273 if ip_address:
Tony Mack70780e92013-06-30 12:46:16 -0400274 # 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 Mack16f51e02013-07-01 10:33:29 -0400282 s, o = commands.getstatusoutput(cmd)
283 #print cmd, "\n", s, o
Tony Mack9b5dd752013-05-09 20:33:21 -0400284
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 Mack0211f1b2013-04-04 23:56:24 -0400314
Tony Mackb0d97422013-06-10 09:57:45 -0400315 def create_keypair(self, name, public_key):
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400316 keys = self.shell.nova.keypairs.findall(name=name)
Tony Mack0211f1b2013-04-04 23:56:24 -0400317 if keys:
Tony Mack52cb9302013-04-09 12:25:35 -0400318 key = keys[0]
Tony Mackb0d97422013-06-10 09:57:45 -0400319 # 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 Mack52cb9302013-04-09 12:25:35 -0400323 else:
Tony Mackb0d97422013-06-10 09:57:45 -0400324 key = self.shell.nova.keypairs.create(name=name, public_key=public_key)
Tony Mack52cb9302013-04-09 12:25:35 -0400325 return key
Tony Mack0211f1b2013-04-04 23:56:24 -0400326
Tony Mack759b57a2013-04-14 21:03:31 -0400327 def delete_keypair(self, id):
328 keys = self.shell.nova.keypairs.findall(id=id)
Tony Mack0211f1b2013-04-04 23:56:24 -0400329 for key in keys:
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400330 self.shell.nova.keypairs.delete(key)
Scott Baker8a6d5122013-08-07 18:57:15 -0700331 return 1
Tony Mack7823b892013-03-29 20:35:26 -0400332
Scott Baker8a6d5122013-08-07 18:57:15 -0700333 def get_private_networks(self, tenant=None):
334 if not tenant:
335 tenant = self.shell.nova.tenant
336 tenant = self.shell.keystone.tenants.find(name=tenant)
337 search_opts = {"tenant_id": tenant.id, "shared": False}
338 private_networks = self.shell.quantum.list_networks(**search_opts)
339 return private_networks
340
341 def get_shared_networks(self):
342 search_opts = {"shared": True}
343 shared_networks = self.shell.quantum.list_networks(**search_opts)
344 return shared_networks
345
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700346 def get_network_subnet(self, network_id):
347 subnet_id = None
348 subnet = None
349 if network_id:
350 os_networks = self.shell.quantum.list_networks(id=network_id)["networks"]
351 if os_networks:
352 os_network = os_networks[0]
353 if os_network['subnets']:
354 subnet_id = os_network['subnets'][0]
355 os_subnets = self.shell.quantum.list_subnets(id=subnet_id)['subnets']
356 if os_subnets:
357 subnet = os_subnets[0]['cidr']
358
359 return (subnet_id, subnet)
360
Scott Baker57368722013-08-13 18:04:38 -0700361 def spawn_instance(self, name, key_name=None, hostname=None, image_id=None, security_group=None, pubkeys=[], nics=None):
Tony Macke7e304f2013-04-30 18:18:03 -0400362 flavor_name = self.config.nova_default_flavor
363 flavor = self.shell.nova.flavors.find(name=flavor_name)
Tony Mack221957d2013-04-13 19:08:34 -0400364 #if not image:
365 # image = self.config.nova_default_imave
Tony Mack01942f52013-03-26 14:26:40 -0400366 if not security_group:
367 security_group = self.config.nova_default_security_group
368
Tony Mack36538ab2013-05-01 17:24:46 -0400369 files = {}
Tony Mack5ff5c452013-06-11 11:19:32 -0400370 if pubkeys:
371 files['/root/.ssh/authorized_keys'] = "\n".join(pubkeys)
Tony Mack01942f52013-03-26 14:26:40 -0400372
Tony Mack7823b892013-03-29 20:35:26 -0400373 hints = {}
Tony Mack79b28ca2013-04-15 21:47:45 -0400374 availability_zone = None
Tony Mack7823b892013-03-29 20:35:26 -0400375 if hostname:
Tony Mack79b28ca2013-04-15 21:47:45 -0400376 availability_zone = 'nova:%s' % hostname
Tony Mack7823b892013-03-29 20:35:26 -0400377 server = self.shell.nova.servers.create(
378 name=name,
379 key_name = key_name,
Tony Macke7e304f2013-04-30 18:18:03 -0400380 flavor=flavor.id,
Tony Mack7823b892013-03-29 20:35:26 -0400381 image=image_id,
382 security_group = security_group,
383 files=files,
Tony Mack79b28ca2013-04-15 21:47:45 -0400384 scheduler_hints=hints,
Scott Baker8a6d5122013-08-07 18:57:15 -0700385 availability_zone=availability_zone,
Scott Baker57368722013-08-13 18:04:38 -0700386 nics=nics)
Tony Mack7823b892013-03-29 20:35:26 -0400387 return server
Tony Mack01942f52013-03-26 14:26:40 -0400388
Tony Mackcdec0902013-04-15 00:38:49 -0400389 def destroy_instance(self, id):
390 servers = self.shell.nova.servers.findall(id=id)
Tony Mack01942f52013-03-26 14:26:40 -0400391 for server in servers:
Tony Mackcdec0902013-04-15 00:38:49 -0400392 self.shell.nova.servers.delete(server)
smbaker6ada7bf2013-04-30 18:15:48 -0700393
394 def update_instance_metadata(self, id, metadata):
395 servers = self.shell.nova.servers.findall(id=id)
396 for server in servers:
397 self.shell.nova.servers.set_meta(server, metadata)
398 # note: set_meta() returns a broken Server() object. Don't try to
399 # print it in the shell or it will fail in __repr__.
400
401 def delete_instance_metadata(self, id, metadata):
402 # note: metadata is a dict. Only the keys matter, not the values.
403 servers = self.shell.nova.servers.findall(id=id)
404 for server in servers:
405 self.shell.nova.servers.delete_meta(server, metadata)
406