From e6464d195110a759daa39137e25ed56df6e979e6 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Fri, 21 Feb 2014 20:39:40 -0300 Subject: [PATCH] Warm up empty buckets with Guest VM files --- CHANGELOG.md | 3 +-- lib/vagrant-cachier/bucket.rb | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9edb27c..effb75e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,9 +20,8 @@ BACKWARDS INCOMPATIBILITY: FEATURES: + - Warm up cache buckets with files available on guest in case bucket is empty - Support for offline provisioning of apt-packages by caching `/var/lib/apt/lists` [GH-84] - Please note that before warming up the cache for this bucket you'll need to `apt-get update` - so that package lists are downloaded. - Support for specifying custom cache bucket synced folder opts - Support to force disabe the plugin [GH-72] - Automatically disable the plugin for cloud providers [GH-45] diff --git a/lib/vagrant-cachier/bucket.rb b/lib/vagrant-cachier/bucket.rb index 1e6852c..d7a5fbd 100644 --- a/lib/vagrant-cachier/bucket.rb +++ b/lib/vagrant-cachier/bucket.rb @@ -42,14 +42,20 @@ module VagrantPlugins machine.communicate end + # TODO: "merge" symlink and user_symlink methods 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}") + unless symlink?(guest_path) comm.sudo("mkdir -p `dirname #{guest_path}`") if create_parent + # Bucket is empty and guest path exists + if empty_dir?(bucket_path) && directory?(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 @@ -60,12 +66,29 @@ module VagrantPlugins @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}") + unless symlink?(guest_path) comm.execute("mkdir -p `dirname #{guest_path}`") + # Bucket is empty and guest path exists + if empty_dir?(bucket_path) && directory?(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 + + def empty_dir?(path) + not comm.test("test \"$(ls -A #{path} 2>/dev/null)\"") + end + + def symlink?(path) + comm.test("test -L #{path}") + end + + def directory?(path) + comm.test("test -d #{path}") + end end end end