From c95549be0782af7ebcc977acc21469251f42c70d Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Mon, 14 Nov 2016 11:15:03 -0500 Subject: [PATCH 1/4] Add a synced folder to persist chef omnibus packages Consume the new chef_omnibus_cache directory by syncing it to the instance where omnitruck will download the artifacts, so we can cache the packages locally and avoid re-pulling down artifacts that you already have on your workstation. Signed-off-by: Salim Afiune --- lib/kitchen/driver/vagrant.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/kitchen/driver/vagrant.rb b/lib/kitchen/driver/vagrant.rb index 07428e7a..f98b6de8 100644 --- a/lib/kitchen/driver/vagrant.rb +++ b/lib/kitchen/driver/vagrant.rb @@ -248,6 +248,27 @@ def finalize_synced_folders! options || "nil" ] end + add_extra_synced_folders! + end + + # Verify if we are using the Provisioner::ChefBase that now has a + # config parameter for the Chef omnibus cache, if that is set then + # we would like to sync a local folder to the instance so we can + # take advantage of the cache packages that we might have, + # therefore we wont download a package we already have in the cache + def add_extra_synced_folders! + if chef_omnibus_cache + FileUtils.mkdir_p(local_kitchen_cache) + config[:synced_folders].push([ + local_kitchen_cache, + chef_omnibus_cache, + "create: true" + ]) + end + end + + def chef_omnibus_cache + instance.provisioner[:chef_omnibus_cache] if instance end # Truncates the length of `:vm_hostname` to 12 characters for @@ -389,6 +410,12 @@ def update_state(state) state[:rdp_port] = hash["RDPPort"] if hash["RDPPort"] end + # @return [String] full absolute path to the kitchen cache directory + # @api private + def local_kitchen_cache + @local_kitchen_cache ||= File.expand_path("~/.kitchen/cache") + end + # @return [String] full local path to the directory containing the # instance's Vagrantfile # @api private From 589ef048984ddc634046340e805024dca66cd4ed Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Wed, 16 Nov 2016 15:32:39 -0500 Subject: [PATCH 2/4] Added test for add_extra_synced_folders Signed-off-by: Salim Afiune --- spec/kitchen/driver/vagrant_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/kitchen/driver/vagrant_spec.rb b/spec/kitchen/driver/vagrant_spec.rb index b11f6952..1baec60a 100644 --- a/spec/kitchen/driver/vagrant_spec.rb +++ b/spec/kitchen/driver/vagrant_spec.rb @@ -360,6 +360,19 @@ def run_command(_cmd, options = {}) ]) end + describe "when chef_omnibus_cache is set" do + before do + allow(driver_object).to receive(:chef_omnibus_cache). + and_return("/tmp/path") + end + + it "adds the extra synced folder for omnibus cache" do + expect(driver[:synced_folders]).to eq([ + [File.expand_path("~/.kitchen/cache"), "/tmp/path", "create: true"] + ]) + end + end + it "sets :vagrant_binary to 'vagrant' by default" do expect(driver[:vagrant_binary]).to eq("vagrant") end From f986e9e4b5d1af6bc94067dbd548c88a73de69f2 Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Tue, 22 Nov 2016 13:16:50 -0500 Subject: [PATCH 3/4] Implement the Driver::cache_directory interface Signed-off-by: Salim Afiune --- lib/kitchen/driver/vagrant.rb | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/kitchen/driver/vagrant.rb b/lib/kitchen/driver/vagrant.rb index f98b6de8..4504d53e 100644 --- a/lib/kitchen/driver/vagrant.rb +++ b/lib/kitchen/driver/vagrant.rb @@ -169,6 +169,13 @@ def winrm_transport? instance.transport.name.downcase =~ /win_?rm/ end + # Setting up the `cache_directory` to store omnibus packages in cache + # and share a local folder to that directory so that we don't pull them + # down every single time + def cache_directory + windows_os? ? "$env:TEMP\\omnibus\\cache" : "/tmp/omnibus/cache" + end + protected WEBSITE = "http://www.vagrantup.com/downloads.html".freeze @@ -251,26 +258,20 @@ def finalize_synced_folders! add_extra_synced_folders! end - # Verify if we are using the Provisioner::ChefBase that now has a - # config parameter for the Chef omnibus cache, if that is set then - # we would like to sync a local folder to the instance so we can - # take advantage of the cache packages that we might have, - # therefore we wont download a package we already have in the cache + # We would like to sync a local folder to the instance so we can + # take advantage of the packages that we might have in cache, + # therefore we wont download a package we already have def add_extra_synced_folders! - if chef_omnibus_cache + if cache_directory FileUtils.mkdir_p(local_kitchen_cache) config[:synced_folders].push([ local_kitchen_cache, - chef_omnibus_cache, + cache_directory, "create: true" ]) end end - def chef_omnibus_cache - instance.provisioner[:chef_omnibus_cache] if instance - end - # Truncates the length of `:vm_hostname` to 12 characters for # Windows-based operating systems. # From 20858aa560c9b1acf22ea435774f358bc13b084d Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Tue, 22 Nov 2016 13:26:27 -0500 Subject: [PATCH 4/4] Tests for the cache_directory Signed-off-by: Salim Afiune --- spec/kitchen/driver/vagrant_spec.rb | 63 ++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/spec/kitchen/driver/vagrant_spec.rb b/spec/kitchen/driver/vagrant_spec.rb index 1baec60a..46ffe314 100644 --- a/spec/kitchen/driver/vagrant_spec.rb +++ b/spec/kitchen/driver/vagrant_spec.rb @@ -147,6 +147,14 @@ def run_command(_cmd, options = {}) describe "configuration" do + let(:cache_directory_array) do + [ + File.expand_path("~/.kitchen/cache"), + "/tmp/omnibus/cache", + "create: true" + ] + end + %W[centos debian fedora opensuse ubuntu].each do |name| context "for known bento platform names starting with #{name}" do @@ -316,8 +324,8 @@ def run_command(_cmd, options = {}) expect(driver[:ssh]).to eq(:a => "b", :c => { :d => "e" }) end - it "sets :synced_folders to an empty array by default" do - expect(driver[:synced_folders]).to eq([]) + it "sets :synced_folders with the cache_directory by default" do + expect(driver[:synced_folders]).to eq([cache_directory_array]) end it "sets :synced_folders to a custom value" do @@ -326,7 +334,11 @@ def run_command(_cmd, options = {}) ] expect(driver[:synced_folders]).to eq([ - [File.expand_path("/host_path"), "/vm_path", "create: true, type: :nfs"] + [ + File.expand_path("/host_path"), + "/vm_path", "create: true, type: :nfs" + ], + cache_directory_array ]) end @@ -336,7 +348,8 @@ def run_command(_cmd, options = {}) ] expect(driver[:synced_folders]).to eq([ - [File.expand_path("/root/suitey-fooos-99"), "/vm_path", "stuff"] + [File.expand_path("/root/suitey-fooos-99"), "/vm_path", "stuff"], + cache_directory_array ]) end @@ -346,7 +359,8 @@ def run_command(_cmd, options = {}) ] expect(driver[:synced_folders]).to eq([ - [File.expand_path("/kroot/a"), "/vm_path", "stuff"] + [File.expand_path("/kroot/a"), "/vm_path", "stuff"], + cache_directory_array ]) end @@ -356,23 +370,11 @@ def run_command(_cmd, options = {}) ] expect(driver[:synced_folders]).to eq([ - [File.expand_path("/host_path"), "/vm_path", "nil"] + [File.expand_path("/host_path"), "/vm_path", "nil"], + cache_directory_array ]) end - describe "when chef_omnibus_cache is set" do - before do - allow(driver_object).to receive(:chef_omnibus_cache). - and_return("/tmp/path") - end - - it "adds the extra synced folder for omnibus cache" do - expect(driver[:synced_folders]).to eq([ - [File.expand_path("~/.kitchen/cache"), "/tmp/path", "create: true"] - ]) - end - end - it "sets :vagrant_binary to 'vagrant' by default" do expect(driver[:vagrant_binary]).to eq("vagrant") end @@ -431,6 +433,14 @@ def run_command(_cmd, options = {}) context "for windows os_types" do + let(:win_cache_directory_array) do + [ + File.expand_path("~/.kitchen/cache"), + "$env:TEMP\\omnibus\\cache", + "create: true" + ] + end + before { allow(platform).to receive(:os_type).and_return("windows") } it "sets :vm_hostname to nil by default" do @@ -442,6 +452,21 @@ def run_command(_cmd, options = {}) expect(driver[:vm_hostname]).to eq("this-is-a--k") end + + it "sets :synced_folders with the cache_directory by default" do + expect(driver[:synced_folders]).to eq([win_cache_directory_array]) + end + + it "replaces %{instance_name} with instance name in :synced_folders" do + config[:synced_folders] = [ + ["/root/%{instance_name}", "/vm_path", "stuff"] + ] + + expect(driver[:synced_folders]).to eq([ + [File.expand_path("/root/suitey-fooos-99"), "/vm_path", "stuff"], + win_cache_directory_array + ]) + end end end