Example #1
0
    def _get_conn(self):
        if self._conn:
            return self._conn

        required_env = [
            "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "EC2_URL"
        ]
        for env_key in required_env:
            if not os.environ.get(env_key):
                raise errors.CloudError("%r environment variable must be set" %
                                        env_key)

        try:
            parsed_url = urlparse.urlsplit(os.environ.get("EC2_URL"))
            host, port = parsed_url.netloc.split(':', 1)
            port = int(port)
        except (ValueError, AttributeError):
            raise errors.CloudError(
                "Failed to parse EC2_URL environmental variable")

        self._conn = boto.connect_euca(
            aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
            aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
            host=host,
            port=port,
            path=parsed_url.path)
        return self._conn
Example #2
0
    def get_instances_by_region(self, region):
        ''' Makes an AWS EC2 API call to the list of instances in a particular
        region '''

        try:
            if self.eucalyptus:
                conn = boto.connect_euca(host=self.eucalyptus_host)
                conn.APIVersion = '2010-08-31'
            else:
                conn = ec2.connect_to_region(region)

            # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
            if conn is None:
                print("region name: %s likely not supported, or AWS is down.  connection to region failed." % region)
                sys.exit(1)
 
            reservations = conn.get_all_instances()
            for reservation in reservations:
                for instance in reservation.instances:
                    self.add_instance(instance, region)
        
        except boto.exception.BotoServerError as e:
            if  not self.eucalyptus:
                print "Looks like AWS is down again:"
            print e
            sys.exit(1)
Example #3
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        ec2_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ec2.ini')
        ec2_ini_path = os.environ.get('EC2_INI_PATH', ec2_default_ini_path)
        config.read(ec2_ini_path)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Return all EC2/RDS instances
        if config.has_option('ec2', 'all_instances'):
            self.all_instances = config.getboolean('ec2', 'all_instances')
        else:
            self.all_instances = False
        if config.has_option('ec2', 'all_rds_instances'):
            self.all_rds_instances = config.getboolean('ec2', 'all_rds_instances')
        else:
            self.all_rds_instances = False

        # Cache related
        cache_dir = os.path.expanduser(config.get('ec2', 'cache_path'))

        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        self.cache_path_cache = cache_dir + "/ansible-ec2.cache"
        self.cache_path_index = cache_dir + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')
Example #4
0
    def _get_conn(self):
        if self._conn:
            return self._conn

        required_env = ["EUCA_ACCESS_KEY", "EUCA_SECRET_KEY", "EUCA_URL"]
        for env_key in required_env:
            if not os.environ.get(env_key):
                raise errors.CloudError("%r environment variable must be set" %
                                        env_key)

        try:
            parsed_url = urlparse.urlsplit(os.environ.get("EUCA_URL"))
            host, port = parsed_url.netloc.split(':', 1)
            port = int(port)
        except (ValueError, AttributeError):
            raise errors.CloudError(
                "Failed to parse EUCA_URL environmental variable")

        self._conn = boto.connect_euca(
            aws_access_key_id=os.environ['EUCA_ACCESS_KEY'],
            aws_secret_access_key=os.environ['EUCA_SECRET_KEY'],
            host=host,
            port=port,
            path='/services/Eucalyptus',
            api_version=EUCALYPTUS_API_VERSION)
        return self._conn
    def get_instances_by_region(self, region):
        """ Makes an AWS EC2 API call to the list of instances in a particular
        region """

        try:
            if self.eucalyptus:
                conn = boto.connect_euca(host=self.eucalyptus_host)
                conn.APIVersion = "2010-08-31"
            else:
                conn = ec2.connect_to_region(region)

            # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
            if conn is None:
                print ("region name: %s likely not supported, or AWS is down.  connection to region failed." % region)
                sys.exit(1)

            reservations = []
            if self.ec2_instance_filters:
                for filter_key, filter_values in self.ec2_instance_filters.iteritems():
                    reservations.extend(conn.get_all_instances(filters={filter_key: filter_values}))
            else:
                reservations = conn.get_all_instances()

            for reservation in reservations:
                for instance in reservation.instances:
                    self.add_instance(instance, region)

        except boto.exception.BotoServerError, e:
            if not self.eucalyptus:
                print "Looks like AWS is down again:"
            print e
            sys.exit(1)
Example #6
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        config.read(os.path.dirname(os.path.realpath(__file__)) + '/ec2.ini')

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Cache related
        cache_path = config.get('ec2', 'cache_path')
        self.cache_path_cache = cache_path + "/ansible-ec2.cache"
        self.cache_path_index = cache_path + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')
Example #7
0
    def get_instances_by_region(self, region):
        ''' Makes an AWS EC2 API call to the list of instances in a particular
        region '''

        try:
            if self.eucalyptus:
                conn = boto.connect_euca(host=self.eucalyptus_host)
                conn.APIVersion = '2010-08-31'
            else:
                conn = ec2.connect_to_region(region)

            # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
            if conn is None:
                print(
                    "region name: %s likely not supported, or AWS is down.  connection to region failed."
                    % region)
                sys.exit(1)

            reservations = conn.get_all_instances()
            for reservation in reservations:
                for instance in reservation.instances:
                    self.add_instance(instance, region)

        except boto.exception.BotoServerError as e:
            if not self.eucalyptus:
                print "Looks like AWS is down again:"
            print e
            sys.exit(1)
Example #8
0
  def open_connection(self, parameters):
    """
    Initialize a connection to the back-end Eucalyptus APIs.

    Args:
      parameters  A dictionary containing the 'credentials' parameter

    Returns:
      An instance of Boto EC2Connection
    """
    credentials = parameters[self.PARAM_CREDENTIALS]
    access_key = str(credentials['EC2_ACCESS_KEY'])
    secret_key = str(credentials['EC2_SECRET_KEY'])
    ec2_url = str(credentials['EC2_URL'])
    result = urlparse(ec2_url)
    if result.port is not None:
      port = result.port
    elif result.scheme == 'http':
      port = 80
    elif result.scheme == 'https':
      port = 443
    else:
      self.handle_failure('Unknown scheme in EC2_URL: ' + result.scheme)
      return None

    return boto.connect_euca(host=result.hostname,
      aws_access_key_id=access_key,
      aws_secret_access_key=secret_key,
      port=port,
      path=result.path,
      is_secure=(result.scheme == 'https'),
      api_version=self.EUCA_API_VERSION, debug=2)
