Class: App::AWSCloudFormation

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/aws_cloudformation.rb

Class Method Summary collapse

Class Method Details

.download_s3_templates(use_cache = true) ⇒ Object

Downloads templates from S3 into /tmp folder (and returns path to /tmp folder).

Returns:

  • string

Raises:

  • (RuntimeError)


75
76
77
78
79
80
# File 'lib/aws/aws_cloudformation.rb', line 75

def self.download_s3_templates(use_cache = true)
    raise RuntimeError, "Missing key in #{App::CONFIG_FILE}: #{App::AWSProfile::CLOUDFORMATION}->Templates->S3Bucket" unless App::AWSProfile::get_profile[App::AWSProfile::CLOUDFORMATION]['Templates'].has_key?('S3Bucket')
    s3       = App::AWSProfile::get_profile[App::AWSProfile::CLOUDFORMATION]['Templates']['S3Bucket']
    tmp_path = Blufin::AWS::download_s3_data(s3['Name'], s3['Path'], profile: App::AWSProfile::get_profile[App::AWSProfile::PROFILE], region: s3['Region'], use_cache: use_cache)
    tmp_path
end

.get_cloudformation_pathObject

Gets the path to the root of the cloudformation templates in blufin-secrets repo.

Returns:

  • string



7
8
9
10
11
12
13
14
15
# File 'lib/aws/aws_cloudformation.rb', line 7

def self.get_cloudformation_path
    if App::AWSProfile::get_profile[App::AWSProfile::CLOUDFORMATION]['Templates'].has_key?('Local')
        path = App::AWSProfile::get_profile['CloudFormation']['Templates']['Local']['Path']
        Blufin::Terminal::error("CloudFormation path not found: #{Blufin::Terminal::format_directory(path)}", 'This probably means directories got renamed or code got changed.', true) unless Blufin::Files::path_exists(path)
    else
        path = download_s3_templates
    end
    path
end

.get_s3_bucket_pathObject

Returns the S3 bucket path based on config parameters.

Returns:

  • string



46
47
48
49
# File 'lib/aws/aws_cloudformation.rb', line 46

def self.get_s3_bucket_path
    bucket_path = App::AWSProfile::get_profile['CloudFormation']['Uploads']['S3Bucket']['Path']
    bucket_path.nil? || bucket_path.to_s.strip == '' ? '' : bucket_path
end

.get_s3_bucket_urlObject

Returns S3 bucket URL based on config parameters.

Returns:

  • string



38
39
40
41
42
# File 'lib/aws/aws_cloudformation.rb', line 38

def self.get_s3_bucket_url
    bucket_path = get_s3_bucket_path
    bucket_path = bucket_path == '' ? '' : "/#{bucket_path}"
    "https://#{App::AWSProfile::get_profile['CloudFormation']['Uploads']['S3Bucket']['Name']}.s3-#{App::AWSProfile::get_profile['CloudFormation']['Uploads']['S3Bucket']['Region']}.amazonaws.com#{bucket_path}"
end

.get_stacksObject

Returns an array of cloudformation stack-names.

Returns:

  • Array of Hashes



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aws/aws_cloudformation.rb', line 53

def self.get_stacks
    results = []
    json    = Blufin::Terminal::execute('awx l -r CloudFormationStacks -j', capture: true, text: "AWS \xe2\x80\x94 Getting CloudFormation Stacks...")
    begin
        data = JSON.parse(json)
    rescue => e
        puts
        puts json
        Blufin::Terminal::error('Failed to parse AWS response JSON.', e.message)
    end
    data.each do |stack|
        results << {
            :name   => stack['StackName'],
            :region => stack['region']
        }
    end
    results.sort_by! { |hsh| hsh[:name] }
    results
end

.upload_cloudformation_template(source, category, template, filename_override: nil) ⇒ Object

Uploads a cloudformation template to S3 (so we can create a stack from it). Returns the S3 URL.

Returns:

  • string

Raises:

  • (RuntimeError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aws/aws_cloudformation.rb', line 20

def self.upload_cloudformation_template(source, category, template, filename_override: nil)
    raise RuntimeError, "File does not exist: #{source}" unless Blufin::Files::file_exists(source)
    tmp_file = "/tmp/aws-cf-upload-#{category}-#{template}-#{Blufin::Strings::random_string}.yml"
    Blufin::Terminal::execute("cp #{source} #{tmp_file}", text: "Preparing template: \x1B[38;5;240m#{tmp_file}\x1B[0m")
    bucket_path = get_s3_bucket_path
    bucket_path = bucket_path == '' ? '' : "#{bucket_path}/"
    if filename_override.nil?
        template_filename = "#{category}-#{template}-#{DateTime.now.strftime('%Y%m%d-%H%M%S')}.yml"
    else
        template_filename = filename_override
    end
    App::AWSCli::s3_upload(tmp_file, App::AWSProfile::get_profile['CloudFormation']['Uploads']['S3Bucket']['Name'], "#{bucket_path}#{template_filename}")
    # Return the S3 bucket URL.
    "#{get_s3_bucket_url}/#{template_filename}"
end