def _find_vpc_by_name(self, vpc_name): vpc_connection = boto.vpc.connect_to_region( self._region_name, aws_access_key_id=self._access_key, aws_secret_access_key=self._secret_key, is_secure=self._secure, host=self._ec2host, port=self._ec2port, path=self._ec2path, ) log.debug("VPC connection has been successful.") for vpc in vpc_connection.get_all_vpcs(): matches = [vpc.id] if 'Name' in vpc.tags: matches.append(vpc.tags['Name']) if vpc_name in matches: vpc_id = vpc.id if vpc_name != vpc_id: # then `vpc_name` is the VPC name log.debug("VPC `%s` has ID `%s`", vpc_name, vpc_id) break else: raise VpcError('Cannot find VPC `{0}`.'.format(vpc_name)) return (vpc_connection, vpc_id)
def _connect(self): """Connects to the ec2 cloud provider :return: :py:class:`boto.ec2.connection.EC2Connection` :raises: Generic exception on error """ # check for existing connection if self._ec2_connection: return self._ec2_connection if not self._vpc: vpc_connection = None try: log.debug("Connecting to EC2 endpoint %s", self._ec2host) # connect to webservice ec2_connection = self._region.connect() log.debug("EC2 connection has been successful.") if self._vpc: vpc_connection = boto.connect_vpc( aws_access_key_id=self._access_key, aws_secret_access_key=self._secret_key, is_secure=self._secure, host=self._ec2host, port=self._ec2port, path=self._ec2path, region=self._region) log.debug("VPC connection has been successful.") for vpc in vpc_connection.get_all_vpcs(): matches = [vpc.id] if 'Name' in vpc.tags: matches.append(vpc.tags['Name']) if self._vpc in matches: self._vpc_id = vpc.id if self._vpc != self._vpc_id: # then `self._vpc` is the VPC's name log.debug( "VPC `%s` has ID `%s`", self._vpc, self._vpc_id) break else: raise VpcError('Cannot find VPC `{0}`.'.format(self._vpc)) # list images to see if the connection works # images = self._ec2_connection.get_all_images() # log.debug("%d images found on cloud %s", # len(images), self._ec2host) except Exception as e: log.error("Error connecting to EC2: %s", e) raise self._ec2_connection, self._vpc_connection = ( ec2_connection, vpc_connection) return self._ec2_connection
def _connect(self): """Connects to the ec2 cloud provider :return: :py:class:`boto.ec2.connection.EC2Connection` :raises: Generic exception on error """ # check for existing connection if self._ec2_connection: return self._ec2_connection try: log.debug("Connecting to ec2 host %s", self._ec2host) region = ec2.regioninfo.RegionInfo(name=self._region_name, endpoint=self._ec2host) # connect to webservice ec2_connection = boto.connect_ec2( aws_access_key_id=self._access_key, aws_secret_access_key=self._secret_key, is_secure=self._secure, host=self._ec2host, port=self._ec2port, path=self._ec2path, region=region) log.debug("EC2 connection has been successful.") if self._vpc: vpc_connection = boto.connect_vpc( aws_access_key_id=self._access_key, aws_secret_access_key=self._secret_key, is_secure=self._secure, host=self._ec2host, port=self._ec2port, path=self._ec2path, region=region) log.debug("VPC connection has been successful.") for vpc in vpc_connection.get_all_vpcs(): log.debug("Checking whether %s matches %s/%s" % (self._vpc, vpc.tags['Name'], vpc.id)) if self._vpc in [vpc.tags['Name'], vpc.id]: self._vpc_id = vpc.id if self._vpc != self._vpc_id: log.debug("VPC %s matches %s" % (self._vpc, self._vpc_id)) break else: raise VpcError('VPC %s does not exist.' % self._vpc) # list images to see if the connection works # images = self._ec2_connection.get_all_images() # log.debug("%d images found on cloud %s", # len(images), self._ec2host) except Exception as e: log.error( "connection to ec2 could not be " "established: message=`%s`", str(e)) raise self._ec2_connection, self._vpc_connection = (ec2_connection, vpc_connection) return self._ec2_connection