Skip to content

Commit

Permalink
Only set custom :box & :box_url values for known Bento boxes.
Browse files Browse the repository at this point in the history
The default values for the `:box` and `:box_url` attributes were always
implicitly based on the Bento project's base boxes
(https://github.com/chef/bento). This meant that for a small number of
common/known platforms, the box name and URLs could be pre-calculated to
help with initial out-of-the-box experience.

For example, given a platform name of `"ubuntu-14.04"`, a default box
name of `"opscode-ubuntu-14.04"` and box URL could be computed (but only
for VirtualBox or VMware-based providers). Any custom platform name
would have to explicitly override the default value for at least `:box`
if not also for `:box_url`.

This commit makes the implicit default code for Bento boxes explicit. In
other words, if your platform name does not match the pre-defined Bento
box platforms, the default of `:box` will be the platform name and the
`:box_url` will be `nil`. Similarly, if you use a provider other than
VirtualBox or VMware-*, the default of `:box` will be the platform name
and the `:box_url` will be `nil`.

This update allows for less boilerplate configuration, for example:

    ---
    driver:
      name: vagrant

    provisioner:
      name: chef_zero

    platforms:
      - name: ubuntu-14.04    # a Bento box, config as before
      - name: ubuntu/trusty64 # defaults to the same name for :box
      - name: windows-8.1     # defaults to the sane name for :box
                              # and a box_url of nil as there is no
                              # sharable windows base box

At a time in the near future, the S3 URLs for Bento boxes could move to
Atlas (formerly Vagrant Cloud) box names, but the same defaulting logic
can apply.
  • Loading branch information
fnichol committed Mar 23, 2015
1 parent 951072d commit a2ef401
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 59 deletions.
39 changes: 30 additions & 9 deletions lib/kitchen/driver/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ module Driver
# @author Fletcher Nichol <fnichol@nichol.ca>
class Vagrant < Kitchen::Driver::SSHBase

default_config :box do |driver|
"opscode-#{driver.instance.platform.name}"
end
default_config(:box) { |driver| driver.default_box }
required_config :box

default_config :box_check_update, nil
Expand Down Expand Up @@ -82,16 +80,26 @@ def create(state)
info("Vagrant instance #{instance.to_str} created.")
end

# @return [String,nil] the Vagrant box for this Instance
def default_box
if bento_boxes.include?(instance.platform.name)
"opscode-#{instance.platform.name}"
else
instance.platform.name
end
end

# @return [String,nil] the Vagrant box URL for this Instance
def default_box_url
# No default neede for 1.5 onwards - Vagrant Cloud only needs a box name
return if Gem::Version.new(vagrant_version) >= Gem::Version.new(1.5)
return unless bento_boxes.include?(instance.platform.name)

bucket = config[:provider]
bucket = "vmware" if config[:provider] =~ /^vmware_(.+)$/
provider = config[:provider]
provider = "vmware" if config[:provider] =~ /^vmware_(.+)$/

"https://opscode-vm-bento.s3.amazonaws.com/vagrant/#{bucket}/" \
"opscode_#{instance.platform.name}_chef-provisionerless.box"
if %w[virtualbox vmware].include?(provider)
"https://opscode-vm-bento.s3.amazonaws.com/vagrant/#{provider}/" \
"opscode_#{instance.platform.name}_chef-provisionerless.box"
end
end

# Destroys an instance.
Expand Down Expand Up @@ -149,6 +157,19 @@ def verify_dependencies
WEBSITE = "http://www.vagrantup.com/downloads.html".freeze
MIN_VER = "1.1.0".freeze

# Retuns a list of Vagrant base boxes produced by the Bento project
# (https://github.com/chef/bento).
#
# @return [Arrau<String>] list of Bento box names
# @api private
def bento_boxes
%W[
centos-5.11 centos-6.6 centos-7.0 debian-6.0.10 debian-7.8 fedora-20
fedora-21 freebsd-9.3 freebsd-10.1 opensuse-13.1 ubuntu-10.04
ubuntu-12.04 ubuntu-14.04 ubuntu-14.10
].map { |name| [name, "#{name}-i386"] }.flatten
end

# Renders and writes out a Vagrantfile dedicated to this instance.
#
# @api private
Expand Down
104 changes: 54 additions & 50 deletions spec/kitchen/driver/vagrant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,62 @@

describe "configuration" do

it "sets :box based on the platform name by default" do
expect(driver[:box]).to eq("opscode-fooos-99")
context "for known bento platform names" do

before { allow(platform).to receive(:name) { "ubuntu-14.04" } }

it "sets :box based on the platform name by default" do
expect(driver[:box]).to eq("opscode-ubuntu-14.04")
end

it "sets :box to a custom value" do
config[:box] = "booya"

expect(driver[:box]).to eq("booya")
end

it "sets :box_url to a bento box URL for a virtualbox provider" do
config[:provider] = "virtualbox"

expect(driver[:box_url]).to eq(
"https://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/" \
"opscode_ubuntu-14.04_chef-provisionerless.box"
)
end

it "sets :box_url to a bento box URL for a vmware-based provider" do
config[:provider] = "vmware_awesometown"

expect(driver[:box_url]).to eq(
"https://opscode-vm-bento.s3.amazonaws.com/vagrant/vmware/" \
"opscode_ubuntu-14.04_chef-provisionerless.box"
)
end

it "sets :box_url to nil for any other provider" do
config[:provider] = "the-next-coolness"

expect(driver[:box_url]).to eq(nil)
end
end

it "sets :box to a custom value" do
config[:box] = "booya"
context "for unknown bento platform names" do

before { allow(platform).to receive(:name) { "slackware-14.1" } }

expect(driver[:box]).to eq("booya")
it "sets :box based on the platform name by default" do
expect(driver[:box]).to eq("slackware-14.1")
end

it "sets :box to a custom value" do
config[:box] = "booya"

expect(driver[:box]).to eq("booya")
end

it "sets :box_url to nil" do
expect(driver[:box_url]).to eq(nil)
end
end

it "sets :box_check_update to nil by default" do
Expand Down Expand Up @@ -256,45 +304,6 @@

expect(driver[:vm_hostname]).to eq("okay")
end

context "with old versions of Vagrant" do

before { with_old_vagrant }

it "sets :box_url to a default based platform" do
expect(driver[:box_url]).to eq(
"https://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/" \
"opscode_fooos-99_chef-provisionerless.box"
)
end

it "sets :box_url to a default based on provider" do
config[:provider] = "vcool"

expect(driver[:box_url]).to eq(
"https://opscode-vm-bento.s3.amazonaws.com/vagrant/vcool/" \
"opscode_fooos-99_chef-provisionerless.box"
)
end

it "sets :box_url to a default for vmware-based providers" do
config[:provider] = "vmware_awesometown"

expect(driver[:box_url]).to eq(
"https://opscode-vm-bento.s3.amazonaws.com/vagrant/vmware/" \
"opscode_fooos-99_chef-provisionerless.box"
)
end
end

context "with modern versions of Vagrant" do

before { with_modern_vagrant }

it "sets :box_url to nil" do
expect(driver[:box_url]).to eq(nil)
end
end
end

describe "#verify_dependencies" do
Expand Down Expand Up @@ -597,7 +606,7 @@
it "sets the vm.box" do
cmd

expect(vagrantfile).to match(regexify(%{c.vm.box = "opscode-fooos-99"}))
expect(vagrantfile).to match(regexify(%{c.vm.box = "fooos-99"}))
end

it "sets the vm.hostname" do
Expand Down Expand Up @@ -1016,11 +1025,6 @@ def with_modern_vagrant
with("vagrant --version", any_args).and_return("Vagrant 1.7.2")
end

def with_old_vagrant
allow(driver).to receive(:run_command).
with("vagrant --version", any_args).and_return("Vagrant 1.4.5")
end

def with_unsupported_vagrant
allow(driver).to receive(:run_command).
with("vagrant --version", any_args).and_return("Vagrant 1.0.5")
Expand Down

0 comments on commit a2ef401

Please sign in to comment.