Tony Mack | 9b5dd75 | 2013-05-09 20:33:21 -0400 | [diff] [blame] | 1 | import commands |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 2 | from planetstack.config import Config |
| 3 | from openstack.client import OpenStackClient |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 4 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 5 | has_openstack = False |
Tony Mack | a033ced | 2013-03-29 17:57:06 -0400 | [diff] [blame] | 6 | class OpenStackDriver: |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 7 | |
Tony Mack | ad8b093 | 2013-04-07 19:32:07 -0400 | [diff] [blame] | 8 | def __init__(self, config = None, client=None): |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 9 | if config: |
| 10 | self.config = Config(config) |
| 11 | else: |
| 12 | self.config = Config() |
Tony Mack | ad8b093 | 2013-04-07 19:32:07 -0400 | [diff] [blame] | 13 | |
Tony Mack | 0dc2f56 | 2013-04-29 19:41:39 -0400 | [diff] [blame] | 14 | self.admin_client = OpenStackClient() |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 15 | if has_openstack: |
| 16 | self.admin_user = self.admin_client.keystone.users.find(name=self.admin_client.keystone.username) |
| 17 | else: |
| 18 | self.admin_user = None |
Tony Mack | 0dc2f56 | 2013-04-29 19:41:39 -0400 | [diff] [blame] | 19 | |
Tony Mack | ad8b093 | 2013-04-07 19:32:07 -0400 | [diff] [blame] | 20 | if client: |
| 21 | self.shell = client |
| 22 | else: |
| 23 | self.shell = OpenStackClient() |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 24 | |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 25 | def create_role(self, name): |
| 26 | roles = self.shell.keystone.roles.findall(name=name) |
| 27 | if not roles: |
| 28 | role = self.shell.keystone.roles.create(name) |
| 29 | else: |
| 30 | role = roles[0] |
| 31 | return role |
| 32 | |
Tony Mack | a704053 | 2013-04-08 21:45:43 -0400 | [diff] [blame] | 33 | def delete_role(self, filter): |
| 34 | roles = self.shell.keystone.roles.findall(**filter) |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 35 | for role in roles: |
| 36 | self.shell.keystone.roles.delete(role) |
| 37 | return 1 |
| 38 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 39 | def create_tenant(self, tenant_name, enabled, description): |
Tony Mack | 5ee2190 | 2013-03-29 20:36:42 -0400 | [diff] [blame] | 40 | """Create keystone tenant. Suggested fields: name, description, enabled""" |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 41 | tenants = self.shell.keystone.tenants.findall(name=tenant_name) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 42 | if not tenants: |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 43 | fields = {'tenant_name': tenant_name, 'enabled': enabled, |
Tony Mack | 488950a | 2013-04-07 20:35:50 -0400 | [diff] [blame] | 44 | 'description': description} |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 45 | tenant = self.shell.keystone.tenants.create(**fields) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 46 | else: |
| 47 | tenant = tenants[0] |
Tony Mack | 0dc2f56 | 2013-04-29 19:41:39 -0400 | [diff] [blame] | 48 | |
| 49 | # always give the admin user the admin role to any tenant created |
| 50 | # by the driver. |
| 51 | self.add_user_role(self.admin_user.id, tenant.id, 'admin') |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 52 | return tenant |
| 53 | |
| 54 | def update_tenant(self, id, **kwds): |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 55 | return self.shell.keystone.tenants.update(id, **kwds) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 56 | |
| 57 | def delete_tenant(self, id): |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 58 | tenants = self.shell.keystone.tenants.findall(id=id) |
| 59 | for tenant in tenants: |
| 60 | self.shell.keystone.tenants.delete(tenant) |
| 61 | return 1 |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 62 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 63 | def create_user(self, name, email, password, enabled): |
| 64 | users = self.shell.keystone.users.findall(email=email) |
| 65 | if not users: |
| 66 | fields = {'name': name, 'email': email, 'password': password, |
| 67 | 'enabled': enabled} |
Tony Mack | fdcbf83 | 2013-04-09 11:47:11 -0400 | [diff] [blame] | 68 | user = self.shell.keystone.users.create(**fields) |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 69 | else: |
| 70 | user = users[0] |
| 71 | return user |
| 72 | |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 73 | def delete_user(self, id): |
| 74 | users = self.shell.keystone.users.findall(id=id) |
| 75 | for user in users: |
| 76 | self.shell.keystone.users.delete(user) |
| 77 | return 1 |
| 78 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 79 | def add_user_role(self, kuser_id, tenant_id, role_name): |
| 80 | user = self.shell.keystone.users.find(id=kuser_id) |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 81 | tenant = self.shell.keystone.tenants.find(id=tenant_id) |
Tony Mack | 54304c6 | 2013-04-28 11:27:07 -0400 | [diff] [blame] | 82 | role = self.shell.keystone.roles.find(name=role_name) |
| 83 | |
| 84 | role_found = False |
| 85 | user_roles = user.list_roles(tenant.id) |
| 86 | for user_role in user_roles: |
| 87 | if user_role.name == role.name: |
| 88 | role_found = True |
| 89 | if not role_found: |
| 90 | tenant.add_user(user, role) |
| 91 | |
| 92 | return 1 |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 93 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 94 | def delete_user_role(self, kuser_id, tenant_id, role_name): |
| 95 | user = self.shell.keystone.users.find(id=kuser_id) |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 96 | tenant = self.shell.keystone.tenants.find(id=tenant_id) |
Tony Mack | 54304c6 | 2013-04-28 11:27:07 -0400 | [diff] [blame] | 97 | role = self.shell.keystone.roles.find(name=role_name) |
| 98 | |
| 99 | role_found = False |
| 100 | user_roles = user.list_roles(tenant.id) |
| 101 | for user_role in user_roles: |
| 102 | if user_role.name == role.name: |
| 103 | role_found = True |
| 104 | if role_found: |
| 105 | tenant.remove_user(user, role) |
| 106 | |
| 107 | return 1 |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 108 | |
| 109 | def update_user(self, id, **kwds): |
| 110 | return self.shell.keystone.users.update(id, **kwds) |
| 111 | |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 112 | def create_router(self, name, set_gateway=True): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 113 | routers = self.shell.quantum.list_routers(name=name)['routers'] |
| 114 | if routers: |
| 115 | router = routers[0] |
| 116 | else: |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 117 | router = self.shell.quantum.create_router({'router': {'name': name}})['router'] |
Tony Mack | c1665b1 | 2013-04-29 16:45:14 -0400 | [diff] [blame] | 118 | # add router to external network |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 119 | if set_gateway: |
Tony Mack | 257bedc | 2013-04-10 08:58:09 -0400 | [diff] [blame] | 120 | nets = self.shell.quantum.list_networks()['networks'] |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 121 | for net in nets: |
| 122 | if net['router:external'] == True: |
Tony Mack | 9dcc050 | 2013-04-10 09:07:45 -0400 | [diff] [blame] | 123 | self.shell.quantum.add_gateway_router(router['id'], |
| 124 | {'network_id': net['id']}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 125 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 126 | return router |
| 127 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 128 | def delete_router(self, id): |
| 129 | routers = self.shell.quantum.list_routers(id=id)['routers'] |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 130 | for router in routers: |
| 131 | self.shell.quantum.delete_router(router['id']) |
Tony Mack | c1665b1 | 2013-04-29 16:45:14 -0400 | [diff] [blame] | 132 | # remove router form external network |
Tony Mack | 0c0e05d | 2013-04-29 17:52:37 -0400 | [diff] [blame] | 133 | #nets = self.shell.quantum.list_networks()['networks'] |
| 134 | #for net in nets: |
| 135 | # if net['router:external'] == True: |
| 136 | # self.shell.quantum.remove_gateway_router(router['id']) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 137 | |
| 138 | def add_router_interface(self, router_id, subnet_id): |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 139 | router = self.shell.quantum.show_router(router_id)['router'] |
| 140 | subnet = self.shell.quantum.show_subnet(subnet_id)['subnet'] |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 141 | if router and subnet: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 142 | self.shell.quantum.add_interface_router(router_id, {'subnet_id': subnet_id}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 143 | |
| 144 | def delete_router_interface(self, router_id, subnet_id): |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 145 | router = self.shell.quantum.show_router(router_id) |
| 146 | subnet = self.shell.quantum.show_subnet(subnet_id) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 147 | if router and subnet: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 148 | self.shell.quantum.remove_interface_router(router_id, {'subnet_id': subnet_id}) |
Tony Mack | 240903f | 2013-04-06 01:31:37 -0400 | [diff] [blame] | 149 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 150 | def create_network(self, name): |
Tony Mack | 28be756 | 2013-04-10 08:45:55 -0400 | [diff] [blame] | 151 | nets = self.shell.quantum.list_networks(name=name)['networks'] |
| 152 | if nets: |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 153 | net = nets[0] |
Tony Mack | 82a688d | 2013-04-10 08:12:58 -0400 | [diff] [blame] | 154 | else: |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 155 | net = self.shell.quantum.create_network({'network': {'name': name}})['network'] |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 156 | return net |
| 157 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 158 | def delete_network(self, id): |
| 159 | nets = self.shell.quantum.list_networks()['networks'] |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 160 | for net in nets: |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 161 | if net['id'] == id: |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 162 | # delete_all ports |
| 163 | self.delete_network_ports(net['id']) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 164 | # delete all subnets: |
| 165 | for subnet_id in net['subnets']: |
| 166 | self.delete_subnet(subnet_id) |
| 167 | self.shell.quantum.delete_network(net['id']) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 168 | return 1 |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 169 | |
| 170 | def delete_network_ports(self, network_id): |
| 171 | ports = self.shell.quantum.list_ports()['ports'] |
| 172 | for port in ports: |
Tony Mack | c1665b1 | 2013-04-29 16:45:14 -0400 | [diff] [blame] | 173 | if port['network_id'] == network_id: |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 174 | self.shell.quantum.delete_port(port['id']) |
| 175 | return 1 |
| 176 | |
| 177 | def delete_subnet_ports(self, subnet_id): |
| 178 | ports = self.shell.quantum.list_ports()['ports'] |
| 179 | for port in ports: |
| 180 | delete = False |
| 181 | for fixed_ip in port['fixed_ips']: |
| 182 | if fixed_ip['subnet_id'] == subnet_id: |
| 183 | delete=True |
| 184 | break |
| 185 | if delete: |
| 186 | self.shell.quantum.delete_port(port['id']) |
| 187 | return 1 |
| 188 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 189 | def create_subnet(self, name, network_id, cidr_ip, ip_version, start, end): |
| 190 | #nets = self.shell.quantum.list_networks(name=network_name)['networks'] |
| 191 | #if not nets: |
| 192 | # raise Exception, "No such network: %s" % network_name |
| 193 | #net = nets[0] |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 194 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 195 | subnet = None |
| 196 | subnets = self.shell.quantum.list_subnets()['subnets'] |
| 197 | for snet in subnets: |
| 198 | if snet['cidr'] == cidr_ip and snet['network_id'] == network_id: |
| 199 | subnet = snet |
| 200 | |
| 201 | if not subnet: |
| 202 | allocation_pools = [{'start': start, 'end': end}] |
| 203 | subnet = {'subnet': {'name': name, |
| 204 | 'network_id': network_id, |
| 205 | 'ip_version': ip_version, |
| 206 | 'cidr': cidr_ip, |
Tony Mack | cdb85aa | 2013-05-02 15:09:45 -0400 | [diff] [blame] | 207 | 'dns_nameservers': ['8.8.8.8', '8.8.4.4'], |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 208 | 'allocation_pools': allocation_pools}} |
| 209 | subnet = self.shell.quantum.create_subnet(subnet)['subnet'] |
Tony Mack | 9b5dd75 | 2013-05-09 20:33:21 -0400 | [diff] [blame] | 210 | self.add_external_route(subnet) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 211 | # TODO: Add route to external network |
| 212 | # e.g. # route add -net 10.0.3.0/24 dev br-ex gw 10.100.0.5 |
| 213 | return subnet |
| 214 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 215 | def update_subnet(self, id, fields): |
| 216 | return self.shell.quantum.update_subnet(id, fields) |
| 217 | |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 218 | def delete_subnet(self, id): |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 219 | #return self.shell.quantum.delete_subnet(id=id) |
| 220 | # inefficient but fault tolerant |
| 221 | subnets = self.shell.quantum.list_subnets()['subnets'] |
| 222 | for subnet in subnets: |
| 223 | if subnet['id'] == id: |
Tony Mack | 94c6ea0 | 2013-04-16 21:41:50 -0400 | [diff] [blame] | 224 | self.delete_subnet_ports(subnet['id']) |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 225 | self.shell.quantum.delete_subnet(id) |
Tony Mack | 9b5dd75 | 2013-05-09 20:33:21 -0400 | [diff] [blame] | 226 | self.delete_external_route(subnet) |
| 227 | return 1 |
| 228 | |
| 229 | def add_external_route(self, subnet): |
| 230 | ports = self.shell.quantum.list_ports()['ports'] |
| 231 | |
| 232 | gw_ip = subnet['gateway_ip'] |
| 233 | subnet_id = subnet['id'] |
| 234 | |
| 235 | # 1. Find the port associated with the subnet's gateway |
| 236 | # 2. Find the router associated with that port |
| 237 | # 3. Find the port associated with this router and on the external net |
| 238 | # 4. Set up route to the subnet through the port from step 3 |
| 239 | ip_address = None |
| 240 | for port in ports: |
| 241 | for fixed_ip in port['fixed_ips']: |
| 242 | if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip: |
| 243 | gw_port = port |
| 244 | router_id = gw_port['device_id'] |
| 245 | router = self.shell.quantum.show_router(router_id)['router'] |
| 246 | ext_net = router['external_gateway_info']['network_id'] |
| 247 | for port in ports: |
| 248 | if port['device_id'] == router_id and port['network_id'] == ext_net: |
| 249 | ip_address = port['fixed_ips'][0]['ip_address'] |
| 250 | |
| 251 | if ip_address: |
| 252 | cmd = "route add -net %s dev br-ex gw %s" % (subnet['cidr'], ip_address) |
| 253 | commands.getstatusoutput(cmd) |
| 254 | |
| 255 | return 1 |
| 256 | |
| 257 | def delete_external_route(self, subnet): |
| 258 | ports = self.shell.quantum.list_ports()['ports'] |
| 259 | |
| 260 | gw_ip = subnet['gateway_ip'] |
| 261 | subnet_id = subnet['id'] |
| 262 | |
| 263 | # 1. Find the port associated with the subnet's gateway |
| 264 | # 2. Find the router associated with that port |
| 265 | # 3. Find the port associated with this router and on the external net |
| 266 | # 4. Set up route to the subnet through the port from step 3 |
| 267 | ip_address = None |
| 268 | for port in ports: |
| 269 | for fixed_ip in port['fixed_ips']: |
| 270 | if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip: |
| 271 | gw_port = port |
| 272 | router_id = gw_port['device_id'] |
| 273 | router = self.shell.quantum.show_router(router_id)['router'] |
| 274 | ext_net = router['external_gateway_info']['network_id'] |
| 275 | for port in ports: |
| 276 | if port['device_id'] == router_id and port['network_id'] == ext_net: |
| 277 | ip_address = port['fixed_ips'][0]['ip_address'] |
| 278 | |
| 279 | if ip_address: |
| 280 | cmd = "route delete -net %s" % (subnet['cidr']) |
| 281 | commands.getstatusoutput(cmd) |
| 282 | |
| 283 | return 1 |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 284 | |
| 285 | def create_keypair(self, name, key): |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 286 | keys = self.shell.nova.keypairs.findall(name=name) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 287 | if keys: |
Tony Mack | 52cb930 | 2013-04-09 12:25:35 -0400 | [diff] [blame] | 288 | key = keys[0] |
| 289 | else: |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 290 | key = self.shell.nova.keypairs.create(name=name, public_key=key) |
Tony Mack | 52cb930 | 2013-04-09 12:25:35 -0400 | [diff] [blame] | 291 | return key |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 292 | |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 293 | def delete_keypair(self, id): |
| 294 | keys = self.shell.nova.keypairs.findall(id=id) |
Tony Mack | 0211f1b | 2013-04-04 23:56:24 -0400 | [diff] [blame] | 295 | for key in keys: |
Tony Mack | c2a1c7c | 2013-04-09 13:06:34 -0400 | [diff] [blame] | 296 | self.shell.nova.keypairs.delete(key) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 297 | return 1 |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 298 | |
Tony Mack | e7e304f | 2013-04-30 18:18:03 -0400 | [diff] [blame] | 299 | def spawn_instance(self, name, key_name=None, hostname=None, image_id=None, security_group=None, pubkeys=[]): |
| 300 | flavor_name = self.config.nova_default_flavor |
| 301 | flavor = self.shell.nova.flavors.find(name=flavor_name) |
Tony Mack | 221957d | 2013-04-13 19:08:34 -0400 | [diff] [blame] | 302 | #if not image: |
| 303 | # image = self.config.nova_default_imave |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 304 | if not security_group: |
| 305 | security_group = self.config.nova_default_security_group |
| 306 | |
Tony Mack | 36538ab | 2013-05-01 17:24:46 -0400 | [diff] [blame] | 307 | #authorized_keys = "\n".join(pubkeys) |
| 308 | #files = {'/root/.ssh/authorized_keys': authorized_keys} |
| 309 | files = {} |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 310 | |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 311 | hints = {} |
Tony Mack | 79b28ca | 2013-04-15 21:47:45 -0400 | [diff] [blame] | 312 | availability_zone = None |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 313 | if hostname: |
Tony Mack | 79b28ca | 2013-04-15 21:47:45 -0400 | [diff] [blame] | 314 | availability_zone = 'nova:%s' % hostname |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 315 | server = self.shell.nova.servers.create( |
| 316 | name=name, |
| 317 | key_name = key_name, |
Tony Mack | e7e304f | 2013-04-30 18:18:03 -0400 | [diff] [blame] | 318 | flavor=flavor.id, |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 319 | image=image_id, |
| 320 | security_group = security_group, |
| 321 | files=files, |
Tony Mack | 79b28ca | 2013-04-15 21:47:45 -0400 | [diff] [blame] | 322 | scheduler_hints=hints, |
| 323 | availability_zone=availability_zone) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 324 | return server |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 325 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 326 | def destroy_instance(self, id): |
| 327 | servers = self.shell.nova.servers.findall(id=id) |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 328 | for server in servers: |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 329 | self.shell.nova.servers.delete(server) |
smbaker | 6ada7bf | 2013-04-30 18:15:48 -0700 | [diff] [blame] | 330 | |
| 331 | def update_instance_metadata(self, id, metadata): |
| 332 | servers = self.shell.nova.servers.findall(id=id) |
| 333 | for server in servers: |
| 334 | self.shell.nova.servers.set_meta(server, metadata) |
| 335 | # note: set_meta() returns a broken Server() object. Don't try to |
| 336 | # print it in the shell or it will fail in __repr__. |
| 337 | |
| 338 | def delete_instance_metadata(self, id, metadata): |
| 339 | # note: metadata is a dict. Only the keys matter, not the values. |
| 340 | servers = self.shell.nova.servers.findall(id=id) |
| 341 | for server in servers: |
| 342 | self.shell.nova.servers.delete_meta(server, metadata) |
| 343 | |