From 3eede6caf97b99523e27f61ae0473363da179b45 Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Tue, 11 Jun 2013 15:43:42 -0400 Subject: [PATCH] Adds basic chef file_cache_path support. --- lib/vagrant-cachier/bucket.rb | 1 + lib/vagrant-cachier/bucket/chef.rb | 34 +++++++++++++++++++ .../cap/linux/chef_file_cache_path.rb | 31 +++++++++++++++++ lib/vagrant-cachier/plugin.rb | 5 +++ 4 files changed, 71 insertions(+) create mode 100644 lib/vagrant-cachier/bucket/chef.rb create mode 100644 lib/vagrant-cachier/cap/linux/chef_file_cache_path.rb diff --git a/lib/vagrant-cachier/bucket.rb b/lib/vagrant-cachier/bucket.rb index 8462618..fc3be49 100644 --- a/lib/vagrant-cachier/bucket.rb +++ b/lib/vagrant-cachier/bucket.rb @@ -34,6 +34,7 @@ module VagrantPlugins end require_relative "bucket/apt" +require_relative "bucket/chef" require_relative "bucket/gem" require_relative "bucket/pacman" require_relative "bucket/yum" diff --git a/lib/vagrant-cachier/bucket/chef.rb b/lib/vagrant-cachier/bucket/chef.rb new file mode 100644 index 0000000..d4e80fb --- /dev/null +++ b/lib/vagrant-cachier/bucket/chef.rb @@ -0,0 +1,34 @@ +module VagrantPlugins + module Cachier + class Bucket + class Chef < Bucket + def self.capability + :chef_file_cache_path + 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 + else + # TODO: Raise a better error + raise "You've configured a Chef cache for a guest machine that does not support it!" + end + end + end + end + end +end diff --git a/lib/vagrant-cachier/cap/linux/chef_file_cache_path.rb b/lib/vagrant-cachier/cap/linux/chef_file_cache_path.rb new file mode 100644 index 0000000..f56a8a2 --- /dev/null +++ b/lib/vagrant-cachier/cap/linux/chef_file_cache_path.rb @@ -0,0 +1,31 @@ +module VagrantPlugins + module Cachier + module Cap + module Linux + module ChefFileCachePath + def self.chef_provisioner?(machine) + provisioners = machine.config.vm.provisioners + chef_provisioners = [:chef_solo, :chef_client] + compat_provisioners = provisioners.keep_if { |p| chef_provisioners.include? p.name } + + if compat_provisioners.size > 1 + raise "One machine is using multiple chef provisioners, which is unsupported." + end + + using_chef = compat_provisioners.empty? ? false : true + + using_chef + end + + def self.chef_file_cache_path(machine) + chef_file_cache_path = nil + # TODO: Determine paths rather than using default. + chef_file_cache_path = '/var/chef/cache' if chef_provisioner?(machine) + + return chef_file_cache_path + end + end + end + end + end +end diff --git a/lib/vagrant-cachier/plugin.rb b/lib/vagrant-cachier/plugin.rb index 0329ad2..45f77cc 100644 --- a/lib/vagrant-cachier/plugin.rb +++ b/lib/vagrant-cachier/plugin.rb @@ -18,6 +18,11 @@ module VagrantPlugins Cap::Linux::RvmPath end + guest_capability 'linux', 'chef_file_cache_path' do + require_relative 'cap/linux/chef_file_cache_path' + Cap::Linux::ChefFileCachePath + end + guest_capability 'debian', 'apt_cache_dir' do require_relative 'cap/debian/apt_cache_dir' Cap::Debian::AptCacheDir