Example #9
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = six.moves.configparser.SafeConfigParser()
        config.read(self.args.inifile)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(
                    boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2',
                                                   'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Cache related
        if 'EC2_CACHE_PATH' in os.environ:
            cache_path = os.environ['EC2_CACHE_PATH']
        elif self.args.cache_path:
            cache_path = self.args.cache_path
        else:
            cache_path = config.get('ec2', 'cache_path')
        if not os.path.exists(cache_path):
            os.makedirs(cache_path)

        if 'AWS_PROFILE' in os.environ:
            aws_profile = "{}-".format(os.environ.get('AWS_PROFILE'))
        else:
            aws_profile = ""

        self.cache_path_cache = cache_path + f"/{aws_profile}ansible-ec2.cache"
        self.cache_path_tags = cache_path + f"/{aws_profile}ansible-ec2.tags.cache"
        self.cache_path_index = cache_path + f"/{aws_profile}ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')
Example #10
0
    def connect(self):
        """Connection to the endpoint specified in the configuration file"""
        try:
            self.region = RegionInfo(name="eucalyptus", endpoint=self.conf.ec2_host)
            # trying connection to endpoint
            self.ec2conn = boto.connect_euca(host=self.conf.ec2_host,
                                             aws_access_key_id=self.conf.ec2_access_key_id,
                                             aws_secret_access_key=self.conf.ec2_secret_access_key,
                                             is_secure=False,
                                             port=int(self.conf.ec2_port),
                                             path=self.conf.ec2_path)

            #cf = OrdinaryCallingFormat()
            cf = SubdomainCallingFormat()

            self.s3conn = S3Connection(self.conf.ec2_access_key_id,
                                       self.conf.ec2_secret_access_key,
                                       host=self.conf.s3_host,
                                       port=int(self.conf.s3_port),
                                       path=self.conf.s3_path,
                                       is_secure=False,
                                       calling_format=cf)

            print("Connection successfully established")
            print("Connection successfully established (s3conn): " + self.s3conn.host)
        except Exception as e:
            print("Connection error({0})".format(e.message))
Example #11
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        config.read(os.path.dirname(os.path.realpath(__file__)) + '/ec2.ini')

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Cache related
        cache_path = config.get('ec2', 'cache_path')
        self.cache_path_cache = cache_path + "/ansible-ec2.cache"
        self.cache_path_index = cache_path + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        ec2_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ec2.ini')
        ec2_ini_path = os.environ.get('EC2_INI_PATH', ec2_default_ini_path)
        config.read(ec2_ini_path)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Return all EC2/RDS instances
        if config.has_option('ec2', 'all_instances'):
            self.all_instances = config.getboolean('ec2', 'all_instances')
        else:
            self.all_instances = False
        if config.has_option('ec2', 'all_rds_instances'):
            self.all_rds_instances = config.getboolean('ec2', 'all_rds_instances')
        else:
            self.all_rds_instances = False

        # Cache related
        cache_dir = os.path.expanduser(config.get('ec2', 'cache_path'))
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        self.cache_path_cache = cache_dir + "/ansible-ec2.cache"
        self.cache_path_index = cache_dir + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')
Example #13
0
 def connect(self, region):
     ''' create connection to api server'''
     if self.eucalyptus:
         conn = boto.connect_euca(host=self.eucalyptus_host, **self.credentials)
         conn.APIVersion = '2010-08-31'
     else:
         conn = self.connect_to_aws(ec2, region)
     return conn
Example #14
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        config.read(self.args.inifile)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Cache related
        if 'EC2_CACHE_PATH' in os.environ:
            cache_path = os.environ['EC2_CACHE_PATH']
        elif self.args.cache_path:
            cache_path = self.args.cache_path
        else:
            cache_path = config.get('ec2', 'cache_path')
        if not os.path.exists(cache_path):
            os.makedirs(cache_path)

        if 'AWS_PROFILE' in os.environ:
            aws_profile = "{}-".format(os.environ.get('AWS_PROFILE'))
        else:
            aws_profile = ""

        self.cache_path_cache = cache_path + "/{}ansible-ec2.cache".format(aws_profile)
        self.cache_path_tags = cache_path + "/{}ansible-ec2.tags.cache".format(aws_profile)
        self.cache_path_index = cache_path + "/{}ansible-ec2.index".format(aws_profile)
        self.cache_max_age = config.getint('ec2', 'cache_max_age')
Example #15
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        ec2_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ec2.ini')
        ec2_ini_path = os.environ.get('EC2_INI_PATH', ec2_default_ini_path)
        config.read(ec2_ini_path)

        boto_aws_config_file = config.get('ec2', 'boto_aws_config')
        # hack to read aws credentials from a local ini file
        if os.path.isfile(boto_aws_config_file):
            creds = ConfigParser.SafeConfigParser()
            creds.read(boto_aws_config_file)
            os.environ['AWS_ACCESS_KEY_ID'] = creds.get('aws', 'access_key_id')
            os.environ['AWS_SECRET_ACCESS_KEY'] = creds.get('aws', 'secret_access_key')

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Cache related
        cache_dir = os.path.expanduser(config.get('ec2', 'cache_path'))
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        self.cache_path_cache = cache_dir + "/ansible-ec2.cache"
        self.cache_path_index = cache_dir + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')
Example #16
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        ec2_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ec2.ini')
        ec2_ini_path = os.environ.get('EC2_INI_PATH', ec2_default_ini_path)
        config.read(ec2_ini_path)

        # Handle multiple AWS accounts
        self.aws_creds = {}
        for i in config.sections():
            if i != 'ec2':
                auth = {'key': config.get(i, 'key'),
                        'secret': config.get(i, 'secret')
                       }
                self.aws_creds[i] = auth

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Cache related
        self.cache_dir = os.path.expanduser(config.get('ec2', 'cache_path'))
        if not os.path.exists(self.cache_dir):
            os.makedirs(self.cache_dir)

        self.cache_max_age = config.getint('ec2', 'cache_max_age')
Example #17
0
 def connect(self, region):
     ''' create connection to api server'''
     if self.eucalyptus:
         conn = boto.connect_euca(host=self.eucalyptus_host)
         conn.APIVersion = '2010-08-31'
     else:
         conn = ec2.connect_to_region(region)
     # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
     if conn is None:
         self.fail_with_error("region name: %s likely not supported, or AWS is down.  connection to region failed." % region)
     return conn
Example #18
0
    def get_instance(self, region, instance_id):
        ''' Gets details about a specific instance '''
        if self.eucalyptus:
            conn = boto.connect_euca(self.eucalyptus_host)
            conn.APIVersion = '2010-08-31'
        else:
            conn = ec2.connect_to_region(region)

        reservations = conn.get_all_instances([instance_id])
        for reservation in reservations:
            for instance in reservation.instances:
                return instance
Example #19
0
    def get_instance(self, region, instance_id):
        ''' Gets details about a specific instance '''
        if self.eucalyptus:
            conn = boto.connect_euca(self.eucalyptus_host)
            conn.APIVersion = '2010-08-31'
        else:
            conn = ec2.connect_to_region(region)

        reservations = conn.get_all_instances([instance_id])
        for reservation in reservations:
            for instance in reservation.instances:
                return instance
Example #20
0
    def get_instances_by_region(self, region):
        ''' Makes an AWS EC2 API call to the list of instances in a particular
        region '''

        if self.eucalyptus:
            conn = boto.connect_euca(host=self.eucalyptus_host)
            conn.APIVersion = '2010-08-31'
        else:
            conn = ec2.connect_to_region(region)

        reservations = conn.get_all_instances()
        for reservation in reservations:
            for instance in reservation.instances:
                self.add_instance(instance, region)
Example #21
0
File: ec2.py Project: svend/ansible
    def get_instances_by_region(self, region):
        ''' Makes an AWS EC2 API call to the list of instances in a particular
        region '''

        if self.eucalyptus:
            conn = boto.connect_euca(host=self.eucalyptus_host)
            conn.APIVersion = '2010-08-31'
        else:
            conn = ec2.connect_to_region(region)

        reservations = conn.get_all_instances()
        for reservation in reservations:
            for instance in reservation.instances:
                self.add_instance(instance, region)
Example #22
0
    def connect(self, region, assumedRoleObject):
        ''' create connection to api server'''
        if self.eucalyptus:
            conn = boto.connect_euca(host=self.eucalyptus_host)
            conn.APIVersion = '2010-08-31'
        else:
            conn = ec2.connect_to_region(region,
                            aws_access_key_id=assumedRoleObject.credentials.access_key,
                            aws_secret_access_key=assumedRoleObject.credentials.secret_key,
                            security_token=assumedRoleObject.credentials.session_token)

        # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
        if conn is None:
            self.fail_with_error("region name: %s likely not supported, or AWS is down.  connection to region failed." % region)
        return conn
Example #23
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        config.read(self.args.inifile)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = os.getenv('AWS_REGION')
        if configRegions is None:
            configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(
                    boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.backup_destination_variable = config.get(
            'ec2', 'backup_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Cache related
        cache_path = config.get('ec2', 'cache_path')
        self.cache_path_cache = cache_path + "/ansible-ec2.cache"
        self.cache_path_index = cache_path + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')
Example #24
0
    def get_instance(self, region, instance_id):
        ''' Gets details about a specific instance '''
        if self.eucalyptus:
            conn = boto.connect_euca(self.eucalyptus_host)
            conn.APIVersion = '2010-08-31'
        conn = ec2.connect_to_region(region)

        # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
        if conn is None:
            print("region name: %s likely not supported, or AWS is down.  connection to region failed." % region)
            sys.exit(1)

        reservations = conn.get_all_instances([instance_id])
        for reservation in reservations:
            for instance in reservation.instances:
                return instance
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        config.read(self.args.inifile)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = os.getenv('AWS_REGION')
        if configRegions is None:
            configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.backup_destination_variable = config.get('ec2', 'backup_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Cache related
        cache_path = config.get('ec2', 'cache_path')
        self.cache_path_cache = cache_path + "/ansible-ec2.cache"
        self.cache_path_index = cache_path + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')
Example #26
0
    def get_instance(self, region, instance_id):
        ''' Gets details about a specific instance '''
        if self.eucalyptus:
            conn = boto.connect_euca(self.eucalyptus_host)
            conn.APIVersion = '2010-08-31'
        else:
            conn = ec2.connect_to_region(region)

        # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
        if conn is None:
            print("region name: %s likely not supported, or AWS is down.  connection to region failed." % region)
            sys.exit(1)

        reservations = conn.get_all_instances([instance_id])
        for reservation in reservations:
            for instance in reservation.instances:
                return instance
    def read_settings(self):
        """ Reads the settings from the ec2.ini file """

        config = ConfigParser.SafeConfigParser()
        config.read(self.args.inifile)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option("ec2", "eucalyptus"):
            self.eucalyptus = config.getboolean("ec2", "eucalyptus")
        if self.eucalyptus and config.has_option("ec2", "eucalyptus_host"):
            self.eucalyptus_host = config.get("ec2", "eucalyptus_host")

        # Regions
        self.regions = []
        configRegions = os.getenv("AWS_REGION")
        if configRegions is None:
            configRegions = config.get("ec2", "regions")
        configRegions_exclude = config.get("ec2", "regions_exclude")
        if configRegions == "all":
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get("ec2", "destination_variable")
        self.backup_destination_variable = config.get("ec2", "backup_destination_variable")

        # Route53
        self.route53_enabled = config.getboolean("ec2", "route53")
        self.route53_excluded_zones = []
        if config.has_option("ec2", "route53_excluded_zones"):
            self.route53_excluded_zones.extend(config.get("ec2", "route53_excluded_zones", "").split(","))

        # Cache related
        cache_path = config.get("ec2", "cache_path")
        self.cache_path_cache = cache_path + "/ansible-ec2.cache"
        self.cache_path_index = cache_path + "/ansible-ec2.index"
        self.cache_max_age = config.getint("ec2", "cache_max_age")
Example #28
0
    def get_instance(self, region, instance_id):
        ''' Gets details about a specific instance '''
        if self.eucalyptus:
            conn = boto.connect_euca(self.eucalyptus_host)
            conn.APIVersion = '2010-08-31'
        else:
        # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
        if conn is None:
            print("region name: %s likely not supported, or AWS is down.  connection to region failed." % region)
            sys.exit(1)

        reservations = conn.get_all_instances([instance_id])
        for reservation in reservations:
            for instance in reservation.instances:
                return instance

    def add_instance(self, instance, region):
        ''' Adds an instance to the inventory and index, as long as it is
        addressable '''

        # Only want running instances unless all_instances is True
        if not self.all_instances and instance.state != 'running':
            return

        else:
        # Select the best destination address
        if instance.subnet_id:
            dest = getattr(instance, self.vpc_destination_variable)
        if not dest:
            # Skip instances we cannot address (e.g. private VPC subnet)
            return

        # Add to index
        if self.nested_groups:
            self.push_group(self.inventory, 'regions', region)
        self.index[dest] = [region, instance.id]
        # Inventory: Group by instance ID (always a group of 1)
        type_name = self.to_safe('type_' + instance.instance_type)
<<<<<<< REMOTE
self.push(self.inventory, type_name, dest)
=======
Example #29
0
    def get_instances_by_region(self, region):
        ''' Makes an AWS EC2 API call to the list of instances in a particular
        region '''

        try:
            if self.eucalyptus:
                conn = boto.connect_euca(host=self.eucalyptus_host)
                conn.APIVersion = '2010-08-31'
            else:
                conn = ec2.connect_to_region(region)

            reservations = conn.get_all_instances()
            for reservation in reservations:
                for instance in reservation.instances:
                    self.add_instance(instance, region)

        except boto.exception.BotoServerError as e:
            if not self.eucalyptus:
                print "Looks like AWS is down again:"
            print e
            sys.exit(1)
Example #30
0
def get_instance(region, instance_id):
    """
    Get details about a specific instance

    """

    if EUCALYPTUS:
        conn = boto.connect_euca(EUCALYPTUS_HOST)
        conn.APIVersion = '2010-08-31'
    else:
        conn = ec2.connect_to_region(region)

    # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
    if conn is None:
        print("region name: %s likely not supported, or AWS is down.  connection to region failed." % region)
        sys.exit(1)

    reservations = conn.get_all_instances([instance_id])
    for reservation in reservations:
        for instance in reservation.instances:
            return instance
Example #31
0
    def get_instances_by_region(self, region):
        ''' Makes an AWS EC2 API call to the list of instances in a particular
        region '''

        try:
            if self.eucalyptus:
                conn = boto.connect_euca(host=self.eucalyptus_host)
                conn.APIVersion = '2010-08-31'
            else:
                conn = ec2.connect_to_region(region)
            
            reservations = conn.get_all_instances()
            for reservation in reservations:
                for instance in reservation.instances:
                    self.add_instance(instance, region)
        
        except boto.exception.BotoServerError as e:
            if  not self.eucalyptus:
                print "Looks like AWS is down again:"
            print e
            sys.exit(1)
Example #32
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        ec2_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ec2.ini')
        ec2_ini_path = os.environ.get('EC2_INI_PATH', ec2_default_ini_path)
        config.read(ec2_ini_path)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))
Example #33
0
    def get_instances_by_region(self, region):
        ''' Makes an AWS EC2 API call to the list of instances in a particular
        region '''

        try:
            if self.eucalyptus:
                conn = boto.connect_euca(host=self.eucalyptus_host)
                conn.APIVersion = '2010-08-31'
            else:
                conn = ec2.connect_to_region(region)

            # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
            if conn is None:
                print(
                    "region name: %s likely not supported, or AWS is down.  connection to region failed."
                    % region)
                sys.exit(1)

            reservations = []
            if self.ec2_instance_filters:
                for filter_key, filter_values in self.ec2_instance_filters.iteritems(
                ):
                    reservations.extend(
                        conn.get_all_instances(
                            filters={filter_key: filter_values}))
            else:
                reservations = conn.get_all_instances()

            for reservation in reservations:
                for instance in reservation.instances:
                    self.add_instance(instance, region)
            #self.inventory["_meta"]["hostvars"]["self"]=["127.0.0.1"]
            self.inventory["seed"] = ["127.0.0.1"]

        except boto.exception.BotoServerError, e:
            if not self.eucalyptus:
                print "Looks like AWS is down again:"
            print e
            sys.exit(1)
Example #34
0
def get_instances_by_region(region, inventory, index, records=None):
    """ 
    Makes an AWS EC2 API call to the list of instances in a particular
    region 

    """

    if EUCALYPTUS:
        conn = boto.connect_euca(host=EUCALYPTUS_HOST)
        conn.APIVersion = '2010-08-31'
    else:
        conn = ec2.connect_to_region(region)

    # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
    if conn is None:
        print("Region name: %s likely not supported, or AWS is down.  Connection to region failed." % region)
        sys.exit(1)

    reservations = conn.get_all_instances()
    for reservation in reservations:
        for instance in reservation.instances:
            add_instance(instance, region, inventory, index)
Example #35
0
    def _get_conn(self):
        if self._conn:
            return self._conn

        required_env = ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "EC2_URL"]
        for env_key in required_env:
            if not os.environ.get(env_key):
                raise errors.CloudError("%r environment variable must be set"
                                        % env_key)

        try:
            parsed_url = urlsplit(os.environ.get("EC2_URL"))
            host, port = parsed_url.netloc.split(':', 1)  # pylint: disable=E1103
            port = int(port)
        except (ValueError, AttributeError):
            raise errors.CloudError("Failed to parse EC2_URL environmental variable")

        self._conn = boto.connect_euca(aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'], aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
                                       host=host,
                                       port=port,
                                       path=parsed_url.path)  # pylint: disable=E1103
        return self._conn
Example #36
0
    def _get_conn(self):
        if self._conn:
            return self._conn

        required_env = ["EUCA_ACCESS_KEY", "EUCA_SECRET_KEY", "EUCA_URL"]
        for env_key in required_env:
            if not os.environ.get(env_key):
                raise errors.CloudError("%r environment variable must be set"
                                        % env_key)

        try:
            parsed_url = urlparse.urlsplit(os.environ.get("EUCA_URL"))
            host, port = parsed_url.netloc.split(':', 1)
            port = int(port)
        except (ValueError, AttributeError):
            raise errors.CloudError("Failed to parse EUCA_URL environmental variable")

        self._conn = boto.connect_euca(aws_access_key_id=os.environ['EUCA_ACCESS_KEY'], aws_secret_access_key=os.environ['EUCA_SECRET_KEY'],
                                       host=host,
                                       port=port,
                                       path='/services/Eucalyptus',
                                       api_version=EUCALYPTUS_API_VERSION)
        return self._conn
Example #37
0
EUCALYPTUS = False
if config.has_option('ec2', 'eucalyptus'):
    EUCALYPTUS = config.getboolean('ec2', 'eucalyptus')
if EUCALYPTUS and config.has_option('ec2', 'eucalyptus_host'):
    EUCALYPTUS_HOST = config.get('ec2', 'eucalyptus_host')

# AWS regions to inventory
REGIONS = []
if config.has_option('ec2', 'regions'):
    config_regions = config.get('ec2', 'regions').strip()
else:
    config_regions = 'all'

if (config_regions == 'all'):
    if EUCALYPTUS_HOST:
        REGIONS.append(boto.connect_euca(host=EUCALYPTUS_HOST).region.name)
    else:
        for region_info in ec2.regions():
            REGIONS.append(region_info.name)
else:
    REGIONS = re.split(", |,|\s", config_regions)

# The name of the ec2 instance variable containing the address that Ansible
# should use to SSH into that instance. Generally you want to use the
# public_dns_name for instances out in the main cloud. Since VPC instances
# generally don't *have* a public_dns_name, use the private_dns_name.
HOST_ADDRESS_VARIABLE = config.get('ec2', 'host_address_variable', 'public_dns_name')
VPC_HOST_ADDRESS_VARIABLE = config.get('ec2', 'vpc_host_address_variable', 'private_dns_name')

# Whether to use Route53 to get host names for instances
# from your zones. If this is enabled, you'll be able 
Example #38
0
    def main(self, **args):
        self.process_args()
        self.cert_path = os.environ['EC2_CERT']
        self.private_key_path = os.environ['EC2_PRIVATE_KEY']
        self.user = os.environ['EC2_USER_ID']
        self.ec2cert_path = os.environ['EUCALYPTUS_CERT']

        # tarball and image option are mutually exclusive
        if (not (self.cli_options.image_name)
                and not (self.cli_options.tarball)):
            print "Error: one of -i or -t must be specified"
            sys.exit(-1)

        if (self.cli_options.image_name and self.cli_options.tarball):
            print "Error: -i and -t cannot be specified together"
            sys.exit(-1)

        if (self.cli_options.tarball and \
            (not(self.cli_options.description) or not(self.cli_options.architecture))):
            print "Error: when -t is specified, -s and -a are required"
            sys.exit(-1)

        if (self.cli_options.architecture and \
            not(self.cli_options.architecture == 'i386' or self.cli_options.architecture == 'x86_64')):
            print "Error: architecture must be either 'i386' or 'x86_64'"
            sys.exit(-1)

        if (self.cli_options.kernel and not(self.cli_options.ramdisk)) or \
           (not(self.cli_options.kernel) and self.cli_options.ramdisk):
            print "Error: kernel and ramdisk must both be overridden"
            sys.exit(-1)

        if (self.cli_options.architecture and self.cli_options.image_name):
            print "Warning: you may be overriding the default architecture of this image!"

        euare_svc = EuareService()
        conn = boto.connect_iam(host=euare_svc.args['host'], \
                    aws_access_key_id=euare_svc.args['aws_access_key_id'],\
                    aws_secret_access_key=euare_svc.args['aws_secret_access_key'],\
                    port=euare_svc.args['port'], path=euare_svc.args['path'],\
                    is_secure=euare_svc.args['is_secure'])
        userinfo = conn.get_user().arn.split(':')
        if not (userinfo[4] == 'eucalyptus') and not (self.cli_options.kernel):
            print "Error: must be cloud admin to upload kernel/ramdisk. try specifying existing ones with --kernel and --ramdisk"
            sys.exit(-1)
        self.eustore_url = self.ServiceClass.StoreBaseURL

        # would be good of this were async, i.e. when the tarball is downloading
        ec2_conn = boto.connect_euca(host=euare_svc.args['host'], \
                        aws_access_key_id=euare_svc.args['aws_access_key_id'],\
                        aws_secret_access_key=euare_svc.args['aws_secret_access_key'])
        self.ImageList = ec2_conn.get_all_images()

        if os.environ.has_key('EUSTORE_URL'):
            self.eustore_url = os.environ['EUSTORE_URL']

        self.destination = "/tmp/"
        if self.cli_options.dir:
            self.destination = self.cli_options.dir
        if not (self.destination.endswith('/')):
            self.destination += '/'
        # for security, add random directory within to work in
        self.destination = tempfile.mkdtemp(prefix=self.destination) + '/'

        if self.cli_options.tarball:
            # local tarball path instead
            print "Installed image: " + self.bundleAll(
                self.cli_options.tarball, self.cli_options.prefix,
                self.cli_options.description, self.cli_options.architecture)
        else:
            catURL = self.eustore_url + "catalog"
            req = urllib2.Request(catURL,
                                  headers=self.ServiceClass.RequestHeaders)
            response = urllib2.urlopen(req).read()
            parsed_cat = json.loads(response)
            if len(parsed_cat) > 0:
                image_list = parsed_cat['images']
                image_found = False
                for image in image_list:
                    if image['name'].find(self.cli_options.image_name) > -1:
                        image_found = True
                        break
                if image_found:
                    # more param checking now
                    if image['single-kernel'] == 'True':
                        if self.cli_options.kernel_type:
                            print "The -k option will be ignored because the image is single-kernel"
                    else:
                        if not (self.cli_options.kernel_type):
                            print "Error: The -k option must be specified because this image has separate kernels"
                            sys.exit(-1)
                    print "Downloading Image : ", image['description']
                    imageURL = self.eustore_url + image['url']
                    req = urllib2.Request(
                        imageURL, headers=self.ServiceClass.RequestHeaders)
                    req = urllib2.urlopen(req)
                    file_size = int(req.info()['Content-Length']) / 1000
                    size_count = 0
                    prog_bar = euca2ools.commands.eustore.progressBar(
                        file_size)
                    BUF_SIZE = 128 * 1024
                    with open(self.destination + 'eucaimage.tar.gz',
                              'wb') as fp:
                        while True:
                            buf = req.read(BUF_SIZE)
                            size_count += len(buf)
                            prog_bar.update(size_count / 1000)
                            if not buf: break
                            fp.write(buf)
                    fp.close()
                    # validate download by re-computing serial # (name)
                    print "Checking image bundle"
                    file = open(fp.name, 'r')
                    m = hashlib.md5()
                    m.update(file.read())
                    hash = m.hexdigest()
                    crc = str(zlib.crc32(hash) & 0xffffffffL)
                    if image['name'] == crc.rjust(10, "0"):
                        print "Installed image: " + self.bundleAll(
                            fp.name, None, image['description'],
                            image['architecture'])
                    else:
                        print "Error: Downloaded image was incomplete or corrupt, please try again"
                    os.remove(fp.name)
                else:
                    print "Image name not found, please run eustore-describe-images"
Example #39
0
    def main(self, **args):
        self.process_args()
        self.cert_path = os.environ['EC2_CERT']
        self.private_key_path = os.environ['EC2_PRIVATE_KEY']
        self.user = os.environ['EC2_USER_ID']
        self.ec2cert_path = os.environ['EUCALYPTUS_CERT']

        # tarball and image option are mutually exclusive
        if (not(self.cli_options.image_name) and not(self.cli_options.tarball)):
            print >> sys.stderr, "Error: one of -i or -t must be specified"
            sys.exit(-1)

        if (self.cli_options.image_name and self.cli_options.tarball):
            print >> sys.stderr, "Error: -i and -t cannot be specified together"
            sys.exit(-1)

        if (self.cli_options.tarball and \
            (not(self.cli_options.description) or not(self.cli_options.architecture))):
            print >> sys.stderr, "Error: when -t is specified, -s and -a are required"
            sys.exit(-1)

        if (self.cli_options.architecture and \
            not(self.cli_options.architecture == 'i386' or self.cli_options.architecture == 'x86_64')):
            print >> sys.stderr, "Error: architecture must be either 'i386' or 'x86_64'"
            sys.exit(-1)

        if (self.cli_options.kernel and not(self.cli_options.ramdisk)) or \
           (not(self.cli_options.kernel) and self.cli_options.ramdisk):
            print >> sys.stderr, "Error: kernel and ramdisk must both be overridden"
            sys.exit(-1)

        if (self.cli_options.architecture and self.cli_options.image_name):
            print >> sys.stderr, "Warning: you may be overriding the default architecture of this image!"


        euare_svc = EuareService()
        conn = boto.connect_iam(host=euare_svc.args['host'], \
                    aws_access_key_id=euare_svc.args['aws_access_key_id'],\
                    aws_secret_access_key=euare_svc.args['aws_secret_access_key'],\
                    port=euare_svc.args['port'], path=euare_svc.args['path'],\
                    is_secure=euare_svc.args['is_secure'])
        userinfo  = conn.get_user().arn.split(':')
        if not(userinfo[4]=='eucalyptus') and not(self.cli_options.kernel):
            print >> sys.stderr, "Error: must be cloud admin to upload kernel/ramdisk. try specifying existing ones with --kernel and --ramdisk"
            sys.exit(-1)
        self.eustore_url = self.ServiceClass.StoreBaseURL

        # would be good of this were async, i.e. when the tarball is downloading
        ec2_conn = boto.connect_euca(host=euare_svc.args['host'], \
                        aws_access_key_id=euare_svc.args['aws_access_key_id'],\
                        aws_secret_access_key=euare_svc.args['aws_secret_access_key'])
        ec2_conn.APIVersion = '2012-03-01'
                        
        self.ImageList = ec2_conn.get_all_images()

        if os.environ.has_key('EUSTORE_URL'):
            self.eustore_url = os.environ['EUSTORE_URL']

        self.destination = "/tmp/"
        if self.cli_options.dir:
            self.destination = self.cli_options.dir
        if not(self.destination.endswith('/')):
            self.destination += '/'
        # for security, add random directory within to work in
        self.destination = tempfile.mkdtemp(prefix=self.destination)+'/'

        if self.cli_options.tarball:
            # local tarball path instead
            print "Installed image: "+self.bundleAll(self.cli_options.tarball, self.cli_options.prefix, self.cli_options.description, self.cli_options.architecture)
        else:
            catURL = self.eustore_url + "catalog"
            req = urllib2.Request(catURL, headers=self.ServiceClass.RequestHeaders)
            response = urllib2.urlopen(req).read()
            parsed_cat = json.loads(response)
            if len(parsed_cat) > 0:
                image_list = parsed_cat['images']
                image_found = False
                for image in image_list:
                    if image['name'].find(self.cli_options.image_name) > -1:
                        image_found = True
                        break
                if image_found:
                    # more param checking now
                    if image['single-kernel']=='True':
                        if self.cli_options.kernel_type:
                            print >> sys.stderr, "The -k option will be ignored because the image is single-kernel"
                    else:
                        if not(self.cli_options.kernel_type):
                            print >> sys.stderr, "Error: The -k option must be specified because this image has separate kernels"
                            sys.exit(-1)
                    print "Downloading Image : ",image['description']
                    imageURL = self.eustore_url+image['url']
                    req = urllib2.Request(imageURL, headers=self.ServiceClass.RequestHeaders)
                    req = urllib2.urlopen(req)
                    file_size = int(req.info()['Content-Length'])/1000
                    size_count = 0;
                    prog_bar = euca2ools.commands.eustore.progressBar(file_size)
                    BUF_SIZE = 128*1024
                    with open(self.destination+'eucaimage.tar.gz', 'wb') as fp:
                        while True:
                            buf = req.read(BUF_SIZE)
                            size_count += len(buf)
                            prog_bar.update(size_count/1000)
                            if not buf: break
                            fp.write(buf)
                    fp.close()
                    # validate download by re-computing serial # (name)
                    print "Checking image bundle"
                    file = open(fp.name, 'r')
                    m = hashlib.md5()
                    m.update(file.read())
                    hash = m.hexdigest()
                    crc = str(zlib.crc32(hash)& 0xffffffffL)
                    if image['name'] == crc.rjust(10,"0"):
                        print "Installed image: "+self.bundleAll(fp.name, None, image['description'], image['architecture'])
                    else:
                        print >> sys.stderr, "Error: Downloaded image was incomplete or corrupt, please try again"
                    os.remove(fp.name)
                else:
                    print >> sys.stderr, "Image name not found, please run eustore-describe-images"
Example #40
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        scriptbasename = __file__
        scriptbasename = os.path.basename(scriptbasename)
        scriptbasename = scriptbasename.replace('.py', '')

        defaults = {
            'ec2': {
                'ini_fallback': os.path.join(os.path.dirname(__file__), 'ec2.ini'),
                'ini_path': os.path.join(os.path.dirname(__file__), '%s.ini' % scriptbasename)
            }
        }

        if six.PY3:
            config = configparser.ConfigParser(DEFAULTS)
        else:
            config = configparser.SafeConfigParser(DEFAULTS)
        ec2_ini_path = os.environ.get('EC2_INI_PATH', defaults['ec2']['ini_path'])
        ec2_ini_path = os.path.expanduser(os.path.expandvars(ec2_ini_path))

        if not os.path.isfile(ec2_ini_path):
            ec2_ini_path = os.path.expanduser(defaults['ec2']['ini_fallback'])

        if os.path.isfile(ec2_ini_path):
            config.read(ec2_ini_path)

        # Add empty sections if they don't exist
        try:
            config.add_section('ec2')
        except configparser.DuplicateSectionError:
            pass

        try:
            config.add_section('credentials')
        except configparser.DuplicateSectionError:
            pass

        # is eucalyptus?
        self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name, **self.credentials)
            else:
                configRegions_exclude = config.get('ec2', 'regions_exclude')

                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")
        if 'auto' in self.regions:
            env_region = os.environ.get('AWS_REGION')
            if env_region is None:
                env_region = os.environ.get('AWS_DEFAULT_REGION')
            self.regions = [env_region]

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')
        self.hostname_variable = config.get('ec2', 'hostname_variable')

        if config.has_option('ec2', 'destination_format') and \
           config.has_option('ec2', 'destination_format_tags'):
            self.destination_format = config.get('ec2', 'destination_format')
            self.destination_format_tags = config.get('ec2', 'destination_format_tags').split(',')
        else:
            self.destination_format = None
            self.destination_format_tags = None

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_hostnames = config.get('ec2', 'route53_hostnames')

        self.route53_excluded_zones = []
        self.route53_excluded_zones = [a for a in config.get('ec2', 'route53_excluded_zones').split(',') if a]

        # Include RDS instances?
        self.rds_enabled = config.getboolean('ec2', 'rds')

        # Include RDS cluster instances?
        self.include_rds_clusters = config.getboolean('ec2', 'include_rds_clusters')

        # Include ElastiCache instances?
        self.elasticache_enabled = config.getboolean('ec2', 'elasticache')

        # Return all EC2 instances?
        self.all_instances = config.getboolean('ec2', 'all_instances')

        # Instance states to be gathered in inventory. Default is 'running'.
        # Setting 'all_instances' to 'yes' overrides this option.
        ec2_valid_instance_states = [
            'pending',
            'running',
            'shutting-down',
            'terminated',
            'stopping',
            'stopped'
        ]
        self.ec2_instance_states = []
        if self.all_instances:
            self.ec2_instance_states = ec2_valid_instance_states
        elif config.has_option('ec2', 'instance_states'):
            for instance_state in config.get('ec2', 'instance_states').split(','):
                instance_state = instance_state.strip()
                if instance_state not in ec2_valid_instance_states:
                    continue
                self.ec2_instance_states.append(instance_state)
        else:
            self.ec2_instance_states = ['running']

        # Return all RDS instances? (if RDS is enabled)
        self.all_rds_instances = config.getboolean('ec2', 'all_rds_instances')

        # Return all ElastiCache replication groups? (if ElastiCache is enabled)
        self.all_elasticache_replication_groups = config.getboolean('ec2', 'all_elasticache_replication_groups')

        # Return all ElastiCache clusters? (if ElastiCache is enabled)
        self.all_elasticache_clusters = config.getboolean('ec2', 'all_elasticache_clusters')

        # Return all ElastiCache nodes? (if ElastiCache is enabled)
        self.all_elasticache_nodes = config.getboolean('ec2', 'all_elasticache_nodes')

        # boto configuration profile (prefer CLI argument then environment variables then config file)
        self.boto_profile = self.args.boto_profile or \
            os.environ.get('AWS_PROFILE') or \
            config.get('ec2', 'boto_profile')

        # AWS credentials (prefer environment variables)
        if not (self.boto_profile or os.environ.get('AWS_ACCESS_KEY_ID') or
                os.environ.get('AWS_PROFILE')):

            aws_access_key_id = config.get('credentials', 'aws_access_key_id')
            aws_secret_access_key = config.get('credentials', 'aws_secret_access_key')
            aws_security_token = config.get('credentials', 'aws_security_token')

            if aws_access_key_id:
                self.credentials = {
                    'aws_access_key_id': aws_access_key_id,
                    'aws_secret_access_key': aws_secret_access_key
                }
                if aws_security_token:
                    self.credentials['security_token'] = aws_security_token

        # Cache related
        cache_dir = os.path.expanduser(config.get('ec2', 'cache_path'))
        if self.boto_profile:
            cache_dir = os.path.join(cache_dir, 'profile_' + self.boto_profile)
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        cache_name = 'ansible-ec2'
        cache_id = self.boto_profile or os.environ.get('AWS_ACCESS_KEY_ID', self.credentials.get('aws_access_key_id'))
        if cache_id:
            cache_name = '%s-%s' % (cache_name, cache_id)
        self.cache_path_cache = os.path.join(cache_dir, "%s.cache" % cache_name)
        self.cache_path_index = os.path.join(cache_dir, "%s.index" % cache_name)
        self.cache_max_age = config.getint('ec2', 'cache_max_age')

        self.expand_csv_tags = config.getboolean('ec2', 'expand_csv_tags')

        # Configure nested groups instead of flat namespace.
        self.nested_groups = config.getboolean('ec2', 'nested_groups')

        # Replace dash or not in group names
        self.replace_dash_in_groups = config.getboolean('ec2', 'replace_dash_in_groups')

        # IAM role to assume for connection
        self.iam_role = config.get('ec2', 'iam_role')

        # Configure which groups should be created.

        group_by_options = [a for a in DEFAULTS if a.startswith('group_by')]
        for option in group_by_options:
            setattr(self, option, config.getboolean('ec2', option))

        # Do we need to just include hosts that match a pattern?
        self.pattern_include = config.get('ec2', 'pattern_include')
        if self.pattern_include:
            self.pattern_include = re.compile(self.pattern_include)

        # Do we need to exclude hosts that match a pattern?
        self.pattern_exclude = config.get('ec2', 'pattern_exclude')
        if self.pattern_exclude:
            self.pattern_exclude = re.compile(self.pattern_exclude)

        # Do we want to stack multiple filters?
        self.stack_filters = config.getboolean('ec2', 'stack_filters')

        # Instance filters (see boto and EC2 API docs). Ignore invalid filters.
        self.ec2_instance_filters = []

        if config.has_option('ec2', 'instance_filters'):
            filters = config.get('ec2', 'instance_filters')

            if self.stack_filters and '&' in filters:
                self.fail_with_error("AND filters along with stack_filter enabled is not supported.\n")

            filter_sets = [f for f in filters.split(',') if f]

            for filter_set in filter_sets:
                filters = {}
                filter_set = filter_set.strip()
                for instance_filter in filter_set.split("&"):
                    instance_filter = instance_filter.strip()
                    if not instance_filter or '=' not in instance_filter:
                        continue
                    filter_key, filter_value = [x.strip() for x in instance_filter.split('=', 1)]
                    if not filter_key:
                        continue
                    filters[filter_key] = filter_value
                self.ec2_instance_filters.append(filters.copy())
