Class: VagrantPlugins::Cachier::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-cachier/bucket.rb,
lib/vagrant-cachier/bucket/apt.rb,
lib/vagrant-cachier/bucket/gem.rb,
lib/vagrant-cachier/bucket/npm.rb,
lib/vagrant-cachier/bucket/rvm.rb,
lib/vagrant-cachier/bucket/yum.rb,
lib/vagrant-cachier/bucket/chef.rb,
lib/vagrant-cachier/bucket/pacman.rb,
lib/vagrant-cachier/bucket/zypper.rb,
lib/vagrant-cachier/bucket/generic.rb,
lib/vagrant-cachier/bucket/composer.rb,
lib/vagrant-cachier/bucket/apt_lists.rb,
lib/vagrant-cachier/bucket/apt_cacher.rb

Direct Known Subclasses

Apt, AptCacher, AptLists, Chef, Composer, Gem, Generic, Npm, Pacman, Rvm, Yum, Zypper

Defined Under Namespace

Classes: Apt, AptCacher, AptLists, Chef, Composer, Gem, Generic, Npm, Pacman, Rvm, Yum, Zypper

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, env, configs) ⇒ Bucket

Returns a new instance of Bucket.



27
28
29
30
31
# File 'lib/vagrant-cachier/bucket.rb', line 27

def initialize(name, env, configs)
  @name    = name
  @env     = env
  @configs = configs
end

Class Method Details

.auto_detect(env) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/vagrant-cachier/bucket.rb', line 9

def self.auto_detect(env)
  @buckets.each do |bucket|
    if bucket.respond_to?(:capability) && env[:machine].guest.capability?(bucket.capability)
      env[:machine].config.cache.enable bucket.bucket_name
    end
  end
end

.bucket_nameObject



17
18
19
20
# File 'lib/vagrant-cachier/bucket.rb', line 17

def self.bucket_name
  class_name = self.name.split('::').last
  class_name.scan(/[A-Z][a-z]*/).map{|x| x.downcase}.join("_")
end

.inherited(base) ⇒ Object



4
5
6
7
# File 'lib/vagrant-cachier/bucket.rb', line 4

def self.inherited(base)
  @buckets ||= []
  @buckets << base
end

.install(name, env, configs) ⇒ Object



22
23
24
25
# File 'lib/vagrant-cachier/bucket.rb', line 22

def self.install(name, env, configs)
  bucket = const_get(name.to_s.split("_").map{|x| x.capitalize}.join(""))
  bucket.new(name, env, configs).install
end

Instance Method Details

#commObject



41
42
43
# File 'lib/vagrant-cachier/bucket.rb', line 41

def comm
  machine.communicate
end

#empty_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/vagrant-cachier/bucket.rb', line 79

def empty_dir?(path)
  not comm.test("test \"$(ls -A #{path} 2>/dev/null)\"")
end

#guestObject



37
38
39
# File 'lib/vagrant-cachier/bucket.rb', line 37

def guest
  machine.guest
end

#machineObject



33
34
35
# File 'lib/vagrant-cachier/bucket.rb', line 33

def machine
  @env[:machine]
end

TODO: “merge” symlink and user_symlink methods



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vagrant-cachier/bucket.rb', line 46

def symlink(guest_path, bucket_path = "/tmp/vagrant-cache/#{@name}", create_parent: true)
  return if @env[:cache_dirs].include?(guest_path)

  @env[:cache_dirs] << guest_path
  comm.execute("mkdir -p #{bucket_path}")
  unless symlink?(guest_path)
    comm.sudo("mkdir -p `dirname #{guest_path}`") if create_parent
    if empty_dir?(bucket_path) && !empty_dir?(guest_path)
      # Warm up cache with guest machine data
      comm.sudo("shopt -s dotglob && mv #{guest_path}/* #{bucket_path}")
    end
    comm.sudo("rm -rf #{guest_path}")
    comm.sudo("ln -s #{bucket_path} #{guest_path}")
  end
end

#symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/vagrant-cachier/bucket.rb', line 83

def symlink?(path)
  comm.test("test -L #{path}")
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vagrant-cachier/bucket.rb', line 62

def user_symlink(guest_path)
  return if @env[:cache_dirs].include?(guest_path)

  @env[:cache_dirs] << guest_path
  bucket_path = "/tmp/vagrant-cache/#{@name}"
  comm.execute("mkdir -p #{bucket_path}")
  unless symlink?(guest_path)
    comm.execute("mkdir -p `dirname #{guest_path}`")
    if empty_dir?(bucket_path) && !empty_dir?(guest_path)
      # Warm up cache with guest machine data
      comm.execute("shopt -s dotglob && mv #{guest_path}/* #{bucket_path}")
    end
    comm.execute("rm -rf #{guest_path}")
    comm.execute("ln -s #{bucket_path} #{guest_path}")
  end
end