blob: 099240aa6bdd0602b9650b936ab42e76c125698c [file] [log] [blame]
Tony Mack01942f52013-03-26 14:26:40 -04001from plstackapi.planetstack.config import Config
2from plstackapi.openstack.shell import OpenStackShell
3
Tony Macka033ced2013-03-29 17:57:06 -04004class OpenStackDriver:
Tony Mack01942f52013-03-26 14:26:40 -04005
6 def __init__(self, config = None):
7 if config:
8 self.config = Config(config)
9 else:
10 self.config = Config()
11 self.shell = OpenStackShell()
12
Tony Mack7823b892013-03-29 20:35:26 -040013 def create_tenant(self, **kwds):
Tony Mack5ee21902013-03-29 20:36:42 -040014 """Create keystone tenant. Suggested fields: name, description, enabled"""
Tony Mack123096d2013-04-02 10:24:24 -040015 required_fields = ['tenant_name', 'enabled', 'description']
Tony Mack7823b892013-03-29 20:35:26 -040016 for field in required_fields:
Tony Mack07ece3b2013-03-29 20:51:07 -040017 if field not in kwds:
Tony Mack7823b892013-03-29 20:35:26 -040018 raise Exception, "Must specify %s" % field
Tony Mack01942f52013-03-26 14:26:40 -040019
Tony Mackb3cefca2013-04-02 10:25:10 -040020 tenants = self.shell.keystone.tenants.findall(name=kwds['tenant_name'])
Tony Mack7823b892013-03-29 20:35:26 -040021 if not tenants:
22 tenant = self.shell.keystone.tenants.create(**kwds)
23 else:
24 tenant = tenants[0]
25 return tenant
26
27 def update_tenant(self, id, **kwds):
28 return self.shell.keystone.tenants.update(self.id, **kwds)
29
30 def delete_tenant(self, id):
31 tenant = self.shell.keystone.tenants.find(id=id)
32 return self.shell.keystone.tenants.delete(tenant)
33
34
35 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 -040036 if not flavor:
37 flavor = self.config.nova_default_flavor
38 if not image:
39 image = self.config.nova_default_imave
40 if not security_group:
41 security_group = self.config.nova_default_security_group
42
43 authorized_keys = "\n".join(pubkeys)
44 files = {'/root/.ssh/authorized_keys': authorized_keys}
45
Tony Mack7823b892013-03-29 20:35:26 -040046 flavor_id = self.shell.nova.flavors.find(name=flavor)
47 images = self.shell.glance.get_images(name=image)
48 if not images:
49 raise Exception, "Image not found: " + image
50 image_id = images[0]['id']
51 hints = {}
52 if hostname:
53 hints['force_hosts']= hostname
54 server = self.shell.nova.servers.create(
55 name=name,
56 key_name = key_name,
57 flavor=flavor_id,
58 image=image_id,
59 security_group = security_group,
60 files=files,
61 scheduler_hints=hints)
62 return server
Tony Mack01942f52013-03-26 14:26:40 -040063
Tony Mack7823b892013-03-29 20:35:26 -040064 def destroy_instance(self, name, id=None):
65 args = {'name': name}
66 if id:
67 args['id'] = id
68 servers = self.shell.nova.servers.findall(**args)
Tony Mack01942f52013-03-26 14:26:40 -040069 for server in servers:
Tony Mack7823b892013-03-29 20:35:26 -040070 if name == server.name:
Tony Mack07ece3b2013-03-29 20:51:07 -040071 if not id or id == server.id:
Tony Mack7823b892013-03-29 20:35:26 -040072 self.shell.nova.servers.delete(server)