blob: 02d49f9d0ad43f5c607b7ed631f2f11eadb5813f [file] [log] [blame]
Tony Mack9b5dd752013-05-09 20:33:21 -04001import commands
Tony Mack0ae3fe32013-10-08 08:47:53 -04002import hashlib
Scott Baker86e132c2015-02-11 21:38:09 -08003from xos.config import Config
Tony Mack06c8e472014-11-30 15:53:08 -05004from core.models import Controller
Tony Mack387a73f2013-09-18 07:59:14 -04005
6try:
7 from openstack.client import OpenStackClient
Tony Mack387a73f2013-09-18 07:59:14 -04008 has_openstack = True
9except:
10 has_openstack = False
11
12manager_enabled = Config().api_nova_enabled
Tony Mack01942f52013-03-26 14:26:40 -040013
Tony Macka033ced2013-03-29 17:57:06 -040014class OpenStackDriver:
Tony Mack01942f52013-03-26 14:26:40 -040015
Tony Mack94466922014-06-19 20:09:40 -040016 def __init__(self, config = None, client=None):
Tony Mack01942f52013-03-26 14:26:40 -040017 if config:
18 self.config = Config(config)
19 else:
Tony Macke4be32f2014-03-11 20:45:25 -040020 self.config = Config()
Tony Mackad8b0932013-04-07 19:32:07 -040021
22 if client:
23 self.shell = client
Tony Mack01942f52013-03-26 14:26:40 -040024
Tony Mackc12d5ca2013-09-24 10:30:39 -040025 self.enabled = manager_enabled
26 self.has_openstack = has_openstack
Tony Mack06c8e472014-11-30 15:53:08 -050027 self.controller = None
Tony Macka16cfb22014-06-19 22:00:22 -040028 self.admin_user = None
Tony Mackc12d5ca2013-09-24 10:30:39 -040029
Tony Mack06c8e472014-11-30 15:53:08 -050030 def client_driver(self, caller=None, tenant=None, controller=None):
Tony Mack387a73f2013-09-18 07:59:14 -040031 if caller:
32 auth = {'username': caller.email,
33 'password': hashlib.md5(caller.password).hexdigest()[:6],
34 'tenant': tenant}
Tony Mack1fbfccc2015-01-08 21:10:55 -050035 client = OpenStackClient(controller=controller, cacert=self.config.nova_ca_ssl_cert, **auth)
Tony Mack387a73f2013-09-18 07:59:14 -040036 else:
Tony Mack06c8e472014-11-30 15:53:08 -050037 admin_driver = self.admin_driver(tenant=tenant, controller=controller)
38 client = OpenStackClient(tenant=tenant, controller=admin_driver.controller)
Tony Macke4be32f2014-03-11 20:45:25 -040039
Tony Mack94466922014-06-19 20:09:40 -040040 driver = OpenStackDriver(client=client)
Sapan Bhatia14356b72014-11-05 10:32:41 -050041 #driver.admin_user = admin_driver.admin_user
Tony Mack06c8e472014-11-30 15:53:08 -050042 #driver.controller = admin_driver.controller
Tony Mack387a73f2013-09-18 07:59:14 -040043 return driver
44
Tony Mack06c8e472014-11-30 15:53:08 -050045 def admin_driver(self, tenant=None, controller=None):
Tony Mackd96353e2015-01-06 18:27:48 -050046 if isinstance(controller, int):
47 controller = Controller.objects.get(id=controller.id)
Tony Mack1fbfccc2015-01-08 21:10:55 -050048 client = OpenStackClient(tenant=tenant, controller=controller, cacert=self.config.nova_ca_ssl_cert)
Tony Mack94466922014-06-19 20:09:40 -040049 driver = OpenStackDriver(client=client)
Tony Mack06c8e472014-11-30 15:53:08 -050050 driver.admin_user = client.keystone.users.find(name=controller.admin_user)
51 driver.controller = controller
Tony Macke4be32f2014-03-11 20:45:25 -040052 return driver
Tony Mack387a73f2013-09-18 07:59:14 -040053
Scott Baker8a6d5122013-08-07 18:57:15 -070054 def create_role(self, name):
Tony Mackb5f339f2013-04-08 16:29:38 -040055 roles = self.shell.keystone.roles.findall(name=name)
Tony Mack0f26b272014-04-15 12:17:15 -040056 roles_title = self.shell.keystone.roles.findall(name=name.title())
57 roles_found = roles + roles_title
58 if not roles_found:
Tony Mackb5f339f2013-04-08 16:29:38 -040059 role = self.shell.keystone.roles.create(name)
60 else:
Tony Mack0f26b272014-04-15 12:17:15 -040061 role = roles_found[0]
Tony Mackb5f339f2013-04-08 16:29:38 -040062 return role
63
Tony Macka7040532013-04-08 21:45:43 -040064 def delete_role(self, filter):
65 roles = self.shell.keystone.roles.findall(**filter)
Tony Mackb5f339f2013-04-08 16:29:38 -040066 for role in roles:
67 self.shell.keystone.roles.delete(role)
68 return 1
69
Tony Mack1fbdeca2013-04-06 00:10:36 -040070 def create_tenant(self, tenant_name, enabled, description):
Tony Mack5ee21902013-03-29 20:36:42 -040071 """Create keystone tenant. Suggested fields: name, description, enabled"""
Tony Mack1fbdeca2013-04-06 00:10:36 -040072 tenants = self.shell.keystone.tenants.findall(name=tenant_name)
Tony Mack7823b892013-03-29 20:35:26 -040073 if not tenants:
Tony Mack1fbdeca2013-04-06 00:10:36 -040074 fields = {'tenant_name': tenant_name, 'enabled': enabled,
Tony Mack488950a2013-04-07 20:35:50 -040075 'description': description}
Tony Mack1fbdeca2013-04-06 00:10:36 -040076 tenant = self.shell.keystone.tenants.create(**fields)
Tony Mack7823b892013-03-29 20:35:26 -040077 else:
78 tenant = tenants[0]
Tony Mack0dc2f562013-04-29 19:41:39 -040079
80 # always give the admin user the admin role to any tenant created
81 # by the driver.
82 self.add_user_role(self.admin_user.id, tenant.id, 'admin')
Tony Mack7823b892013-03-29 20:35:26 -040083 return tenant
84
85 def update_tenant(self, id, **kwds):
Tony Mack1fbdeca2013-04-06 00:10:36 -040086 return self.shell.keystone.tenants.update(id, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -040087
88 def delete_tenant(self, id):
Tony Mackac3e0142013-06-26 11:28:57 -040089 ctx = self.shell.nova_db.ctx
Tony Mackfd24d0d2013-04-14 00:59:17 -040090 tenants = self.shell.keystone.tenants.findall(id=id)
91 for tenant in tenants:
Tony Mackac3e0142013-06-26 11:28:57 -040092 # nova does not automatically delete the tenant's instances
93 # so we manually delete instances before deleteing the tenant
Scott Baker8a6d5122013-08-07 18:57:15 -070094 instances = self.shell.nova_db.instance_get_all_by_filters(ctx,
Tony Mackac3e0142013-06-26 11:28:57 -040095 {'project_id': tenant.id}, 'id', 'asc')
Tony Mack28897e12013-07-09 07:25:38 -040096 client = OpenStackClient(tenant=tenant.name)
Tony Mackac3e0142013-06-26 11:28:57 -040097 driver = OpenStackDriver(client=client)
98 for instance in instances:
99 driver.destroy_instance(instance.id)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400100 self.shell.keystone.tenants.delete(tenant)
101 return 1
Tony Mack0211f1b2013-04-04 23:56:24 -0400102
Tony Mack1fbdeca2013-04-06 00:10:36 -0400103 def create_user(self, name, email, password, enabled):
104 users = self.shell.keystone.users.findall(email=email)
105 if not users:
106 fields = {'name': name, 'email': email, 'password': password,
107 'enabled': enabled}
Tony Mackfdcbf832013-04-09 11:47:11 -0400108 user = self.shell.keystone.users.create(**fields)
Tony Mack1fbdeca2013-04-06 00:10:36 -0400109 else:
110 user = users[0]
111 return user
112
Tony Mackfd24d0d2013-04-14 00:59:17 -0400113 def delete_user(self, id):
114 users = self.shell.keystone.users.findall(id=id)
115 for user in users:
Tony Mackc59fcaf2013-06-10 11:14:04 -0400116 # delete users keys
117 keys = self.shell.nova.keypairs.findall()
118 for key in keys:
119 self.shell.nova.keypairs.delete(key)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400120 self.shell.keystone.users.delete(user)
Tony Mackac2ebaa2014-04-10 12:26:18 -0400121 return 1
122
123 def get_admin_role(self):
124 role = None
125 for admin_role_name in ['admin', 'Admin']:
126 roles = self.shell.keystone.roles.findall(name=admin_role_name)
127 if roles:
128 role = roles[0]
129 break
130 return role
Tony Mackfd24d0d2013-04-14 00:59:17 -0400131
Siobhan Tully30fd4292013-05-10 08:59:56 -0400132 def add_user_role(self, kuser_id, tenant_id, role_name):
133 user = self.shell.keystone.users.find(id=kuser_id)
Tony Mack1fbdeca2013-04-06 00:10:36 -0400134 tenant = self.shell.keystone.tenants.find(id=tenant_id)
Tony Macka4f1feb2014-03-28 20:09:21 -0400135 # admin role can be lowercase or title. Look for both
136 role = None
137 if role_name.lower() == 'admin':
Tony Mackac2ebaa2014-04-10 12:26:18 -0400138 role = self.get_admin_role()
139 else:
Tony Macka4f1feb2014-03-28 20:09:21 -0400140 # look up non admin role or force exception when admin role isnt found
141 role = self.shell.keystone.roles.find(name=role_name)
Tony Mack54304c62013-04-28 11:27:07 -0400142
143 role_found = False
144 user_roles = user.list_roles(tenant.id)
145 for user_role in user_roles:
146 if user_role.name == role.name:
147 role_found = True
148 if not role_found:
149 tenant.add_user(user, role)
150
151 return 1
Tony Mack1fbdeca2013-04-06 00:10:36 -0400152
Siobhan Tully30fd4292013-05-10 08:59:56 -0400153 def delete_user_role(self, kuser_id, tenant_id, role_name):
154 user = self.shell.keystone.users.find(id=kuser_id)
Tony Mack1fbdeca2013-04-06 00:10:36 -0400155 tenant = self.shell.keystone.tenants.find(id=tenant_id)
Tony Mackac2ebaa2014-04-10 12:26:18 -0400156 # admin role can be lowercase or title. Look for both
157 role = None
158 if role_name.lower() == 'admin':
159 role = self.get_admin_role()
160 else:
161 # look up non admin role or force exception when admin role isnt found
162 role = self.shell.keystone.roles.find(name=role_name)
Tony Mack54304c62013-04-28 11:27:07 -0400163
164 role_found = False
165 user_roles = user.list_roles(tenant.id)
166 for user_role in user_roles:
167 if user_role.name == role.name:
168 role_found = True
169 if role_found:
170 tenant.remove_user(user, role)
171
172 return 1
Tony Mack1fbdeca2013-04-06 00:10:36 -0400173
Tony Mack6795fbe2013-06-11 10:10:24 -0400174 def update_user(self, id, fields):
175 if 'password' in fields:
176 self.shell.keystone.users.update_password(id, fields['password'])
177 if 'enabled' in fields:
178 self.shell.keystone.users.update_enabled(id, fields['enabled'])
179 return 1
Tony Mack1fbdeca2013-04-06 00:10:36 -0400180
Tony Mack240903f2013-04-06 01:31:37 -0400181 def create_router(self, name, set_gateway=True):
Tony Mack28be7562013-04-10 08:45:55 -0400182 routers = self.shell.quantum.list_routers(name=name)['routers']
183 if routers:
184 router = routers[0]
185 else:
Tony Mack62bc59a2013-04-14 23:27:12 -0400186 router = self.shell.quantum.create_router({'router': {'name': name}})['router']
Tony Mackc1665b12013-04-29 16:45:14 -0400187 # add router to external network
Tony Mack240903f2013-04-06 01:31:37 -0400188 if set_gateway:
Tony Mack257bedc2013-04-10 08:58:09 -0400189 nets = self.shell.quantum.list_networks()['networks']
Tony Mack240903f2013-04-06 01:31:37 -0400190 for net in nets:
191 if net['router:external'] == True:
Tony Mack9dcc0502013-04-10 09:07:45 -0400192 self.shell.quantum.add_gateway_router(router['id'],
193 {'network_id': net['id']})
Tony Mack240903f2013-04-06 01:31:37 -0400194
Tony Mack0211f1b2013-04-04 23:56:24 -0400195 return router
196
Tony Mack62bc59a2013-04-14 23:27:12 -0400197 def delete_router(self, id):
198 routers = self.shell.quantum.list_routers(id=id)['routers']
Tony Mack28be7562013-04-10 08:45:55 -0400199 for router in routers:
200 self.shell.quantum.delete_router(router['id'])
Tony Mackc1665b12013-04-29 16:45:14 -0400201 # remove router form external network
Tony Mack0c0e05d2013-04-29 17:52:37 -0400202 #nets = self.shell.quantum.list_networks()['networks']
203 #for net in nets:
204 # if net['router:external'] == True:
205 # self.shell.quantum.remove_gateway_router(router['id'])
Tony Mack240903f2013-04-06 01:31:37 -0400206
207 def add_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400208 router = self.shell.quantum.show_router(router_id)['router']
209 subnet = self.shell.quantum.show_subnet(subnet_id)['subnet']
Tony Mack240903f2013-04-06 01:31:37 -0400210 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400211 self.shell.quantum.add_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400212
213 def delete_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400214 router = self.shell.quantum.show_router(router_id)
215 subnet = self.shell.quantum.show_subnet(subnet_id)
Tony Mack240903f2013-04-06 01:31:37 -0400216 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400217 self.shell.quantum.remove_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400218
Scott Baker1ac5ddb2013-08-14 10:50:48 -0700219 def create_network(self, name, shared=False):
Tony Mack28be7562013-04-10 08:45:55 -0400220 nets = self.shell.quantum.list_networks(name=name)['networks']
221 if nets:
Tony Mack53c63342013-04-10 08:11:45 -0400222 net = nets[0]
Tony Mack82a688d2013-04-10 08:12:58 -0400223 else:
Scott Baker1ac5ddb2013-08-14 10:50:48 -0700224 net = self.shell.quantum.create_network({'network': {'name': name, 'shared': shared}})['network']
Tony Mack53c63342013-04-10 08:11:45 -0400225 return net
226
Tony Mack62bc59a2013-04-14 23:27:12 -0400227 def delete_network(self, id):
228 nets = self.shell.quantum.list_networks()['networks']
Tony Mack0211f1b2013-04-04 23:56:24 -0400229 for net in nets:
Tony Mack62bc59a2013-04-14 23:27:12 -0400230 if net['id'] == id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400231 # delete_all ports
232 self.delete_network_ports(net['id'])
Tony Mack62bc59a2013-04-14 23:27:12 -0400233 # delete all subnets:
234 for subnet_id in net['subnets']:
235 self.delete_subnet(subnet_id)
236 self.shell.quantum.delete_network(net['id'])
Tony Mackfd24d0d2013-04-14 00:59:17 -0400237 return 1
Tony Mack94c6ea02013-04-16 21:41:50 -0400238
239 def delete_network_ports(self, network_id):
240 ports = self.shell.quantum.list_ports()['ports']
241 for port in ports:
Tony Mackc1665b12013-04-29 16:45:14 -0400242 if port['network_id'] == network_id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400243 self.shell.quantum.delete_port(port['id'])
244 return 1
245
246 def delete_subnet_ports(self, subnet_id):
247 ports = self.shell.quantum.list_ports()['ports']
248 for port in ports:
249 delete = False
250 for fixed_ip in port['fixed_ips']:
251 if fixed_ip['subnet_id'] == subnet_id:
252 delete=True
253 break
254 if delete:
255 self.shell.quantum.delete_port(port['id'])
256 return 1
257
Tony Mack48952032013-04-12 11:49:34 -0400258 def create_subnet(self, name, network_id, cidr_ip, ip_version, start, end):
259 #nets = self.shell.quantum.list_networks(name=network_name)['networks']
260 #if not nets:
261 # raise Exception, "No such network: %s" % network_name
262 #net = nets[0]
Tony Mack0211f1b2013-04-04 23:56:24 -0400263
Tony Mack48952032013-04-12 11:49:34 -0400264 subnet = None
265 subnets = self.shell.quantum.list_subnets()['subnets']
266 for snet in subnets:
267 if snet['cidr'] == cidr_ip and snet['network_id'] == network_id:
268 subnet = snet
Scott Baker8a6d5122013-08-07 18:57:15 -0700269
Tony Mack48952032013-04-12 11:49:34 -0400270 if not subnet:
Andy Bavierd2b0c6d2014-04-25 11:44:15 -0400271 # HACK: Add metadata route -- Neutron does not reliably supply this
272 metadata_ip = cidr_ip.replace("0/24", "3")
273
Tony Mack48952032013-04-12 11:49:34 -0400274 allocation_pools = [{'start': start, 'end': end}]
275 subnet = {'subnet': {'name': name,
276 'network_id': network_id,
277 'ip_version': ip_version,
278 'cidr': cidr_ip,
Tony Mackf549eb42014-04-18 15:33:24 -0400279 #'dns_nameservers': ['8.8.8.8', '8.8.4.4'],
Tony Mack8b047732014-04-25 12:42:21 -0400280 'host_routes': [{'destination':'169.254.169.254/32','nexthop':metadata_ip}],
Andy Bavierd2b0c6d2014-04-25 11:44:15 -0400281 'gateway_ip': None,
Scott Baker8a6d5122013-08-07 18:57:15 -0700282 'allocation_pools': allocation_pools}}
Tony Mack48952032013-04-12 11:49:34 -0400283 subnet = self.shell.quantum.create_subnet(subnet)['subnet']
Andy Bavierd2b0c6d2014-04-25 11:44:15 -0400284 # self.add_external_route(subnet)
285
Tony Mack0211f1b2013-04-04 23:56:24 -0400286 return subnet
287
Tony Mack48952032013-04-12 11:49:34 -0400288 def update_subnet(self, id, fields):
289 return self.shell.quantum.update_subnet(id, fields)
290
Tony Mack0211f1b2013-04-04 23:56:24 -0400291 def delete_subnet(self, id):
Tony Mackfd24d0d2013-04-14 00:59:17 -0400292 #return self.shell.quantum.delete_subnet(id=id)
293 # inefficient but fault tolerant
294 subnets = self.shell.quantum.list_subnets()['subnets']
295 for subnet in subnets:
296 if subnet['id'] == id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400297 self.delete_subnet_ports(subnet['id'])
Tony Mack31d34132013-04-15 00:02:06 -0400298 self.shell.quantum.delete_subnet(id)
Tony Mack9b5dd752013-05-09 20:33:21 -0400299 self.delete_external_route(subnet)
300 return 1
301
Tony Mack70780e92013-06-30 12:46:16 -0400302 def get_external_routes(self):
303 status, output = commands.getstatusoutput('route')
304 routes = output.split('\n')[3:]
305 return routes
306
307 def add_external_route(self, subnet, routes=[]):
308 if not routes:
309 routes = self.get_external_routes()
310
Tony Mack9b5dd752013-05-09 20:33:21 -0400311 ports = self.shell.quantum.list_ports()['ports']
312
313 gw_ip = subnet['gateway_ip']
314 subnet_id = subnet['id']
315
316 # 1. Find the port associated with the subnet's gateway
317 # 2. Find the router associated with that port
318 # 3. Find the port associated with this router and on the external net
319 # 4. Set up route to the subnet through the port from step 3
320 ip_address = None
321 for port in ports:
322 for fixed_ip in port['fixed_ips']:
323 if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip:
324 gw_port = port
325 router_id = gw_port['device_id']
326 router = self.shell.quantum.show_router(router_id)['router']
Tony Mack44187012013-07-09 07:15:17 -0400327 if router and router.get('external_gateway_info'):
Tony Mack16f51e02013-07-01 10:33:29 -0400328 ext_net = router['external_gateway_info']['network_id']
329 for port in ports:
330 if port['device_id'] == router_id and port['network_id'] == ext_net:
331 ip_address = port['fixed_ips'][0]['ip_address']
Tony Mack9b5dd752013-05-09 20:33:21 -0400332
333 if ip_address:
Tony Mack70780e92013-06-30 12:46:16 -0400334 # check if external route already exists
335 route_exists = False
336 if routes:
337 for route in routes:
338 if subnet['cidr'] in route and ip_address in route:
339 route_exists = True
340 if not route_exists:
341 cmd = "route add -net %s dev br-ex gw %s" % (subnet['cidr'], ip_address)
Tony Mack16f51e02013-07-01 10:33:29 -0400342 s, o = commands.getstatusoutput(cmd)
343 #print cmd, "\n", s, o
Tony Mack9b5dd752013-05-09 20:33:21 -0400344
345 return 1
346
347 def delete_external_route(self, subnet):
348 ports = self.shell.quantum.list_ports()['ports']
349
350 gw_ip = subnet['gateway_ip']
351 subnet_id = subnet['id']
352
353 # 1. Find the port associated with the subnet's gateway
354 # 2. Find the router associated with that port
355 # 3. Find the port associated with this router and on the external net
356 # 4. Set up route to the subnet through the port from step 3
357 ip_address = None
358 for port in ports:
359 for fixed_ip in port['fixed_ips']:
360 if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip:
361 gw_port = port
362 router_id = gw_port['device_id']
363 router = self.shell.quantum.show_router(router_id)['router']
364 ext_net = router['external_gateway_info']['network_id']
365 for port in ports:
366 if port['device_id'] == router_id and port['network_id'] == ext_net:
367 ip_address = port['fixed_ips'][0]['ip_address']
368
369 if ip_address:
370 cmd = "route delete -net %s" % (subnet['cidr'])
371 commands.getstatusoutput(cmd)
372
373 return 1
Tony Mack0211f1b2013-04-04 23:56:24 -0400374
Tony Mackb0d97422013-06-10 09:57:45 -0400375 def create_keypair(self, name, public_key):
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400376 keys = self.shell.nova.keypairs.findall(name=name)
Tony Mack0211f1b2013-04-04 23:56:24 -0400377 if keys:
Tony Mack52cb9302013-04-09 12:25:35 -0400378 key = keys[0]
Tony Mackb0d97422013-06-10 09:57:45 -0400379 # update key
380 if key.public_key != public_key:
381 self.delete_keypair(key.id)
382 key = self.shell.nova.keypairs.create(name=name, public_key=public_key)
Tony Mack52cb9302013-04-09 12:25:35 -0400383 else:
Tony Mackb0d97422013-06-10 09:57:45 -0400384 key = self.shell.nova.keypairs.create(name=name, public_key=public_key)
Tony Mack52cb9302013-04-09 12:25:35 -0400385 return key
Tony Mack0211f1b2013-04-04 23:56:24 -0400386
Tony Mack759b57a2013-04-14 21:03:31 -0400387 def delete_keypair(self, id):
388 keys = self.shell.nova.keypairs.findall(id=id)
Tony Mack0211f1b2013-04-04 23:56:24 -0400389 for key in keys:
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400390 self.shell.nova.keypairs.delete(key)
Scott Baker8a6d5122013-08-07 18:57:15 -0700391 return 1
Tony Mack7823b892013-03-29 20:35:26 -0400392
Scott Baker8a6d5122013-08-07 18:57:15 -0700393 def get_private_networks(self, tenant=None):
394 if not tenant:
395 tenant = self.shell.nova.tenant
396 tenant = self.shell.keystone.tenants.find(name=tenant)
397 search_opts = {"tenant_id": tenant.id, "shared": False}
398 private_networks = self.shell.quantum.list_networks(**search_opts)
399 return private_networks
400
401 def get_shared_networks(self):
402 search_opts = {"shared": True}
403 shared_networks = self.shell.quantum.list_networks(**search_opts)
404 return shared_networks
405
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700406 def get_network_subnet(self, network_id):
407 subnet_id = None
408 subnet = None
409 if network_id:
410 os_networks = self.shell.quantum.list_networks(id=network_id)["networks"]
411 if os_networks:
412 os_network = os_networks[0]
413 if os_network['subnets']:
414 subnet_id = os_network['subnets'][0]
415 os_subnets = self.shell.quantum.list_subnets(id=subnet_id)['subnets']
416 if os_subnets:
417 subnet = os_subnets[0]['cidr']
418
419 return (subnet_id, subnet)
420
Tony Mack6eb99b52014-09-29 16:21:57 -0400421 def spawn_instance(self, name, key_name=None, availability_zone=None, hostname=None, image_id=None, security_group=None, pubkeys=[], nics=None, metadata=None, userdata=None, flavor_name=None):
Scott Baker074e2ff2014-09-04 16:55:08 -0700422 if not flavor_name:
423 flavor_name = self.config.nova_default_flavor
424
Tony Macke7e304f2013-04-30 18:18:03 -0400425 flavor = self.shell.nova.flavors.find(name=flavor_name)
Scott Baker074e2ff2014-09-04 16:55:08 -0700426
Tony Mack01942f52013-03-26 14:26:40 -0400427 if not security_group:
Scott Baker3a0e66b2013-09-12 11:21:34 -0700428 security_group = self.config.nova_default_security_group
Tony Mack01942f52013-03-26 14:26:40 -0400429
Tony Mack8538b442014-04-16 07:49:41 -0400430 files = {}
Tony Mack38c158b2014-04-16 20:15:55 -0400431 #if pubkeys:
432 # files["/root/.ssh/authorized_keys"] = "\n".join(pubkeys).encode('base64')
Tony Mack7823b892013-03-29 20:35:26 -0400433 hints = {}
Tony Mack6eb99b52014-09-29 16:21:57 -0400434
435 # determine availability zone and compute host
436 availability_zone_filter = None
Tony Mackf436f732014-11-14 00:56:04 -0500437 if availability_zone is None or not availability_zone:
Tony Mack6eb99b52014-09-29 16:21:57 -0400438 availability_zone_filter = 'nova'
439 else:
440 availability_zone_filter = availability_zone
Tony Mack7823b892013-03-29 20:35:26 -0400441 if hostname:
Tony Mackf436f732014-11-14 00:56:04 -0500442 availability_zone_filter += ':%s' % hostname
Tony Mack6eb99b52014-09-29 16:21:57 -0400443
Tony Mack7823b892013-03-29 20:35:26 -0400444 server = self.shell.nova.servers.create(
445 name=name,
446 key_name = key_name,
Tony Macke7e304f2013-04-30 18:18:03 -0400447 flavor=flavor.id,
Tony Mack7823b892013-03-29 20:35:26 -0400448 image=image_id,
449 security_group = security_group,
Tony Mack38c158b2014-04-16 20:15:55 -0400450 #files = files,
Tony Mack79b28ca2013-04-15 21:47:45 -0400451 scheduler_hints=hints,
Tony Mack6eb99b52014-09-29 16:21:57 -0400452 availability_zone=availability_zone_filter,
Scott Baker3a0e66b2013-09-12 11:21:34 -0700453 nics=nics,
Tony Mack67198112014-04-09 01:46:03 -0400454 networks=nics,
Scott Baker9c7a1e12014-05-30 14:42:42 -0700455 meta=metadata,
456 userdata=userdata)
Tony Mack7823b892013-03-29 20:35:26 -0400457 return server
Scott Baker6a50b022013-08-26 08:25:37 -0700458
Tony Mackcdec0902013-04-15 00:38:49 -0400459 def destroy_instance(self, id):
Scott Baker6a50b022013-08-26 08:25:37 -0700460 if (self.shell.nova.tenant=="admin"):
461 # findall() is implemented as a list() followed by a python search of the
462 # list. Since findall() doesn't accept "all_tenants", we do this using
463 # list() ourselves. This allows us to delete an instance as admin.
464 servers = self.shell.nova.servers.list(search_opts={"all_tenants": True})
465 else:
466 servers = self.shell.nova.servers.list()
Tony Mack01942f52013-03-26 14:26:40 -0400467 for server in servers:
Scott Baker6a50b022013-08-26 08:25:37 -0700468 if server.id == id:
469 result=self.shell.nova.servers.delete(server)
smbaker6ada7bf2013-04-30 18:15:48 -0700470
471 def update_instance_metadata(self, id, metadata):
472 servers = self.shell.nova.servers.findall(id=id)
473 for server in servers:
474 self.shell.nova.servers.set_meta(server, metadata)
475 # note: set_meta() returns a broken Server() object. Don't try to
476 # print it in the shell or it will fail in __repr__.
477
478 def delete_instance_metadata(self, id, metadata):
479 # note: metadata is a dict. Only the keys matter, not the values.
480 servers = self.shell.nova.servers.findall(id=id)
481 for server in servers:
482 self.shell.nova.servers.delete_meta(server, metadata)
483