Class: Lono::Script::Upload

Inherits:
Base
  • Object
show all
Includes:
AwsServices
Defined in:
lib/lono/script/upload.rb

Constant Summary

Constants inherited from Base

Base::SCRIPTS_INFO_PATH

Instance Method Summary collapse

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 inherited from Base

#initialize

Methods included from Blueprint::Root

#bundler_groups, #find_blueprint_root, #require_bundle_gems, #set_blueprint_root

Constructor Details

This class inherits a constructor from Lono::Script::Base

Instance Method Details

#bucket_nameObject



37
38
39
# File 'lib/lono/script/upload.rb', line 37

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

#dest_folderObject



41
42
43
# File 'lib/lono/script/upload.rb', line 41

def dest_folder
  "#{Lono.env}/scripts"
end

#filesizeObject



24
25
26
# File 'lib/lono/script/upload.rb', line 24

def filesize
  Filesize.from(File.size(tarball_path).to_s + " B").pretty
end

#keyObject



32
33
34
35
# File 'lib/lono/script/upload.rb', line 32

def key
  # Example key: cloudformation/development/scripts/scripts-md5
  "#{dest_folder}/#{File.basename(tarball_path)}"
end

#pretty_time(total_seconds) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/lono/script/upload.rb', line 55

def pretty_time(total_seconds)
  minutes = (total_seconds / 60) % 60
  seconds = total_seconds % 60
  if total_seconds < 60
    "#{seconds.to_i}s"
  else
    "#{minutes.to_i}m #{seconds.to_i}s"
  end
end

#runObject



7
8
9
10
11
12
13
# File 'lib/lono/script/upload.rb', line 7

def run
  Lono::ProjectChecker.check
  return unless scripts_built?

  upload(tarball_path)
  puts "Uploaded #{File.basename(s3_dest)} to s3."
end

#s3_destObject



28
29
30
# File 'lib/lono/script/upload.rb', line 28

def s3_dest
  "s3://#{bucket_name}/#{key}"
end

#scripts_built?Boolean

Scripts are only built if the app/scripts folder is non empty

Returns:

  • (Boolean)


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

def scripts_built?
  File.exist?(SCRIPTS_INFO_PATH) && !tarball_path.empty?
end

#tarball_pathObject



50
51
52
# File 'lib/lono/script/upload.rb', line 50

def tarball_path
  IO.read(SCRIPTS_INFO_PATH).strip
end

#upload(tarball_path) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/lono/script/upload.rb', line 15

def upload(tarball_path)
  puts "Uploading scripts.tgz (#{filesize}) to #{s3_dest}"
  obj = s3_resource.bucket(bucket_name).object(key)
  start_time = Time.now
  obj.upload_file(tarball_path)
  time_took = pretty_time(Time.now-start_time).color(:green)
  puts "Time to upload code to s3: #{time_took}"
end