Class: Lono::Script::Upload

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

Constant Summary

Constants inherited from Base

Base::SCRIPTS_INFO_PATH

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

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

Instance Method Details

#bucket_nameObject

Example:

s3_folder: s3://infra-bucket/cloudformation
bucket_name: infra-bucket


39
40
41
# File 'lib/lono/script/upload.rb', line 39

def bucket_name
  s3_folder.sub('s3://','').split('/').first
end

#dest_folderObject

Removes s3://bucket-name and adds Lono.env. Example:

s3_folder: s3://infra-bucket/cloudformation
bucket_name: cloudformation/development/scripts


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

def dest_folder
  folder = s3_folder.sub('s3://','').split('/')[1..-1].join('/')
  "#{folder}/#{Lono.env}/scripts"
end

#filesizeObject



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

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

#keyObject



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

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

#pretty_time(total_seconds) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/lono/script/upload.rb', line 71

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



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

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

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

#s3_destObject



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

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

#s3_folderObject

s3_folder example:



61
62
63
64
# File 'lib/lono/script/upload.rb', line 61

def s3_folder
  setting = Lono::Setting.new
  setting.s3_folder
end

#s3_resourceObject



66
67
68
# File 'lib/lono/script/upload.rb', line 66

def s3_resource
  @s3_resource ||= Aws::S3::Resource.new
end

#scripts_built?Boolean

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/lono/script/upload.rb', line 52

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

#tarball_pathObject



56
57
58
# File 'lib/lono/script/upload.rb', line 56

def tarball_path
  IO.read(SCRIPTS_INFO_PATH).strip
end

#upload(tarball_path) ⇒ Object



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

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).colorize(:green)
  puts "Time to upload code to s3: #{time_took}"
end