blob: f84fbb8d2b039c0d07c26fac97912b6aee70e6d1 [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
Tony Mackb5f339f2013-04-08 16:29:38 -040021 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 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 Mackfd24d0d2013-04-14 00:59:17 -040054 tenants = self.shell.keystone.tenants.findall(id=id)
55 for tenant in tenants:
56 self.shell.keystone.tenants.delete(tenant)
57 return 1
Tony Mack0211f1b2013-04-04 23:56:24 -040058
Tony Mack1fbdeca2013-04-06 00:10:36 -040059 def create_user(self, name, email, password, enabled):
60 users = self.shell.keystone.users.findall(email=email)
61 if not users:
62 fields = {'name': name, 'email': email, 'password': password,
63 'enabled': enabled}
Tony Mackfdcbf832013-04-09 11:47:11 -040064 user = self.shell.keystone.users.create(**fields)
Tony Mack1fbdeca2013-04-06 00:10:36 -040065 else:
66 user = users[0]
67 return user
68
Tony Mackfd24d0d2013-04-14 00:59:17 -040069 def delete_user(self, id):
70 users = self.shell.keystone.users.findall(id=id)
71 for user in users:
72 self.shell.keystone.users.delete(user)
73 return 1
74
Siobhan Tully30fd4292013-05-10 08:59:56 -040075 def add_user_role(self, kuser_id, tenant_id, role_name):
76 user = self.shell.keystone.users.find(id=kuser_id)
Tony Mack1fbdeca2013-04-06 00:10:36 -040077 tenant = self.shell.keystone.tenants.find(id=tenant_id)
Tony Mack54304c62013-04-28 11:27:07 -040078 role = self.shell.keystone.roles.find(name=role_name)
79
80 role_found = False
81 user_roles = user.list_roles(tenant.id)
82 for user_role in user_roles:
83 if user_role.name == role.name:
84 role_found = True
85 if not role_found:
86 tenant.add_user(user, role)
87
88 return 1
Tony Mack1fbdeca2013-04-06 00:10:36 -040089
Siobhan Tully30fd4292013-05-10 08:59:56 -040090 def delete_user_role(self, kuser_id, tenant_id, role_name):
91 user = self.shell.keystone.users.find(id=kuser_id)
Tony Mack1fbdeca2013-04-06 00:10:36 -040092 tenant = self.shell.keystone.tenants.find(id=tenant_id)
Tony Mack54304c62013-04-28 11:27:07 -040093 role = self.shell.keystone.roles.find(name=role_name)
94
95 role_found = False
96 user_roles = user.list_roles(tenant.id)
97 for user_role in user_roles:
98 if user_role.name == role.name:
99 role_found = True
100 if role_found:
101 tenant.remove_user(user, role)
102
103 return 1
Tony Mack1fbdeca2013-04-06 00:10:36 -0400104
105 def update_user(self, id, **kwds):
106 return self.shell.keystone.users.update(id, **kwds)
107
Tony Mack240903f2013-04-06 01:31:37 -0400108 def create_router(self, name, set_gateway=True):
Tony Mack28be7562013-04-10 08:45:55 -0400109 routers = self.shell.quantum.list_routers(name=name)['routers']
110 if routers:
111 router = routers[0]
112 else:
Tony Mack62bc59a2013-04-14 23:27:12 -0400113 router = self.shell.quantum.create_router({'router': {'name': name}})['router']
Tony Mackc1665b12013-04-29 16:45:14 -0400114 # add router to external network
Tony Mack240903f2013-04-06 01:31:37 -0400115 if set_gateway:
Tony Mack257bedc2013-04-10 08:58:09 -0400116 nets = self.shell.quantum.list_networks()['networks']
Tony Mack240903f2013-04-06 01:31:37 -0400117 for net in nets:
118 if net['router:external'] == True:
Tony Mack9dcc0502013-04-10 09:07:45 -0400119 self.shell.quantum.add_gateway_router(router['id'],
120 {'network_id': net['id']})
Tony Mack240903f2013-04-06 01:31:37 -0400121
Tony Mack0211f1b2013-04-04 23:56:24 -0400122 return router
123
Tony Mack62bc59a2013-04-14 23:27:12 -0400124 def delete_router(self, id):
125 routers = self.shell.quantum.list_routers(id=id)['routers']
Tony Mack28be7562013-04-10 08:45:55 -0400126 for router in routers:
127 self.shell.quantum.delete_router(router['id'])
Tony Mackc1665b12013-04-29 16:45:14 -0400128 # remove router form external network
Tony Mack0c0e05d2013-04-29 17:52:37 -0400129 #nets = self.shell.quantum.list_networks()['networks']
130 #for net in nets:
131 # if net['router:external'] == True:
132 # self.shell.quantum.remove_gateway_router(router['id'])
Tony Mack240903f2013-04-06 01:31:37 -0400133
134 def add_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400135 router = self.shell.quantum.show_router(router_id)['router']
136 subnet = self.shell.quantum.show_subnet(subnet_id)['subnet']
Tony Mack240903f2013-04-06 01:31:37 -0400137 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400138 self.shell.quantum.add_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400139
140 def delete_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400141 router = self.shell.quantum.show_router(router_id)
142 subnet = self.shell.quantum.show_subnet(subnet_id)
Tony Mack240903f2013-04-06 01:31:37 -0400143 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400144 self.shell.quantum.remove_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400145
Tony Mack0211f1b2013-04-04 23:56:24 -0400146 def create_network(self, name):
Tony Mack28be7562013-04-10 08:45:55 -0400147 nets = self.shell.quantum.list_networks(name=name)['networks']
148 if nets:
Tony Mack53c63342013-04-10 08:11:45 -0400149 net = nets[0]
Tony Mack82a688d2013-04-10 08:12:58 -0400150 else:
Tony Mack62bc59a2013-04-14 23:27:12 -0400151 net = self.shell.quantum.create_network({'network': {'name': name}})['network']
Tony Mack53c63342013-04-10 08:11:45 -0400152 return net
153
Tony Mack62bc59a2013-04-14 23:27:12 -0400154 def delete_network(self, id):
155 nets = self.shell.quantum.list_networks()['networks']
Tony Mack0211f1b2013-04-04 23:56:24 -0400156 for net in nets:
Tony Mack62bc59a2013-04-14 23:27:12 -0400157 if net['id'] == id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400158 # delete_all ports
159 self.delete_network_ports(net['id'])
Tony Mack62bc59a2013-04-14 23:27:12 -0400160 # delete all subnets:
161 for subnet_id in net['subnets']:
162 self.delete_subnet(subnet_id)
163 self.shell.quantum.delete_network(net['id'])
Tony Mackfd24d0d2013-04-14 00:59:17 -0400164 return 1
Tony Mack94c6ea02013-04-16 21:41:50 -0400165
166 def delete_network_ports(self, network_id):
167 ports = self.shell.quantum.list_ports()['ports']
168 for port in ports:
Tony Mackc1665b12013-04-29 16:45:14 -0400169 if port['network_id'] == network_id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400170 self.shell.quantum.delete_port(port['id'])
171 return 1
172
173 def delete_subnet_ports(self, subnet_id):
174 ports = self.shell.quantum.list_ports()['ports']
175 for port in ports:
176 delete = False
177 for fixed_ip in port['fixed_ips']:
178 if fixed_ip['subnet_id'] == subnet_id:
179 delete=True
180 break
181 if delete:
182 self.shell.quantum.delete_port(port['id'])
183 return 1
184
Tony Mack48952032013-04-12 11:49:34 -0400185 def create_subnet(self, name, network_id, cidr_ip, ip_version, start, end):
186 #nets = self.shell.quantum.list_networks(name=network_name)['networks']
187 #if not nets:
188 # raise Exception, "No such network: %s" % network_name
189 #net = nets[0]
Tony Mack0211f1b2013-04-04 23:56:24 -0400190
Tony Mack48952032013-04-12 11:49:34 -0400191 subnet = None
192 subnets = self.shell.quantum.list_subnets()['subnets']
193 for snet in subnets:
194 if snet['cidr'] == cidr_ip and snet['network_id'] == network_id:
195 subnet = snet
196
197 if not subnet:
198 allocation_pools = [{'start': start, 'end': end}]
199 subnet = {'subnet': {'name': name,
200 'network_id': network_id,
201 'ip_version': ip_version,
202 'cidr': cidr_ip,
Tony Mackcdb85aa2013-05-02 15:09:45 -0400203 'dns_nameservers': ['8.8.8.8', '8.8.4.4'],
Tony Mack48952032013-04-12 11:49:34 -0400204 'allocation_pools': allocation_pools}}
205 subnet = self.shell.quantum.create_subnet(subnet)['subnet']
Tony Mack9b5dd752013-05-09 20:33:21 -0400206 self.add_external_route(subnet)
Tony Mack0211f1b2013-04-04 23:56:24 -0400207 # TODO: Add route to external network
208 # e.g. # route add -net 10.0.3.0/24 dev br-ex gw 10.100.0.5
209 return subnet
210
Tony Mack48952032013-04-12 11:49:34 -0400211 def update_subnet(self, id, fields):
212 return self.shell.quantum.update_subnet(id, fields)
213
Tony Mack0211f1b2013-04-04 23:56:24 -0400214 def delete_subnet(self, id):
Tony Mackfd24d0d2013-04-14 00:59:17 -0400215 #return self.shell.quantum.delete_subnet(id=id)
216 # inefficient but fault tolerant
217 subnets = self.shell.quantum.list_subnets()['subnets']
218 for subnet in subnets:
219 if subnet['id'] == id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400220 self.delete_subnet_ports(subnet['id'])
Tony Mack31d34132013-04-15 00:02:06 -0400221 self.shell.quantum.delete_subnet(id)
Tony Mack9b5dd752013-05-09 20:33:21 -0400222 self.delete_external_route(subnet)
223 return 1
224
225 def add_external_route(self, subnet):
226 ports = self.shell.quantum.list_ports()['ports']
227
228 gw_ip = subnet['gateway_ip']
229 subnet_id = subnet['id']
230
231 # 1. Find the port associated with the subnet's gateway
232 # 2. Find the router associated with that port
233 # 3. Find the port associated with this router and on the external net
234 # 4. Set up route to the subnet through the port from step 3
235 ip_address = None
236 for port in ports:
237 for fixed_ip in port['fixed_ips']:
238 if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip:
239 gw_port = port
240 router_id = gw_port['device_id']
241 router = self.shell.quantum.show_router(router_id)['router']
242 ext_net = router['external_gateway_info']['network_id']
243 for port in ports:
244 if port['device_id'] == router_id and port['network_id'] == ext_net:
245 ip_address = port['fixed_ips'][0]['ip_address']
246
247 if ip_address:
248 cmd = "route add -net %s dev br-ex gw %s" % (subnet['cidr'], ip_address)
249 commands.getstatusoutput(cmd)
250
251 return 1
252
253 def delete_external_route(self, subnet):
254 ports = self.shell.quantum.list_ports()['ports']
255
256 gw_ip = subnet['gateway_ip']
257 subnet_id = subnet['id']
258
259 # 1. Find the port associated with the subnet's gateway
260 # 2. Find the router associated with that port
261 # 3. Find the port associated with this router and on the external net
262 # 4. Set up route to the subnet through the port from step 3
263 ip_address = None
264 for port in ports:
265 for fixed_ip in port['fixed_ips']:
266 if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip:
267 gw_port = port
268 router_id = gw_port['device_id']
269 router = self.shell.quantum.show_router(router_id)['router']
270 ext_net = router['external_gateway_info']['network_id']
271 for port in ports:
272 if port['device_id'] == router_id and port['network_id'] == ext_net:
273 ip_address = port['fixed_ips'][0]['ip_address']
274
275 if ip_address:
276 cmd = "route delete -net %s" % (subnet['cidr'])
277 commands.getstatusoutput(cmd)
278
279 return 1
Tony Mack0211f1b2013-04-04 23:56:24 -0400280
281 def create_keypair(self, name, key):
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400282 keys = self.shell.nova.keypairs.findall(name=name)
Tony Mack0211f1b2013-04-04 23:56:24 -0400283 if keys:
Tony Mack52cb9302013-04-09 12:25:35 -0400284 key = keys[0]
285 else:
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400286 key = self.shell.nova.keypairs.create(name=name, public_key=key)
Tony Mack52cb9302013-04-09 12:25:35 -0400287 return key
Tony Mack0211f1b2013-04-04 23:56:24 -0400288
Tony Mack759b57a2013-04-14 21:03:31 -0400289 def delete_keypair(self, id):
290 keys = self.shell.nova.keypairs.findall(id=id)
Tony Mack0211f1b2013-04-04 23:56:24 -0400291 for key in keys:
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400292 self.shell.nova.keypairs.delete(key)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400293 return 1
Tony Mack7823b892013-03-29 20:35:26 -0400294
Tony Macke7e304f2013-04-30 18:18:03 -0400295 def spawn_instance(self, name, key_name=None, hostname=None, image_id=None, security_group=None, pubkeys=[]):
296 flavor_name = self.config.nova_default_flavor
297 flavor = self.shell.nova.flavors.find(name=flavor_name)
Tony Mack221957d2013-04-13 19:08:34 -0400298 #if not image:
299 # image = self.config.nova_default_imave
Tony Mack01942f52013-03-26 14:26:40 -0400300 if not security_group:
301 security_group = self.config.nova_default_security_group
302
Tony Mack36538ab2013-05-01 17:24:46 -0400303 #authorized_keys = "\n".join(pubkeys)
304 #files = {'/root/.ssh/authorized_keys': authorized_keys}
305 files = {}
Tony Mack01942f52013-03-26 14:26:40 -0400306
Tony Mack7823b892013-03-29 20:35:26 -0400307 hints = {}
Tony Mack79b28ca2013-04-15 21:47:45 -0400308 availability_zone = None
Tony Mack7823b892013-03-29 20:35:26 -0400309 if hostname:
Tony Mack79b28ca2013-04-15 21:47:45 -0400310 availability_zone = 'nova:%s' % hostname
Tony Mack7823b892013-03-29 20:35:26 -0400311 server = self.shell.nova.servers.create(
312 name=name,
313 key_name = key_name,
Tony Macke7e304f2013-04-30 18:18:03 -0400314 flavor=flavor.id,
Tony Mack7823b892013-03-29 20:35:26 -0400315 image=image_id,
316 security_group = security_group,
317 files=files,
Tony Mack79b28ca2013-04-15 21:47:45 -0400318 scheduler_hints=hints,
319 availability_zone=availability_zone)
Tony Mack7823b892013-03-29 20:35:26 -0400320 return server
Tony Mack01942f52013-03-26 14:26:40 -0400321
Tony Mackcdec0902013-04-15 00:38:49 -0400322 def destroy_instance(self, id):
323 servers = self.shell.nova.servers.findall(id=id)
Tony Mack01942f52013-03-26 14:26:40 -0400324 for server in servers:
Tony Mackcdec0902013-04-15 00:38:49 -0400325 self.shell.nova.servers.delete(server)
smbaker6ada7bf2013-04-30 18:15:48 -0700326
327 def update_instance_metadata(self, id, metadata):
328 servers = self.shell.nova.servers.findall(id=id)
329 for server in servers:
330 self.shell.nova.servers.set_meta(server, metadata)
331 # note: set_meta() returns a broken Server() object. Don't try to
332 # print it in the shell or it will fail in __repr__.
333
334 def delete_instance_metadata(self, id, metadata):
335 # note: metadata is a dict. Only the keys matter, not the values.
336 servers = self.shell.nova.servers.findall(id=id)
337 for server in servers:
338 self.shell.nova.servers.delete_meta(server, metadata)
339