Module: Vagrant

Defined in:
lib/vagrant/util.rb,
lib/vagrant/ui.rb,
lib/vagrant/box.rb,
lib/vagrant/cli.rb,
lib/vagrant/host.rb,
lib/vagrant/alias.rb,
lib/vagrant/guest.rb,
lib/vagrant/action.rb,
lib/vagrant/config.rb,
lib/vagrant/errors.rb,
lib/vagrant/plugin.rb,
lib/vagrant/bundler.rb,
lib/vagrant/machine.rb,
lib/vagrant/util/io.rb,
lib/vagrant/version.rb,
lib/vagrant/registry.rb,
lib/vagrant/util/env.rb,
lib/vagrant/util/ssh.rb,
lib/vagrant/config/v1.rb,
lib/vagrant/config/v2.rb,
lib/vagrant/plugin/v1.rb,
lib/vagrant/plugin/v2.rb,
lib/vagrant/util/busy.rb,
lib/vagrant/util/caps.rb,
lib/vagrant/util/mime.rb,
lib/vagrant/util/which.rb,
lib/vagrant/action/hook.rb,
lib/vagrant/environment.rb,
lib/vagrant/vagrantfile.rb,
lib/vagrant/batch_action.rb,
lib/vagrant/box_metadata.rb,
lib/vagrant/util/counter.rb,
lib/vagrant/util/keypair.rb,
lib/vagrant/util/numeric.rb,
lib/vagrant/action/runner.rb,
lib/vagrant/action/warden.rb,
lib/vagrant/config/loader.rb,
lib/vagrant/machine_index.rb,
lib/vagrant/machine_state.rb,
lib/vagrant/util/platform.rb,
lib/vagrant/util/presence.rb,
lib/vagrant/util/safe_env.rb,
lib/vagrant/util/uploader.rb,
lib/vagrant/action/builder.rb,
lib/vagrant/box_collection.rb,
lib/vagrant/config/v1/root.rb,
lib/vagrant/config/v2/root.rb,
lib/vagrant/config/v2/util.rb,
lib/vagrant/plugin/manager.rb,
lib/vagrant/plugin/v1/host.rb,
lib/vagrant/plugin/v2/host.rb,
lib/vagrant/plugin/v2/push.rb,
lib/vagrant/shared_helpers.rb,
lib/vagrant/util/directory.rb,
lib/vagrant/util/file_mode.rb,
lib/vagrant/util/retryable.rb,
lib/vagrant/util/safe_exec.rb,
lib/vagrant/util/safe_puts.rb,
lib/vagrant/capability_host.rb,
lib/vagrant/plugin/v1/guest.rb,
lib/vagrant/plugin/v2/guest.rb,
lib/vagrant/util/deep_merge.rb,
lib/vagrant/util/downloader.rb,
lib/vagrant/util/file_mutex.rb,
lib/vagrant/util/network_ip.rb,
lib/vagrant/util/powershell.rb,
lib/vagrant/util/safe_chdir.rb,
lib/vagrant/util/subprocess.rb,
lib/vagrant/config/v1/loader.rb,
lib/vagrant/config/v2/loader.rb,
lib/vagrant/plugin/v1/config.rb,
lib/vagrant/plugin/v1/errors.rb,
lib/vagrant/plugin/v1/plugin.rb,
lib/vagrant/plugin/v2/config.rb,
lib/vagrant/plugin/v2/errors.rb,
lib/vagrant/plugin/v2/plugin.rb,
lib/vagrant/util/curl_helper.rb,
lib/vagrant/util/guest_hosts.rb,
lib/vagrant/util/line_buffer.rb,
lib/vagrant/util/shell_quote.rb,
lib/vagrant/plugin/state_file.rb,
lib/vagrant/plugin/v1/command.rb,
lib/vagrant/plugin/v1/manager.rb,
lib/vagrant/plugin/v2/command.rb,
lib/vagrant/plugin/v2/manager.rb,
lib/vagrant/plugin/v2/trigger.rb,
lib/vagrant/util/experimental.rb,
lib/vagrant/util/is_port_open.rb,
lib/vagrant/util/windows_path.rb,
lib/vagrant/plugin/v1/provider.rb,
lib/vagrant/plugin/v2/provider.rb,
lib/vagrant/util/file_checksum.rb,
lib/vagrant/action/builtin/call.rb,
lib/vagrant/action/builtin/disk.rb,
lib/vagrant/action/builtin/lock.rb,
lib/vagrant/config/version_base.rb,
lib/vagrant/util/guest_networks.rb,
lib/vagrant/plugin/v2/components.rb,
lib/vagrant/util/ipv4_interfaces.rb,
lib/vagrant/action/primary_runner.rb,
lib/vagrant/plugin/v1/provisioner.rb,
lib/vagrant/plugin/v2/provisioner.rb,
lib/vagrant/util/guest_inspection.rb,
lib/vagrant/util/silence_warnings.rb,
lib/vagrant/action/builtin/box_add.rb,
lib/vagrant/action/builtin/confirm.rb,
lib/vagrant/action/builtin/delayed.rb,
lib/vagrant/action/builtin/env_set.rb,
lib/vagrant/action/builtin/message.rb,
lib/vagrant/action/builtin/ssh_run.rb,
lib/vagrant/action/builtin/trigger.rb,
lib/vagrant/action/general/package.rb,
lib/vagrant/config/v1/dummy_config.rb,
lib/vagrant/config/v2/dummy_config.rb,
lib/vagrant/plugin/v1/communicator.rb,
lib/vagrant/plugin/v2/communicator.rb,
lib/vagrant/util/checkpoint_client.rb,
lib/vagrant/util/logging_formatter.rb,
lib/vagrant/util/template_renderer.rb,
lib/vagrant/action/builtin/is_state.rb,
lib/vagrant/action/builtin/ssh_exec.rb,
lib/vagrant/plugin/v2/synced_folder.rb,
lib/vagrant/action/builtin/provision.rb,
lib/vagrant/util/command_deprecation.rb,
lib/vagrant/util/credential_scrubber.rb,
lib/vagrant/util/line_ending_helpers.rb,
lib/vagrant/util/map_command_options.rb,
lib/vagrant/util/stacked_proc_runner.rb,
lib/vagrant/util/string_block_editor.rb,
lib/vagrant/action/builtin/box_remove.rb,
lib/vagrant/action/builtin/box_update.rb,
lib/vagrant/action/builtin/handle_box.rb,
lib/vagrant/action/builtin/is_env_set.rb,
lib/vagrant/util/scoped_hash_override.rb,
lib/vagrant/action/builtin/set_hostname.rb,
lib/vagrant/action/builtin/cleanup_disks.rb,
lib/vagrant/action/builtin/graceful_halt.rb,
lib/vagrant/action/builtin/prepare_clone.rb,
lib/vagrant/action/builtin/handle_box_url.rb,
lib/vagrant/action/builtin/synced_folders.rb,
lib/vagrant/util/ansi_escape_code_remover.rb,
lib/vagrant/util/install_cli_autocomplete.rb,
lib/vagrant/action/builtin/cloud_init_wait.rb,
lib/vagrant/action/builtin/config_validate.rb,
lib/vagrant/action/builtin/destroy_confirm.rb,
lib/vagrant/action/builtin/has_provisioner.rb,
lib/vagrant/action/builtin/cloud_init_setup.rb,
lib/vagrant/action/builtin/box_check_outdated.rb,
lib/vagrant/action/builtin/mixin_provisioners.rb,
lib/vagrant/util/hash_with_indifferent_access.rb,
lib/vagrant/action/builtin/provisioner_cleanup.rb,
lib/vagrant/action/general/package_setup_files.rb,
lib/vagrant/action/builtin/mixin_synced_folders.rb,
lib/vagrant/action/builtin/synced_folder_cleanup.rb,
lib/vagrant/action/builtin/wait_for_communicator.rb,
lib/vagrant/action/general/package_setup_folders.rb,
lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb,
ext/vagrant/vagrant_ssl/vagrant_ssl.c,
lib/vagrant.rb,
lib/vagrant.rb