Example #41
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        ec2_default_ini_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), 'ec2.ini')
        ec2_ini_path = os.environ.get('EC2_INI_PATH', ec2_default_ini_path)
        config.read(ec2_ini_path)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(
                    boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2',
                                                   'vpc_destination_variable')

        if config.has_option('ec2', 'destination_format') and \
           config.has_option('ec2', 'destination_format_tags'):
            self.destination_format = config.get('ec2', 'destination_format')
            self.destination_format_tags = config.get(
                'ec2', 'destination_format_tags').split(',')
        else:
            self.destination_format = None
            self.destination_format_tags = None

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Include RDS instances?
        self.rds_enabled = True
        if config.has_option('ec2', 'rds'):
            self.rds_enabled = config.getboolean('ec2', 'rds')

        # Return all EC2 and RDS instances (if RDS is enabled)
        if config.has_option('ec2', 'all_instances'):
            self.all_instances = config.getboolean('ec2', 'all_instances')
        else:
            self.all_instances = False
        if config.has_option('ec2', 'all_rds_instances') and self.rds_enabled:
            self.all_rds_instances = config.getboolean('ec2',
                                                       'all_rds_instances')
        else:
            self.all_rds_instances = False

        # Cache related
        cache_dir = os.path.expanduser(config.get('ec2', 'cache_path'))
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        self.cache_path_cache = cache_dir + "/ansible-ec2.cache"
        self.cache_path_index = cache_dir + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')

        # Configure nested groups instead of flat namespace.
        if config.has_option('ec2', 'nested_groups'):
            self.nested_groups = config.getboolean('ec2', 'nested_groups')
        else:
            self.nested_groups = False

        # Configure which groups should be created.
        group_by_options = [
            'group_by_instance_id',
            'group_by_region',
            'group_by_availability_zone',
            'group_by_ami_id',
            'group_by_instance_type',
            'group_by_key_pair',
            'group_by_vpc_id',
            'group_by_security_group',
            'group_by_tag_keys',
            'group_by_tag_none',
            'group_by_route53_names',
            'group_by_rds_engine',
            'group_by_rds_parameter_group',
        ]
        for option in group_by_options:
            if config.has_option('ec2', option):
                setattr(self, option, config.getboolean('ec2', option))
            else:
                setattr(self, option, True)

        # Do we need to just include hosts that match a pattern?
        try:
            pattern_include = config.get('ec2', 'pattern_include')
            if pattern_include and len(pattern_include) > 0:
                self.pattern_include = re.compile(pattern_include)
            else:
                self.pattern_include = None
        except ConfigParser.NoOptionError, e:
            self.pattern_include = None
