Class: Lono::Template::Upload

Inherits:
AbstractBase show all
Includes:
AwsServices
Defined in:
lib/lono/template/upload.rb

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #ec2, #iam, #s3, #s3_presigner, #s3_resource, #sts

Methods included from AwsServices::Helper

#rollback_complete?, #testing_update?

Methods included from AwsServices::StackSet

#find_stack_set, #stack_set_exists?

Methods included from AwsServices::Stack

#find_stack, #stack_exists?

Methods inherited from AbstractBase

#reinitialize, #template_path

Methods included from Blueprint::Root

#find_blueprint_root, #set_blueprint_root

Constructor Details

#initialize(options = {}) ⇒ Upload

Returns a new instance of Upload.



10
11
12
13
14
# File 'lib/lono/template/upload.rb', line 10

def initialize(options={})
  super
  @checksums = {}
  @prefix = Lono.env # s3://s3-bucket/development
end

Instance Method Details

#load_checksums!Object

Read existing files on s3 to grab their md5 checksum. We do this so we can see if we should avoid re-uploading the s3 child template entirely. If we upload a new child template that does not change AWS CloudFormation is not smart enough to know that it not has changed. I think all AWS CloudFormation does is check if the file’s timestamp.

Thought this would result in better AWS Change Set info but AWS still reports child stacks being changed even though they should not be reported. Leaving this s3 checksum in for now.



36
37
38
39
40
41
42
43
44
# File 'lib/lono/template/upload.rb', line 36

def load_checksums!
  return if @options[:noop]

  resp = s3.list_objects(bucket: s3_bucket, prefix: @prefix)
  resp.contents.each do |object|
    @checksums[object.key] = strip_surrounding_quotes(object.etag)
  end
  @checksums
end

#remote_checksum(path) ⇒ Object

path = ./output/templates/docker.yml s3_folder = s3://boltops-dev/s3_folder/templates/development/docker.yml



84
85
86
87
88
89
90
# File 'lib/lono/template/upload.rb', line 84

def remote_checksum(path)
  # first convert the local path to the path format that is stored in @checksums keys
  # ./output/templates/docker.yml => s3_folder/templates/development/docker.yml
  pretty_path = path.sub(/^\.\//, '')
  key = "#{@prefix}/#{pretty_path.sub(%r{output/templates/},'')}"
  @checksums[key]
end

#runObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/lono/template/upload.rb', line 16

def run
  load_checksums!

  say "Uploading CloudFormation templates..."
  paths = Dir.glob("#{Lono.config.output_path}/#{@blueprint}/templates/**/*")
  paths.select { |p| File.file?(p) }.each do |path|
    upload(path)
  end
  say "Templates uploaded to s3."
end

#s3_bucketObject

Parse the s3_folder setting and remove the folder portion to leave the “s3_bucket” portion Example:

s3_bucket('s3://mybucket/templates/storage/path')
=> mybucket


109
110
111
# File 'lib/lono/template/upload.rb', line 109

def s3_bucket
  Lono::S3::Bucket.name
end

#s3_https_url(template_path) ⇒ Object



93
94
95
# File 'lib/lono/template/upload.rb', line 93

def s3_https_url(template_path)
  "https://s3.amazonaws.com/#{s3_bucket}/#{@prefix}/#{template_path}"
end

#s3_presigned_url(template_output_path) ⇒ Object

used for cfn/base.rb def set_template_url!(options)



98
99
100
101
102
# File 'lib/lono/template/upload.rb', line 98

def s3_presigned_url(template_output_path)
  template_path = template_output_path.sub('output/templates/','')
  key = "#{@prefix}/#{template_path}"
  s3_presigner.presigned_url(:get_object, bucket: s3_bucket, key: key)
end

#say(message) ⇒ Object



113
114
115
# File 'lib/lono/template/upload.rb', line 113

def say(message)
  puts message unless @options[:quiet]
end

#strip_surrounding_quotes(string) ⇒ Object



46
47
48
# File 'lib/lono/template/upload.rb', line 46

def strip_surrounding_quotes(string)
  string.sub(/^"/,'').sub(/"$/,'')
end

#upload(path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lono/template/upload.rb', line 50

def upload(path)
  return if @options[:noop]

  path = path.sub("#{Lono.root}/",'')
  pretty_path = path.sub(/^\.\//, '')
  key = "#{@prefix}/#{pretty_path.sub(%r{output/templates/},'')}"
  s3_full_path = "s3://#{s3_bucket}/#{key}"

  local_checksum = Digest::MD5.hexdigest(IO.read(path))
  remote_checksum = remote_checksum(path)
  if local_checksum == remote_checksum
    say("Not modified: #{pretty_path} to #{s3_full_path}".color(:yellow)) unless @options[:noop]
    return # do not upload unless the checksum has changed
  end

  resp = s3.put_object(
    body: IO.read(path),
    bucket: s3_bucket,
    key: key,
    storage_class: "REDUCED_REDUNDANCY"
  ) unless @options[:noop]

  # Example output:
  # Uploaded: output/templates/docker.yml to s3://boltops-dev/s3_folder/templates/development/docker.yml
  # Uploaded: output/templates/ecs/private.yml to s3://boltops-dev/s3_folder/templates/development/ecs/private.yml
  message = "Uploaded: #{pretty_path} to #{s3_full_path}".color(:green)
  message = "NOOP: #{message}" if @options[:noop]
  say message
end