Overview

Copyright (c) HashiCorp, Inc. SPDX-License-Identifier: BUSL-1.1

Defined Under Namespace

Modules: Action, CapabilityHost, Config, Errors, Plugin, UI, Util Classes: Alias, BatchAction, Box, BoxCollection, BoxMetadata, Bundler, CLI, Environment, Guest, Host, Machine, MachineIndex, MachineState, OptionParser, Registry, Vagrantfile

Constant Summary collapse

VERSION =

This will always be up to date with the current version of Vagrant, since it is used to generate the gemspec and is also the source of the version for vagrant -v

File.read(
File.expand_path("../../../version.txt", __FILE__)).chomp
DEFAULT_SERVER_URL =

This is the default endpoint of the Vagrant Cloud in use. API calls will be made to this for various functions of Vagrant that may require remote access.

Returns:

  • (String)
"https://vagrantcloud.com".freeze
THREAD_MAX_JOIN_TIMEOUT =
Note:

This is not the maximum time for a thread to complete.

Max number of seconds to wait for joining an active thread.

Returns:

  • (Integer)
60
REQUIRED_EXTERNAL_TOOLS =

List of required external tools that are expected to be present when running outside of the installers

["bsdtar", "curl", "ssh"].map(&:freeze).freeze
PLUGIN_COMPONENTS =

