Python boto.connect_ec2() Examples

The following are 14 code examples of boto.connect_ec2(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module boto , or try the search function .
Example #1
Source File: aws_resource_locator.py    From Elasticd with Apache License 2.0 6 votes vote down vote up
def get_resources(self):
        ResourceLocator.get_resources(self)
        ec2 = boto.connect_ec2()
        #search for the backend servers

        # Build set of filters for instance lookup
        filterDict = {}

        # Add tag filters from config
        for ignore, kvmap in self._dataMap['aws_tags'].items():
            print kvmap['key'] + "=>" + kvmap['value']
            tagKey = "tag:" + kvmap['key']
            filterDict[tagKey] = kvmap['value']

        # Add instance state filter
        filterDict['instance-state-name'] = 'running'
        reservations = ec2.get_all_instances(filters = filterDict)
        instances = [i for reservation in reservations for i in reservation.instances]
        backends = []
        for server in instances:
            ip_address = IPResource(server.private_ip_address)
            backends.append(ip_address)

        return backends 
Example #2
Source File: bs.py    From aws-extender with MIT License 6 votes vote down vote up
def do_start(self):
        ami_id = self.sd.get('ami_id')
        instance_type = self.sd.get('instance_type', 'm1.small')
        security_group = self.sd.get('security_group', 'default')
        if not ami_id:
            self.parser.error('ami_id option is required when starting the service')
        ec2 = boto.connect_ec2()
        if not self.sd.has_section('Credentials'):
            self.sd.add_section('Credentials')
            self.sd.set('Credentials', 'aws_access_key_id', ec2.aws_access_key_id)
            self.sd.set('Credentials', 'aws_secret_access_key', ec2.aws_secret_access_key)
        s = StringIO()
        self.sd.write(s)
        rs = ec2.get_all_images([ami_id])
        img = rs[0]
        r = img.run(user_data=s.getvalue(), key_name=self.options.keypair,
                    max_count=self.options.num_instances,
                    instance_type=instance_type,
                    security_groups=[security_group])
        print('Starting AMI: %s' % ami_id)
        print('Reservation %s contains the following instances:' % r.id)
        for i in r.instances:
            print('\t%s' % i.id) 
Example #3
Source File: bs.py    From canvas with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def do_start(self):
        ami_id = self.sd.get('ami_id')
        instance_type = self.sd.get('instance_type', 'm1.small')
        security_group = self.sd.get('security_group', 'default')
        if not ami_id:
            self.parser.error('ami_id option is required when starting the service')
        ec2 = boto.connect_ec2()
        if not self.sd.has_section('Credentials'):
            self.sd.add_section('Credentials')
            self.sd.set('Credentials', 'aws_access_key_id', ec2.aws_access_key_id)
            self.sd.set('Credentials', 'aws_secret_access_key', ec2.aws_secret_access_key)
        s = StringIO.StringIO()
        self.sd.write(s)
        rs = ec2.get_all_images([ami_id])
        img = rs[0]
        r = img.run(user_data=s.getvalue(), key_name=self.options.keypair,
                    max_count=self.options.num_instances,
                    instance_type=instance_type,
                    security_groups=[security_group])
        print 'Starting AMI: %s' % ami_id
        print 'Reservation %s contains the following instances:' % r.id
        for i in r.instances:
            print '\t%s' % i.id 
Example #4
Source File: environmentutil.py    From cloudformation-environmentbase with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_ami_map(self,
                    aws_region=None, image_names=None):
        """
        Method iterates on all AWS regions for a given set of AMI names to gather AMI IDs and
        to create a regionmap for CloudFormation templates.
        @param aws_region [string] - optionally provides the region to start querying when gathering the list of regions globally.
        """
        if aws_region is None:
            aws_region = self.configuration.get('boto', {}).get('region_name', 'us-east-1')
            logging.debug('Setting default AWS Region for API access from overall configuration [' + aws_region + ']')
        region_map = {}
        vpc_conn = boto.connect_vpc(aws_region)
        logging.debug('Connected to VPC in region [' + aws_region + ']')
        for region in vpc_conn.get_all_regions():
            if region.name not in region_map.keys():
                logging.debug('Adding region [' + region.name + '] to region map.')
                region_map[region.name] = {}
            ec2_conn = boto.connect_ec2(region.name)
            logging.debug('Connected to EC2 API in region [' + region.name + ']')
            for k, v in image_names:
                logging.debug('Looking for Image [' + k + ': ' + v + '] in region [' + region.name + ']')
                images = ec2_conn.get_all_images(filters={'name': v})
                if len(images) == 0:
                    logging.warn('No image found for [' + k + ': ' + v + '] in region [' + region.name + ']')
                elif len(images) > 1:
                    logging.warn('Found ' + str(len(images)) + ' images for [' + k + ': ' + v + '] in region [' + region.name + ']')
                else:
                    logging.debug('Adding image [' + images[0].id + '] to region [' + region.name + '] for key [' + k + ']')
                    region_map[region.name][k] = images[0].id
        logging.debug('AMI Region Map Contents: ' + json.dumps(region_map))
        return region_map 
Example #5
Source File: iam.py    From bcbio-nextgen-vm with MIT License 5 votes vote down vote up
def create_keypair(econfig_file=None, region=None, keyname="bcbio"):
    """Create a bcbio keypair and import to ec2. Gives us access to keypair locally and at AWS.
    """
    import boto
    import boto.ec2
    if econfig_file:
        keypair_dir = os.path.dirname(econfig_file).replace("elasticluster", "aws_keypairs")
    else:
        keypair_dir = os.path.join(os.getcwd(), "aws_keypairs")
    if not os.path.exists(keypair_dir):
        os.makedirs(keypair_dir)
    private_key = os.path.join(os.path.join(keypair_dir, keyname))
    new_key = not os.path.exists(private_key)
    if new_key:
        cmd = ["ssh-keygen", "-t", "rsa", "-N", "", "-f", private_key, "-C", "bcbio_aws_keypair"]
        subprocess.check_call(cmd)
    public_key = private_key + ".pub"
    if region:
        ec2 = boto.ec2.connect_to_region(region)
    else:
        ec2 = boto.connect_ec2()
    key = ec2.get_key_pair(keyname)
    if key and new_key:
        print("Non matching key %s found in AWS, removing." % keyname)
        ec2.delete_key_pair(keyname)
        key = None
    if not key:
        print("Key %s not found in AWS, importing created key" % keyname)
        with open(public_key) as in_handle:
            body = in_handle.read()
            try:
                ec2.import_key_pair(keyname, body)
            except TypeError as e:
                body = body.encode('utf-8')
                ec2.import_key_pair(keyname, body)
    return {"user_key_name": keyname, "user_key_private": private_key,
            "user_key_public": public_key} 
Example #6
Source File: copybot.py    From aws-extender with MIT License 5 votes vote down vote up
def main(self):
        fp = StringIO.StringIO()
        boto.config.dump_safe(fp)
        self.notify('%s (%s) Starting' % (self.name, self.instance_id), fp.getvalue())
        if self.src and self.dst:
            self.copy_keys()
        if self.dst:
            self.copy_log()
        self.notify('%s (%s) Stopping' % (self.name, self.instance_id),
                    'Copy Operation Complete')
        if boto.config.getbool(self.name, 'exit_on_completion', True):
            ec2 = boto.connect_ec2()
            ec2.terminate_instances([self.instance_id]) 
Example #7
Source File: ebs.py    From aws-extender with MIT License 5 votes vote down vote up
def attach(self):
        ec2 = boto.connect_ec2()
        if self.logical_volume_name:
            # if a logical volume was specified, override the specified volume_id
            # (if there was one) with the current AWS volume for the logical volume:
            logical_volume = next(Volume.find(name=self.logical_volume_name))
            self.volume_id = logical_volume._volume_id
        volume = ec2.get_all_volumes([self.volume_id])[0]
        # wait for the volume to be available. The volume may still be being created
        # from a snapshot.
        while volume.update() != 'available':
            boto.log.info('Volume %s not yet available. Current status = %s.' % (volume.id, volume.status))
            time.sleep(5)
        instance = ec2.get_only_instances([self.instance_id])[0]
        attempt_attach = True
        while attempt_attach:
            try:
                ec2.attach_volume(self.volume_id, self.instance_id, self.device)
                attempt_attach = False
            except EC2ResponseError as e:
                if e.error_code != 'IncorrectState':
                    # if there's an EC2ResonseError with the code set to IncorrectState, delay a bit for ec2
                    # to realize the instance is running, then try again. Otherwise, raise the error:
                    boto.log.info('Attempt to attach the EBS volume %s to this instance (%s) returned %s. Trying again in a bit.' % (self.volume_id, self.instance_id, e.errors))
                    time.sleep(2)
                else:
                    raise e
        boto.log.info('Attached volume %s to instance %s as device %s' % (self.volume_id, self.instance_id, self.device))
        # now wait for the volume device to appear
        while not os.path.exists(self.device):
            boto.log.info('%s still does not exist, waiting 2 seconds' % self.device)
            time.sleep(2) 
Example #8
Source File: service.py    From aws-extender with MIT License 5 votes vote down vote up
def shutdown(self):
        on_completion = self.sd.get('on_completion', 'shutdown')
        if on_completion == 'shutdown':
            if self.instance_id:
                time.sleep(60)
                c = boto.connect_ec2()
                c.terminate_instances([self.instance_id]) 
Example #9
Source File: server.py    From aws-extender with MIT License 5 votes vote down vote up
def ec2(self):
        if self._ec2 is None:
            self._ec2 = boto.connect_ec2()
        return self._ec2 
Example #10
Source File: launch.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def AwsConnect(configSpec,awsKey,awsSecret):
    if configSpec['Verbose']:
        logging.info("[AwsConnect]")

    targetRegion = None
    if 'Region' in configSpec:
        targetRegion = boto.ec2.get_region(configSpec['Region'])

    conn = boto.connect_ec2(awsKey,awsSecret,region = targetRegion)
    return conn 
Example #11
Source File: copybot.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def main(self):
        fp = StringIO.StringIO()
        boto.config.dump_safe(fp)
        self.notify('%s (%s) Starting' % (self.name, self.instance_id), fp.getvalue())
        if self.src and self.dst:
            self.copy_keys()
        if self.dst:
            self.copy_log()
        self.notify('%s (%s) Stopping' % (self.name, self.instance_id),
                    'Copy Operation Complete')
        if boto.config.getbool(self.name, 'exit_on_completion', True):
            ec2 = boto.connect_ec2()
            ec2.terminate_instances([self.instance_id]) 
Example #12
Source File: ebs.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def attach(self):
        ec2 = boto.connect_ec2()
        if self.logical_volume_name:
            # if a logical volume was specified, override the specified volume_id
            # (if there was one) with the current AWS volume for the logical volume:
            logical_volume = Volume.find(name = self.logical_volume_name).next()
            self.volume_id = logical_volume._volume_id
        volume = ec2.get_all_volumes([self.volume_id])[0]
        # wait for the volume to be available. The volume may still be being created
        # from a snapshot.
        while volume.update() != 'available':
            boto.log.info('Volume %s not yet available. Current status = %s.' % (volume.id, volume.status))
            time.sleep(5)
        instance = ec2.get_all_instances([self.instance_id])[0].instances[0]
        attempt_attach = True
        while attempt_attach:
            try:
                ec2.attach_volume(self.volume_id, self.instance_id, self.device)
                attempt_attach = False
            except EC2ResponseError, e:
                if e.error_code != 'IncorrectState':
                    # if there's an EC2ResonseError with the code set to IncorrectState, delay a bit for ec2 
                    # to realize the instance is running, then try again. Otherwise, raise the error:
                    boto.log.info('Attempt to attach the EBS volume %s to this instance (%s) returned %s. Trying again in a bit.' % (self.volume_id, self.instance_id, e.errors))
                    time.sleep(2)
                else:
                    raise e 
Example #13
Source File: service.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def shutdown(self):
        on_completion = self.sd.get('on_completion', 'shutdown')
        if on_completion == 'shutdown':
            if self.instance_id:
                time.sleep(60)
                c = boto.connect_ec2()
                c.terminate_instances([self.instance_id]) 
Example #14
Source File: server.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ec2(self):
        if self._ec2 is None:
            self._ec2 = boto.connect_ec2()
        return self._ec2