Class: Lono::Template::Upload
- Inherits:
-
Object
- Object
- Lono::Template::Upload
- Includes:
- AwsServices, Blueprint::Root
- Defined in:
- lib/lono/template/upload.rb
Instance Method Summary collapse
-
#initialize(blueprint, options = {}) ⇒ Upload
constructor
A new instance of Upload.
-
#load_checksums! ⇒ Object
Read existing files on s3 to grab their md5 checksum.
-
#remote_checksum(path) ⇒ Object
path = ./output/templates/docker.yml s3_folder = s3://boltops-dev/s3_folder/templates/development/docker.yml.
- #run ⇒ Object
-
#s3_bucket ⇒ Object
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.
- #s3_https_url(template_path) ⇒ Object
-
#s3_presigned_url(template_output_path) ⇒ Object
used for cfn/base.rb def set_template_body!(params).
- #say(message) ⇒ Object
- #strip_surrounding_quotes(string) ⇒ Object
- #upload(path) ⇒ Object
Methods included from AwsServices
#cfn, #ec2, #iam, #s3, #s3_presigner, #s3_resource, #sts
Methods included from AwsServices::Util
#find_stack, #rollback_complete?, #stack_exists?, #testing_update?
Methods included from Blueprint::Root
#bundler_groups, #find_blueprint_root, #require_bundle_gems, #set_blueprint_root
Constructor Details
#initialize(blueprint, options = {}) ⇒ Upload
Returns a new instance of Upload.
11 12 13 14 15 16 17 18 19 |
# File 'lib/lono/template/upload.rb', line 11 def initialize(blueprint, ={}) @blueprint, @options = blueprint, @template = @options[:template] || @blueprint Lono::ProjectChecker.check set_blueprint_root(@blueprint) @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.
41 42 43 44 45 46 47 48 49 |
# File 'lib/lono/template/upload.rb', line 41 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
89 90 91 92 93 94 95 |
# File 'lib/lono/template/upload.rb', line 89 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 |
#run ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/lono/template/upload.rb', line 21 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_bucket ⇒ Object
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
114 115 116 |
# File 'lib/lono/template/upload.rb', line 114 def s3_bucket Lono::S3::Bucket.name end |
#s3_https_url(template_path) ⇒ Object
98 99 100 |
# File 'lib/lono/template/upload.rb', line 98 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_body!(params)
103 104 105 106 107 |
# File 'lib/lono/template/upload.rb', line 103 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
118 119 120 |
# File 'lib/lono/template/upload.rb', line 118 def say() puts unless @options[:quiet] end |
#strip_surrounding_quotes(string) ⇒ Object
51 52 53 |
# File 'lib/lono/template/upload.rb', line 51 def strip_surrounding_quotes(string) string.sub(/^"/,'').sub(/"$/,'') end |
#upload(path) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lono/template/upload.rb', line 55 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 = "Uploaded: #{pretty_path} to #{s3_full_path}".color(:green) = "NOOP: #{}" if @options[:noop] say end |