These are the various plugin versions and their components in a lazy loaded Hash-like structure.

Registry.new.tap do |c|
  c.register(:"1")                  { Plugin::V1::Plugin }
  c.register([:"1", :command])      { Plugin::V1::Command }
  c.register([:"1", :communicator]) { Plugin::V1::Communicator }
  c.register([:"1", :config])       { Plugin::V1::Config }
  c.register([:"1", :guest])        { Plugin::V1::Guest }
  c.register([:"1", :host])         { Plugin::V1::Host }
  c.register([:"1", :provider])     { Plugin::V1::Provider }
  c.register([:"1", :provisioner])  { Plugin::V1::Provisioner }

  c.register(:"2")                  { Plugin::V2::Plugin }
  c.register([:"2", :command])      { Plugin::V2::Command }
  c.register([:"2", :communicator]) { Plugin::V2::Communicator }
  c.register([:"2", :config])       { Plugin::V2::Config }
  c.register([:"2", :guest])        { Plugin::V2::Guest }
  c.register([:"2", :host])         { Plugin::V2::Host }
  c.register([:"2", :provider])     { Plugin::V2::Provider }
  c.register([:"2", :provisioner])  { Plugin::V2::Provisioner }
  c.register([:"2", :push])         { Plugin::V2::Push }
  c.register([:"2", :synced_folder]) { Plugin::V2::SyncedFolder }

  c.register(:remote)               { Plugin::Remote::Plugin }
end
@@global_lock =
Mutex.new

Class Method Summary collapse

Class Method Details

.add_default_cli_options(block) ⇒ nil

Add a new block of default CLI options which should be automatically added to all commands

Parameters:

  • block (Proc)

    Proc instance containing OptParser configuration

Returns:

  • (nil)


210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/vagrant/shared_helpers.rb', line 210

def self.add_default_cli_options(block)
  if !block.is_a?(Proc)
    raise TypeError,
      "Expecting type `Proc` but received `#{block.class}`"
  end
  if block.arity != 1 && block.arity != -1
    raise ArgumentError,
      "Proc must accept OptionParser argument"
  end
  @_default_cli_options = [] if !@_default_cli_options
  @_default_cli_options << block
  nil
end

.allow_prerelease_dependencies?Boolean

This returns true/false if the Vagrant should allow prerelease versions when resolving plugin dependency constraints

Returns:

  • (Boolean)


