Skip to content

Commit

Permalink
Allow explicit disabling of auto detected buckets
Browse files Browse the repository at this point in the history
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 fgrehm#115
  • Loading branch information
electrofelix authored and Darragh Bailey committed Mar 3, 2015
1 parent 2bb7f21 commit 7807d2b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
16 changes: 16 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/vagrant-cachier/action/install_buckets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def configure_cache_buckets(env)
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
Expand Down
4 changes: 4 additions & 0 deletions lib/vagrant-cachier/bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def self.inherited(base)
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
Expand Down
16 changes: 14 additions & 2 deletions lib/vagrant-cachier/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ def initialize
@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)
Expand All @@ -32,6 +37,10 @@ def validate(machine)
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

Expand All @@ -50,14 +59,17 @@ def finalize!

@auto_detect = true if @auto_detect == UNSET_VALUE
@synced_folder_opts = nil if @synced_folder_opts == UNSET_VALUE
@buckets = @buckets ? @buckets.dup : {}
end

private

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
3 changes: 3 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bucket>` 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.
Expand Down
15 changes: 15 additions & 0 deletions spec/acceptance/fixtures/auto-detect-disable-apt.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions spec/acceptance/sanity_check.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7807d2b

Please sign in to comment.