Skip to content

Commit

Permalink
Merge pull request #7 from opscode/no-vagrant-gem
Browse files Browse the repository at this point in the history
[Breaking] Support Vagrant 1.1+ and remove vagrant gem dependency.
  • Loading branch information
fnichol committed Mar 23, 2013
2 parents 8c9570b + 45809df commit 2719522
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 131 deletions.
6 changes: 1 addition & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ require 'cane/rake_task'
require 'tailor/rake_task'

desc "Run cane to check quality metrics"
Cane::RakeTask.new do |cane|
cane.abc_exclude = %w(
Kitchen::Vagrant.define_vagrant_vm
)
end
Cane::RakeTask.new

Tailor::RakeTask.new

Expand Down
1 change: 0 additions & 1 deletion kitchen-vagrant.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]

gem.add_dependency 'test-kitchen', '~> 1.0.0.alpha.0'
gem.add_dependency 'vagrant', '~> 1.0'

gem.add_development_dependency 'cane'
gem.add_development_dependency 'tailor'
Expand Down
67 changes: 43 additions & 24 deletions lib/kitchen/driver/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'fileutils'

require 'kitchen'
require 'kitchen/vagrant/vagrantfile_creator'

module Kitchen

Expand All @@ -25,67 +28,83 @@ module Driver
# Vagrant driver for Kitchen. It communicates to Vagrant via the CLI.
#
# @author Fletcher Nichol <fnichol@nichol.ca>
#
# @todo Vagrant installation check and version will be placed into any
# dependency hook checks when feature is released
class Vagrant < Kitchen::Driver::SSHBase

default_config :customize, {:memory => '256'}

no_parallel_for :create, :destroy

def create(state)
# @todo Vagrantfile setup will be placed in any dependency hook
# checks when feature is released
vagrantfile = File.join(config[:kitchen_root], "Vagrantfile")
create_vagrantfile(vagrantfile) unless File.exists?(vagrantfile)

state[:hostname] = instance.name
run "vagrant up #{state[:hostname]} --no-provision"
create_vagrantfile(state)
run "vagrant up --no-provision"
info("Vagrant instance <#{state[:hostname]}> created.")
end

def converge(state)
create_vagrantfile(state)
ssh_args = build_ssh_args(state)
install_omnibus(ssh_args) if config[:require_chef_omnibus]
run "vagrant provision #{state[:hostname]}"
run "vagrant provision"
end

def setup(state)
create_vagrantfile(state)
super
end

def verify(state)
create_vagrantfile(state)
super
end

def destroy(state)
return if state[:hostname].nil?

run "vagrant destroy #{state[:hostname]} -f"
create_vagrantfile(state)
run "vagrant destroy -f"
FileUtils.rm_rf(vagrant_root)
info("Vagrant instance <#{state[:hostname]}> destroyed.")
state.delete(:hostname)
end

def login_command(state)
%W{vagrant ssh #{state[:hostname]}}
create_vagrantfile(state)
LoginCommand.new(%W{vagrant ssh}, :chdir => vagrant_root)
end

protected

def ssh(ssh_args, cmd)
run %{vagrant ssh #{ssh_args.first} --command '#{cmd}'}
run %{vagrant ssh --command '#{cmd}'}
end

def run(cmd)
cmd = "echo #{cmd}" if config[:dry_run]
run_command(cmd)
run_command(cmd, :cwd => vagrant_root)
end

def create_vagrantfile(vagrantfile)
File.open(vagrantfile, "wb") { |f| f.write(vagrantfile_contents) }
def vagrant_root
@vagrant_root ||= File.join(
config[:kitchen_root], %w{.kitchen kitchen-vagrant}, instance.name
)
end

def create_vagrantfile(state)
return if @vagrantfile_created

vagrantfile = File.join(vagrant_root, "Vagrantfile")
debug("Creating Vagrantfile for <#{state[:hostname]}> (#{vagrantfile})")
FileUtils.mkdir_p(vagrant_root)
File.open(vagrantfile, "wb") { |f| f.write(creator.render) }
@vagrantfile_created = true
end

def vagrantfile_contents
arr = []
arr << %{require 'kitchen/vagrant'}
if File.exists?(File.join(config[:kitchen_root], "Berksfile"))
arr << %{require 'berkshelf/vagrant'}
end
arr << %{}
arr << %{Vagrant::Config.run do |config|}
arr << %{ Kitchen::Vagrant.define_vms(config)}
arr << %{end\n}
arr.join("\n")
def creator
Kitchen::Vagrant::VagrantfileCreator.new(instance, config)
end
end
end
Expand Down
101 changes: 0 additions & 101 deletions lib/kitchen/vagrant.rb

This file was deleted.

110 changes: 110 additions & 0 deletions lib/kitchen/vagrant/vagrantfile_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# -*- encoding: utf-8 -*-
#
# Author:: Fletcher Nichol (<fnichol@nichol.ca>)
#
# Copyright (C) 2013, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Kitchen

module Vagrant

# Class to render Vagrantfiles to be used by the Kitchen Vagrant driver.
#
# @author Fletcher Nichol <fnichol@nichol.ca>
class VagrantfileCreator

def initialize(instance, config)
@instance = instance
@config = config
end

def render
arr = []
arr << %{Vagrant.configure("2") do |c|}
common_block(arr)
network_block(arr)
provider_block(arr)
chef_block(arr)
berkshelf_block(arr)
arr << %{end}
arr.join("\n")
end

private

attr_reader :instance, :config

def common_block(arr)
arr << %{ c.vm.box = "#{config[:box]}"}
arr << %{ c.vm.box_url = "#{config[:box_url]}"} if config[:box_url]
arr << %{ c.vm.hostname = "#{instance.name}.vagrantup.com"}
end

def network_block(arr)
Array(config[:network]).each do |network_options|
options = Array(network_options.dup)
type = options.shift
arr << %{ c.vm.network(:#{type}, #{options.join(", ")})}
end
end

def provider_block(arr)
arr << %{ c.vm.provider :virtualbox do |p|}
config[:customize].each do |key, value|
arr << %{ p.customize ["modifyvm", :id, "--#{key}", #{value}]}
end
arr << %{ end}
end

def chef_block(arr)
arr << %{ c.vm.provision :chef_solo do |chef|}
arr << %{ chef.log_level = #{vagrant_logger_level}}
arr << %{ chef.run_list = #{instance.run_list.inspect}}
arr << %{ chef.json = #{instance.attributes.to_s}}
if instance.suite.data_bags_path
arr << %{ chef.data_bags_path = #{instance.suite.data_bags_path}}
end
if instance.suite.roles_path
arr << %{ chef.roles_path = #{instance.suite.roles_path}}
end
arr << %{ end}
end

def berkshelf_block(arr)
if File.exists?(berksfile)
arr << %{ c.berkshelf.berksfile_path = "#{berksfile}"}
end
end

def vagrant_logger_level
if instance.logger.debug?
":debug"
elsif instance.logger.info?
":info"
elsif instance.logger.error?
":error"
elsif instance.logger.fatal?
":fatal"
else
":info"
end
end

def berksfile
File.join(config[:kitchen_root], "Berksfile")
end
end
end
end

0 comments on commit 2719522

Please sign in to comment.