1 from plstackapi.planetstack.config import Config
2 from plstackapi.openstack.shell import OpenStackShell
6 def __init__(self, config = None):
8 self.config = Config(config)
10 self.config = Config()
11 self.shell = OpenStackShell()
14 def spawn_instances(self, name, key_name, hostnames=[], flavor=None, image=None, security_group=None, pubkeys=[]):
16 flavor = self.config.nova_default_flavor
18 image = self.config.nova_default_imave
19 if not security_group:
20 security_group = self.config.nova_default_security_group
22 authorized_keys = "\n".join(pubkeys)
23 files = {'/root/.ssh/authorized_keys': authorized_keys}
25 for hostname in hostnames:
26 flavor_id = self.shell.nova.flavors.find(name=flavor)
27 images = self.shell.glance.get_images(name=image)
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(
37 security_group = security_group,
39 scheduler_hints=hints)
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)