From 7807d2bbaa27acc1c3f9cc4930411cfaf1b82733 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Mon, 9 Feb 2015 13:31:35 +0000 Subject: [PATCH] Allow explicit disabling of auto detected buckets Add config option to identify buckets that should not be automatically enabled with using `config.cache.auto_detect = true`. After turning on the auto_detect behaviour, with this change you can now explicitly disable any bucket type through your Vagrantfile e.g. `config.cache.disable = :chef_gem`. Fixes #115 --- docs/usage.md | 16 ++++++++++++++++ lib/vagrant-cachier/action/install_buckets.rb | 1 + lib/vagrant-cachier/bucket.rb | 4 ++++ lib/vagrant-cachier/config.rb | 16 ++++++++++++++-- locales/en.yml | 3 +++ .../fixtures/auto-detect-disable-apt.rb | 15 +++++++++++++++ spec/acceptance/sanity_check.bats | 16 ++++++++++++++++ 7 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 spec/acceptance/fixtures/auto-detect-disable-apt.rb diff --git a/docs/usage.md b/docs/usage.md index 09e82fa..105ef7e 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -95,6 +95,22 @@ end _Please refer to the "Available Buckets" menu above to find out which buckets are supported._ +## Disable buckets in auto-detect mode + +If for some reason you need to disable certain buckets but want the remaining +buckets to be configured you can do so by disabling on an individual basis in +your `Vagrantfile`: + +```ruby +Vagrant.configure("2") do |config| + config.cache.auto_detect = true + config.cache.disable = :chef_gem +end +``` + +_Please refer to the "Available Buckets" menu above to find out which buckets +are supported._ + ## Custom cache buckets synced folders options For fine grained control over the cache bucket synced folder options you can use diff --git a/lib/vagrant-cachier/action/install_buckets.rb b/lib/vagrant-cachier/action/install_buckets.rb index 8a749cf..e8fe0a8 100644 --- a/lib/vagrant-cachier/action/install_buckets.rb +++ b/lib/vagrant-cachier/action/install_buckets.rb @@ -39,6 +39,7 @@ module VagrantPlugins env[:ui].info 'Configuring cache buckets...' cache_config = env[:machine].config.cache cache_config.buckets.each do |bucket_name, configs| + next if configs[:disabled] @logger.info "Installing #{bucket_name} with configs #{configs.inspect}" Bucket.install(bucket_name, env, configs) end diff --git a/lib/vagrant-cachier/bucket.rb b/lib/vagrant-cachier/bucket.rb index ac4e76b..9b2c270 100644 --- a/lib/vagrant-cachier/bucket.rb +++ b/lib/vagrant-cachier/bucket.rb @@ -9,6 +9,10 @@ module VagrantPlugins def self.auto_detect(env) @buckets.each do |bucket| if bucket.respond_to?(:capability) && env[:machine].guest.capability?(bucket.capability) + if env[:machine].config.cache.buckets.fetch(bucket.bucket_name, {})[:disabled] + env[:machine].ui.warn("Ignoring bucket `#{bucket.bucket_name}` disabled by config") + next + end env[:machine].config.cache.enable bucket.bucket_name end end diff --git a/lib/vagrant-cachier/config.rb b/lib/vagrant-cachier/config.rb index ad8495c..73070f9 100644 --- a/lib/vagrant-cachier/config.rb +++ b/lib/vagrant-cachier/config.rb @@ -11,10 +11,15 @@ module VagrantPlugins @auto_detect = UNSET_VALUE @synced_folder_opts = UNSET_VALUE @ui = Vagrant::UI::Colored.new + @buckets = {} end def enable(bucket, opts = {}) - (@buckets ||= {})[bucket] = opts + bucket_set(bucket.to_s, opts) + end + + def disable=(bucket) + bucket_set(bucket.to_s, {:disabled => true}) end def validate(machine) @@ -32,6 +37,10 @@ module VagrantPlugins cache_scope: @scope) end + if !@auto_detect && @buckets.values.each.any? { |x| x[:disabled] } + errors << I18n.t('vagrant_cachier.disable_requires_auto') + end + { "vagrant cachier" => errors } end @@ -50,7 +59,6 @@ module VagrantPlugins @auto_detect = true if @auto_detect == UNSET_VALUE @synced_folder_opts = nil if @synced_folder_opts == UNSET_VALUE - @buckets = @buckets ? @buckets.dup : {} end private @@ -58,6 +66,10 @@ module VagrantPlugins def backed_by_cloud_provider?(machine) CLOUD_PROVIDERS.include?(machine.provider_name.to_s) end + + def bucket_set(bucket, opts = {}) + @buckets[bucket] = opts + end end end end diff --git a/locales/en.yml b/locales/en.yml index 445c0d7..40ecc3d 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -6,6 +6,9 @@ en: Skipping %{bucket} cache bucket as the guest machine does not support it unknown_cache_scope: |- Unknown cache scope '%{cache_scope}' (allowed scopes: %{allowed}) + disable_requires_auto: |- + `config.cache.disable ` only works if `config.cache.auto_detect` + is set to 'true'. nfs_required: |- The '%{bucket}' cache bucket requires NFS to be enabled, please add `config.cache.synced_folder_opts = {type: :nfs}` to your Vagrantfile. diff --git a/spec/acceptance/fixtures/auto-detect-disable-apt.rb b/spec/acceptance/fixtures/auto-detect-disable-apt.rb new file mode 100644 index 0000000..12e5247 --- /dev/null +++ b/spec/acceptance/fixtures/auto-detect-disable-apt.rb @@ -0,0 +1,15 @@ +Vagrant.configure("2") do |config| + config.vm.provider :virtualbox do |_, override| + override.vm.box = "chef/ubuntu-14.04" + end + + config.vm.provider :lxc do |_, override| + override.vm.box = "fgrehm/trusty64-lxc" + end + + config.cache.auto_detect = true + config.cache.disable = :apt + config.cache.scope = :machine + + config.vm.provision :shell, inline: 'apt-get update && apt-get install -y git' +end diff --git a/spec/acceptance/sanity_check.bats b/spec/acceptance/sanity_check.bats index 9635b3b..12fefdd 100644 --- a/spec/acceptance/sanity_check.bats +++ b/spec/acceptance/sanity_check.bats @@ -43,3 +43,19 @@ load test_helper empty_cache } + +@test "APT cache bucket disabled skips the cache dir properly" { + configure_env "auto-detect-disable-apt.rb" + + test ! -d tmp/.vagrant/machines/default/cache/apt + + vagrant_up + [ "$status" -eq 0 ] + + # Make sure cache dir does not exist + test ! -d tmp/.vagrant/machines/default/cache/apt + + vagrant_destroy + + empty_cache +}