139
140
141
# File 'lib/vagrant/shared_helpers.rb', line 139

def self.allow_prerelease_dependencies?
  !!ENV["VAGRANT_ALLOW_PRERELEASE"]
end

.auto_install_local_plugins?Boolean

Automatically install locally defined plugins instead of waiting for user confirmation.

Returns:

  • (Boolean)


160
161
162
163
164
165
166
# File 'lib/vagrant/shared_helpers.rb', line 160

def self.auto_install_local_plugins?
  if ENV["VAGRANT_INSTALL_LOCAL_PLUGINS"]
    true
  else
    false
  end
end

.configure(version, &block) ⇒ Object

Configure a Vagrant environment. The version specifies the version of the configuration that is expected by the block. The block, based on that version, configures the environment.

Note that the block isn't run immediately. Instead, the configuration block is stored until later, and is run when an environment is loaded.

Parameters:

  • version (String)

    Version of the configuration



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

def self.configure(version, &block)
  Config.run(version, &block)
end

.default_cli_optionsArray<Proc>

Array of default CLI options to automatically add to commands.

Returns:

  • (Array<Proc>)

    Default optparse options



228
229
230
231
# File 'lib/vagrant/shared_helpers.rb', line 228

def self.default_cli_options
  @_default_cli_options = [] if !@_default_cli_options
  @_default_cli_options.dup
end

.detect_missing_toolsObject



233
234
235
236
237
# File 'lib/vagrant/shared_helpers.rb', line 233

def self.detect_missing_tools
  REQUIRED_EXTERNAL_TOOLS.find_all do |tool|
    !Vagrant::Util::Which.which(tool)
  end
end

.enable_resolv_replaceboolean

Use Ruby Resolv in place of libc

Returns:

  • (boolean)

    enabled or not



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/vagrant/shared_helpers.rb', line 171

def self.enable_resolv_replace
  if ENV["VAGRANT_ENABLE_RESOLV_REPLACE"]
    if !ENV["VAGRANT_DISABLE_RESOLV_REPLACE"]
      begin
        require "resolv-replace"
        true
      rescue
        false
      end
    else
      false
    end
  end
end

.global_lockObject

This holds a global lock for the duration of the block. This should be invoked around anything that is modifying process state (such as environmental variables).



31
32
33
34
35
# File 'lib/vagrant/shared_helpers.rb', line 31

def self.global_lock
  @@global_lock.synchronize do
    return yield
  end
end

.global_loggerLogger

Get the global logger instance

Returns:

  • (Logger)


197
198
199
200
201
202
203
# File 'lib/vagrant/shared_helpers.rb', line 197

def self.global_logger
  if @_global_logger.nil?
    require "log4r"
    @_global_logger = Log4r::Logger.new("vagrant::global")
  end
  @_global_logger
end

.global_logger=(log) ⇒ Logger

Set the global logger

Parameters:

  • log

    Logger

Returns:

  • (Logger)


190
191
192
# File 'lib/vagrant/shared_helpers.rb', line 190

def self.global_logger=(log)
  @_global_logger = log
end

.has_plugin?(name, version = nil) ⇒ Boolean

This checks if a plugin with the given name is available (installed and enabled). This can be used from the Vagrantfile to easily branch based on plugin availability.

Returns:

  • (Boolean)


229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/vagrant.rb', line 229

def self.has_plugin?(name, version=nil)
  return false unless Vagrant.plugins_enabled?

  if !version
    # We check the plugin names first because those are cheaper to check
    return true if plugin("2").manager.registered.any? { |p| p.name == name }
  end

  # Now check the plugin gem names
  require "vagrant/plugin/manager"
  Plugin::Manager.instance.plugin_installed?(name, version)
end

.in_bundler?Boolean

This returns a true/false if we are running within a bundler environment

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/vagrant/shared_helpers.rb', line 48

def self.in_bundler?
  !!ENV["BUNDLE_GEMFILE"] &&
    !defined?(::Bundler).nil?
end

.in_installer?Boolean

