blob: 940be6c2df28810d0a76a723fd683859248d6989 [file] [log] [blame]
Tony Mack01942f52013-03-26 14:26:40 -04001from plstackapi.planetstack.config import Config
Tony Mackad8b0932013-04-07 19:32:07 -04002from plstackapi.openstack.client import OpenStackClient
Tony Mack01942f52013-03-26 14:26:40 -04003
Tony Macka033ced2013-03-29 17:57:06 -04004class OpenStackDriver:
Tony Mack01942f52013-03-26 14:26:40 -04005
Tony Mackad8b0932013-04-07 19:32:07 -04006 def __init__(self, config = None, client=None):
Tony Mack01942f52013-03-26 14:26:40 -04007 if config:
8 self.config = Config(config)
9 else:
10 self.config = Config()
Tony Mackad8b0932013-04-07 19:32:07 -040011
12 if client:
13 self.shell = client
14 else:
15 self.shell = OpenStackClient()
Tony Mack01942f52013-03-26 14:26:40 -040016
Tony Mackb5f339f2013-04-08 16:29:38 -040017 def create_role(self, name):
18 roles = self.shell.keystone.roles.findall(name=name)
19 if not roles:
20 role = self.shell.keystone.roles.create(name)
21 else:
22 role = roles[0]
23 return role
24
Tony Macka7040532013-04-08 21:45:43 -040025 def delete_role(self, filter):
26 roles = self.shell.keystone.roles.findall(**filter)
Tony Mackb5f339f2013-04-08 16:29:38 -040027 for role in roles:
28 self.shell.keystone.roles.delete(role)
29 return 1
30
Tony Mack1fbdeca2013-04-06 00:10:36 -040031 def create_tenant(self, tenant_name, enabled, description):
Tony Mack5ee21902013-03-29 20:36:42 -040032 """Create keystone tenant. Suggested fields: name, description, enabled"""
Tony Mack1fbdeca2013-04-06 00:10:36 -040033 tenants = self.shell.keystone.tenants.findall(name=tenant_name)
Tony Mack7823b892013-03-29 20:35:26 -040034 if not tenants:
Tony Mack1fbdeca2013-04-06 00:10:36 -040035 fields = {'tenant_name': tenant_name, 'enabled': enabled,
Tony Mack488950a2013-04-07 20:35:50 -040036 'description': description}
Tony Mack1fbdeca2013-04-06 00:10:36 -040037 tenant = self.shell.keystone.tenants.create(**fields)
Tony Mack7823b892013-03-29 20:35:26 -040038 else:
39 tenant = tenants[0]
40 return tenant
41
42 def update_tenant(self, id, **kwds):
Tony Mack1fbdeca2013-04-06 00:10:36 -040043 return self.shell.keystone.tenants.update(id, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -040044
45 def delete_tenant(self, id):
Tony Mackfd24d0d2013-04-14 00:59:17 -040046 tenants = self.shell.keystone.tenants.findall(id=id)
47 for tenant in tenants:
48 self.shell.keystone.tenants.delete(tenant)
49 return 1
Tony Mack0211f1b2013-04-04 23:56:24 -040050
Tony Mack1fbdeca2013-04-06 00:10:36 -040051 def create_user(self, name, email, password, enabled):
52 users = self.shell.keystone.users.findall(email=email)
53 if not users:
54 fields = {'name': name, 'email': email, 'password': password,
55 'enabled': enabled}
Tony Mackfdcbf832013-04-09 11:47:11 -040056 user = self.shell.keystone.users.create(**fields)
Tony Mack1fbdeca2013-04-06 00:10:36 -040057 else:
58 user = users[0]
59 return user
60
Tony Mackfd24d0d2013-04-14 00:59:17 -040061 def delete_user(self, id):
62 users = self.shell.keystone.users.findall(id=id)
63 for user in users:
64 self.shell.keystone.users.delete(user)
65 return 1
66
Tony Mack1fbdeca2013-04-06 00:10:36 -040067 def add_user_role(self, user_id, tenant_id, role_name):
68 user = self.shell.keystone.users.find(id=user_id)
69 tenant = self.shell.keystone.tenants.find(id=tenant_id)
70 role = self.shell.keystone.roles.find(role_name)
71 return tenant.add_user(user, role)
72
73 def delete_user_role(self, user_id, tenant_id, role_name):
74 user = self.shell.keystone.users.find(id=user_id)
75 tenant = self.shell.keystone.tenants.find(id=tenant_id)
76 role = self.shell.keystone.roles.find(role_name)
77 return tenant.delete_user(user, role)
78
79 def update_user(self, id, **kwds):
80 return self.shell.keystone.users.update(id, **kwds)
81
Tony Mack240903f2013-04-06 01:31:37 -040082 def create_router(self, name, set_gateway=True):
Tony Mack28be7562013-04-10 08:45:55 -040083 routers = self.shell.quantum.list_routers(name=name)['routers']
84 if routers:
85 router = routers[0]
86 else:
Tony Mack62bc59a2013-04-14 23:27:12 -040087 router = self.shell.quantum.create_router({'router': {'name': name}})['router']
Tony Mack240903f2013-04-06 01:31:37 -040088 if set_gateway:
Tony Mack257bedc2013-04-10 08:58:09 -040089 nets = self.shell.quantum.list_networks()['networks']
Tony Mack240903f2013-04-06 01:31:37 -040090 for net in nets:
91 if net['router:external'] == True:
Tony Mack9dcc0502013-04-10 09:07:45 -040092 self.shell.quantum.add_gateway_router(router['id'],
93 {'network_id': net['id']})
Tony Mack240903f2013-04-06 01:31:37 -040094
Tony Mack0211f1b2013-04-04 23:56:24 -040095 return router
96
Tony Mack62bc59a2013-04-14 23:27:12 -040097 def delete_router(self, id):
98 routers = self.shell.quantum.list_routers(id=id)['routers']
Tony Mack28be7562013-04-10 08:45:55 -040099 for router in routers:
100 self.shell.quantum.delete_router(router['id'])
Tony Mack240903f2013-04-06 01:31:37 -0400101
102 def add_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400103 router = self.shell.quantum.show_router(router_id)['router']
104 subnet = self.shell.quantum.show_subnet(subnet_id)['subnet']
Tony Mack240903f2013-04-06 01:31:37 -0400105 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400106 self.shell.quantum.add_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400107
108 def delete_router_interface(self, router_id, subnet_id):
Tony Mack48952032013-04-12 11:49:34 -0400109 router = self.shell.quantum.show_router(router_id)
110 subnet = self.shell.quantum.show_subnet(subnet_id)
Tony Mack240903f2013-04-06 01:31:37 -0400111 if router and subnet:
Tony Mack48952032013-04-12 11:49:34 -0400112 self.shell.quantum.remove_interface_router(router_id, {'subnet_id': subnet_id})
Tony Mack240903f2013-04-06 01:31:37 -0400113
Tony Mack0211f1b2013-04-04 23:56:24 -0400114 def create_network(self, name):
Tony Mack28be7562013-04-10 08:45:55 -0400115 nets = self.shell.quantum.list_networks(name=name)['networks']
116 if nets:
Tony Mack53c63342013-04-10 08:11:45 -0400117 net = nets[0]
Tony Mack82a688d2013-04-10 08:12:58 -0400118 else:
Tony Mack62bc59a2013-04-14 23:27:12 -0400119 net = self.shell.quantum.create_network({'network': {'name': name}})['network']
Tony Mack53c63342013-04-10 08:11:45 -0400120 return net
121
Tony Mack62bc59a2013-04-14 23:27:12 -0400122 def delete_network(self, id):
123 nets = self.shell.quantum.list_networks()['networks']
Tony Mack0211f1b2013-04-04 23:56:24 -0400124 for net in nets:
Tony Mack62bc59a2013-04-14 23:27:12 -0400125 if net['id'] == id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400126 # delete_all ports
127 self.delete_network_ports(net['id'])
Tony Mack62bc59a2013-04-14 23:27:12 -0400128 # delete all subnets:
129 for subnet_id in net['subnets']:
130 self.delete_subnet(subnet_id)
131 self.shell.quantum.delete_network(net['id'])
Tony Mackfd24d0d2013-04-14 00:59:17 -0400132 return 1
Tony Mack94c6ea02013-04-16 21:41:50 -0400133
134 def delete_network_ports(self, network_id):
135 ports = self.shell.quantum.list_ports()['ports']
136 for port in ports:
137 if port['network_id'] == 'network_id':
138 self.shell.quantum.delete_port(port['id'])
139 return 1
140
141 def delete_subnet_ports(self, subnet_id):
142 ports = self.shell.quantum.list_ports()['ports']
143 for port in ports:
144 delete = False
145 for fixed_ip in port['fixed_ips']:
146 if fixed_ip['subnet_id'] == subnet_id:
147 delete=True
148 break
149 if delete:
150 self.shell.quantum.delete_port(port['id'])
151 return 1
152
Tony Mack48952032013-04-12 11:49:34 -0400153 def create_subnet(self, name, network_id, cidr_ip, ip_version, start, end):
154 #nets = self.shell.quantum.list_networks(name=network_name)['networks']
155 #if not nets:
156 # raise Exception, "No such network: %s" % network_name
157 #net = nets[0]
Tony Mack0211f1b2013-04-04 23:56:24 -0400158
Tony Mack48952032013-04-12 11:49:34 -0400159 subnet = None
160 subnets = self.shell.quantum.list_subnets()['subnets']
161 for snet in subnets:
162 if snet['cidr'] == cidr_ip and snet['network_id'] == network_id:
163 subnet = snet
164
165 if not subnet:
166 allocation_pools = [{'start': start, 'end': end}]
167 subnet = {'subnet': {'name': name,
168 'network_id': network_id,
169 'ip_version': ip_version,
170 'cidr': cidr_ip,
171 'dns_nameservers': ['8.8.8.8', '8.8.8.4'],
172 'allocation_pools': allocation_pools}}
173 subnet = self.shell.quantum.create_subnet(subnet)['subnet']
Tony Mack0211f1b2013-04-04 23:56:24 -0400174
175 # TODO: Add route to external network
176 # e.g. # route add -net 10.0.3.0/24 dev br-ex gw 10.100.0.5
177 return subnet
178
Tony Mack48952032013-04-12 11:49:34 -0400179 def update_subnet(self, id, fields):
180 return self.shell.quantum.update_subnet(id, fields)
181
Tony Mack0211f1b2013-04-04 23:56:24 -0400182 def delete_subnet(self, id):
Tony Mackfd24d0d2013-04-14 00:59:17 -0400183 #return self.shell.quantum.delete_subnet(id=id)
184 # inefficient but fault tolerant
185 subnets = self.shell.quantum.list_subnets()['subnets']
186 for subnet in subnets:
187 if subnet['id'] == id:
Tony Mack94c6ea02013-04-16 21:41:50 -0400188 self.delete_subnet_ports(subnet['id'])
Tony Mack31d34132013-04-15 00:02:06 -0400189 self.shell.quantum.delete_subnet(id)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400190 return
Tony Mack0211f1b2013-04-04 23:56:24 -0400191
192 def create_keypair(self, name, key):
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400193 keys = self.shell.nova.keypairs.findall(name=name)
Tony Mack0211f1b2013-04-04 23:56:24 -0400194 if keys:
Tony Mack52cb9302013-04-09 12:25:35 -0400195 key = keys[0]
196 else:
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400197 key = self.shell.nova.keypairs.create(name=name, public_key=key)
Tony Mack52cb9302013-04-09 12:25:35 -0400198 return key
Tony Mack0211f1b2013-04-04 23:56:24 -0400199
Tony Mack759b57a2013-04-14 21:03:31 -0400200 def delete_keypair(self, id):
201 keys = self.shell.nova.keypairs.findall(id=id)
Tony Mack0211f1b2013-04-04 23:56:24 -0400202 for key in keys:
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400203 self.shell.nova.keypairs.delete(key)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400204 return 1
Tony Mack7823b892013-03-29 20:35:26 -0400205
Tony Mack221957d2013-04-13 19:08:34 -0400206 def spawn_instance(self, name, key_name=None, hostname=None, flavor_id=None, image_id=None, security_group=None, pubkeys=[]):
207 #if not flavor_id:
208 # flavor = self.config.nova_default_flavor
209 #if not image:
210 # image = self.config.nova_default_imave
Tony Mack01942f52013-03-26 14:26:40 -0400211 if not security_group:
212 security_group = self.config.nova_default_security_group
213
214 authorized_keys = "\n".join(pubkeys)
215 files = {'/root/.ssh/authorized_keys': authorized_keys}
216
Tony Mack7823b892013-03-29 20:35:26 -0400217 hints = {}
Tony Mack79b28ca2013-04-15 21:47:45 -0400218 availability_zone = None
Tony Mack7823b892013-03-29 20:35:26 -0400219 if hostname:
Tony Mack79b28ca2013-04-15 21:47:45 -0400220 availability_zone = 'nova:%s' % hostname
Tony Mack7823b892013-03-29 20:35:26 -0400221 server = self.shell.nova.servers.create(
222 name=name,
223 key_name = key_name,
224 flavor=flavor_id,
225 image=image_id,
226 security_group = security_group,
227 files=files,
Tony Mack79b28ca2013-04-15 21:47:45 -0400228 scheduler_hints=hints,
229 availability_zone=availability_zone)
Tony Mack7823b892013-03-29 20:35:26 -0400230 return server
Tony Mack01942f52013-03-26 14:26:40 -0400231
Tony Mackcdec0902013-04-15 00:38:49 -0400232 def destroy_instance(self, id):
233 servers = self.shell.nova.servers.findall(id=id)
Tony Mack01942f52013-03-26 14:26:40 -0400234 for server in servers:
Tony Mackcdec0902013-04-15 00:38:49 -0400235 self.shell.nova.servers.delete(server)