Class: BinCache

Inherits:
Object
  • Object
show all
Defined in:
lib/bincache.rb

Instance Method Summary collapse

Constructor Details

#initializeBinCache

Returns a new instance of BinCache.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bincache.rb', line 17

def initialize()
  ## set cache_dir to'/var/tmp/bincache' or ENV['BINCACHE_DIR'] if it is set 
  @cache_dir = (ENV['BINCACHE_DIR']  && ENV['BINCACHE_DIR']) || '/var/tmp/bincache'

  print_and_exit "S3 bucket and path not set. Please set BINCACHE_S3_BUCKET and BINCACHE_S3_PREFIX" unless ENV['BINCACHE_S3_BUCKET'] && ENV['BINCACHE_S3_PREFIX']
  @bucket = ENV['BINCACHE_S3_BUCKET']
  @prefix = ENV['BINCACHE_S3_PREFIX']


  print_and_exit "S3 keys not set. Please set BINCACHE_S3_ACCESS_KEY and BINCACHE_S3_SECRET_KEY" unless ENV['BINCACHE_S3_ACCESS_KEY'] && ENV['BINCACHE_S3_SECRET_KEY']
  @right_s3 = RightAws::S3.new(ENV['BINCACHE_S3_ACCESS_KEY'],ENV['BINCACHE_S3_SECRET_KEY'])
  @right_s3_bucket = @right_s3.bucket(@bucket)
  @right_s3_interface = RightAws::S3Interface.new(ENV['BINCACHE_S3_ACCESS_KEY'],ENV['BINCACHE_S3_SECRET_KEY'])
end

Instance Method Details

#check_for_hash?(hash) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/bincache.rb', line 56

def check_for_hash?(hash)
  ## return true if the hash is already on our local fs
  return true if File.exists?(File.join(@cache_dir,hash))

  key = RightAws::S3::Key.create( @right_s3_bucket, "#{@prefix}#{hash}" )
  key.exists?
end

#run_series(directory, scripts, cwd = nil, hash = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bincache.rb', line 37

def run_series(directory, scripts, cwd=nil, hash=nil)
  ## exit if given bogus input
  print_and_exit "bogus input in run_series" if directory.nil? || scripts.nil? 

  ## clear out directory if we are starting a new sequence
  `rm -rf #{directory} && mkdir -p #{directory}` && return if scripts.empty?

  hash ||= Digest::MD5.hexdigest("#{directory.inspect}#{scripts.inspect}")
     
  ## pop the last script   
  pop = scripts.pop

  ## recurse if we have not ran this script yet
  eval("#{__method__}(directory,scripts)")  unless check_for_hash?(hash)

  ## step this script
  step(pop,directory,hash,cwd)
end

#run_series_once(directory = nil, scripts = nil, cwd = nil, hash = nil) ⇒ Object



32
33
34
35
# File 'lib/bincache.rb', line 32

def run_series_once(directory=nil, scripts=nil, cwd=nil, hash=nil)
  hash ||= Digest::MD5.hexdigest("#{directory.inspect}#{scripts.inspect}")
  run_series(directory,scripts,cwd) unless File.exist?(File.join(directory,".#{hash}"))
end

#step(script, directory, hash, cwd = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bincache.rb', line 64

def step(script,directory,hash,cwd=nil)
  if download_hash? hash
    `rm -rf #{directory}`
    `cd #{File.dirname directory} && tar -xzf #{File.join(@cache_dir,hash)} `
  else
    run_or_exit "mkdir -p #{directory} #{@cache_dir}"
    Dir.chdir cwd unless cwd == nil
    puts "pwd = #{`pwd`}"
    run_or_exit(script, cwd)
    run_or_exit "touch #{File.join directory, '.' + hash }"
    run_or_exit "cd #{File.dirname directory} && tar -czf #{@cache_dir}/#{hash} #{File.basename directory} "
    upload_file("#{@cache_dir}/#{hash}")
  end
end