Example #42
0
    def main(self, **args):
        self.process_args()
        self.cert_path = os.environ['EC2_CERT']
        self.private_key_path = os.environ['EC2_PRIVATE_KEY']
        self.user = os.environ['EC2_USER_ID']
        self.ec2cert_path = os.environ['EUCALYPTUS_CERT']

        if self.cli_options.bfebs_url:
            if (not self.cli_options.architecture or not (self.cli_options.architecture == 'i386' or\
                                                          self.cli_options.architecture == 'x86_64')):
                print >> sys.stderr, "Error: architecture must be either 'i386' or 'x86_64'"
                sys.exit(-1)
            if not self.cli_options.key:
                print >> sys.stderr, "Error: an ssh key name must be specified to create an EBS image"
                sys.exit(-1)
            if not self.cli_options.volume_size:
                self.cli_options.volume_size = 2
            if not self.cli_options.image_name:
                print >> sys.stderr, "Error: An image name is required when creating an EBS image"
                sys.exit(-1)
            if not self.cli_options.description:
                print >> sys.stderr, "Error: A description is required when creating an EBS image"
                sys.exit(-1)
                
        else:
          # tarball and image option are mutually exclusive
          if (not(self.cli_options.image_name) and not(self.cli_options.tarball)):
              print >> sys.stderr, "Error: one of -i or -t must be specified"
              sys.exit(-1)

          if (self.cli_options.image_name and self.cli_options.tarball):
              print >> sys.stderr, "Error: -i and -t cannot be specified together"
              sys.exit(-1)

          if (self.cli_options.tarball and \
              (not(self.cli_options.description) or not(self.cli_options.architecture))):
              print >> sys.stderr, "Error: when -t is specified, -s and -a are required"
              sys.exit(-1)

          if (self.cli_options.architecture and \
              not(self.cli_options.architecture == 'i386' or self.cli_options.architecture == 'x86_64')):
              print >> sys.stderr, "Error: architecture must be either 'i386' or 'x86_64'"
              sys.exit(-1)

          if (self.cli_options.kernel and not(self.cli_options.ramdisk)) or \
             (not(self.cli_options.kernel) and self.cli_options.ramdisk):
              print >> sys.stderr, "Error: kernel and ramdisk must both be overridden"
              sys.exit(-1)

          if (self.cli_options.architecture and self.cli_options.image_name):
              print >> sys.stderr, "Warning: you may be overriding the default architecture of this image!"

          if not self.cli_options.bucket:
              print >> sys.stderr, "Error: Required parameters are missing: (-b, --bucket)"
              sys.exit(-1)


        euare_svc = EuareService()
        conn = boto.connect_iam(host=euare_svc.args['host'], \
                    aws_access_key_id=euare_svc.args['aws_access_key_id'],\
                    aws_secret_access_key=euare_svc.args['aws_secret_access_key'],\
                    port=euare_svc.args['port'], path=euare_svc.args['path'],\
                    is_secure=euare_svc.args['is_secure'])
        userinfo  = conn.get_user().arn.split(':')
        if not(userinfo[4]=='eucalyptus') and not(self.cli_options.kernel):
            print >> sys.stderr, "Error: must be cloud admin to upload kernel/ramdisk. try specifying existing ones with --kernel and --ramdisk"
            sys.exit(-1)
        self.eustore_url = self.ServiceClass.StoreBaseURL

        # would be good of this were async, i.e. when the tarball is downloading
        ec2_conn = boto.connect_euca(host=euare_svc.args['host'], \
                        aws_access_key_id=euare_svc.args['aws_access_key_id'],\
                        aws_secret_access_key=euare_svc.args['aws_secret_access_key'])
        ec2_conn.APIVersion = '2012-03-01'
                        
        self.ImageList = ec2_conn.get_all_images()

        if self.cli_options.bfebs_url:
            #run first emi
            emi = None
            for i in range(0,len(self.ImageList)):
                #check to see if any images are named the same as that being passed
                if self.ImageList[i].name == self.cli_options.image_name:
                    print >> sys.stderr, "Error: an image already exists with the name you specified - please choose another."
                    sys.exit(-1)
                #only use an instance store image as we don't want an already attached EBS vol to deal with
                if self.ImageList[i].root_device_type != 'instance-store':
                    continue
                elif str(self.ImageList[i]).split(":")[1].startswith("emi"):
                  emi =  str(self.ImageList[i]).split(":")[1]
            if not emi:
                print >> sys.stderr, "Error: You just first register an instance-store image before importing an EBS image."
                sys.exit(-1)
            reservation = ec2_conn.run_instances(image_id=emi,key_name=self.cli_options.key)
            #we should have only one instance
            instance = reservation.instances[0]
            zones = ec2_conn.get_all_zones()
            zone = random.choice(zones).name
            #create 2 volumes - one to download the data to and one to be used for the EBS snapshot later
            volume1 = ec2_conn.create_volume(zone=zone,size=self.cli_options.volume_size + 1)
            volume2 = ec2_conn.create_volume(zone=zone,size=self.cli_options.volume_size)
            device_name1 = "/dev/vdb"
            device_name2 = "/dev/vdc"
            #make sure instance is running, timeout after 2 mins
            print "Waiting for instance to launch..."
            for i in range(0,120):
                if instance.state.lower() == "running": 
                    state = "running"
                    break
                else:
                    instance.update()
                    time.sleep(1)
                    continue
            if state == "pending":
                print >> sys.stderr, "Error: Instance failed to enter 'running' state."
                sys.exit(-1)
            #attach our volumes
            retval = ec2_conn.attach_volume(volume1.id,instance.id,device_name1)
            if not retval:
                print >> sys.stderr, "Error: Failed to attach newly created volume to instance at " + device_name1
                sys.exit(-1)
            retval = ec2_conn.attach_volume(volume2.id,instance.id,device_name2)
            if not retval:
                print >> sys.stderr, "Error: Failed to attach newly created volume to instance at " + device_name2
                sys.exit(-1)
            #locate the users' ssh key
            home_contents = os.listdir(os.path.expanduser('~'))
            keys = []
            for fname in home_contents:
                if fname.startswith(self.cli_options.key):
                    keys.append(fname)
            if len(keys) == 0: 
                print >> sys.stderr, "Error: Unable to find your ssh key in your home directory. Please place it there and name it "\
                                     + self.cli_options.key + ".priv" 
                sys.exit(-1)
            elif len(keys) > 1:
                print >> sys.stderr, "Error: Located multiple files beginning with your key name in your home directory"
                print >> sys.stderr, "Please ensure that your key is in your home directory and is the only file that begins with: "\
                                     + self.cli_options.key
                sys.exit(-1)
            key = keys[0] 
            #we need to ensure ssh has started and keys have been injected
            print "Waiting on volume attachment..."
            time.sleep(20)
            #format the volume where we will download the image to
            cmd = "ssh -i ~/" + key + " root@" + instance.ip_address + " mkfs.ext3 " + device_name1
            retval = os.system(cmd)
            if retval != 0:
                print >> sys.stderr, "Error: Failed to format the volume attached at " + device_name1
                sys.exit(-1)
            #mount the volume
            cmd = "ssh -i ~/" + key + " root@" + instance.ip_address + " mkdir /volume1"
            os.system(cmd)
            cmd = "ssh -i ~/" + key + " root@" + instance.ip_address + " mount -t ext3 " + device_name1 + " /volume1" 
            retval = os.system(cmd)
            if retval != 0:
                print >> sys.stderr, "Error: Failed to mount the volume attached at " + device_name1
                sys.exit(-1)
            #download the image
            image_fname = "/volume1/" + os.path.basename(self.cli_options.bfebs_url)
            cmd = "ssh -i ~/" + key + " root@" + instance.ip_address + " wget " + self.cli_options.bfebs_url + " -O " + image_fname
            print "Downloading the EBS image - this could take a while..."
            retval = os.system(cmd)
            if retval != 0:
                print >> sys.stderr, "Error: Failed to download image"
                sys.exit(-1)
            #write the image to the second volume
            print "Creating EBS volume - this may take a while..."
            cmd = "ssh -i ~/" + key + " root@" + instance.ip_address + " dd if=" + image_fname + " of=" + device_name2 + " bs=1M" 
            retval = os.system(cmd)
            if retval != 0:
                print >> sys.stderr, "Error: Failed to write image to volume"
                sys.exit(-1)
            #unmount volume1
            cmd = "ssh -i ~/" + key + " root@" + instance.ip_address + " umount /volume1"
            retval = os.system(cmd)
            if retval != 0:
                print >> sys.stderr, "Error: Failed to unmount volume (non-fatal)"
            #delete volume1
            retval = ec2_conn.detach_volume(volume1.id) 
            if not retval:
                print >> sys.stderr, "Error: Failed to delete volume (non-fatal)"
            else:
                print "Waiting for volume to detach..."
                while not volume1.status == "available":
                    time.sleep(5)
                    volume1.update()
                ec2_conn.delete_volume(volume1.id) 
            #take snapshot
            snapshot = ec2_conn.create_snapshot(volume2.id)
            print "Preparing snapshot - this will take some time..."
            while not snapshot.progress.startswith("100"):
                print "Progress: " + snapshot.progress
                snapshot.update()
                time.sleep(5)
            print "Successfully created snapshot: " + snapshot.id
            #delete volume2
            retval = ec2_conn.detach_volume(volume2.id) 
            if not retval:
                print >> sys.stderr, "Error: Failed to delete volume (non-fatal)"
            else:
                print "Waiting for volume to detach..."
                while not volume2.status == "available":
                    time.sleep(5)
                    volume2.update()
                ec2_conn.delete_volume(volume2.id) 
            obj = LocalRegister()
            obj.image_location=None
            obj.name=self.cli_options.image_name
            obj.description=self.cli_options.description
            obj.snapshot=snapshot.id
            obj.architecture=self.cli_options.architecture
            obj.block_device_mapping=[]
            obj.root_device_name="/dev/sda1"
            obj.kernel=None
            obj.ramdisk=None
            print "Successfully registered EBS image: " + obj.main()
            #terminate instance
            instance.terminate()
            
        else:
            if os.environ.has_key('EUSTORE_URL'):
                self.eustore_url = os.environ['EUSTORE_URL']

            self.destination = "/tmp/"
            if self.cli_options.dir:
                self.destination = self.cli_options.dir
            if not(self.destination.endswith('/')):
                self.destination += '/'
            # for security, add random directory within to work in
            self.destination = tempfile.mkdtemp(prefix=self.destination)+'/'

            if self.cli_options.tarball:
                # local tarball path instead
                print "Installed image: "+self.bundleAll(self.cli_options.tarball, self.cli_options.prefix, self.cli_options.description, self.cli_options.architecture)
            else:
                catURL = self.eustore_url + "catalog"
                req = urllib2.Request(catURL, headers=self.ServiceClass.RequestHeaders)
                response = urllib2.urlopen(req).read()
                parsed_cat = json.loads(response)
                if len(parsed_cat) > 0:
                    image_list = parsed_cat['images']
                    image_found = False
                    for image in image_list:
                        if image['name'].find(self.cli_options.image_name) > -1:
                            image_found = True
                            break
                    if image_found:
                        # more param checking now
                        if image['single-kernel']=='True':
                            if self.cli_options.kernel_type:
                                print >> sys.stderr, "The -k option will be ignored because the image is single-kernel"
                        else:
                            # Warn about kernel type for multi-kernel images, but not if already installed
                            # kernel/ramdisk have been specified.
                            if not(self.cli_options.kernel_type) and not(self.cli_options.kernel):
                                print >> sys.stderr, "Error: The -k option must be specified because this image has separate kernels"
                                sys.exit(-1)
                        print "Downloading Image : ",image['description']
                        imageURL = self.eustore_url+image['url']
                        req = urllib2.Request(imageURL, headers=self.ServiceClass.RequestHeaders)
                        req = urllib2.urlopen(req)
                        file_size = int(req.info()['Content-Length'])/1000
                        size_count = 0;
                        prog_bar = euca2ools.commands.eustore.progressBar(file_size)
                        BUF_SIZE = 128*1024
                        with open(self.destination+'eucaimage.tar.gz', 'wb') as fp:
                            while True:
                                buf = req.read(BUF_SIZE)
                                size_count += len(buf)
                                prog_bar.update(size_count/1000)
                                if not buf: break
                                fp.write(buf)
                        fp.close()
                        # validate download by re-computing serial # (name)
                        print "Checking image bundle"
                        file = open(fp.name, 'r')
                        m = hashlib.md5()
                        m.update(file.read())
                        hash = m.hexdigest()
                        crc = str(zlib.crc32(hash)& 0xffffffffL)
                        if image['name'] == crc.rjust(10,"0"):
                            print "Installed image: "+self.bundleAll(fp.name, None, image['description'], image['architecture'])
                        else:
                            print >> sys.stderr, "Error: Downloaded image was incomplete or corrupt, please try again"
                    else:
                        print >> sys.stderr, "Image name not found, please run eustore-describe-images"
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        ec2_default_ini_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), 'ec2.ini')
        ec2_ini_path = os.environ.get('EC2_INI_PATH', ec2_default_ini_path)
        config.read(ec2_ini_path)

        # AWS credentials
        if config.has_option('ec2', 'aws_access_key_id'):
            self.aws_access_key = config.get('ec2', 'aws_access_key_id')
        else:
            self.aws_access_key = os.environ.get('AWS_ACCESS_KEY_ID')

        if config.has_option('ec2', 'aws_secret_access_key'):
            self.aws_secret_key = config.get('ec2', 'aws_secret_access_key')
        else:
            self.aws_secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(
                    boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2',
                                                   'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Include RDS instances?
        self.rds_enabled = True
        if config.has_option('ec2', 'rds'):
            self.rds_enabled = config.getboolean('ec2', 'rds')

        # Return all EC2 and RDS instances (if RDS is enabled)
        if config.has_option('ec2', 'all_instances'):
            self.all_instances = config.getboolean('ec2', 'all_instances')
        else:
            self.all_instances = False
        if config.has_option('ec2', 'all_rds_instances') and self.rds_enabled:
            self.all_rds_instances = config.getboolean('ec2',
                                                       'all_rds_instances')
        else:
            self.all_rds_instances = False

        # Cache related
        cache_dir = os.path.expanduser(config.get('ec2', 'cache_path'))
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        self.cache_path_cache = cache_dir + "/ansible-ec2.cache"
        self.cache_path_index = cache_dir + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')

        # Configure nested groups instead of flat namespace.
        if config.has_option('ec2', 'nested_groups'):
            self.nested_groups = config.getboolean('ec2', 'nested_groups')
        else:
            self.nested_groups = False

        # Do we need to just include hosts that match a pattern?
        try:
            pattern_include = config.get('ec2', 'pattern_include')
            if pattern_include and len(pattern_include) > 0:
                self.pattern_include = re.compile(pattern_include)
            else:
                self.pattern_include = None
        except ConfigParser.NoOptionError, e:
            self.pattern_include = None
Example #44
0
    def read_settings(self):
        """ Reads the settings from the ec2.ini file """

        config = ConfigParser.SafeConfigParser()
        ec2_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "ec2.ini")
        ec2_ini_path = os.environ.get("EC2_INI_PATH", ec2_default_ini_path)
        config.read(ec2_ini_path)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option("ec2", "eucalyptus"):
            self.eucalyptus = config.getboolean("ec2", "eucalyptus")
        if self.eucalyptus and config.has_option("ec2", "eucalyptus_host"):
            self.eucalyptus_host = config.get("ec2", "eucalyptus_host")

        # Regions
        self.regions = []
        configRegions = config.get("ec2", "regions")
        configRegions_exclude = config.get("ec2", "regions_exclude")
        if configRegions == "all":
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get("ec2", "destination_variable")
        self.vpc_destination_variable = config.get("ec2", "vpc_destination_variable")

        # Route53
        self.route53_enabled = config.getboolean("ec2", "route53")
        self.route53_excluded_zones = []
        if config.has_option("ec2", "route53_excluded_zones"):
            self.route53_excluded_zones.extend(config.get("ec2", "route53_excluded_zones", "").split(","))

        # Include RDS instances?
        self.rds_enabled = True
        if config.has_option("ec2", "rds"):
            self.rds_enabled = config.getboolean("ec2", "rds")

        # Return all EC2 and RDS instances (if RDS is enabled)
        if config.has_option("ec2", "all_instances"):
            self.all_instances = config.getboolean("ec2", "all_instances")
        else:
            self.all_instances = False
        if config.has_option("ec2", "all_rds_instances") and self.rds_enabled:
            self.all_rds_instances = config.getboolean("ec2", "all_rds_instances")
        else:
            self.all_rds_instances = False

        # Cache related
        cache_dir = os.path.expanduser(config.get("ec2", "cache_path"))
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        self.cache_path_cache = cache_dir + "/ansible-ec2.cache"
        self.cache_path_index = cache_dir + "/ansible-ec2.index"
        self.cache_max_age = config.getint("ec2", "cache_max_age")

        # Configure nested groups instead of flat namespace.
        if config.has_option("ec2", "nested_groups"):
            self.nested_groups = config.getboolean("ec2", "nested_groups")
        else:
            self.nested_groups = False

        # Configure which groups should be created.
        group_by_options = [
            "group_by_instance_id",
            "group_by_region",
            "group_by_availability_zone",
            "group_by_ami_id",
            "group_by_instance_type",
            "group_by_key_pair",
            "group_by_vpc_id",
            "group_by_security_group",
            "group_by_tag_keys",
            "group_by_tag_none",
            "group_by_route53_names",
            "group_by_rds_engine",
            "group_by_rds_parameter_group",
        ]
        for option in group_by_options:
            if config.has_option("ec2", option):
                setattr(self, option, config.getboolean("ec2", option))
            else:
                setattr(self, option, True)

        # Do we need to just include hosts that match a pattern?
        try:
            pattern_include = config.get("ec2", "pattern_include")
            if pattern_include and len(pattern_include) > 0:
                self.pattern_include = re.compile(pattern_include)
            else:
                self.pattern_include = None
        except ConfigParser.NoOptionError, e:
            self.pattern_include = None
