def __init__(self, image_id, key_name, instance_class, availability_zone, security_groups=None, user_data='', id=None, env_type=None, tags={}): self.settings = import_settings(env_type) self._credentials = { "aws_access_key_id": self.settings['AWS_ACCESS_KEY_ID'], "aws_secret_access_key": self.settings['AWS_SECRET_ACCESS_KEY'], "region": EC2RegionInfo( name=self.settings['AWS_REGION'], endpoint=self.settings['EC2_ENDPOINT'], ) } self.creation_data = { 'image_id': image_id, 'key_name': key_name, 'security_groups': security_groups, 'user_data': user_data, 'instance_type': instance_class, 'placement': availability_zone } self.name = id self.instance = None self.tags = tags self.created = bool(self.instance)
def make_connection(credentials): """ A general function to connect cloud provider endpoing using EC2 API @param credentials: A dictionary containing ec2 specific parameters for connecting to the endpoint. @type credentials: dictionary @return A boto ec2 connection object """ url = credentials['ec2_url'] url_path = str() url_endpoint = url.split('/')[2] url_protocol = url.split('/')[0].split(':')[0] if url_protocol == "https": secure = True elif url_protocol == "http": secure = False if len(url.split(':')) > 2: url_port = url.split(':')[2].split('/')[0] url_path = url.split(url_port)[1] hs_region = EC2RegionInfo(name=credentials['ec2_region'], endpoint=url_endpoint) conn = EC2Connection(aws_access_key_id=credentials['ec2_access_key'], aws_secret_access_key=credentials['ec2_secret_key'], is_secure=secure, path=url_path, region=hs_region) return conn
def connect_cloud(): ''' This function uses ec2 API to connect to various cloud providers. Note that, it only connects to one cloud at a time. @type cred_dict: dictionary @param cred_dict: A dictionary containing access, secret and connection urls. @return: NULL @todo: add support for other cloud providers ''' global conn url = cred_dict['ec2_url'] url_endpoint = url.split('/')[2] url_port = url.split(':')[2].split('/')[0] url_path = url.split(url_port)[1] url_protocol = url.split(":")[0] provider = cloud_provider(url) # A default region is required by boto for initiating connection. region_var = EC2RegionInfo(name="tmp.hadoopstack", endpoint=url_endpoint) if provider == "openstack": if url_protocol == "http": conn = EC2Connection(cred_dict['ec2_access_key'], cred_dict['ec2_secret_key'], region=region_var, is_secure=False, path=url_path) print conn return
def __connections_setup(self, is_secure, boto_debug): """ Creates FCU, OSU and EIM connections if endpoints are configured :param is_secure: allow connection without SSL :type is_secure: bool :param boto_debug: debug level for boto :type boto_debug: int :raises OCBError: When connections can not be created because AK and SK are not set up in environment variable """ access_key_id, secret_access_key, endpoints, icu_conn = self.__load_config() if endpoints['fcu']: fcu_endpoint = EC2RegionInfo(endpoint=endpoints['fcu']) self.fcu = FCUConnection(access_key_id, secret_access_key, region=fcu_endpoint, is_secure=is_secure, debug=boto_debug) else: self.__logger.info('No FCU connection configured') self.fcu = None if endpoints['lbu']: lbu_endpoint = EC2RegionInfo(endpoint=endpoints['lbu']) self.lbu = ELBConnection(access_key_id, secret_access_key, region=lbu_endpoint, debug=boto_debug) else: self.__logger.info('No LBU connection configured') self.lbu = None if endpoints['eim']: self.eim = IAMConnection(access_key_id, secret_access_key, host=endpoints['eim'], debug=boto_debug) else: self.__logger.info('No EIM connection configured') self.eim = None if endpoints['osu']: self.osu = boto.connect_s3(access_key_id, secret_access_key, host=endpoints['osu'], calling_format=boto.s3.connection.ProtocolIndependentOrdinaryCallingFormat()) else: self.__logger.info('No OSU connection configured') self.osu = None if icu_conn['endpoint']: self.icu = self.__connect_icu(icu_conn['endpoint'], access_key_id, secret_access_key, icu_conn['login'], icu_conn['password']) else: self.__logger.info('No ICU connection configured') self.icu = None
def init_connection(conf): # Load general configuration file cnf = ConfigParser() try: cnf.read(conf) region_os = EC2RegionInfo(endpoint=cnf.get('outscale', 'endpoint')) ows = VPCConnection(cnf.get('outscale', 'access_key'), cnf.get('outscale', 'secret_key'), region=region_os) return ows except Exception as e: print 'error in the configuration file: ', e return None
def make_connection(): url = config.EC2_URL url_endpoint = url.split('/')[2] url_port = url.split(':')[2].split('/')[0] url_path = url.split(url_port)[1] hs_region = EC2RegionInfo(name="siel", endpoint=url_endpoint) conn = EC2Connection(aws_access_key_id=config.EC2_ACCESS_KEY, aws_secret_access_key=config.EC2_SECRET_KEY, is_secure=False, path=url_path, region=hs_region) return conn
def connect(zone, url, secret_key, access_key): """ It is a function creates a connection with the boto library for communicate with instances. You need of more informations like: - API url - Access name - Region - Access key """ try: region = EC2RegionInfo(name=zone, endpoint=url) conn = boto.vpc.VPCConnection(region=region, aws_access_key_id=access_key, aws_secret_access_key=secret_key) except Exception as ex: print ex return conn
import boto from boto.ec2.regioninfo import EC2RegionInfo from boto.ec2.connection import EC2Connection from fabric.api import * import fabric from time import sleep import socket siel_cloud_region = EC2RegionInfo(name="siel", endpoint="10.2.4.129:8773") print siel_cloud_region conn = EC2Connection("trial123", "trial123", region=siel_cloud_region, is_secure=False, path='services/Cloud') res_id = conn.run_instances('ami-00000024', 1, 1, 'demo', ['default'], instance_type='m1.tiny') instance = res_id.instances[0] print "Instance booted", instance.id while res_id.instances[0].state == "pending": for res in conn.get_all_instances(): if res.id == res_id.id: res_id = res sleep(1)