|
|
@ -6,8 +6,8 @@ from employ import commands |
|
|
|
|
|
|
|
|
class Manager(object): |
|
|
class Manager(object): |
|
|
def __init__( |
|
|
def __init__( |
|
|
self, ami_image_id, num_instances=1, instance_name="deployed", |
|
|
|
|
|
region="us-east-1", instance_type="t1.micro", |
|
|
|
|
|
|
|
|
self, ami_image_id="ami-da0cf8b3", num_instances=1, instance_name="deployed", |
|
|
|
|
|
region="us-east-1", instance_type="t1.micro", key_name=None, security_group=None |
|
|
): |
|
|
): |
|
|
self.instances = [] |
|
|
self.instances = [] |
|
|
self.ami_image_id = ami_image_id |
|
|
self.ami_image_id = ami_image_id |
|
|
@ -15,19 +15,9 @@ class Manager(object): |
|
|
self.instance_name = instance_name |
|
|
self.instance_name = instance_name |
|
|
self.region = region |
|
|
self.region = region |
|
|
self.instance_type = instance_type |
|
|
self.instance_type = instance_type |
|
|
|
|
|
|
|
|
def setup_instances(self): |
|
|
|
|
|
print "starting %s %s instances named %s in region %s" % ( |
|
|
|
|
|
self.num_instances, self.instance_type, self.instance_name, self.region |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
__enter__ = setup_instances |
|
|
|
|
|
|
|
|
|
|
|
def cleanup_instances(self): |
|
|
|
|
|
print "tearing down instances" |
|
|
|
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback): |
|
|
|
|
|
self.cleanup_instances() |
|
|
|
|
|
|
|
|
self.key_name = key_name |
|
|
|
|
|
self.security_groups = [security_group] if security_group else [] |
|
|
|
|
|
self._connection = None |
|
|
|
|
|
|
|
|
@classmethod |
|
|
@classmethod |
|
|
def from_config(cls, config): |
|
|
def from_config(cls, config): |
|
|
@ -50,6 +40,34 @@ class Manager(object): |
|
|
all_commands[command_cls.name] = command_cls |
|
|
all_commands[command_cls.name] = command_cls |
|
|
return all_commands |
|
|
return all_commands |
|
|
|
|
|
|
|
|
|
|
|
def connection(self): |
|
|
|
|
|
if not self._connection: |
|
|
|
|
|
logger.info("Connecting to EC2") |
|
|
|
|
|
self._connection = boto.ec2.connect_to_region(self.region) |
|
|
|
|
|
return self._connection |
|
|
|
|
|
|
|
|
|
|
|
def setup_instances(self): |
|
|
|
|
|
connection = self.connection() |
|
|
|
|
|
reservation = connection.run_instances( |
|
|
|
|
|
image_id=self.ami_image_id, |
|
|
|
|
|
min_count=self.num_instances, |
|
|
|
|
|
max_count=self.num_instances, |
|
|
|
|
|
instance_type=self.instance_type, |
|
|
|
|
|
key_name=self.key_name, |
|
|
|
|
|
security_groups=self.security_groups, |
|
|
|
|
|
) |
|
|
|
|
|
self.instances = reservation.instances |
|
|
|
|
|
|
|
|
|
|
|
__enter__ = setup_instances |
|
|
|
|
|
|
|
|
|
|
|
def cleanup_instances(self): |
|
|
|
|
|
instance_ids = [instance.id for instance in self.instances] |
|
|
|
|
|
connection = self.connection() |
|
|
|
|
|
connection.terminate_instances(instance_ids=instance_ids) |
|
|
|
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback): |
|
|
|
|
|
self.cleanup_instances() |
|
|
|
|
|
|
|
|
def setup(self, script): |
|
|
def setup(self, script): |
|
|
print "executing setup script: %s" % script |
|
|
print "executing setup script: %s" % script |
|
|
|
|
|
|
|
|
|