Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 1 | from plstackapi.planetstack.config import Config |
| 2 | from plstackapi.openstack.shell import OpenStackShell |
| 3 | |
Tony Mack | a033ced | 2013-03-29 17:57:06 -0400 | [diff] [blame] | 4 | class OpenStackDriver: |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 5 | |
| 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 Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 13 | def create_tenant(self, **kwds): |
Tony Mack | 5ee2190 | 2013-03-29 20:36:42 -0400 | [diff] [blame] | 14 | """Create keystone tenant. Suggested fields: name, description, enabled""" |
Tony Mack | 123096d | 2013-04-02 10:24:24 -0400 | [diff] [blame] | 15 | required_fields = ['tenant_name', 'enabled', 'description'] |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 16 | for field in required_fields: |
Tony Mack | 07ece3b | 2013-03-29 20:51:07 -0400 | [diff] [blame] | 17 | if field not in kwds: |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 18 | raise Exception, "Must specify %s" % field |
Tony Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 19 | |
Tony Mack | b3cefca | 2013-04-02 10:25:10 -0400 | [diff] [blame] | 20 | tenants = self.shell.keystone.tenants.findall(name=kwds['tenant_name']) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 21 | 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 Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 36 | 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 Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 46 | 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 Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 63 | |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 64 | 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 Mack | 01942f5 | 2013-03-26 14:26:40 -0400 | [diff] [blame] | 69 | for server in servers: |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 70 | if name == server.name: |
Tony Mack | 07ece3b | 2013-03-29 20:51:07 -0400 | [diff] [blame] | 71 | if not id or id == server.id: |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 72 | self.shell.nova.servers.delete(server) |