Class: Kitchen::Driver::Vagrant

Inherits:
Base
  • Object
show all
Includes:
HypervHelpers, ShellOut
Defined in:
lib/kitchen/driver/vagrant.rb

Overview

Vagrant driver for Kitchen. It communicates to Vagrant via the CLI.

Author:

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from HypervHelpers

#encode_command, #execute_command, #hyperv_default_switch_ps, #hyperv_switch, #is_32bit?, #is_64bit?, #powershell_64_bit, #run_ps, #sanitize_stdout, #wrap_command

Class Attribute Details

.vagrant_versionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the version of Vagrant installed on the workstation.

Returns:

  • (String)

    the version of Vagrant installed on the workstation



226
227
228
# File 'lib/kitchen/driver/vagrant.rb', line 226

def vagrant_version
  @vagrant_version
end

.winrm_plugin_passedtrue, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether or not the vagrant-winrm plugin is installed.

Returns:

  • (true, false)

    whether or not the vagrant-winrm plugin is installed



222
223
224
# File 'lib/kitchen/driver/vagrant.rb', line 222

def winrm_plugin_passed
  @winrm_plugin_passed
end

Instance Method Details

#cache_directoryObject

Setting up the ‘cache_directory` to store omnibus packages in cache and share a local folder to that directory so that we don’t pull them down every single time



204
205
206
207
208
209
210
# File 'lib/kitchen/driver/vagrant.rb', line 204

def cache_directory
  if enable_cache?
    config[:cache_directory]
  else
    false
  end
end

#create(state) ⇒ Object

Creates a Vagrant VM instance.

Parameters:

  • state (Hash)

    mutable instance state

Raises:

  • (ActionFailed)

    if the action could not be completed



106
107
108
109
110
111
112
113
# File 'lib/kitchen/driver/vagrant.rb', line 106

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

#default_boxString?

Returns the Vagrant box for this Instance.

Returns:

  • (String, nil)

    the Vagrant box for this Instance



116
117
118
119
120
121
122
# File 'lib/kitchen/driver/vagrant.rb', line 116

def default_box
  if bento_box?(instance.platform.name)
    "bento/#{instance.platform.name}"
  else
    instance.platform.name
  end
end

#default_box_urlString?

Returns the Vagrant box URL for this Instance.

Returns:

  • (String, nil)

    the Vagrant box URL for this Instance



125
126
127
# File 'lib/kitchen/driver/vagrant.rb', line 125

def default_box_url
  nil
end

#destroy(state) ⇒ Object

Destroys an instance.

Parameters:

  • state (Hash)

    mutable instance state

Raises:

  • (ActionFailed)

    if the action could not be completed



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/kitchen/driver/vagrant.rb', line 133

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

  create_vagrantfile
  @vagrantfile_created = false
  instance.transport.connection(state).close
  run("#{config[:vagrant_binary]} destroy -f")
  FileUtils.rm_rf(vagrant_root)
  info("Vagrant instance #{instance.to_str} destroyed.")
  state.delete(:hostname)
end

#finalize_config!(instance) ⇒ self

A lifecycle method that should be invoked when the object is about ready to be used. A reference to an Instance is required as configuration dependant data may be access through an Instance. This also acts as a hook point where the object may wish to perform other last minute checks, validations, or configuration expansions.

Parameters:

  • instance (Instance)

    an associated instance

Returns:

  • (self)

    itself, for use in chaining

Raises:

  • (ClientError)

    if instance parameter is nil



168
169
170
171
172
173
174
175
176
# File 'lib/kitchen/driver/vagrant.rb', line 168

def finalize_config!(instance)
  super
  finalize_vm_hostname!
  finalize_pre_create_command!
  finalize_synced_folders!
  finalize_ca_cert!
  finalize_network!
  self
end

#package(state) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/kitchen/driver/vagrant.rb', line 145

def package(state)
  if state[:hostname].nil?
    raise UserError, "Vagrant instance not created!"
  end
  if not (config[:ssh] && config[:ssh][:insert_key] == false)
    m = "Disable vagrant ssh key replacement to preserve the default key!"
    warn(m)
  end
  instance.transport.connection(state).close
  box_name = File.join(Dir.pwd, instance.name + ".box")
  run("#{config[:vagrant_binary]} package --output #{box_name}")
  destroy(state)
end

#verify_dependenciesObject

Performs whatever tests that may be required to ensure that this driver will be able to function in the current environment. This may involve checking for the presence of certain directories, software installed, etc.

Raises:

  • (UserError)

    if the driver will not be able to perform or if a documented dependency is missing from the system



185
186
187
188
189
190
191
192
# File 'lib/kitchen/driver/vagrant.rb', line 185

def verify_dependencies
  super
  if Gem::Version.new(vagrant_version) < Gem::Version.new(MIN_VER.dup)
    raise UserError, "Detected an old version of Vagrant " \
      "(#{vagrant_version})." \
      " Please upgrade to version #{MIN_VER} or higher from #{WEBSITE}."
  end
end

#winrm_transport?TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether or not the transport’s name implies a WinRM-based transport.

Returns:

  • (TrueClass, FalseClass)

    whether or not the transport’s name implies a WinRM-based transport



197
198
199
# File 'lib/kitchen/driver/vagrant.rb', line 197

def winrm_transport?
  instance.transport.name.downcase =~ /win_?rm/
end