lib/kitchen/driver/vagrant.rb in kitchen-vagrant-0.16.0 vs lib/kitchen/driver/vagrant.rb in kitchen-vagrant-0.17.0.beta.1

- old
+ new

@@ -27,12 +27,14 @@ module Driver # Vagrant driver for Kitchen. It communicates to Vagrant via the CLI. # # @author Fletcher Nichol <fnichol@nichol.ca> - class Vagrant < Kitchen::Driver::SSHBase + class Vagrant < Kitchen::Driver::Base + include ShellOut + default_config(:box) { |driver| driver.default_box } required_config :box default_config :box_check_update, nil @@ -44,10 +46,14 @@ default_config :gui, nil default_config :network, [] + default_config :password do |driver| + "vagrant" if driver.winrm_transport? + end + default_config :pre_create_command, nil default_config :provision, false default_config :provider do |_| @@ -56,17 +62,24 @@ default_config :ssh, {} default_config :synced_folders, [] + default_config :username do |driver| + "vagrant" if driver.winrm_transport? + end + default_config :vagrantfile_erb, File.join(File.dirname(__FILE__), "../../../templates/Vagrantfile.erb") expand_path_for :vagrantfile_erb default_config :vagrantfiles, [] + expand_path_for :vagrantfiles - default_config(:vm_hostname) { |driver| driver.instance.name } + default_config(:vm_hostname) do |driver| + driver.windows_os? ? nil : driver.instance.name + end no_parallel_for :create, :destroy # Creates a Vagrant VM instance. # @@ -75,10 +88,11 @@ def create(state) create_vagrantfile run_pre_create_command run_vagrant_up update_state(state) + instance.transport.connection(state).wait_until_ready info("Vagrant instance #{instance.to_str} created.") end # @return [String,nil] the Vagrant box for this Instance def default_box @@ -126,13 +140,11 @@ # @param instance [Instance] an associated instance # @return [self] itself, for use in chaining # @raise [ClientError] if instance parameter is nil def finalize_config!(instance) super - config[:vagrantfiles] = config[:vagrantfiles].map do |path| - File.expand_path(path, config[:kitchen_root]) - end + finalize_vm_hostname! finalize_pre_create_command! finalize_synced_folders! self end @@ -150,10 +162,17 @@ "(#{vagrant_version})." \ " Please upgrade to version #{MIN_VER} or higher from #{WEBSITE}." end end + # @return [TrueClass,FalseClass] whether or not the transport's name + # implies a WinRM-based transport + # @api private + def winrm_transport? + instance.transport.name.downcase =~ /win_?rm/ + end + protected WEBSITE = "http://www.vagrantup.com/downloads.html".freeze MIN_VER = "1.1.0".freeze @@ -221,10 +240,22 @@ options || "nil" ] end end + # Truncates the length of `:vm_hostname` to 12 characters for + # Windows-based operating systems. + # + # @api private + def finalize_vm_hostname! + string = config[:vm_hostname] + + if windows_os? && string.is_a?(String) && string.size >= 12 + config[:vm_hostname] = "#{string[0...10]}-#{string[-1]}" + end + end + # Renders the Vagrantfile ERb template. # # @return [String] the contents for a Vagrantfile # @raise [ActionFailed] if the Vagrantfile template was not found # @api private @@ -248,10 +279,26 @@ def run(cmd, options = {}) cmd = "echo #{cmd}" if config[:dry_run] run_command(cmd, { :cwd => vagrant_root }.merge(options)) end + # Delegates to Kitchen::ShellOut.run_command, overriding some default + # options: + # + # * `:use_sudo` defaults to the value of `config[:use_sudo]` in the + # Driver object + # * `:log_subject` defaults to a String representation of the Driver's + # class name + # + # @see Kitchen::ShellOut#run_command + def run_command(cmd, options = {}) + merged = { + :use_sudo => config[:use_sudo], :log_subject => name + }.merge(options) + super(cmd, merged) + end + # Runs a local command before `vagrant up` has been called. # # @api private def run_pre_create_command if config[:pre_create_command] @@ -286,13 +333,19 @@ # @api private def update_state(state) hash = vagrant_ssh_config state[:hostname] = hash["HostName"] - state[:username] = hash["User"] - state[:ssh_key] = hash["IdentityFile"] - state[:port] = hash["Port"] - state[:proxy_command] = hash["ProxyCommand"] if hash["ProxyCommand"] + + if winrm_transport? + state[:username] = config[:username] + state[:password] = config[:password] + else + state[:username] = hash["User"] + state[:ssh_key] = hash["IdentityFile"] + state[:port] = hash["Port"] + state[:proxy_command] = hash["ProxyCommand"] if hash["ProxyCommand"] + end end # @return [String] full local path to the directory containing the # instance's Vagrantfile # @api private