Class: DistributedCache::Bundle

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, cache_name) ⇒ Bundle

Returns a new instance of Bundle.



7
8
9
10
11
12
# File 'lib/distributed_cache/bundle.rb', line 7

def initialize(config, cache_name)
  @config = config
  @cache_name = cache_name

  @new_files = []
end

Instance Attribute Details

#cache_nameObject (readonly)

Returns the value of attribute cache_name.



5
6
7
# File 'lib/distributed_cache/bundle.rb', line 5

def cache_name
  @cache_name
end

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/distributed_cache/bundle.rb', line 5

def config
  @config
end

#new_filesObject (readonly)

Returns the value of attribute new_files.



5
6
7
# File 'lib/distributed_cache/bundle.rb', line 5

def new_files
  @new_files
end

Instance Method Details

#cache_dirObject



35
36
37
38
39
# File 'lib/distributed_cache/bundle.rb', line 35

def cache_dir
  config.cache_dir.tap do |dir|
    FileUtils.mkdir_p(dir) unless File.exists?(dir)
  end
end

#create_tar_file(file) ⇒ Object



45
46
47
48
49
50
# File 'lib/distributed_cache/bundle.rb', line 45

def create_tar_file(file)
  "#{config.bundle_dir}/#{file}".tap do |f|
    dir = File.dirname f
    FileUtils.mkdir_p(dir) unless File.exists?(dir)
  end
end

#download_file(remote_file, local_file) ⇒ Object



62
63
64
65
66
# File 'lib/distributed_cache/bundle.rb', line 62

def download_file(remote_file, local_file)
  open(local_file, 'wb+') do |f|
    f.write open("http://#{config.file_server_with_port}/#{config.remote_bundle_dir}/#{remote_file}", 'rb').read
  end
end

#download_manifestObject



58
59
60
# File 'lib/distributed_cache/bundle.rb', line 58

def download_manifest
  download_file "#{cache_name}/latest/manifest.yml", manifest_file
end

#installObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/distributed_cache/bundle.rb', line 14

def install
  download_manifest

  manifest.files.each do |file, md5|
    tar_file = create_tar_file file
    if ! valid_tar_file?(tar_file, md5)
      download_file file, tar_file
      raise "invalid tar file (#{tar_file}) with md5 (#{md5})" unless valid_tar_file?(tar_file, md5)
      @new_files << tar_file
    elsif @new_files.size > 0
      @new_files << tar_file
    end
  end

  Dir.chdir(cache_dir) do
    @new_files.each do |tar_file|
      DistributedCache::Utils.untar tar_file
    end
  end
end

#latest_dirObject



52
53
54
55
56
# File 'lib/distributed_cache/bundle.rb', line 52

def latest_dir
  "#{config.bundle_dir}/#{cache_name}".tap do |dir|
    FileUtils.mkdir_p(dir) unless File.exists?(dir)
  end
end

#manifestObject



72
73
74
# File 'lib/distributed_cache/bundle.rb', line 72

def manifest
  @manifest ||= DistributedCache::Manifest.load(config, manifest_file)
end

#manifest_fileObject



68
69
70
# File 'lib/distributed_cache/bundle.rb', line 68

def manifest_file
  "#{latest_dir}/manifest.yml"
end

#valid_tar_file?(file, md5) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/distributed_cache/bundle.rb', line 41

def valid_tar_file?(file, md5)
  File.exists?(file) && Digest::MD5.file(file).to_s == md5
end