Example #45
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''

        config = ConfigParser.SafeConfigParser()
        ec2_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ec2.ini')
        ec2_ini_path = os.environ.get('EC2_INI_PATH', ec2_default_ini_path)
        config.read(ec2_ini_path)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Include RDS instances?
        self.rds_enabled = True
        if config.has_option('ec2', 'rds'):
            self.rds_enabled = config.getboolean('ec2', 'rds')

        # Return all EC2 and RDS instances (if RDS is enabled)
        if config.has_option('ec2', 'all_instances'):
            self.all_instances = config.getboolean('ec2', 'all_instances')
        else:
            self.all_instances = False
        if config.has_option('ec2', 'all_rds_instances') and self.rds_enabled:
            self.all_rds_instances = config.getboolean('ec2', 'all_rds_instances')
        else:
            self.all_rds_instances = False

        # Cache related
        cache_dir = os.path.expanduser(config.get('ec2', 'cache_path'))
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        self.cache_path_cache = cache_dir + "/ansible-ec2.cache"
        self.cache_path_index = cache_dir + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')

        # Configure nested groups instead of flat namespace.
        if config.has_option('ec2', 'nested_groups'):
            self.nested_groups = config.getboolean('ec2', 'nested_groups')
        else:
            self.nested_groups = False

        # Do we need to just include hosts that match a pattern?
        try:
            pattern_include = config.get('ec2', 'pattern_include')
            if pattern_include and len(pattern_include) > 0:
                self.pattern_include = re.compile(pattern_include)
            else:
                self.pattern_include = None
        except ConfigParser.NoOptionError, e:
            self.pattern_include = None
