Class: Forger::Script::Upload
- Inherits:
-
Base
- Object
- Base
- Forger::Script::Upload
show all
- Extended by:
- Memoist
- Defined in:
- lib/forger/script/upload.rb
Constant Summary
Constants inherited
from Base
Base::BUILD_ROOT, Base::SCRIPTS_INFO_PATH
Instance Method Summary
collapse
Methods inherited from Base
#derandomize, #randomize
Constructor Details
#initialize(options = {}) ⇒ Upload
Returns a new instance of Upload.
11
12
13
14
|
# File 'lib/forger/script/upload.rb', line 11
def initialize(options={})
@options = options
@compile = @options[:compile] ? @options[:compile] : true
end
|
Instance Method Details
#bucket_name ⇒ Object
68
69
70
|
# File 'lib/forger/script/upload.rb', line 68
def bucket_name
Forger::S3::Bucket.name
end
|
#compiler ⇒ Object
101
102
103
|
# File 'lib/forger/script/upload.rb', line 101
def compiler
@compiler ||= Compile.new(@options)
end
|
#compressor ⇒ Object
105
106
107
|
# File 'lib/forger/script/upload.rb', line 105
def compressor
@compressor ||= Compress.new(@options)
end
|
#empty? ⇒ Boolean
44
45
46
47
48
|
# File 'lib/forger/script/upload.rb', line 44
def empty?
Dir.glob("#{Forger.root}/app/scripts/**/*").select do |path|
File.file?(path)
end.empty?
end
|
#ensure_bucket_exists ⇒ Object
#filesize ⇒ Object
54
55
56
|
# File 'lib/forger/script/upload.rb', line 54
def filesize
Filesize.from(File.size(tarball_path).to_s + " B").pretty
end
|
#pretty_time(total_seconds) ⇒ Object
86
87
88
89
90
91
92
93
94
|
# File 'lib/forger/script/upload.rb', line 86
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
|
#run ⇒ Object
16
17
18
19
20
21
22
23
24
|
# File 'lib/forger/script/upload.rb', line 16
def run
return if empty?
ensure_bucket_exists
compiler.compile_scripts if @compile
compressor.compress
upload(tarball_path)
compressor.clean
compiler.clean if @compile and Forger.settings["compile_clean"]
end
|
#s3_dest ⇒ Object
58
59
60
|
# File 'lib/forger/script/upload.rb', line 58
def s3_dest
"s3://#{bucket_name}/#{s3_key}"
end
|
#s3_key ⇒ Object
62
63
64
65
66
|
# File 'lib/forger/script/upload.rb', line 62
def s3_key
dest_folder = "#{Forger.env}/scripts"
"#{dest_folder}/#{File.basename(tarball_path)}"
end
|
#s3_resource ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/forger/script/upload.rb', line 72
def s3_resource
options = {}
options[:endpoint] = ENV['FORGER_S3_ENDPOINT'] if ENV['FORGER_S3_ENDPOINT']
if options[:endpoint]
options[:region] = options[:endpoint].split('.')[1]
end
Aws::S3::Resource.new(options)
end
|
#sh(command) ⇒ Object
96
97
98
99
|
# File 'lib/forger/script/upload.rb', line 96
def sh(command)
puts "=> #{command}"
system command
end
|
#tarball_path ⇒ Object
50
51
52
|
# File 'lib/forger/script/upload.rb', line 50
def tarball_path
IO.read(SCRIPTS_INFO_PATH).strip
end
|
#upload(tarball_path) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/forger/script/upload.rb', line 30
def upload(tarball_path)
if @options[:noop]
puts "NOOP: Not uploading file to s3"
return
end
puts "Uploading scripts.tgz (#{filesize}) to #{s3_dest}".color(:green)
obj = s3_resource.bucket(bucket_name).object(s3_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
|