Fix multi generic bucket set up

The previously documented way to specify multiple generic buckets
doesn't work because vagrant-cachier can't enable a bucket type more
than once.

Here we generalize the configs hash that the generic bucket gets making
it possible to specify multiple buckets with a single hash. The
documentation is changed accordingly.

Note that we keep it backwards compatible for single generic bucket
specification.

Issue: https://github.com/fgrehm/vagrant-cachier/issues/99
This commit is contained in:
Gustavo L. de M. Chaves 2014-04-13 17:17:26 -03:00
parent 184330dfb4
commit 8ac8d7f6f6
2 changed files with 26 additions and 12 deletions

View file

@ -10,20 +10,22 @@ end
``` ```
The `:cache_dir` parameter is required. It specifies the directory on the guest The `:cache_dir` parameter is required. It specifies the directory on the guest
that will be cached under the "generic" bucket. that will be cached under the "/tmp/vagrant-cache/generic" bucket.
You may enable more than one generic bucket by giving them different names via You may enable more than one generic bucket by giving them different names,
the `:name` parameter, like this: like this:
```ruby ```ruby
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.cache.enable :generic, { name: "one", cache_dir: "/var/cache/one" } config.cache.enable :generic, {
config.cache.enable :generic, { name: "two", cache_dir: "/var/cache/two" } "one" => { cache_dir: "/var/cache/one" },
"two" => { cache_dir: "/var/cache/two" },
}
end end
``` ```
In this case you get two buckets called "generic-one" and "generic-two" under guest's In this case you get two buckets called "one" and "two" under the guest's
`/tmp/vagrant-cache`. `/tmp/vagrant-cache` directory.
The Generic bucket is useful if you want to implement a caching mechanism by The Generic bucket is useful if you want to implement a caching mechanism by
hand. For instance, if you want to cache your wget downloads under hand. For instance, if you want to cache your wget downloads under
@ -31,7 +33,9 @@ hand. For instance, if you want to cache your wget downloads under
```ruby ```ruby
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.cache.enable :generic, { name: "wget", cache_dir: "/var/cache/wget" } config.cache.enable :generic, {
"wget" => { cache_dir: "/var/cache/wget" },
}
end end
``` ```

View file

@ -3,12 +3,22 @@ module VagrantPlugins
class Bucket class Bucket
class Generic < Bucket class Generic < Bucket
def install def install
# First we normalize the @configs hash as a hash of hashes
if @configs.has_key?(:cache_dir) if @configs.has_key?(:cache_dir)
@name = @configs.has_key?(:name) ? "generic-#{@configs[:name]}" : "generic" @configs = { @name => @configs }
symlink(@configs[:cache_dir]) end
# Now we iterate through all generic buckets's configurations and
# set them up.
@configs.each do |key, conf|
if conf.has_key?(:cache_dir)
symlink(conf[:cache_dir], "/tmp/vagrant-cache/#{key}")
else else
@env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'Generic') @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: "Generic[#{key}]")
end end
end
end end
end end
end end