Example #46
0
 def __init__(self, clc_host, access_id, secret_key):
     self.conn = boto.connect_euca(host=clc_host, aws_access_key_id=access_id, aws_secret_access_key=secret_key)
Example #47
0
    def read_settings(self):
        ''' Reads the settings from the ec2.ini file '''
        if six.PY2:
            config = configparser.SafeConfigParser()
        else:
            config = configparser.ConfigParser()
        ec2_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ec2.ini')
        ec2_ini_path = os.environ.get('EC2_INI_PATH', ec2_default_ini_path)
        config.read(ec2_ini_path)

        # is eucalyptus?
        self.eucalyptus_host = None
        self.eucalyptus = False
        if config.has_option('ec2', 'eucalyptus'):
            self.eucalyptus = config.getboolean('ec2', 'eucalyptus')
        if self.eucalyptus and config.has_option('ec2', 'eucalyptus_host'):
            self.eucalyptus_host = config.get('ec2', 'eucalyptus_host')

        # Regions
        self.regions = []
        configRegions = config.get('ec2', 'regions')
        configRegions_exclude = config.get('ec2', 'regions_exclude')
        if (configRegions == 'all'):
            if self.eucalyptus_host:
                self.regions.append(boto.connect_euca(host=self.eucalyptus_host).region.name)
            else:
                for regionInfo in ec2.regions():
                    if regionInfo.name not in configRegions_exclude:
                        self.regions.append(regionInfo.name)
        else:
            self.regions = configRegions.split(",")

        # Destination addresses
        self.destination_variable = config.get('ec2', 'destination_variable')
        self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')

        # Route53
        self.route53_enabled = config.getboolean('ec2', 'route53')
        self.route53_excluded_zones = []
        if config.has_option('ec2', 'route53_excluded_zones'):
            self.route53_excluded_zones.extend(
                config.get('ec2', 'route53_excluded_zones', '').split(','))

        # Include RDS instances?
        self.rds_enabled = True
        if config.has_option('ec2', 'rds'):
            self.rds_enabled = config.getboolean('ec2', 'rds')

        # Return all EC2 and RDS instances (if RDS is enabled)
        if config.has_option('ec2', 'all_instances'):
            self.all_instances = config.getboolean('ec2', 'all_instances')
        else:
            self.all_instances = False
        if config.has_option('ec2', 'all_rds_instances') and self.rds_enabled:
            self.all_rds_instances = config.getboolean('ec2', 'all_rds_instances')
        else:
            self.all_rds_instances = False

        # Cache related
        cache_dir = os.path.expanduser(config.get('ec2', 'cache_path'))
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        self.cache_path_cache = cache_dir + "/ansible-ec2.cache"
        self.cache_path_index = cache_dir + "/ansible-ec2.index"
        self.cache_max_age = config.getint('ec2', 'cache_max_age')

        # Configure nested groups instead of flat namespace.
        if config.has_option('ec2', 'nested_groups'):
            self.nested_groups = config.getboolean('ec2', 'nested_groups')
        else:
            self.nested_groups = False

        # Configure which groups should be created.
        group_by_options = [
            'group_by_instance_id',
            'group_by_region',
            'group_by_availability_zone',
            'group_by_ami_id',
            'group_by_instance_type',
            'group_by_key_pair',
            'group_by_vpc_id',
            'group_by_security_group',
            'group_by_tag_keys',
            'group_by_tag_none',
            'group_by_route53_names',
            'group_by_rds_engine',
            'group_by_rds_parameter_group',
        ]
        for option in group_by_options:
            if config.has_option('ec2', option):
                setattr(self, option, config.getboolean('ec2', option))
            else:
                setattr(self, option, True)

        # Do we need to just include hosts that match a pattern?
        try:
            pattern_include = config.get('ec2', 'pattern_include')
            if pattern_include and len(pattern_include) > 0:
                self.pattern_include = re.compile(pattern_include)
            else:
                self.pattern_include = None
        except configparser.NoOptionError as e:
            self.pattern_include = None

        # Do we need to exclude hosts that match a pattern?
        try:
            pattern_exclude = config.get('ec2', 'pattern_exclude');
            if pattern_exclude and len(pattern_exclude) > 0:
                self.pattern_exclude = re.compile(pattern_exclude)
            else:
                self.pattern_exclude = None
        except configparser.NoOptionError as e:
            self.pattern_exclude = None

        # Instance filters (see boto and EC2 API docs). Ignore invalid filters.
        self.ec2_instance_filters = defaultdict(list)
        if config.has_option('ec2', 'instance_filters'):
            for instance_filter in config.get('ec2', 'instance_filters', '').split(','):
                instance_filter = instance_filter.strip()
                if not instance_filter or '=' not in instance_filter:
                    continue
                filter_key, filter_value = [x.strip() for x in instance_filter.split('=', 1)]
                if not filter_key:
                    continue
                self.ec2_instance_filters[filter_key].append(filter_value)