class Blufin::AWS

Constants

FILE_AWS_CONFIG
FILE_AWS_CREDENTIALS
S3_EXCLUDE

Public Class Methods

download_s3_data(bucket_name, bucket_path, file: nil, profile: nil, region: nil, use_cache: true) click to toggle source

Pulls a file from S3 and saves it to the /tmp folder. To avoid making multiple calls, caching is turned on by default. @return string (path to tmp file).

# File lib/core/aws.rb, line 51
def self.download_s3_data(bucket_name, bucket_path, file: nil, profile: nil, region: nil, use_cache: true)
    raise RuntimeError, 'profile cannot be nil.' if profile.nil?
    raise RuntimeError, 'region cannot be nil.' if region.nil?
    bucket_path     = Blufin::Strings::remove_surrounding_slashes(bucket_path).strip
    bucket_path_tmp = bucket_path.length > 0 ? "-#{bucket_path.gsub('/', '-')}" : ''
    bucket_path_s3  = bucket_path.length > 0 ? "/#{bucket_path}" : ''
    unless file.nil?
        bucket_path_tmp = "#{bucket_path_tmp}-#{file}"
        bucket_path_s3  = "#{bucket_path_s3}/#{file}"
    end
    tmp_location = "/tmp/awx-s3-cache-#{bucket_name}#{bucket_path_tmp}"
    # If path/file exists and we're using cached values, simply return.
    if file.nil?
        return tmp_location if Blufin::Files::path_exists(tmp_location) && use_cache && Blufin::Files::get_files_in_dir(tmp_location).any?
    else
        return tmp_location if Blufin::Files::file_exists(tmp_location) && use_cache
    end
    # Get profile name.
    profile_name = profile.nil? ? App::AWSProfile::get_profile_name : " --profile #{profile}"
    begin
        # Clear out all (possibly stale) files.
        puts
        raise RuntimeError unless Blufin::Terminal::execute_proc("Wiping temp-data: #{Blufin::Terminal::format_directory(tmp_location)}", Proc.new {
            system("rm -rf #{tmp_location}") if file.nil? && Blufin::Files::path_exists(tmp_location)
            system("rm #{tmp_location}") if !file.nil? && Blufin::Files::file_exists(tmp_location)
            Blufin::Files::create_directory(tmp_location) if file.nil?
            true
        }, verbose: true)
        raise RuntimeError unless Blufin::Terminal::execute("aws s3 cp s3://#{bucket_name}#{bucket_path_s3} #{tmp_location}#{file.nil? ? ' --recursive' : ''} --region #{region}#{profile_name}", verbose: true)[0]
        tmp_location
    rescue
        system("rm -rf #{tmp_location}") if file.nil?
        system("rm #{tmp_location}") unless file.nil?
        Blufin::Terminal::error("Unable to download from S3 bucket: #{Blufin::Terminal::format_highlight("s3://#{bucket_name}#{bucket_path_s3}")}", "Either the bucket doesn't exist, you don't have permissions or something else is wrong.", true, false)
    end

end
get_aws_credentials(profile) click to toggle source

Checks if AWS credentials exist. Does nothing if on EC2. @return Array (of errors).

# File lib/core/aws.rb, line 91
def self.get_aws_credentials(profile)
    credentials = nil
    errors      = []
    if Blufin::Files::file_exists(FILE_AWS_CREDENTIALS)
        credentials        = Blufin::AWSCredentials.new
        config             = Blufin::Files::file_exists(FILE_AWS_CONFIG) ? ParseConfig.new(FILE_AWS_CONFIG) : nil
        credentials_parsed = ParseConfig.new(FILE_AWS_CREDENTIALS)
        unless credentials_parsed.params[profile].nil?
            # Currently not used/required (but here just in case).
            unless config.nil? || config.params[profile].nil?
                credentials.region = config.params[profile]['region'] unless config.params[profile]['region'].nil?
                credentials.output = config.params[profile]['output'] unless config.params[profile]['output'].nil?

            end
            credentials.aws_key    = credentials_parsed.params[profile]['aws_access_key_id'] unless credentials_parsed.params[profile]['aws_access_key_id'].nil?
            credentials.aws_secret = credentials_parsed.params[profile]['aws_secret_access_key'] unless credentials_parsed.params[profile]['aws_secret_access_key'].nil?
        end
        errors << "aws-cli error. Cannot find #{profile}: #{Blufin::Terminal::format_invalid('aws_access_key_id')} in: #{Blufin::Terminal::format_directory(FILE_AWS_CREDENTIALS)}" if credentials.aws_key.nil?
        errors << "aws-cli error. Cannot find #{profile}: #{Blufin::Terminal::format_invalid('aws_secret_access_key')} in: #{Blufin::Terminal::format_directory(FILE_AWS_CREDENTIALS)}" if credentials.aws_secret.nil?
    else
        # Returns 'yes' if running on EC2 instance, 'no' if not.
        unless `#{Blufin::Base::get_base_path}/#{Blufin::Base::OPT_PATH}/shell/ec2-check`.to_s.gsub("\n", '') =~ /yes/i
            errors << "aws-cli error. Cannot find file: #{Blufin::Terminal::format_invalid(FILE_AWS_CREDENTIALS)}"
        end
    end
    return credentials, errors
end
is_ec2() click to toggle source

Checks if script is running on an EC2 instance. Not sure if this is 100% reliable, but it's about as good as it gets I think. @return bool

# File lib/core/aws.rb, line 13
def self.is_ec2
    res = `#{Blufin::Base::get_base_path}#{Blufin::Base::OPT_PATH}/shell/ec2-check`.to_s
    res = Blufin::Strings::strip_newline(res)
    res.downcase.strip == 'yes'
end
upload_s3_data(bucket_name, bucket_path, file_or_path, profile: nil, region: nil, is_file: false, dryrun: false) click to toggle source

Uploads a file (or path) to S3. If path, will upload recursively (which is mandatory with s3 sync command). @return string (S3 URL).

# File lib/core/aws.rb, line 21
def self.upload_s3_data(bucket_name, bucket_path, file_or_path, profile: nil, region: nil, is_file: false, dryrun: false)
    raise RuntimeError, 'profile cannot be nil.' if profile.nil?
    raise RuntimeError, 'region cannot be nil.' if region.nil?
    raise RuntimeError, "File or path not found: #{file_or_path}" unless Blufin::Files::file_exists(file_or_path)
    file_or_path   = File.expand_path(file_or_path)
    bucket_path    = Blufin::Strings::remove_surrounding_slashes(bucket_path).strip
    bucket_path_s3 = bucket_path.length > 0 ? "/#{bucket_path}" : ''
    s3_url         = "s3://#{bucket_name}#{bucket_path_s3}"
    begin
        if is_file
            cmd = "aws s3 cp #{file_or_path} #{s3_url} --region #{region}#{App::AWS::get_profile_for_cli}"
        else
            cmd = "aws s3 sync #{file_or_path} #{s3_url} --delete --region #{region}#{App::AWS::get_profile_for_cli}"
        end
        if dryrun
            cmd = "#{cmd} --dryrun"
            Blufin::Terminal::info('Performing Dry Run:', Blufin::Terminal::format_command(cmd))
            system("#{cmd} #{S3_EXCLUDE} --exclude '*.blank*'")
        else
            raise RuntimeError unless Blufin::Terminal::execute("#{cmd} #{S3_EXCLUDE} --exclude '*.blank*'", verbose: true, text: cmd)[0]
        end
        s3_url
    rescue
        Blufin::Terminal::error("Unable to download from S3 bucket: #{Blufin::Terminal::format_highlight(s3_url)}", "Either the bucket doesn't exist, you don't have permissions or something else is wrong.", true, false)
    end
end