This returns a true/false showing whether we're running from the environment setup by the Vagrant installers.

Returns:

  • (Boolean)


41
42
43
# File 'lib/vagrant/shared_helpers.rb', line 41

def self.in_installer?
  !!ENV["VAGRANT_INSTALLER_ENV"]
end

.installer_embedded_dirString

Returns the path to the embedded directory of the Vagrant installer, if there is one (if we're running in an installer).

Returns:

  • (String)


57
58
59
60
# File 'lib/vagrant/shared_helpers.rb', line 57

def self.installer_embedded_dir
  return nil if !Vagrant.in_installer?
  ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]
end

.log_levelString

The current log level for Vagrant

Returns:

  • (String)


86
87
88
# File 'lib/vagrant/shared_helpers.rb', line 86

def self.log_level
  ENV.fetch("VAGRANT_LOG", "fatal").downcase
end

.original_envHash

This allows plugin developers to access the original environment before Vagrant even ran. This is useful when shelling out, especially to other Ruby processes.

Returns:

  • (Hash)


323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/vagrant.rb', line 323

def self.original_env
  {}.tap do |h|
    ENV.each do |k,v|
      if k.start_with?("VAGRANT_OLD_ENV")
        key = k.sub(/^VAGRANT_OLD_ENV_/, "")
        if !key.empty?
          h[key] = v
        end
      end
    end
  end
end

.plugin(version, component = nil) ⇒ Class

Returns a superclass to use when creating a plugin for Vagrant. Given a specific version, this returns a proper superclass to use to register plugins for that version.

Optionally, if you give a specific component, then it will return the proper superclass for that component as well.

Plugins and plugin components should subclass the classes returned by this method. This method lets Vagrant core control these superclasses and change them over time without affecting plugins. For example, if the V1 superclass happens to be "Vagrant::V1," future versions of Vagrant may move it to "Vagrant::Plugins::V1" and plugins will not be affected.

Parameters:

  • version (String)
  • component (String) (defaults to: nil)

Returns:

  • (Class)

Raises:

  • (ArgumentError)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/vagrant.rb', line 259

def self.plugin(version, component=nil)
  # Build up the key and return a result
  key    = version.to_s.to_sym
  key    = [key, component.to_s.to_sym] if component
  result = PLUGIN_COMPONENTS.get(key)

  # If we found our component then we return that
  return result if result

  # If we didn't find a result, then raise an exception, depending
  # on if we got a component or not.
  raise ArgumentError, "Plugin superclass not found for version/component: " +
    "#{version} #{component}"
end

.plugins_enabled?Boolean

This returns whether or not 3rd party plugins should and can be loaded.

Returns:

  • (Boolean)


72
73
74
# File 'lib/vagrant/shared_helpers.rb', line 72

def self.plugins_enabled?
  !ENV["VAGRANT_NO_PLUGINS"]
end

.plugins_init?Boolean

Should the plugin system be initialized

Returns:

  • (Boolean)


65
66
67
# File 'lib/vagrant/shared_helpers.rb', line 65

def self.plugins_init?
  !ENV['VAGRANT_DISABLE_PLUGIN_INIT']
end

.prerelease?Boolean

This returns true/false if the running version of Vagrant is a pre-release version (development)

Returns:

  • (Boolean)


131
132
133
# File 'lib/vagrant/shared_helpers.rb', line 131

def self.prerelease?
  Gem::Version.new(Vagrant::VERSION).prerelease?
end

.require_plugin(name) ⇒ Object

Deprecated.


275
276
277
278
279
# File 'lib/vagrant.rb', line 275

def self.require_plugin(name)
  puts "require_plugin is deprecated and has no effect any longer."
  puts "Use `vagrant plugin` commands to manage plugins. This warning will"
  puts "be removed in the next version of Vagrant."
end

.require_version(*requirements) ⇒ Object

This allows a Vagrantfile to specify the version of Vagrant that is required. You can specify a list of requirements which will all be checked against the running Vagrant version.

