Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache omnibus download, expose config options #73

Merged
merged 8 commits into from
Apr 25, 2014
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ Vagrant.configure("2") do |config|
end
```

Specify a custom install script:

```ruby
Vagrant.configure("2") do |config|

config.omnibus.install_url = 'http://acme.com/install.sh'
# config.omnibus.install_url = 'http://acme.com/install.msi'
# config.omnibus.install_url = '/some/path/on/the/host'

...

end
```

If [vagrant-cachier](https://github.com/fgrehm/vagrant-cachier) is present
and `config.cache.auto_detect` enabled the downloaded omnibus packages will
be cached by vagrant-cachier. In case you want to turn caching off:

```ruby
Vagrant.configure("2") do |config|

config.omnibus.cache_packages = false

...

end
```

This plugin is also multi-vm aware so it would possible to say install a
different version of Chef on each VM:

Expand Down
51 changes: 44 additions & 7 deletions lib/vagrant-omnibus/action/install_chef.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,51 @@ def call(env)

private

# Determines what flavor of install script should be used to
# install Omnibus Chef package.
# Determines which install script should be used to install the
# Omnibus Chef package. Order of precedence:
# 1. from config
# 2. from env var
# 3. default
def find_install_script
if !ENV['OMNIBUS_INSTALL_URL'].nil?
ENV['OMNIBUS_INSTALL_URL']
elsif windows_guest?
config_install_url || env_install_url || default_install_url
end

def default_install_url
if windows_guest?
'http://www.getchef.com/chef/install.msi'
else
'https://www.getchef.com/chef/install.sh'
end
end

def config_install_url
@machine.config.omnibus.install_script
end

def env_install_url
ENV['OMNIBUS_INSTALL_URL']
end

def cached_omnibus_download_dir
'/tmp/vagrant-cache/vagrant_omnibus'
end

def cache_packages?
@machine.config.omnibus.cache_packages
end

def cachier_present?
defined?(VagrantPlugins::Cachier::Plugin)
end

def cachier_autodetect_enabled?
@machine.config.cache.auto_detect
end

def download_to_cached_dir?
cache_packages? && cachier_present? && cachier_autodetect_enabled?
end

def install_script_name
if windows_guest?
'install.bat'
Expand Down Expand Up @@ -129,8 +162,12 @@ def install(version, env)
if windows_guest?
install_cmd = "cmd.exe /c #{install_script_name} #{version}"
else
install_cmd =
"sh #{install_script_name} -v #{shell_escaped_version} 2>&1"
install_cmd = "sh #{install_script_name}"
install_cmd << " -v #{shell_escaped_version}"
if download_to_cached_dir?
install_cmd << " -d #{cached_omnibus_download_dir}"
end
install_cmd << ' 2>&1'
end
comm.sudo(install_cmd, communication_opts) do |type, data|
if [:stderr, :stdout].include?(type)
Expand Down
8 changes: 7 additions & 1 deletion lib/vagrant-omnibus/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ module Omnibus
class Config < Vagrant.plugin('2', :config)
# @return [String]
# The version of Chef to install.
attr_accessor :chef_version
attr_accessor :chef_version, :install_url, :cache_packages

def initialize
@chef_version = UNSET_VALUE
@install_url = UNSET_VALUE
@cache_packages = UNSET_VALUE
@logger = Log4r::Logger.new('vagrantplugins::omnibus::config')
end

Expand All @@ -41,6 +43,10 @@ def finalize!
# resolve `latest` to a real version
@chef_version = retrieve_latest_chef_version
end
# enable caching by default
@cache_packages = true if @cache_packages == UNSET_VALUE
# nil means default install.sh|msi
@install_url = nil if @install_url == UNSET_VALUE
end

#
Expand Down
24 changes: 24 additions & 0 deletions test/unit/vagrant-omnibus/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
subject(:config) do
instance.tap do |o|
o.chef_version = chef_version if defined?(chef_version)
o.install_url = install_url if defined?(install_url)
o.cache_packages = cache_packages if defined?(cache_packages)
o.finalize!
end
end

describe 'defaults' do
its(:chef_version) { should be_nil }
its(:install_url) { should be_nil }
its(:cache_packages) { should be_true }
end

describe 'resolving `:latest` to a real Chef version' do
Expand All @@ -29,6 +33,26 @@
its(:chef_version) { should match(/\d*\.\d*\.\d*/) }
end

describe 'setting a custom `install_url`' do
let(:install_url) { 'http://some_path.com/install.sh' }
its(:install_url) { should eq('http://some_path.com/install.sh') }
end

describe 'the `cache_packages` config option behaves truthy' do
[true, 'something', :cachier].each do |obj|
describe "when `#{obj}` (#{obj.class})" do
let(:cache_packages) { obj }
its(:cache_packages) { should be_true }
end
end
[nil, false].each do |obj|
describe "when `#{obj}` (#{obj.class})" do
let(:cache_packages) { obj }
its(:cache_packages) { should be_false }
end
end
end

describe 'validate' do
it 'should be no-op' do
expect(subject.validate(machine)).to eq('VagrantPlugins::Omnibus::Config' => [])
Expand Down
2 changes: 1 addition & 1 deletion vagrant-omnibus.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'rubocop', '0.18.1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THANK YOU! I think that was the last sane version of Rubocop! 😄

end