def run(self): log.init_logging(2) conn = create_ec2_connection(hostname=self.hostname, path=self.path, port=self.port) if conn == None: raise DeploymentException, "AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are not set." print "Creating instance" reservation = conn.run_instances(self.base_ami, min_count=1, max_count=1, instance_type=self.instance_type, security_groups=self.security_groups, key_name=self.keypair) instance = reservation.instances[0] print "Instance %s created. Waiting for it to start..." % instance.id while True: try: newstate = instance.update() if newstate == "running": break time.sleep(2) except EC2ResponseError, ec2err: if ec2err.error_code == "InvalidInstanceID.NotFound": # If the instance was just created, this is a transient error. # We just have to wait until the instance appears. pass else: raise ec2err
def run(self): log.init_logging(2) conn = create_ec2_connection(hostname=self.hostname, path=self.path, port=self.port) print "Creating instance" reservation = conn.run_instances(self.base_ami, min_count=1, max_count=1, instance_type='m1.small', key_name=self.keypair) instance = reservation.instances[0] print "Instance %s created. Waiting for it to start..." % instance.id while instance.update() != "running": time.sleep(2) print "Instance running." print "Opening SSH connection." ssh = SSH(self.username, instance.public_dns_name, self.keyfile) ssh.open() print "Copying files" for src, dst in self.files: ssh.scp(src, dst) print "Removing private data and authorized keys" ssh.run("sudo find /root/.*history /home/*/.*history -exec rm -f {} \;", exception_on_error = False) ssh.run("sudo find / -name authorized_keys -exec rm -f {} \;", exception_on_error = False) # Apparently instance.stop() will terminate # the instance (this is a known bug), so we # use stop_instances instead. print "Stopping instance" conn.stop_instances([instance.id]) while instance.update() != "stopped": time.sleep(2) print "Instance stopped" print "Creating AMI" # Doesn't actually return AMI. Have to make it public manually. ami = conn.create_image(instance.id, self.ami_name, description=self.ami_name) if ami != None: print ami print "Cleaning up" print "Terminating instance" conn.terminate_instances([instance.id]) while instance.update() != "terminated": time.sleep(2) print "Instance terminated"
def __connect(self): config = self.instance.config try: log.debug("Connecting to EC2...") ec2_server_hostname = config.get("ec2-server-hostname") ec2_server_port = config.get("ec2-server-port") ec2_server_path = config.get("ec2-server-path") if ec2_server_hostname != None: self.conn = create_ec2_connection(ec2_server_hostname, ec2_server_path, ec2_server_port) else: self.conn = create_ec2_connection() if self.conn == None: raise DeploymentException, "AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are not set." log.debug("Connected to EC2.") except BotoClientError, exc: raise DeploymentException, "Could not connect to EC2. %s" % exc.reason
def run(self): log.init_logging(2) conn = create_ec2_connection(hostname=self.hostname, path=self.path, port=self.port) print "Creating instance" reservation = conn.run_instances(self.base_ami, min_count=1, max_count=1, instance_type='m1.small', key_name=self.keypair) instance = reservation.instances[0] print "Instance %s created. Waiting for it to start..." % instance.id while instance.update() != "running": time.sleep(2) print "Instance running" print self.username, instance.public_dns_name, self.keyfile ssh = SSH(self.username, instance.public_dns_name, self.keyfile, None, None) try: ssh.open() except Exception, e: print e.message exit(1)