blob: ef7662a3894239b47c37a93b636d0b068f50feb6 [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):
46 tenant = self.shell.keystone.tenants.find(id=id)
47 return self.shell.keystone.tenants.delete(tenant)
Tony Mack0211f1b2013-04-04 23:56:24 -040048
Tony Mack1fbdeca2013-04-06 00:10:36 -040049 def create_user(self, name, email, password, enabled):
50 users = self.shell.keystone.users.findall(email=email)
51 if not users:
52 fields = {'name': name, 'email': email, 'password': password,
53 'enabled': enabled}
Tony Mackfdcbf832013-04-09 11:47:11 -040054 user = self.shell.keystone.users.create(**fields)
Tony Mack1fbdeca2013-04-06 00:10:36 -040055 else:
56 user = users[0]
57 return user
58
59 def add_user_role(self, user_id, tenant_id, role_name):
60 user = self.shell.keystone.users.find(id=user_id)
61 tenant = self.shell.keystone.tenants.find(id=tenant_id)
62 role = self.shell.keystone.roles.find(role_name)
63 return tenant.add_user(user, role)
64
65 def delete_user_role(self, user_id, tenant_id, role_name):
66 user = self.shell.keystone.users.find(id=user_id)
67 tenant = self.shell.keystone.tenants.find(id=tenant_id)
68 role = self.shell.keystone.roles.find(role_name)
69 return tenant.delete_user(user, role)
70
71 def update_user(self, id, **kwds):
72 return self.shell.keystone.users.update(id, **kwds)
73
74 def delete_user(self, id):
75 user = self.shell.keystone.users.find(id=id)
76 return self.shell.keystone.users.delete(user)
77
Tony Mack240903f2013-04-06 01:31:37 -040078 def create_router(self, name, set_gateway=True):
Tony Mack0211f1b2013-04-04 23:56:24 -040079 router = self.shell.quantum.create_router(name=name)
Tony Mack240903f2013-04-06 01:31:37 -040080 if set_gateway:
81 nets = self.shell.quantum.list_networks()
82 for net in nets:
83 if net['router:external'] == True:
84 self.shell.quantum.add_gateway_router(router, net)
85
Tony Mack0211f1b2013-04-04 23:56:24 -040086 return router
87
88 def delete_router(self, name):
89 return self.shell.quantum.delete_router(name=name)
Tony Mack240903f2013-04-06 01:31:37 -040090
91 def add_router_interface(self, router_id, subnet_id):
92 router = None
93 subnet = None
94 for r in self.shell.quantum.list_routers():
95 if r['id'] == router_id:
96 router = r
97 break
98 for s in self.shell.quantum.list_subnets():
99 if s['id'] == subnet_id:
100 subnet = s
101 break
102
103 if router and subnet:
104 self.shell.quantum.router_add_interface(router, subnet)
105
106 def delete_router_interface(self, router_id, subnet_id):
107 router = None
108 subnet = None
109 for r in self.shell.quantum.list_routers():
110 if r['id'] == router_id:
111 router = r
112 break
113 for s in self.shell.quantum.list_subnets():
114 if s['id'] == subnet_id:
115 subnet = s
116 break
117
118 if router and subnet:
119 self.shell.quantum.router_remove_interface(router, subnet)
120
Tony Mack0211f1b2013-04-04 23:56:24 -0400121 def create_network(self, name):
122 return self.shell.quantum.create_network(name=name, admin_state_up=True)
123
124 def delete_network(self, name):
125 nets = self.shell.quantum.list_networks(name=name)
126 for net in nets:
127 # delete all subnets:
128 #subnets = self.api.client_shell.quantum.list_subnets(network_id=net['network_id'])['subnets']
129 for subnet_id in net['subnets']:
130 self.delete_subnet(subnet_id)
131 self.shell.quantum.delete_network(net['id'])
132
133 def create_subnet(self, network_name, cidr_ip, ip_version, start, end):
134 nets = self.shell.quantum.list_networks(name=network_name)
135 if not nets:
136 raise Exception, "No such network: %s" % network_name
137 nets = nets[0]
138
139 subnets = self.shell.quantum.list_subnets(name=self.name)
140 allocation_pools = [{'start': start, 'end': end}]
141 subnet = self.shell.quantum.create_subnet(network_id=net['id'],
142 ip_version=ip_version,
143 cidr=cidr_ip,
Tony Mack240903f2013-04-06 01:31:37 -0400144 dns_nameservers=['8.8.8.8', '8.8.8.4'],
Tony Mack0211f1b2013-04-04 23:56:24 -0400145 allocation_pools=allocation_pools)
146
147 # TODO: Add route to external network
148 # e.g. # route add -net 10.0.3.0/24 dev br-ex gw 10.100.0.5
149 return subnet
150
151 def delete_subnet(self, id):
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400152 return self.shell.quantum.delete_subnet(id=id)
Tony Mack0211f1b2013-04-04 23:56:24 -0400153
154
155 def create_keypair(self, name, key):
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400156 keys = self.shell.nova.keypairs.findall(name=name)
Tony Mack0211f1b2013-04-04 23:56:24 -0400157 if keys:
Tony Mack52cb9302013-04-09 12:25:35 -0400158 key = keys[0]
159 else:
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400160 key = self.shell.nova.keypairs.create(name=name, public_key=key)
Tony Mack52cb9302013-04-09 12:25:35 -0400161 return key
Tony Mack0211f1b2013-04-04 23:56:24 -0400162
163 def delete_keypair(self, name):
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400164 keys = self.shell.nova.keypairs.findall(name=name)
Tony Mack0211f1b2013-04-04 23:56:24 -0400165 for key in keys:
Tony Mackc2a1c7c2013-04-09 13:06:34 -0400166 self.shell.nova.keypairs.delete(key)
Tony Mack7823b892013-03-29 20:35:26 -0400167
168 def spawn_instance(self, name, key_name=None, hostname=None, flavor=None, image=None, security_group=None, pubkeys=[]):
Tony Mack01942f52013-03-26 14:26:40 -0400169 if not flavor:
170 flavor = self.config.nova_default_flavor
171 if not image:
172 image = self.config.nova_default_imave
173 if not security_group:
174 security_group = self.config.nova_default_security_group
175
176 authorized_keys = "\n".join(pubkeys)
177 files = {'/root/.ssh/authorized_keys': authorized_keys}
178
Tony Mack7823b892013-03-29 20:35:26 -0400179 flavor_id = self.shell.nova.flavors.find(name=flavor)
180 images = self.shell.glance.get_images(name=image)
181 if not images:
182 raise Exception, "Image not found: " + image
183 image_id = images[0]['id']
184 hints = {}
185 if hostname:
186 hints['force_hosts']= hostname
187 server = self.shell.nova.servers.create(
188 name=name,
189 key_name = key_name,
190 flavor=flavor_id,
191 image=image_id,
192 security_group = security_group,
193 files=files,
194 scheduler_hints=hints)
195 return server
Tony Mack01942f52013-03-26 14:26:40 -0400196
Tony Mack7823b892013-03-29 20:35:26 -0400197 def destroy_instance(self, name, id=None):
198 args = {'name': name}
199 if id:
200 args['id'] = id
201 servers = self.shell.nova.servers.findall(**args)
Tony Mack01942f52013-03-26 14:26:40 -0400202 for server in servers:
Tony Mack7823b892013-03-29 20:35:26 -0400203 if name == server.name:
Tony Mack07ece3b2013-03-29 20:51:07 -0400204 if not id or id == server.id:
Tony Mack7823b892013-03-29 20:35:26 -0400205 self.shell.nova.servers.delete(server)