Module: CloudFormationTool::Storable

Includes:
CloudFormationTool
Included in:
CloudFormation::LambdaCode, CloudFormation::NestedStack, CloudFormation::Stack, CloudInit
Defined in:
lib/cloud_formation_tool/storable.rb

Constant Summary

Constants included from CloudFormationTool

VERSION

Instance Method Summary collapse

Methods included from CloudFormationTool

#aws_config, #awsas, #awscdn, #awscf, #awscreds, #awsec2, #awss3, #cf_bucket_name, #find_profile, #profile, #region, #s3_bucket_name

Instance Method Details

#cached_object(md5) ⇒ Object



17
18
19
# File 'lib/cloud_formation_tool/storable.rb', line 17

def cached_object(md5)
  Aws::S3::Bucket.new(s3_bucket_name(region), client: awss3(region)).objects(prefix: "cf-compiled/#{md5[0]}/#{md5[1..2]}/#{md5}/").first
end

#make_filename(ext = '') ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/cloud_formation_tool/storable.rb', line 8

def make_filename(ext = '')
  base = "#{File.basename(Dir.pwd)}-#{Time.now.strftime("%Y%m%d%H%M%S")}"
  if ext.empty?
    base
  else
    "#{base}.#{ext}"
  end
end

#upload(path, content, mime_type: 'text/yaml', gzip: true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cloud_formation_tool/storable.rb', line 21

def upload(path, content, mime_type: 'text/yaml', gzip: true)
  md5 = Digest::MD5.hexdigest content
  prefix = "#{md5[0]}/#{md5[1..2]}/#{md5}"
  b = Aws::S3::Bucket.new(s3_bucket_name(region), client: awss3(region))
  # return early if we already have a copy of this object stored.
  # if this object was previously uploaded, we use its URLs (and not, for example,
  # do a local copy to the requested path) because this way cloudformation can see
  # that the updated template is exactly the same as the old one and will not force
  # an unneeded update.
  o = cached_object(md5)
  if o.nil?
    # no such luck, we need to actually upload the file
    o = b.object("cf-compiled/#{prefix}/#{path}")
    file_opts = {
      acl: 'public-read',
      body: content,
      content_disposition: 'attachment',
      content_type: mime_type,
      storage_class: 'REDUCED_REDUNDANCY'
    }
    file_opts.merge!({content_encoding: 'gzip'}) if gzip
    o.put(file_opts)
  else
    debug "re-using cached object"
  end
  o.public_url
end