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
Example:
s3_folder: s3://infra-bucket/ec2
bucket_name: infra-bucket
82
83
84
|
# File 'lib/forger/script/upload.rb', line 82
def bucket_name
s3_folder.sub('s3://','').split('/').first
end
|
#compiler ⇒ Object
128
129
130
|
# File 'lib/forger/script/upload.rb', line 128
def compiler
@compiler ||= Compile.new(@options)
end
|
#compressor ⇒ Object
132
133
134
|
# File 'lib/forger/script/upload.rb', line 132
def compressor
@compressor ||= Compress.new(@options)
end
|
#dest_folder ⇒ Object
Removes s3://bucket-name and adds Forger.env. Example:
s3_folder: s3://infra-bucket/ec2
bucket_name: ec2/development/scripts
89
90
91
92
|
# File 'lib/forger/script/upload.rb', line 89
def dest_folder
folder = s3_folder.sub('s3://','').split('/')[1..-1].join('/')
"#{folder}/#{Forger.env}/scripts"
end
|
#empty? ⇒ Boolean
56
57
58
59
60
|
# File 'lib/forger/script/upload.rb', line 56
def empty?
Dir.glob("#{Forger.root}/app/scripts/**/*").select do |path|
File.file?(path)
end.empty?
end
|
#filesize ⇒ Object
66
67
68
|
# File 'lib/forger/script/upload.rb', line 66
def filesize
Filesize.from(File.size(tarball_path).to_s + " B").pretty
end
|
#key ⇒ Object
74
75
76
77
|
# File 'lib/forger/script/upload.rb', line 74
def key
"#{dest_folder}/#{File.basename(tarball_path)}"
end
|
#pretty_time(total_seconds) ⇒ Object
113
114
115
116
117
118
119
120
121
|
# File 'lib/forger/script/upload.rb', line 113
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
|
# File 'lib/forger/script/upload.rb', line 16
def run
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
70
71
72
|
# File 'lib/forger/script/upload.rb', line 70
def s3_dest
"s3://#{bucket_name}/#{key}"
end
|
#s3_folder ⇒ Object
95
96
97
|
# File 'lib/forger/script/upload.rb', line 95
def s3_folder
Forger.settings["s3_folder"]
end
|
#s3_resource ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/forger/script/upload.rb', line 99
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
123
124
125
126
|
# File 'lib/forger/script/upload.rb', line 123
def sh(command)
puts "=> #{command}"
system command
end
|
#tarball_path ⇒ Object
62
63
64
|
# File 'lib/forger/script/upload.rb', line 62
def tarball_path
IO.read(SCRIPTS_INFO_PATH).strip
end
|
#upload(tarball_path) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/forger/script/upload.rb', line 24
def upload(tarball_path)
puts "Uploading scripts.tgz (#{filesize}) to #{s3_dest}".colorize(:green)
obj = s3_resource.bucket(bucket_name).object(key)
start_time = Time.now
if @options[:noop]
puts "NOOP: Not uploading file to s3"
else
upload_to_s3(obj, tarball_path)
end
time_took = pretty_time(Time.now-start_time).colorize(:green)
puts "Time to upload code to s3: #{time_took}"
end
|
#upload_to_s3(obj, tarball_path) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/forger/script/upload.rb', line 37
def upload_to_s3(obj, tarball_path)
obj.upload_file(tarball_path)
rescue Aws::S3::Errors::PermanentRedirect => e
puts "ERROR: #{e.class} #{e.message}".colorize(:red)
puts "The bucket you are trying to upload scripts to is in a different region than the region the instance is being launched in."
puts "You must configured FORGER_S3_ENDPOINT env variable to prevent this error. Example:"
puts " FORGER_S3_ENDPOINT=https://s3.us-west-2.amazonaws.com"
puts "Check your ~/.aws/config for the region being used for the ec2 instance."
exit 1
rescue Aws::S3::Errors::AccessDenied, Aws::S3::Errors::AllAccessDisabled
e = $!
puts "ERROR: #{e.class} #{e.message}".colorize(:red)
puts "You do not have permission to upload scripts to this bucket: #{bucket_name}. Are you sure the right bucket is configured?"
if ENV['AWS_PROFILE']
puts "Also maybe check your AWS_PROFILE env. Current AWS_PROFILE=#{ENV['AWS_PROFILE']}"
end
exit 1
end
|