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 | |
| 4 | class Manager: |
| 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 | |
| 13 | |
| 14 | def spawn_instances(self, name, key_name, hostnames=[], flavor=None, image=None, security_group=None, pubkeys=[]): |
| 15 | if not flavor: |
| 16 | flavor = self.config.nova_default_flavor |
| 17 | if not image: |
| 18 | image = self.config.nova_default_imave |
| 19 | if not security_group: |
| 20 | security_group = self.config.nova_default_security_group |
| 21 | |
| 22 | authorized_keys = "\n".join(pubkeys) |
| 23 | files = {'/root/.ssh/authorized_keys': authorized_keys} |
| 24 | |
| 25 | for hostname in hostnames: |
| 26 | flavor_id = self.shell.nova.flavors.find(name=flavor) |
| 27 | images = self.shell.glance.get_images(name=image) |
| 28 | if not images: |
| 29 | raise Exception, "Image not found: " + image |
| 30 | image_id = images[0]['id'] |
| 31 | hints = {'force_hosts': hostname} |
| 32 | server = self.shell.nova.servers.create( |
| 33 | name=name, |
| 34 | key_name = key_name, |
| 35 | flavor=flavor_id, |
| 36 | image=image_id, |
| 37 | security_group = security_group, |
| 38 | files=files, |
| 39 | scheduler_hints=hints) |
| 40 | |
| 41 | def destroy_instances(self, name, hostnames=[]): |
| 42 | servers = self.shell.nova.servers.list() |
| 43 | for server in servers: |
| 44 | hostname = server._info['OS-EXT-SRV-ATTR:host'] |
| 45 | if name == server.name and hostname in hostnames: |
| 46 | self.shell.nova.servers.delete(server) |