Class: AwsEc2::Script::Upload

Inherits:
Base
  • Object
show all
Defined in:
lib/aws_ec2/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.



8
9
10
11
# File 'lib/aws_ec2/script/upload.rb', line 8

def initialize(options={})
  @options = options
  @compile = @options[:compile] ? @options[:compile] : true
end

Instance Method Details

#bucket_nameObject

Example:

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


54
55
56
# File 'lib/aws_ec2/script/upload.rb', line 54

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

#compilerObject



91
92
93
# File 'lib/aws_ec2/script/upload.rb', line 91

def compiler
  @compiler ||= Compile.new(@options)
end

#compressorObject



95
96
97
# File 'lib/aws_ec2/script/upload.rb', line 95

def compressor
  @compressor ||= Compress.new(@options)
end

#dest_folderObject

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

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


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

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

#filesizeObject



38
39
40
# File 'lib/aws_ec2/script/upload.rb', line 38

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

#keyObject



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

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

#pretty_time(total_seconds) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/aws_ec2/script/upload.rb', line 76

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



13
14
15
16
17
18
19
# File 'lib/aws_ec2/script/upload.rb', line 13

def run
  compiler.compile_scripts if @compile
  compressor.compress
  upload(tarball_path)
  compressor.clean
  compiler.clean if @compile and AwsEc2.settings["compile_clean"]
end

#s3_destObject



42
43
44
# File 'lib/aws_ec2/script/upload.rb', line 42

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

#s3_folderObject

s3_folder example:



67
68
69
# File 'lib/aws_ec2/script/upload.rb', line 67

def s3_folder
  AwsEc2.settings["s3_folder"]
end

#s3_resourceObject



71
72
73
# File 'lib/aws_ec2/script/upload.rb', line 71

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

#sh(command) ⇒ Object



86
87
88
89
# File 'lib/aws_ec2/script/upload.rb', line 86

def sh(command)
  puts "=> #{command}"
  system command
end

#tarball_pathObject



34
35
36
# File 'lib/aws_ec2/script/upload.rb', line 34

def tarball_path
  IO.read(SCRIPTS_INFO_PATH).strip
end

#upload(tarball_path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aws_ec2/script/upload.rb', line 21

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
    obj.upload_file(tarball_path)
  end
  time_took = pretty_time(Time.now-start_time).colorize(:green)
  puts "Time to upload code to s3: #{time_took}"
end