Class: AwsEc2::Script::Compress

Inherits:
Base
  • Object
show all
Defined in:
lib/aws_ec2/script/compress.rb

Constant Summary

Constants inherited from Base

Base::BUILD_ROOT, Base::SCRIPTS_INFO_PATH

Instance Method Summary collapse

Methods inherited from Base

#derandomize, #initialize, #randomize

Constructor Details

This class inherits a constructor from AwsEc2::Base

Instance Method Details

#cleanObject



25
26
27
# File 'lib/aws_ec2/script/compress.rb', line 25

def clean
  FileUtils.rm_f("#{BUILD_ROOT}/scripts/scripts-#{md5sum}.tgz")
end

#compressObject



5
6
7
8
9
10
11
# File 'lib/aws_ec2/script/compress.rb', line 5

def compress
  reset
  puts "Tarballing #{BUILD_ROOT}/app/scripts folder to scripts.tgz".colorize(:green)
  tarball_path = create_tarball
  save_scripts_info(tarball_path)
  puts "Tarball created at #{tarball_path}"
end

#create_tarballObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aws_ec2/script/compress.rb', line 13

def create_tarball
  # https://apple.stackexchange.com/questions/14980/why-are-dot-underscore-files-created-and-how-can-i-avoid-them
  sh "cd #{BUILD_ROOT}/app && dot_clean ." if system("type dot_clean > /dev/null")

  # https://serverfault.com/questions/110208/different-md5sums-for-same-tar-contents
  # Using tar czf directly results in a new m5sum each time because the gzip
  # timestamp is included.  So using:  tar -c ... | gzip -n
  sh "cd #{BUILD_ROOT}/app && tar -c scripts | gzip -n > scripts.tgz" # temporary app/scripts.tgz file

  rename_with_md5!
end

#md5sumObject

cache this because the file will get removed



44
45
46
# File 'lib/aws_ec2/script/compress.rb', line 44

def md5sum
  @md5sum ||= Digest::MD5.file("#{BUILD_ROOT}/app/scripts.tgz").to_s[0..7]
end

#rename_with_md5!Object

Apppend a md5 to file after it’s been created and moves it to output/scripts/scripts-.tgz



31
32
33
34
35
36
# File 'lib/aws_ec2/script/compress.rb', line 31

def rename_with_md5!
  md5_path = "#{BUILD_ROOT}/scripts/scripts-#{md5sum}.tgz"
  FileUtils.mkdir_p(File.dirname(md5_path))
  FileUtils.mv("#{BUILD_ROOT}/app/scripts.tgz", md5_path)
  md5_path
end

#resetObject



58
59
60
# File 'lib/aws_ec2/script/compress.rb', line 58

def reset
  FileUtils.rm_f(SCRIPTS_INFO_PATH)
end

#save_scripts_info(scripts_name) ⇒ Object



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

def save_scripts_info(scripts_name)
  FileUtils.mkdir_p(File.dirname(SCRIPTS_INFO_PATH))
  IO.write(SCRIPTS_INFO_PATH, scripts_name)
end

#scripts_nameObject

Only avaialble after script has been built.



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

def scripts_name
  IO.read(SCRIPTS_INFO_PATH).strip
end

#sh(command) ⇒ Object



48
49
50
51
# File 'lib/aws_ec2/script/compress.rb', line 48

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