blob: 5a42d79084de9c1a927f506c77a721a95f56bb60 [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 Mack1fbdeca2013-04-06 00:10:36 -040017 def create_tenant(self, tenant_name, enabled, description):
Tony Mack5ee21902013-03-29 20:36:42 -040018 """Create keystone tenant. Suggested fields: name, description, enabled"""
Tony Mack1fbdeca2013-04-06 00:10:36 -040019 tenants = self.shell.keystone.tenants.findall(name=tenant_name)
Tony Mack7823b892013-03-29 20:35:26 -040020 if not tenants:
Tony Mack1fbdeca2013-04-06 00:10:36 -040021 fields = {'tenant_name': tenant_name, 'enabled': enabled,
22 'description', description}
23 tenant = self.shell.keystone.tenants.create(**fields)
Tony Mack7823b892013-03-29 20:35:26 -040024 else:
25 tenant = tenants[0]
26 return tenant
27
28 def update_tenant(self, id, **kwds):
Tony Mack1fbdeca2013-04-06 00:10:36 -040029 return self.shell.keystone.tenants.update(id, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -040030
31 def delete_tenant(self, id):
32 tenant = self.shell.keystone.tenants.find(id=id)
33 return self.shell.keystone.tenants.delete(tenant)
Tony Mack0211f1b2013-04-04 23:56:24 -040034
Tony Mack1fbdeca2013-04-06 00:10:36 -040035 def create_user(self, name, email, password, enabled):
36 users = self.shell.keystone.users.findall(email=email)
37 if not users:
38 fields = {'name': name, 'email': email, 'password': password,
39 'enabled': enabled}
40 user = self.shell.keystone.create(**fields)
41 else:
42 user = users[0]
43 return user
44
45 def add_user_role(self, user_id, tenant_id, role_name):
46 user = self.shell.keystone.users.find(id=user_id)
47 tenant = self.shell.keystone.tenants.find(id=tenant_id)
48 role = self.shell.keystone.roles.find(role_name)
49 return tenant.add_user(user, role)
50
51 def delete_user_role(self, user_id, tenant_id, role_name):
52 user = self.shell.keystone.users.find(id=user_id)
53 tenant = self.shell.keystone.tenants.find(id=tenant_id)
54 role = self.shell.keystone.roles.find(role_name)
55 return tenant.delete_user(user, role)
56
57 def update_user(self, id, **kwds):
58 return self.shell.keystone.users.update(id, **kwds)
59
60 def delete_user(self, id):
61 user = self.shell.keystone.users.find(id=id)
62 return self.shell.keystone.users.delete(user)
63
Tony Mack240903f2013-04-06 01:31:37 -040064 def create_router(self, name, set_gateway=True):
Tony Mack0211f1b2013-04-04 23:56:24 -040065 router = self.shell.quantum.create_router(name=name)
Tony Mack240903f2013-04-06 01:31:37 -040066 if set_gateway:
67 nets = self.shell.quantum.list_networks()
68 for net in nets:
69 if net['router:external'] == True:
70 self.shell.quantum.add_gateway_router(router, net)
71
Tony Mack0211f1b2013-04-04 23:56:24 -040072 return router
73
74 def delete_router(self, name):
75 return self.shell.quantum.delete_router(name=name)
Tony Mack240903f2013-04-06 01:31:37 -040076
77 def add_router_interface(self, router_id, subnet_id):
78 router = None
79 subnet = None
80 for r in self.shell.quantum.list_routers():
81 if r['id'] == router_id:
82 router = r
83 break
84 for s in self.shell.quantum.list_subnets():
85 if s['id'] == subnet_id:
86 subnet = s
87 break
88
89 if router and subnet:
90 self.shell.quantum.router_add_interface(router, subnet)
91
92 def delete_router_interface(self, router_id, subnet_id):
93 router = None
94 subnet = None
95 for r in self.shell.quantum.list_routers():
96 if r['id'] == router_id:
97 router = r
98 break
99 for s in self.shell.quantum.list_subnets():
100 if s['id'] == subnet_id:
101 subnet = s
102 break
103
104 if router and subnet:
105 self.shell.quantum.router_remove_interface(router, subnet)
106
Tony Mack0211f1b2013-04-04 23:56:24 -0400107 def create_network(self, name):
108 return self.shell.quantum.create_network(name=name, admin_state_up=True)
109
110 def delete_network(self, name):
111 nets = self.shell.quantum.list_networks(name=name)
112 for net in nets:
113 # delete all subnets:
114 #subnets = self.api.client_shell.quantum.list_subnets(network_id=net['network_id'])['subnets']
115 for subnet_id in net['subnets']:
116 self.delete_subnet(subnet_id)
117 self.shell.quantum.delete_network(net['id'])
118
119 def create_subnet(self, network_name, cidr_ip, ip_version, start, end):
120 nets = self.shell.quantum.list_networks(name=network_name)
121 if not nets:
122 raise Exception, "No such network: %s" % network_name
123 nets = nets[0]
124
125 subnets = self.shell.quantum.list_subnets(name=self.name)
126 allocation_pools = [{'start': start, 'end': end}]
127 subnet = self.shell.quantum.create_subnet(network_id=net['id'],
128 ip_version=ip_version,
129 cidr=cidr_ip,
Tony Mack240903f2013-04-06 01:31:37 -0400130 dns_nameservers=['8.8.8.8', '8.8.8.4'],
Tony Mack0211f1b2013-04-04 23:56:24 -0400131 allocation_pools=allocation_pools)
132
133 # TODO: Add route to external network
134 # e.g. # route add -net 10.0.3.0/24 dev br-ex gw 10.100.0.5
135 return subnet
136
137 def delete_subnet(self, id):
138 return self.client.quantum.delete_subnet(id=id)
139
140
141 def create_keypair(self, name, key):
142 keys = self.client.nova.keypairs.findall(name=name)
143 if keys:
144 raise Exception, "Key name already exists: %s" % name
145 return self.client.nova.keypairs.create(name=name, public_key=key)
146
147 def delete_keypair(self, name):
148 keys = self.client.nova.keypairs.findall(name=name)
149 for key in keys:
150 self.client.nova.keypairs.delete(key)
Tony Mack7823b892013-03-29 20:35:26 -0400151
152 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 -0400153 if not flavor:
154 flavor = self.config.nova_default_flavor
155 if not image:
156 image = self.config.nova_default_imave
157 if not security_group:
158 security_group = self.config.nova_default_security_group
159
160 authorized_keys = "\n".join(pubkeys)
161 files = {'/root/.ssh/authorized_keys': authorized_keys}
162
Tony Mack7823b892013-03-29 20:35:26 -0400163 flavor_id = self.shell.nova.flavors.find(name=flavor)
164 images = self.shell.glance.get_images(name=image)
165 if not images:
166 raise Exception, "Image not found: " + image
167 image_id = images[0]['id']
168 hints = {}
169 if hostname:
170 hints['force_hosts']= hostname
171 server = self.shell.nova.servers.create(
172 name=name,
173 key_name = key_name,
174 flavor=flavor_id,
175 image=image_id,
176 security_group = security_group,
177 files=files,
178 scheduler_hints=hints)
179 return server
Tony Mack01942f52013-03-26 14:26:40 -0400180
Tony Mack7823b892013-03-29 20:35:26 -0400181 def destroy_instance(self, name, id=None):
182 args = {'name': name}
183 if id:
184 args['id'] = id
185 servers = self.shell.nova.servers.findall(**args)
Tony Mack01942f52013-03-26 14:26:40 -0400186 for server in servers:
Tony Mack7823b892013-03-29 20:35:26 -0400187 if name == server.name:
Tony Mack07ece3b2013-03-29 20:51:07 -0400188 if not id or id == server.id:
Tony Mack7823b892013-03-29 20:35:26 -0400189 self.shell.nova.servers.delete(server)