module Vagrant

This file contains all of the internal errors in Vagrant’s core commands, actions, etc.

This file contains all the errors that the V1 plugin interface may throw.

This file contains all the errors that the V2 plugin interface may throw.

lib/remote.rb

Constants

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.

@return [String]

PLUGIN_COMPONENTS

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

SERVER_MODE_CALLBACKS
THREAD_MAX_JOIN_TIMEOUT

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

@return [Integer] @note This is not the maximum time for a thread to complete.

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`

Public Class Methods

[]=(n, l) click to toggle source
# File lib/vagrant/shared_helpers.rb, line 246
def self.[]=(n, l)
  self.synchronize do
    l.outputters = Log4r::Outputter["hclog"] if l.parent == Log4r::RootLogger.instance
    instance.loggers[n] = l
  end
end
add_default_cli_options(block) click to toggle source

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

@param [Proc] block Proc instance containing OptParser configuration @return [nil]

# File lib/vagrant/shared_helpers.rb, line 203
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?() click to toggle source

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

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 132
def self.allow_prerelease_dependencies?
  !!ENV["VAGRANT_ALLOW_PRERELEASE"]
end
auto_install_local_plugins?() click to toggle source

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

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 153
def self.auto_install_local_plugins?
  if ENV["VAGRANT_INSTALL_LOCAL_PLUGINS"]
    true
  else
    false
  end
end
configure(version, &block) click to toggle source

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.

@param [String] version Version of the configuration

# File lib/vagrant.rb, line 204
def self.configure(version, &block)
  Config.run(version, &block)
end
default_cli_options() click to toggle source

Array of default CLI options to automatically add to commands.

@return [Array<Proc>] Default optparse options

# File lib/vagrant/shared_helpers.rb, line 221
def self.default_cli_options
  @_default_cli_options = [] if !@_default_cli_options
  @_default_cli_options.dup
end
enable_resolv_replace() click to toggle source

Use Ruby Resolv in place of libc

@return [boolean] enabled or not

# File lib/vagrant/shared_helpers.rb, line 164
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
enable_server_mode!() click to toggle source

Flag Vagrant as running in server mode

@return [true]

# File lib/vagrant/shared_helpers.rb, line 236
def self.enable_server_mode!
  if !server_mode?
    load_vagrant_proto!
    SERVER_MODE_CALLBACKS.each(&:call)
    Util::HCLogOutputter.new("hclog")
    Log4r::Outputter["hclog"].formatter = Util::HCLogFormatter.new
    Log4r::Logger.each_logger do |l|
      l.outputters = Log4r::Outputter["hclog"] if l.parent == Log4r::RootLogger.instance
    end
    Log4r::Logger::Repository.class_eval do
      def self.[]=(n, l)
        self.synchronize do
          l.outputters = Log4r::Outputter["hclog"] if l.parent == Log4r::RootLogger.instance
          instance.loggers[n] = l
        end
      end
    end
  end
  if ENV["VAGRANT_LOG_MAPPER"].to_s == ""
    l = Log4r::Logger.new("vagrantplugins::commandserve::mappers::internal")
    l.level = Log4r::ERROR
  end
  @_server_mode = true
end
global_lock() { || ... } click to toggle source

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).

# File lib/vagrant/shared_helpers.rb, line 24
def self.global_lock
  @@global_lock.synchronize do
    return yield
  end
end
global_logger() click to toggle source

Get the global logger instance

@return [Logger]

# File lib/vagrant/shared_helpers.rb, line 190
def self.global_logger
  if @_global_logger.nil?
    require "log4r"
    @_global_logger = Log4r::Logger.new("vagrant::global")
  end
  @_global_logger
end
global_logger=(log) click to toggle source

Set the global logger

@param log Logger @return [Logger]

# File lib/vagrant/shared_helpers.rb, line 183
def self.global_logger=(log)
  @_global_logger = log
end
has_plugin?(name, version=nil) click to toggle source

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.

# File lib/vagrant.rb, line 211
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?() click to toggle source

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

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 41
def self.in_bundler?
  !!ENV["BUNDLE_GEMFILE"] &&
    !defined?(::Bundler).nil?
end
in_installer?() click to toggle source

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

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 34
def self.in_installer?
  !!ENV["VAGRANT_INSTALLER_ENV"]
end
installer_embedded_dir() click to toggle source

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

@return [String]

# File lib/vagrant/shared_helpers.rb, line 50
def self.installer_embedded_dir
  return nil if !Vagrant.in_installer?
  ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"]
end
load_vagrant_proto!() click to toggle source

Load the vagrant proto messages

# File lib/vagrant/shared_helpers.rb, line 262
def self.load_vagrant_proto!
  return if @_vagrant_proto_loaded
  # Update the load path so our protos can be located
  $LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs").to_s
  $LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs/proto").to_s
  $LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs/proto/vagrant_plugin_sdk").to_s

  # Load our protos so they are available
  require 'vagrant/protobufs/proto/vagrant_server/server_pb'
  require 'vagrant/protobufs/proto/vagrant_server/server_services_pb'
  require 'vagrant/protobufs/proto/ruby_vagrant/ruby-server_pb'
  require 'vagrant/protobufs/proto/ruby_vagrant/ruby-server_services_pb'
  require 'vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb'
  require 'vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb'
  require 'vagrant/protobufs/proto/plugin/grpc_broker_pb'
  require 'vagrant/protobufs/proto/plugin/grpc_broker_services_pb'
  @_vagrant_proto_loaded = true
end
log_level() click to toggle source

The current log level for Vagrant

@return [String]

# File lib/vagrant/shared_helpers.rb, line 79
def self.log_level
  ENV.fetch("VAGRANT_LOG", "fatal").downcase
end
original_env() click to toggle source

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

@return [Hash]

# File lib/vagrant.rb, line 305
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) click to toggle source

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.

@param [String] version @param [String] component @return [Class]

# File lib/vagrant.rb, line 241
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?() click to toggle source

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

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 65
def self.plugins_enabled?
  !ENV["VAGRANT_NO_PLUGINS"]
end
plugins_init?() click to toggle source

Should the plugin system be initialized

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 58
def self.plugins_init?
  !ENV['VAGRANT_DISABLE_PLUGIN_INIT']
end
prerelease?() click to toggle source

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

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 124
def self.prerelease?
  Gem::Version.new(Vagrant::VERSION).prerelease?
end
require_plugin(name) click to toggle source

@deprecated

# File lib/vagrant.rb, line 257
def self.require_plugin(name)
  puts "Vagrant.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) click to toggle source

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:

Vagrant.require_version(">= 1.3.5")
Vagrant.require_version(">= 1.3.5", "< 1.4.0")
Vagrant.require_version("~> 1.3.5")
# File lib/vagrant.rb, line 286
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_mode?() click to toggle source

Check if Vagrant is running in server mode

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 229
def self.server_mode?
  !!@_server_mode
end
server_url(config_server_url=nil) click to toggle source

Returns the URL prefix to the server.

@return [String]

# File lib/vagrant/shared_helpers.rb, line 86
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_root() click to toggle source

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

@return [Pathname]

# File lib/vagrant/shared_helpers.rb, line 95
def self.source_root
  @source_root ||= Pathname.new(File.expand_path('../../../', __FILE__))
end
strict_dependency_enforcement() click to toggle source

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

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 141
def self.strict_dependency_enforcement
  if ENV["VAGRANT_DISABLE_STRICT_DEPENDENCY_ENFORCEMENT"]
    false
  else
    true
  end
end
user_data_path() click to toggle source

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

@return [Pathname]

# File lib/vagrant/shared_helpers.rb, line 103
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
version?(*requirements) click to toggle source

This checks if Vagrant is installed in a specific version.

Example:

Vagrant.version?(">= 2.1.0")
# File lib/vagrant.rb, line 269
def self.version?(*requirements)
  req = Gem::Requirement.new(*requirements)
  req.satisfied_by?(Gem::Version.new(VERSION))
end
very_quiet?() click to toggle source

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

@return [Boolean]

# File lib/vagrant/shared_helpers.rb, line 72
def self.very_quiet?
  !!ENV["VAGRANT_I_KNOW_WHAT_IM_DOING_PLEASE_BE_QUIET"]
end