blob: d467dc197869cb4bac1eb777b3eb0779fc865364 [file] [log] [blame]
Tony Mack9b5dd752013-05-09 20:33:21 -04001import commands
Tony Mack0ae3fe32013-10-08 08:47:53 -04002import hashlib
Siobhan Tully30fd4292013-05-10 08:59:56 -04003from planetstack.config import Config
Tony Mack387a73f2013-09-18 07:59:14 -04004
5try:
6 from openstack.client import OpenStackClient
Tony Mack387a73f2013-09-18 07:59:14 -04007 has_openstack = True
8except:
9 has_openstack = False
10
11manager_enabled = Config().api_nova_enabled
Tony Mack01942f52013-03-26 14:26:40 -040012
Tony Macka033ced2013-03-29 17:57:06 -040013class OpenStackDriver:
Tony Mack01942f52013-03-26 14:26:40 -040014
Tony Macke4be32f2014-03-11 20:45:25 -040015 def __init__(self, config = None, client=None, deployment=None):
Tony Mack01942f52013-03-26 14:26:40 -040016 if config:
17 self.config = Config(config)
18 else:
Tony Macke4be32f2014-03-11 20:45:25 -040019 self.config = Config()
Tony Mackad8b0932013-04-07 19:32:07 -040020
Tony Macke4be32f2014-03-11 20:45:25 -040021 self.admin_client = OpenStackClient(deployment=deployment)
Tony Mack89f70f12013-05-10 20:20:03 -040022 self.admin_user = self.admin_client.keystone.users.find(name=self.admin_client.keystone.username)
Tony Mack0dc2f562013-04-29 19:41:39 -040023
Tony Mackad8b0932013-04-07 19:32:07 -040024 if client:
25 self.shell = client
26 else:
Tony Macke4be32f2014-03-11 20:45:25 -040027 self.shell = OpenStackClient(deployment=deployment)
Tony Mack01942f52013-03-26 14:26:40 -040028
Tony Mackc12d5ca2013-09-24 10:30:39 -040029 self.enabled = manager_enabled
30 self.has_openstack = has_openstack
31
Tony Macke4be32f2014-03-11 20:45:25 -040032 def client_driver(self, caller=None, tenant=None, deployment=None):
Tony Mack387a73f2013-09-18 07:59:14 -040033 if caller:
34 auth = {'username': caller.email,
35 'password': hashlib.md5(caller.password).hexdigest()[:6],
36 'tenant': tenant}
Tony Macke4be32f2014-03-11 20:45:25 -040037 client = OpenStackClient(deployment=deployment, **auth)
Tony Mack387a73f2013-09-18 07:59:14 -040038 else:
Tony Macke4be32f2014-03-11 20:45:25 -040039 client = OpenStackClient(tenant=tenant, deployment=deployment)
40
41 driver = OpenStackDriver(client=client, deployment=deployment)
Tony Mack387a73f2013-09-18 07:59:14 -040042 return driver
43
Tony Macke4be32f2014-03-11 20:45:25 -040044 def admin_driver(self, tenant=None, deployment=None):
45 client = OpenStackClient(tenant=tenant, deployment=deployment)
46 driver = OpenStackDriver(client=client, deployment=deployment)
47 return driver
Tony Mack387a73f2013-09-18 07:59:14 -040048
Scott Baker8a6d5122013-08-07 18:57:15 -070049 def create_role(self, name):
Tony Mackb5f339f2013-04-08 16:29:38 -040050 roles = self.shell.keystone.roles.findall(name=name)
51 if not roles:
52 role = self.shell.keystone.roles.create(name)
53 else:
Scott Baker8a6d5122013-08-07 18:57:15 -070054 role = roles[0]
Tony Mackb5f339f2013-04-08 16:29:38 -040055 return role
56
Tony Macka7040532013-04-08 21:45:43 -040057 def delete_role(self, filter):
58 roles = self.shell.keystone.roles.findall(**filter)
Tony Mackb5f339f2013-04-08 16:29:38 -040059 for role in roles:
60 self.shell.keystone.roles.delete(role)
61 return 1
62
Tony Mack1fbdeca2013-04-06 00:10:36 -040063 def create_tenant(self, tenant_name, enabled, description):
Tony Mack5ee21902013-03-29 20:36:42 -040064 """Create keystone tenant. Suggested fields: name, description, enabled"""
Tony Mack1fbdeca2013-04-06 00:10:36 -040065 tenants = self.shell.keystone.tenants.findall(name=tenant_name)
Tony Mack7823b892013-03-29 20:35:26 -040066 if not tenants:
Tony Mack1fbdeca2013-04-06 00:10:36 -040067 fields = {'tenant_name': tenant_name, 'enabled': enabled,
Tony Mack488950a2013-04-07 20:35:50 -040068 'description': description}
Tony Mack1fbdeca2013-04-06 00:10:36 -040069 tenant = self.shell.keystone.tenants.create(**fields)
Tony Mack7823b892013-03-29 20:35:26 -040070 else:
71 tenant = tenants[0]
Tony Mack0dc2f562013-04-29 19:41:39 -040072
73 # always give the admin user the admin role to any tenant created
74 # by the driver.
75 self.add_user_role(self.admin_user.id, tenant.id, 'admin')
Tony Mack7823b892013-03-29 20:35:26 -040076 return tenant
77
78 def update_tenant(self, id, **kwds):
Tony Mack1fbdeca2013-04-06 00:10:36 -040079 return self.shell.keystone.tenants.update(id, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -040080
81 def delete_tenant(self, id):
Tony Mackac3e0142013-06-26 11:28:57 -040082 ctx = self.shell.nova_db.ctx
Tony Mackfd24d0d2013-04-14 00:59:17 -040083 tenants = self.shell.keystone.tenants.findall(id=id)
84 for tenant in tenants:
Tony Mackac3e0142013-06-26 11:28:57 -040085 # nova does not automatically delete the tenant's instances
86 # so we manually delete instances before deleteing the tenant
Scott Baker8a6d5122013-08-07 18:57:15 -070087 instances = self.shell.nova_db.instance_get_all_by_filters(ctx,
Tony Mackac3e0142013-06-26 11:28:57 -040088 {'project_id': tenant.id}, 'id', 'asc')
Tony Mack28897e12013-07-09 07:25:38 -040089 client = OpenStackClient(tenant=tenant.name)
Tony Mackac3e0142013-06-26 11:28:57 -040090 driver = OpenStackDriver(client=client)
91 for instance in instances:
92 driver.destroy_instance(instance.id)
Tony Mackfd24d0d2013-04-14 00:59:17 -040093 self.shell.keystone.tenants.delete(tenant)
94 return 1
Tony Mack0211f1b2013-04-04 23:56:24 -040095
Tony Mack1fbdeca2013-04-06 00:10:36 -040096 def create_user(self, name, email, password, enabled):
97 users = self.shell.keystone.users.findall(email=email)
98 if not users:
99 fields = {'name': name, 'email': email, 'password': password,
100 'enabled': enabled}
Tony Mackfdcbf832013-04-09 11:47:11 -0400101 user = self.shell.keystone.users.create(**fields)
Tony Mack1fbdeca2013-04-06 00:10:36 -0400102 else:
103 user = users[0]
104 return user
105
Tony Mackfd24d0d2013-04-14 00:59:17 -0400106 def delete_user(self, id):
107 users = self.shell.keystone.users.findall(id=id)
108 for user in users:
Tony Mackc59fcaf2013-06-10 11:14:04 -0400109 # delete users keys
110 keys = self.shell.nova.keypairs.findall()
111 for key in keys:
112 self.shell.nova.keypairs.delete(key)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400113 self.shell.keystone.users.delete(user)
114 return 1
115
Siobhan Tully30fd4292013-05-10 08:59:56 -0400116 def add_user_role(self, kuser_id, tenant_id, role_name):
117 user = self.shell.keystone.users.find(id=kuser_id)
Tony Mack1fbdeca2013-04-06 00:10:36 -0400118 tenant = self.shell.keystone.tenants.find(id=tenant_id)
Tony Macka4f1feb2014-03-28 20:09:21 -0400119 # admin role can be lowercase or title. Look for both
120 role = None
121 if role_name.lower() == 'admin':
122 for admin_role_name in ['admin', 'Admin']:
123 roles = self.shell.keystone.roles.findall(name=admin_role_name)
124 if roles:
125 role = roles[0]
126 break
127
128 if not role:
129 # look up non admin role or force exception when admin role isnt found
130 role = self.shell.keystone.roles.find(name=role_name)
Tony Mack54304c62013-04-28 11:27:07 -0400131
132 role_found = False
133 user_roles = user.list_roles(tenant.id)
134 for user_role in user_roles:
135 if user_role.name == role.name:
136 role_found = True
137 if not role_found:
138 tenant.add_user(user, role)
139
140 return 1
Tony Mack1fbdeca2013-04-06 00:10:36 -0400141
Siobhan Tully30fd4292013-05-10 08:59:56 -0400142 def delete_user_role(self, kuser_id, tenant_id, role_name):
143 user = self.shell.keystone.users.find(id=kuser_id)
Tony Mack1fbdeca2013-04-06 00:10:36 -0400144 tenant = self.shell.keystone.tenants.find(id=tenant_id)
Tony Mack54304c62013-04-28 11:27:07 -0400145 role = self.shell.keystone.roles.find(name=role_name)
146
147 role_found = False
148 user_roles = user.list_roles(tenant.id)
149 for user_role in user_roles:
150 if user_role.name == role.name:
151 role_found = True
152 if role_found:
153 tenant.remove_user(user, role)
154
155 return 1
Tony Mack1fbdeca2013-04-06 00:10:36 -0400156
Tony Mack6795fbe2013-06-11 10:10:24 -0400157 def update_user(self, id, fields):
158 if 'password' in fields:
159 self.shell.keystone.users.update_password(id, fields['password'])
160 if 'enabled' in fields:
161 self.shell.keystone.users.update_enabled(id, fields['enabled'])
162 return 1
Tony Mack1fbdeca2013-04-06 00:10:36 -0400163
Tony Mack240903f2013-04-06 01:31:37 -0400164 def create_router(self, name, set_gateway=True):
Tony Mack28be7562013-04-10 08:45:55 -0400165 routers = self.shell.quantum.list_routers(name=name)['routers']
166 if routers:
167 router = routers[0]
168 else:
Tony Mack62bc59a2013-04-14 23:27:12 -0400169 router = self.shell.quantum.create_router({'router': {'name': name}})['router']
Tony Mackc1665b12013-04-29 16:45:14 -0400170 # add router to external network
Tony Mack240903f2013-04-06 01:31:37 -0400171 if set_gateway:
Tony Mack257bedc2013-04-10 08:58:09 -0400172 nets = self.shell.quantum.list_networks()['networks']
Tony Mack240903f2013-04-06 01:31:37 -0400173 for net in nets:
174 if net['router:external'] == True:
Tony Mack9dcc0502013-04-10 09:07:45 -0400175 self.shell.quantum.add_gateway_router(router['id'],
176 {'network_id': net['id']})
Tony Mack240903f2013-04-06 01:31:37 -0400177
Tony Mack0211f1b2013-04-04 23:56:24 -0400178 return router
179
Tony Mack62bc59a2013-04-14 23:27:12 -0400180 def delete_router(self, id):
181 routers = self.shell.quantum.list_routers(id=id)['routers']
Tony Mack28be7562013-04-10 08:45:55 -0400182 for router in routers:
183 self.shell.quantum.delete_router(router['id'])
Tony Mackc1665b12013-04-29 16:45:14 -0400184 # remove router form external network
Tony Mack0c0e05d2013-04-29 17:52:37 -0400185 #nets = self.shell.quantum.list_networks()['networks']
186 #for net in nets:
187 # if net['router:external'] == True:
188 # self.shell.quantum.remove_gateway_router(router['id'])
Tony Mack240903f2013-04-06 01:31:37 -0400189
190 def add_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400191 router = self.shell.quantum.show_router(router_id)['router']
192 subnet = self.shell.quantum.show_subnet(subnet_id)['subnet']
Tony Mack240903f2013-04-06 01:31:37 -0400193 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400194 self.shell.quantum.add_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400195
196 def delete_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400197 router = self.shell.quantum.show_router(router_id)
198 subnet = self.shell.quantum.show_subnet(subnet_id)
Tony Mack240903f2013-04-06 01:31:37 -0400199 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400200 self.shell.quantum.remove_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400201
Scott Baker1ac5ddb2013-08-14 10:50:48 -0700202 def create_network(self, name, shared=False):
Tony Mack28be7562013-04-10 08:45:55 -0400203 nets = self.shell.quantum.list_networks(name=name)['networks']
204 if nets:
Tony Mack53c63342013-04-10 08:11:45 -0400205 net = nets[0]
Tony Mack82a688d2013-04-10 08:12:58 -0400206 else:
Scott Baker1ac5ddb2013-08-14 10:50:48 -0700207 net = self.shell.quantum.create_network({'network': {'name': name, 'shared': shared}})['network']
Tony Mack53c63342013-04-10 08:11:45 -0400208 return net
209
Tony Mack62bc59a2013-04-14 23:27:12 -0400210 def delete_network(self, id):
211 nets = self.shell.quantum.list_networks()['networks']
Tony Mack0211f1b2013-04-04 23:56:24 -0400212 for net in nets:
Tony Mack62bc59a2013-04-14 23:27:12 -0400213 if net['id'] == id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400214 # delete_all ports
215 self.delete_network_ports(net['id'])
Tony Mack62bc59a2013-04-14 23:27:12 -0400216 # delete all subnets:
217 for subnet_id in net['subnets']:
218 self.delete_subnet(subnet_id)
219 self.shell.quantum.delete_network(net['id'])
Tony Mackfd24d0d2013-04-14 00:59:17 -0400220 return 1
Tony Mack94c6ea02013-04-16 21:41:50 -0400221
222 def delete_network_ports(self, network_id):
223 ports = self.shell.quantum.list_ports()['ports']
224 for port in ports:
Tony Mackc1665b12013-04-29 16:45:14 -0400225 if port['network_id'] == network_id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400226 self.shell.quantum.delete_port(port['id'])
227 return 1
228
229 def delete_subnet_ports(self, subnet_id):
230 ports = self.shell.quantum.list_ports()['ports']
231 for port in ports:
232 delete = False
233 for fixed_ip in port['fixed_ips']:
234 if fixed_ip['subnet_id'] == subnet_id:
235 delete=True
236 break
237 if delete:
238 self.shell.quantum.delete_port(port['id'])
239 return 1
240
Tony Mack48952032013-04-12 11:49:34 -0400241 def create_subnet(self, name, network_id, cidr_ip, ip_version, start, end):
242 #nets = self.shell.quantum.list_networks(name=network_name)['networks']
243 #if not nets:
244 # raise Exception, "No such network: %s" % network_name
245 #net = nets[0]
Tony Mack0211f1b2013-04-04 23:56:24 -0400246
Tony Mack48952032013-04-12 11:49:34 -0400247 subnet = None
248 subnets = self.shell.quantum.list_subnets()['subnets']
249 for snet in subnets:
250 if snet['cidr'] == cidr_ip and snet['network_id'] == network_id:
251 subnet = snet
Scott Baker8a6d5122013-08-07 18:57:15 -0700252
Tony Mack48952032013-04-12 11:49:34 -0400253 if not subnet:
254 allocation_pools = [{'start': start, 'end': end}]
255 subnet = {'subnet': {'name': name,
256 'network_id': network_id,
257 'ip_version': ip_version,
258 'cidr': cidr_ip,
Tony Mackcdb85aa2013-05-02 15:09:45 -0400259 'dns_nameservers': ['8.8.8.8', '8.8.4.4'],
Scott Baker8a6d5122013-08-07 18:57:15 -0700260 'allocation_pools': allocation_pools}}
Tony Mack48952032013-04-12 11:49:34 -0400261 subnet = self.shell.quantum.create_subnet(subnet)['subnet']
Tony Mack9b5dd752013-05-09 20:33:21 -0400262 self.add_external_route(subnet)
Tony Mack0211f1b2013-04-04 23:56:24 -0400263 # TODO: Add route to external network
264 # e.g. # route add -net 10.0.3.0/24 dev br-ex gw 10.100.0.5
265 return subnet
266
Tony Mack48952032013-04-12 11:49:34 -0400267 def update_subnet(self, id, fields):
268 return self.shell.quantum.update_subnet(id, fields)
269
Tony Mack0211f1b2013-04-04 23:56:24 -0400270 def delete_subnet(self, id):
Tony Mackfd24d0d2013-04-14 00:59:17 -0400271 #return self.shell.quantum.delete_subnet(id=id)
272 # inefficient but fault tolerant
273 subnets = self.shell.quantum.list_subnets()['subnets']
274 for subnet in subnets:
275 if subnet['id'] == id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400276 self.delete_subnet_ports(subnet['id'])
Tony Mack31d34132013-04-15 00:02:06 -0400277 self.shell.quantum.delete_subnet(id)
Tony Mack9b5dd752013-05-09 20:33:21 -0400278 self.delete_external_route(subnet)
279 return 1
280
Tony Mack70780e92013-06-30 12:46:16 -0400281 def get_external_routes(self):
282 status, output = commands.getstatusoutput('route')
283 routes = output.split('\n')[3:]
284 return routes
285
286 def add_external_route(self, subnet, routes=[]):
287 if not routes:
288 routes = self.get_external_routes()
289
Tony Mack9b5dd752013-05-09 20:33:21 -0400290 ports = self.shell.quantum.list_ports()['ports']
291
292 gw_ip = subnet['gateway_ip']
293 subnet_id = subnet['id']
294
295 # 1. Find the port associated with the subnet's gateway
296 # 2. Find the router associated with that port
297 # 3. Find the port associated with this router and on the external net
298 # 4. Set up route to the subnet through the port from step 3
299 ip_address = None
300 for port in ports:
301 for fixed_ip in port['fixed_ips']:
302 if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip:
303 gw_port = port
304 router_id = gw_port['device_id']
305 router = self.shell.quantum.show_router(router_id)['router']
Tony Mack44187012013-07-09 07:15:17 -0400306 if router and router.get('external_gateway_info'):
Tony Mack16f51e02013-07-01 10:33:29 -0400307 ext_net = router['external_gateway_info']['network_id']
308 for port in ports:
309 if port['device_id'] == router_id and port['network_id'] == ext_net:
310 ip_address = port['fixed_ips'][0]['ip_address']
Tony Mack9b5dd752013-05-09 20:33:21 -0400311
312 if ip_address:
Tony Mack70780e92013-06-30 12:46:16 -0400313 # check if external route already exists
314 route_exists = False
315 if routes:
316 for route in routes:
317 if subnet['cidr'] in route and ip_address in route:
318 route_exists = True
319 if not route_exists:
320 cmd = "route add -net %s dev br-ex gw %s" % (subnet['cidr'], ip_address)
Tony Mack16f51e02013-07-01 10:33:29 -0400321 s, o = commands.getstatusoutput(cmd)
322 #print cmd, "\n", s, o
Tony Mack9b5dd752013-05-09 20:33:21 -0400323
324 return 1
325
326 def delete_external_route(self, subnet):
327 ports = self.shell.quantum.list_ports()['ports']
328
329 gw_ip = subnet['gateway_ip']
330 subnet_id = subnet['id']
331
332 # 1. Find the port associated with the subnet's gateway
333 # 2. Find the router associated with that port
334 # 3. Find the port associated with this router and on the external net
335 # 4. Set up route to the subnet through the port from step 3
336 ip_address = None
337 for port in ports:
338 for fixed_ip in port['fixed_ips']:
339 if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip:
340 gw_port = port
341 router_id = gw_port['device_id']
342 router = self.shell.quantum.show_router(router_id)['router']
343 ext_net = router['external_gateway_info']['network_id']
344 for port in ports:
345 if port['device_id'] == router_id and port['network_id'] == ext_net:
346 ip_address = port['fixed_ips'][0]['ip_address']
347
348 if ip_address:
349 cmd = "route delete -net %s" % (subnet['cidr'])
350 commands.getstatusoutput(cmd)
351
352 return 1
Tony Mack0211f1b2013-04-04 23:56:24 -0400353
Tony Mackb0d97422013-06-10 09:57:45 -0400354 def create_keypair(self, name, public_key):
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400355 keys = self.shell.nova.keypairs.findall(name=name)
Tony Mack0211f1b2013-04-04 23:56:24 -0400356 if keys:
Tony Mack52cb9302013-04-09 12:25:35 -0400357 key = keys[0]
Tony Mackb0d97422013-06-10 09:57:45 -0400358 # update key
359 if key.public_key != public_key:
360 self.delete_keypair(key.id)
361 key = self.shell.nova.keypairs.create(name=name, public_key=public_key)
Tony Mack52cb9302013-04-09 12:25:35 -0400362 else:
Tony Mackb0d97422013-06-10 09:57:45 -0400363 key = self.shell.nova.keypairs.create(name=name, public_key=public_key)
Tony Mack52cb9302013-04-09 12:25:35 -0400364 return key
Tony Mack0211f1b2013-04-04 23:56:24 -0400365
Tony Mack759b57a2013-04-14 21:03:31 -0400366 def delete_keypair(self, id):
367 keys = self.shell.nova.keypairs.findall(id=id)
Tony Mack0211f1b2013-04-04 23:56:24 -0400368 for key in keys:
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400369 self.shell.nova.keypairs.delete(key)
Scott Baker8a6d5122013-08-07 18:57:15 -0700370 return 1
Tony Mack7823b892013-03-29 20:35:26 -0400371
Scott Baker8a6d5122013-08-07 18:57:15 -0700372 def get_private_networks(self, tenant=None):
373 if not tenant:
374 tenant = self.shell.nova.tenant
375 tenant = self.shell.keystone.tenants.find(name=tenant)
376 search_opts = {"tenant_id": tenant.id, "shared": False}
377 private_networks = self.shell.quantum.list_networks(**search_opts)
378 return private_networks
379
380 def get_shared_networks(self):
381 search_opts = {"shared": True}
382 shared_networks = self.shell.quantum.list_networks(**search_opts)
383 return shared_networks
384
Scott Baker0d5ea5c2013-08-09 14:49:56 -0700385 def get_network_subnet(self, network_id):
386 subnet_id = None
387 subnet = None
388 if network_id:
389 os_networks = self.shell.quantum.list_networks(id=network_id)["networks"]
390 if os_networks:
391 os_network = os_networks[0]
392 if os_network['subnets']:
393 subnet_id = os_network['subnets'][0]
394 os_subnets = self.shell.quantum.list_subnets(id=subnet_id)['subnets']
395 if os_subnets:
396 subnet = os_subnets[0]['cidr']
397
398 return (subnet_id, subnet)
399
Scott Baker3a0e66b2013-09-12 11:21:34 -0700400 def spawn_instance(self, name, key_name=None, hostname=None, image_id=None, security_group=None, pubkeys=[], nics=None, metadata=None):
Tony Macke7e304f2013-04-30 18:18:03 -0400401 flavor_name = self.config.nova_default_flavor
402 flavor = self.shell.nova.flavors.find(name=flavor_name)
Tony Mack221957d2013-04-13 19:08:34 -0400403 #if not image:
404 # image = self.config.nova_default_imave
Tony Mack01942f52013-03-26 14:26:40 -0400405 if not security_group:
Scott Baker3a0e66b2013-09-12 11:21:34 -0700406 security_group = self.config.nova_default_security_group
Tony Mack01942f52013-03-26 14:26:40 -0400407
Tony Mack36538ab2013-05-01 17:24:46 -0400408 files = {}
Scott Baker3a0e66b2013-09-12 11:21:34 -0700409 if pubkeys:
Tony Mack5ff5c452013-06-11 11:19:32 -0400410 files['/root/.ssh/authorized_keys'] = "\n".join(pubkeys)
Tony Mack7823b892013-03-29 20:35:26 -0400411 hints = {}
Tony Mack79b28ca2013-04-15 21:47:45 -0400412 availability_zone = None
Tony Mack7823b892013-03-29 20:35:26 -0400413 if hostname:
Tony Mack67198112014-04-09 01:46:03 -0400414 availability_zone = 'nova:%s' % hostname.split('.')[0]
Tony Mack7823b892013-03-29 20:35:26 -0400415 server = self.shell.nova.servers.create(
416 name=name,
417 key_name = key_name,
Tony Macke7e304f2013-04-30 18:18:03 -0400418 flavor=flavor.id,
Tony Mack7823b892013-03-29 20:35:26 -0400419 image=image_id,
420 security_group = security_group,
421 files=files,
Tony Mack79b28ca2013-04-15 21:47:45 -0400422 scheduler_hints=hints,
Scott Baker8a6d5122013-08-07 18:57:15 -0700423 availability_zone=availability_zone,
Scott Baker3a0e66b2013-09-12 11:21:34 -0700424 nics=nics,
Tony Mack67198112014-04-09 01:46:03 -0400425 networks=nics,
Scott Baker3a0e66b2013-09-12 11:21:34 -0700426 meta=metadata)
Tony Mack7823b892013-03-29 20:35:26 -0400427 return server
Scott Baker6a50b022013-08-26 08:25:37 -0700428
Tony Mackcdec0902013-04-15 00:38:49 -0400429 def destroy_instance(self, id):
Scott Baker6a50b022013-08-26 08:25:37 -0700430 if (self.shell.nova.tenant=="admin"):
431 # findall() is implemented as a list() followed by a python search of the
432 # list. Since findall() doesn't accept "all_tenants", we do this using
433 # list() ourselves. This allows us to delete an instance as admin.
434 servers = self.shell.nova.servers.list(search_opts={"all_tenants": True})
435 else:
436 servers = self.shell.nova.servers.list()
Tony Mack01942f52013-03-26 14:26:40 -0400437 for server in servers:
Scott Baker6a50b022013-08-26 08:25:37 -0700438 if server.id == id:
439 result=self.shell.nova.servers.delete(server)
smbaker6ada7bf2013-04-30 18:15:48 -0700440
441 def update_instance_metadata(self, id, metadata):
442 servers = self.shell.nova.servers.findall(id=id)
443 for server in servers:
444 self.shell.nova.servers.set_meta(server, metadata)
445 # note: set_meta() returns a broken Server() object. Don't try to
446 # print it in the shell or it will fail in __repr__.
447
448 def delete_instance_metadata(self, id, metadata):
449 # note: metadata is a dict. Only the keys matter, not the values.
450 servers = self.shell.nova.servers.findall(id=id)
451 for server in servers:
452 self.shell.nova.servers.delete_meta(server, metadata)
453