lib/kitchen/driver/vagrant.rb in kitchen-vagrant-0.11.3 vs lib/kitchen/driver/vagrant.rb in kitchen-vagrant-0.12.0

- old
+ new

@@ -18,11 +18,10 @@ require 'fileutils' require 'rubygems/version' require 'kitchen' -require 'kitchen/vagrant/vagrantfile_creator' module Kitchen module Driver @@ -33,14 +32,24 @@ # @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' } - default_config :synced_folders, {} + default_config :network, [] + default_config :synced_folders, [] + default_config :pre_create_command, nil + + default_config :vagrantfile_erb, + File.join(File.dirname(__FILE__), "../../../templates/Vagrantfile.erb") + + default_config :provider, + ENV.fetch('VAGRANT_DEFAULT_PROVIDER', "virtualbox") + default_config :box do |driver| "opscode-#{driver.instance.platform.name}" end + default_config :box_url do |driver| "https://opscode-vm-bento.s3.amazonaws.com/vagrant/" \ "opscode_#{driver.instance.platform.name}_provisionerless.box" end @@ -48,12 +57,13 @@ no_parallel_for :create, :destroy def create(state) create_vagrantfile + run_pre_create_command cmd = "vagrant up --no-provision" - cmd += " --provider=#{@config[:provider]}" if @config[:provider] + cmd += " --provider=#{config[:provider]}" if config[:provider] run cmd set_ssh_state(state) info("Vagrant instance #{instance.to_str} created.") end @@ -85,10 +95,15 @@ def verify_dependencies check_vagrant_version end + def instance=(instance) + @instance = instance + resolve_config! + end + protected WEBSITE = "http://downloads.vagrantup.com/" MIN_VER = "1.1.0" @@ -100,10 +115,16 @@ def silently_run(cmd) run_command(cmd, :live_stream => nil, :quiet => logger.debug? ? false : true) end + def run_pre_create_command + if config[:pre_create_command] + run(config[:pre_create_command], :cwd => config[:kitchen_root]) + end + end + def vagrant_root @vagrant_root ||= File.join( config[:kitchen_root], %w{.kitchen kitchen-vagrant}, instance.name ) end @@ -112,18 +133,27 @@ return if @vagrantfile_created vagrantfile = File.join(vagrant_root, "Vagrantfile") debug("Creating Vagrantfile for #{instance.to_str} (#{vagrantfile})") FileUtils.mkdir_p(vagrant_root) - File.open(vagrantfile, "wb") { |f| f.write(creator.render) } + File.open(vagrantfile, "wb") { |f| f.write(render_template) } + debug_vagrantfile(vagrantfile) @vagrantfile_created = true end - def creator - Kitchen::Vagrant::VagrantfileCreator.new(instance, config) + def render_template + if File.exists?(template) + ERB.new(IO.read(template)).result(binding).gsub(%r{^\s*$\n}, '') + else + raise ActionFailed, "Could not find Vagrantfile template #{template}" + end end + def template + File.expand_path(config[:vagrantfile_erb], config[:kitchen_root]) + end + def set_ssh_state(state) hash = vagrant_ssh_config state[:hostname] = hash["HostName"] state[:username] = hash["User"] @@ -136,9 +166,28 @@ lines = output.split("\n").map do |line| tokens = line.strip.partition(" ") [tokens.first, tokens.last.gsub(/"/, '')] end Hash[lines] + end + + def debug_vagrantfile(vagrantfile) + if logger.debug? + debug("------------") + IO.read(vagrantfile).each_line { |l| debug("#{l.chomp}") } + debug("------------") + end + end + + def resolve_config! + unless config[:vagrantfile_erb].nil? + config[:vagrantfile_erb] = + File.expand_path(config[:vagrantfile_erb], config[:kitchen_root]) + end + unless config[:pre_create_command].nil? + config[:pre_create_command] = + config[:pre_create_command].gsub("{{vagrant_root}}", vagrant_root) + end end def vagrant_version version_string = silently_run("vagrant --version") version_string = version_string.chomp.split(" ").last