This should be specified at the top of any Vagrantfile.

Examples are shown below:

require_version(">= 1.3.5") require_version(">= 1.3.5", "< 1.4.0") require_version("~> 1.3.5")



304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/vagrant.rb', line 304

def self.require_version(*requirements)
  logger = Log4r::Logger.new("vagrant::root")
  logger.info("Version requirements from Vagrantfile: #{requirements.inspect}")

  if version?(*requirements)
    logger.info("  - Version requirements satisfied!")
    return
  end

  raise Errors::VagrantVersionBad,
    requirements: requirements.join(", "),
    version: VERSION
end

.server_url(config_server_url = nil) ⇒ String

Returns the URL prefix to the server.

Returns:

  • (String)


93
94
95
96
97
# File 'lib/vagrant/shared_helpers.rb', line 93

def self.server_url(config_server_url=nil)
  result = ENV["VAGRANT_SERVER_URL"]
  result = config_server_url if result == "" or result == nil
  result || DEFAULT_SERVER_URL
end

.source_rootPathname

The source root is the path to the root directory of the Vagrant source.

Returns:

  • (Pathname)


102
103
104
# File 'lib/vagrant/shared_helpers.rb', line 102

def self.source_root
  @source_root ||= Pathname.new(File.expand_path('../../../', __FILE__))
end

.strict_dependency_enforcementBoolean

This allows control over dependency resolution when installing plugins into vagrant. When true, dependency libraries that Vagrant core relies upon will be hard constraints.

Returns:

  • (Boolean)


148
149
150
151
152
153
154
# File 'lib/vagrant/shared_helpers.rb', line 148

def self.strict_dependency_enforcement
  if ENV["VAGRANT_DISABLE_STRICT_DEPENDENCY_ENFORCEMENT"]
    false
  else
    true
  end
end

.user_data_pathPathname

This returns the path to the ~/.vagrant.d folder where Vagrant's per-user state is stored.

Returns:

  • (Pathname)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vagrant/shared_helpers.rb', line 110

def self.user_data_path
  # Use user specified env var if available
  path = ENV["VAGRANT_HOME"]

  # On Windows, we default to the USERPROFILE directory if it
  # is available. This is more compatible with Cygwin and sharing
  # the home directory across shells.
  if !path && ENV["USERPROFILE"]
    path = "#{ENV["USERPROFILE"]}/.vagrant.d"
  end

  # Fallback to the default
  path ||= "~/.vagrant.d"

  Pathname.new(path).expand_path
end

.vagrant_ssl_loadObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'ext/vagrant/vagrant_ssl/vagrant_ssl.c', line 10

static VALUE vagrant_ssl_load(VALUE self) {
  OSSL_PROVIDER *legacy;
  OSSL_PROVIDER *deflt;

  legacy = OSSL_PROVIDER_load(NULL, "legacy");
  if(legacy == NULL) {
    rb_raise(rb_eStandardError, "Failed to load OpenSSL legacy provider");
    return self;
  }

  deflt = OSSL_PROVIDER_load(NULL, "default");
  if(deflt == NULL) {
    rb_raise(rb_eStandardError, "Failed to load OpenSSL default provider");
    return self;
  }
}

.version?(*requirements) ⇒ Boolean

This checks if Vagrant is installed in a specific version.

Example:

Vagrant.version?(">= 2.1.0")

Returns:

  • (Boolean)


287
288
289
290
# File 'lib/vagrant.rb', line 287

def self.version?(*requirements)
  req = Gem::Requirement.new(*requirements)
  req.satisfied_by?(Gem::Version.new(VERSION))
end

.very_quiet?Boolean

Whether or not super quiet mode is enabled. This is ill-advised.

Returns:

  • (Boolean)


79
80
81
# File 'lib/vagrant/shared_helpers.rb', line 79

def self.very_quiet?
  !!ENV["VAGRANT_I_KNOW_WHAT_IM_DOING_PLEASE_BE_QUIET"]
end