def _get_vcpus_by_instance_type(pricing_file, instance_type): """ Get vcpus for the given instance type from the pricing file. :param pricing_file: pricing file path :param instance_type: The instance type to search for :return: the number of vcpus for the given instance type :raise CriticalError if unable to find the given instance or whatever error. """ try: # read vcpus value from file with open(pricing_file) as f: instances = json.load(f) vcpus = int(instances[instance_type]["vcpus"]) log.info("Instance %s has %s vcpus." % (instance_type, vcpus)) return vcpus except KeyError: error_msg = "Unable to get vcpus from file {0}. Instance type {1} not found.".format( pricing_file, instance_type) log.critical(error_msg) raise CriticalError(error_msg) except Exception: error_msg = "Unable to get vcpus for the instance type {0} from file {1}".format( instance_type, pricing_file) log.critical(error_msg) raise CriticalError(error_msg)
def _get_ddb_table(region, table_name, proxy_config): """ Get DynamoDB table by name. :param region: AWS region :param table_name: Table name to search for :param proxy_config: proxy configuration :return: the Table object """ log.debug("Getting DynamoDB table '%s'", table_name) ddb_client = boto3.client("dynamodb", region_name=region, config=proxy_config) try: tables = ddb_client.list_tables().get("TableNames") if table_name not in tables: error_msg = "Unable to find the DynamoDB table '{0}'".format( table_name) log.critical(error_msg) raise CriticalError(error_msg) ddb_resource = boto3.resource("dynamodb", region_name=region, config=proxy_config) table = ddb_resource.Table(table_name) log.debug("DynamoDB table found correctly.") except ClientError as e: log.critical( "Unable to get the DynamoDB table '%s'. Failed with exception: %s", table_name, e) raise return table
def _get_metadata(metadata_path): """ Get EC2 instance metadata. :param metadata_path: the metadata relative path :return the metadata value. """ try: metadata_value = requests.get( "http://169.254.169.254/latest/meta-data/{0}".format( metadata_path)).text except Exception as e: error_msg = "Unable to get {0} metadata. Failed with exception: {1}".format( metadata_path, e) log.critical(error_msg) raise CriticalError(error_msg) log.debug("%s=%s", metadata_path, metadata_value) return metadata_value