diff --git a/lib/vagrant-cachier/bucket.rb b/lib/vagrant-cachier/bucket.rb index 66fbd6b..5aa7cb5 100644 --- a/lib/vagrant-cachier/bucket.rb +++ b/lib/vagrant-cachier/bucket.rb @@ -29,6 +29,43 @@ module VagrantPlugins @env = env @configs = configs end + + def machine + @env[:machine] + end + + def guest + machine.guest + end + + def comm + machine.communicate + end + + 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 comm.test("test -L #{guest_path}") + comm.sudo("rm -rf #{guest_path}") + comm.sudo("mkdir -p `dirname #{guest_path}`") if create_parent + comm.sudo("ln -s #{bucket_path} #{guest_path}") + end + end + + 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 comm.test("test -L #{guest_path}") + comm.execute("rm -rf #{guest_path}") + comm.execute("mkdir -p `dirname #{guest_path}`") if create_parent + comm.execute("ln -s #{bucket_path} #{guest_path}") + end + end end end end diff --git a/lib/vagrant-cachier/bucket/apt.rb b/lib/vagrant-cachier/bucket/apt.rb index 3b326df..44cf0fc 100644 --- a/lib/vagrant-cachier/bucket/apt.rb +++ b/lib/vagrant-cachier/bucket/apt.rb @@ -7,22 +7,13 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:apt_cache_dir) guest_path = guest.capability(:apt_cache_dir) - @env[:cache_dirs] << guest_path + return if @env[:cache_dirs].include?(guest_path) - machine.communicate.tap do |comm| - comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}/partial") - unless comm.test("test -L #{guest_path}") - comm.sudo("rm -rf #{guest_path}") - comm.sudo("mkdir -p `dirname #{guest_path}`") - comm.sudo("ln -s /tmp/vagrant-cache/#{@name} #{guest_path}") - end - end + comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}/partial") + symlink(guest_path) else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'APT') end diff --git a/lib/vagrant-cachier/bucket/apt_cacher.rb b/lib/vagrant-cachier/bucket/apt_cacher.rb index 57c6f8d..5e81405 100644 --- a/lib/vagrant-cachier/bucket/apt_cacher.rb +++ b/lib/vagrant-cachier/bucket/apt_cacher.rb @@ -9,22 +9,10 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:apt_cacher_dir) if guest_path = guest.capability(:apt_cacher_dir) if machine.config.cache.enable_nfs - @env[:cache_dirs] << guest_path - - machine.communicate.tap do |comm| - comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}") - unless comm.test("test -L #{guest_path}") - comm.sudo("rm -rf #{guest_path}") - comm.sudo("mkdir -p `dirname #{guest_path}`") - comm.sudo("ln -s /tmp/vagrant-cache/#{@name} #{guest_path}") - end - end + symlink(guest_path) else @env[:ui].info I18n.t('vagrant_cachier.nfs_required', bucket: 'apt-cacher') end diff --git a/lib/vagrant-cachier/bucket/apt_lists.rb b/lib/vagrant-cachier/bucket/apt_lists.rb index 65ed60f..e6bef12 100644 --- a/lib/vagrant-cachier/bucket/apt_lists.rb +++ b/lib/vagrant-cachier/bucket/apt_lists.rb @@ -7,22 +7,13 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:apt_lists_dir) guest_path = guest.capability(:apt_lists_dir) - @env[:cache_dirs] << guest_path + return if @env[:cache_dirs].include?(guest_path) - machine.communicate.tap do |comm| - comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}/partial") - unless comm.test("test -L #{guest_path}") - comm.sudo("rm -rf #{guest_path}") - comm.sudo("mkdir -p `dirname #{guest_path}`") - comm.sudo("ln -s /tmp/vagrant-cache/#{@name} #{guest_path}") - end - end + comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}/partial") + symlink(guest_path) else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'apt-lists') end diff --git a/lib/vagrant-cachier/bucket/chef.rb b/lib/vagrant-cachier/bucket/chef.rb index b78bbe4..ac28aaa 100644 --- a/lib/vagrant-cachier/bucket/chef.rb +++ b/lib/vagrant-cachier/bucket/chef.rb @@ -7,22 +7,9 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:chef_file_cache_path) guest_path = guest.capability(:chef_file_cache_path) - - @env[:cache_dirs] << guest_path - - machine.communicate.tap do |comm| - comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}") - unless comm.test("test -L #{guest_path}") - comm.sudo("rm -rf #{guest_path}") - comm.sudo("mkdir -p `dirname #{guest_path}`") - comm.sudo("ln -s /tmp/vagrant-cache/#{@name} #{guest_path}") - end - end + symlink(guest_path) if guest_path else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'Chef') end diff --git a/lib/vagrant-cachier/bucket/composer.rb b/lib/vagrant-cachier/bucket/composer.rb index cb939ad..f13661e 100644 --- a/lib/vagrant-cachier/bucket/composer.rb +++ b/lib/vagrant-cachier/bucket/composer.rb @@ -7,22 +7,10 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:composer_path) if composer_path = guest.capability(:composer_path) bucket_path = "/tmp/vagrant-cache/#{@name}" - @env[:cache_dirs] << composer_path - - machine.communicate.tap do |comm| - comm.execute("mkdir -p #{bucket_path}") - - unless comm.test("test -L #{composer_path}") - comm.sudo("rm -rf #{composer_path}") - comm.sudo("ln -s #{bucket_path} #{composer_path}") - end - end + symlink(composer_path, create_parent: false) end else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'Composer') diff --git a/lib/vagrant-cachier/bucket/gem.rb b/lib/vagrant-cachier/bucket/gem.rb index e166520..7e7b886 100644 --- a/lib/vagrant-cachier/bucket/gem.rb +++ b/lib/vagrant-cachier/bucket/gem.rb @@ -7,26 +7,13 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:gemdir) if gemdir_path = guest.capability(:gemdir) - prefix = gemdir_path.split('/').last - bucket_path = "/tmp/vagrant-cache/#{@name}/#{prefix}" - machine.communicate.tap do |comm| - comm.execute("mkdir -p #{bucket_path}") + prefix = gemdir_path.split('/').last + bucket_path = "/tmp/vagrant-cache/#{@name}/#{prefix}" + gem_cache_path = "#{gemdir_path}/cache" - gem_cache_path = "#{gemdir_path}/cache" - - @env[:cache_dirs] << gem_cache_path - - unless comm.test("test -L #{gem_cache_path}") - comm.sudo("rm -rf #{gem_cache_path}") - comm.sudo("mkdir -p `dirname #{gem_cache_path}`") - comm.sudo("ln -s #{bucket_path} #{gem_cache_path}") - end - end + symlink(gem_cache_path, bucket_path) end else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'RubyGems') diff --git a/lib/vagrant-cachier/bucket/npm.rb b/lib/vagrant-cachier/bucket/npm.rb index f10a2ce..cb3bca7 100644 --- a/lib/vagrant-cachier/bucket/npm.rb +++ b/lib/vagrant-cachier/bucket/npm.rb @@ -7,22 +7,9 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:npm_cache_dir) guest_path = guest.capability(:npm_cache_dir) - - @env[:cache_dirs] << guest_path - - machine.communicate.tap do |comm| - comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}") - unless comm.test("test -L #{guest_path}") - comm.execute("rm -rf #{guest_path}") - comm.execute("mkdir -p `dirname #{guest_path}`") - comm.execute("ln -s /tmp/vagrant-cache/#{@name} #{guest_path}") - end - end + user_symlink(guest_path) if guest_path else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'npm') end diff --git a/lib/vagrant-cachier/bucket/pacman.rb b/lib/vagrant-cachier/bucket/pacman.rb index c9d9cdc..0f91a7e 100644 --- a/lib/vagrant-cachier/bucket/pacman.rb +++ b/lib/vagrant-cachier/bucket/pacman.rb @@ -7,22 +7,9 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:pacman_cache_dir) guest_path = guest.capability(:pacman_cache_dir) - - @env[:cache_dirs] << guest_path - - machine.communicate.tap do |comm| - comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}") - unless comm.test("test -L #{guest_path}") - comm.sudo("rm -rf #{guest_path}") - comm.sudo("mkdir -p `dirname #{guest_path}`") - comm.sudo("ln -s /tmp/vagrant-cache/#{@name} #{guest_path}") - end - end + symlink(guest_path) else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'Pacman') end diff --git a/lib/vagrant-cachier/bucket/rvm.rb b/lib/vagrant-cachier/bucket/rvm.rb index ce57ff8..2c51bf9 100644 --- a/lib/vagrant-cachier/bucket/rvm.rb +++ b/lib/vagrant-cachier/bucket/rvm.rb @@ -7,26 +7,13 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:rvm_path) if rvm_path = guest.capability(:rvm_path) - prefix = rvm_path.split('/').last - bucket_path = "/tmp/vagrant-cache/#{@name}/#{prefix}" - machine.communicate.tap do |comm| - comm.execute("mkdir -p #{bucket_path}") + prefix = rvm_path.split('/').last + bucket_path = "/tmp/vagrant-cache/#{@name}/#{prefix}" + rvm_cache_path = "#{rvm_path}/archives" - rvm_cache_path = "#{rvm_path}/archives" - - @env[:cache_dirs] << rvm_cache_path - - unless comm.test("test -L #{rvm_cache_path}") - comm.sudo("rm -rf #{rvm_cache_path}") - comm.sudo("mkdir -p `dirname #{rvm_cache_path}`") - comm.sudo("ln -s #{bucket_path} #{rvm_cache_path}") - end - end + symlink(rvm_cache_path, bucket_path) end else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'RVM') diff --git a/lib/vagrant-cachier/bucket/yum.rb b/lib/vagrant-cachier/bucket/yum.rb index d43af7c..b58ce19 100644 --- a/lib/vagrant-cachier/bucket/yum.rb +++ b/lib/vagrant-cachier/bucket/yum.rb @@ -7,25 +7,14 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:yum_cache_dir) guest_path = guest.capability(:yum_cache_dir) + return if @env[:cache_dirs].include?(guest_path) - @env[:cache_dirs] << guest_path + # Ensure caching is enabled + comm.sudo("sed -i 's/keepcache=0/keepcache=1/g' /etc/yum.conf") - machine.communicate.tap do |comm| - # Ensure caching is enabled - comm.sudo("sed -i 's/keepcache=0/keepcache=1/g' /etc/yum.conf") - - comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}") - unless comm.test("test -L #{guest_path}") - comm.sudo("rm -rf #{guest_path}") - comm.sudo("mkdir -p `dirname #{guest_path}`") - comm.sudo("ln -s /tmp/vagrant-cache/#{@name} #{guest_path}") - end - end + symlink(guest_path) else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'Yum') end diff --git a/lib/vagrant-cachier/bucket/zypper.rb b/lib/vagrant-cachier/bucket/zypper.rb index f556b21..26aec74 100644 --- a/lib/vagrant-cachier/bucket/zypper.rb +++ b/lib/vagrant-cachier/bucket/zypper.rb @@ -7,25 +7,14 @@ module VagrantPlugins end def install - machine = @env[:machine] - guest = machine.guest - if guest.capability?(:zypper_cache_dir) guest_path = guest.capability(:zypper_cache_dir) + return if @env[:cache_dirs].include?(guest_path) - @env[:cache_dirs] << guest_path + # Ensure caching is enabled + comm.sudo("zypper modifyrepo --keep-packages --all") - machine.communicate.tap do |comm| - # Ensure caching is enabled - comm.sudo("zypper modifyrepo --keep-packages --all") - - comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}") - unless comm.test("test -L #{guest_path}") - comm.sudo("rm -rf #{guest_path}") - comm.sudo("mkdir -p `dirname #{guest_path}`") - comm.sudo("ln -s /tmp/vagrant-cache/#{@name} #{guest_path}") - end - end + symlink(guest_path) else @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'Zypper') end