2012-02-22 18:00:19 +00:00
|
|
|
require 'log4r'
|
|
|
|
|
2011-12-04 01:12:07 +00:00
|
|
|
# Enable logging if it is requested. We do this before
|
|
|
|
# anything else so that we can setup the output before
|
|
|
|
# any logging occurs.
|
2012-01-07 21:13:17 +00:00
|
|
|
if ENV["VAGRANT_LOG"] && ENV["VAGRANT_LOG"] != ""
|
2012-01-08 04:39:57 +00:00
|
|
|
# Require Log4r and define the levels we'll be using
|
|
|
|
require 'log4r/config'
|
|
|
|
Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)
|
2012-01-07 21:13:17 +00:00
|
|
|
|
|
|
|
level = nil
|
|
|
|
begin
|
|
|
|
level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
|
|
|
|
rescue NameError
|
|
|
|
# This means that the logging constant wasn't found,
|
|
|
|
# which is fine. We just keep `level` as `nil`. But
|
|
|
|
# we tell the user.
|
2012-01-18 17:40:01 +00:00
|
|
|
level = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Some constants, such as "true" resolve to booleans, so the
|
|
|
|
# above error checking doesn't catch it. This will check to make
|
|
|
|
# sure that the log level is an integer, as Log4r requires.
|
|
|
|
level = nil if !level.is_a?(Integer)
|
|
|
|
|
|
|
|
if !level
|
|
|
|
# We directly write to stderr here because the VagrantError system
|
|
|
|
# is not setup yet.
|
2012-01-08 03:23:03 +00:00
|
|
|
$stderr.puts "Invalid VAGRANT_LOG level is set: #{ENV["VAGRANT_LOG"]}"
|
2012-01-18 17:40:01 +00:00
|
|
|
$stderr.puts ""
|
|
|
|
$stderr.puts "Please use one of the standard log levels: debug, info, warn, or error"
|
|
|
|
exit 1
|
2012-01-07 21:13:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set the logging level on all "vagrant" namespaced
|
|
|
|
# logs as long as we have a valid level.
|
|
|
|
if level
|
|
|
|
logger = Log4r::Logger.new("vagrant")
|
2012-01-20 01:12:18 +00:00
|
|
|
logger.outputters = Log4r::Outputter.stderr
|
2012-01-07 21:13:17 +00:00
|
|
|
logger.level = level
|
|
|
|
logger = nil
|
|
|
|
end
|
2011-12-04 01:12:07 +00:00
|
|
|
end
|
|
|
|
|
2013-03-01 19:55:06 +00:00
|
|
|
require 'json'
|
2010-09-04 02:25:48 +00:00
|
|
|
require 'pathname'
|
2013-03-01 19:55:06 +00:00
|
|
|
require 'stringio'
|
|
|
|
|
2011-12-19 02:14:20 +00:00
|
|
|
require 'childprocess'
|
2010-08-27 04:56:38 +00:00
|
|
|
require 'i18n'
|
2010-03-13 11:08:26 +00:00
|
|
|
|
2011-07-09 18:12:15 +00:00
|
|
|
# OpenSSL must be loaded here since when it is loaded via `autoload`
|
|
|
|
# there are issues with ciphers not being properly loaded.
|
|
|
|
require 'openssl'
|
|
|
|
|
2012-02-07 03:39:35 +00:00
|
|
|
# Always make the version available
|
|
|
|
require 'vagrant/version'
|
2012-06-24 21:24:52 +00:00
|
|
|
global_logger = Log4r::Logger.new("vagrant::global")
|
|
|
|
global_logger.info("Vagrant version: #{Vagrant::VERSION}")
|
2012-02-07 03:39:35 +00:00
|
|
|
|
2012-06-26 22:47:26 +00:00
|
|
|
# We need these components always so instead of an autoload we
|
|
|
|
# just require them explicitly here.
|
|
|
|
require "vagrant/registry"
|
|
|
|
|
2010-08-24 04:33:14 +00:00
|
|
|
module Vagrant
|
2011-01-07 09:12:16 +00:00
|
|
|
autoload :Action, 'vagrant/action'
|
2010-09-11 17:17:26 +00:00
|
|
|
autoload :Box, 'vagrant/box'
|
|
|
|
autoload :BoxCollection, 'vagrant/box_collection'
|
|
|
|
autoload :CLI, 'vagrant/cli'
|
2011-12-17 07:34:30 +00:00
|
|
|
autoload :Command, 'vagrant/command'
|
2010-09-11 17:17:26 +00:00
|
|
|
autoload :Config, 'vagrant/config'
|
2011-01-07 09:12:16 +00:00
|
|
|
autoload :Downloaders, 'vagrant/downloaders'
|
2011-12-19 02:14:20 +00:00
|
|
|
autoload :Driver, 'vagrant/driver'
|
2011-01-07 09:12:16 +00:00
|
|
|
autoload :Environment, 'vagrant/environment'
|
2010-09-11 17:17:26 +00:00
|
|
|
autoload :Errors, 'vagrant/errors'
|
2011-12-16 05:05:19 +00:00
|
|
|
autoload :Guest, 'vagrant/guest'
|
2011-01-07 09:12:16 +00:00
|
|
|
autoload :Hosts, 'vagrant/hosts'
|
2012-07-16 17:28:42 +00:00
|
|
|
autoload :Machine, 'vagrant/machine'
|
2013-01-21 17:25:28 +00:00
|
|
|
autoload :MachineState, 'vagrant/machine_state'
|
2010-09-15 05:10:51 +00:00
|
|
|
autoload :Plugin, 'vagrant/plugin'
|
2010-09-15 15:19:38 +00:00
|
|
|
autoload :TestHelpers, 'vagrant/test_helpers'
|
2011-01-07 09:12:16 +00:00
|
|
|
autoload :UI, 'vagrant/ui'
|
2010-09-11 17:17:26 +00:00
|
|
|
autoload :Util, 'vagrant/util'
|
2010-08-24 17:27:36 +00:00
|
|
|
|
2012-06-26 22:47:26 +00:00
|
|
|
# These are the various plugin versions and their components in
|
|
|
|
# a lazy loaded Hash-like structure.
|
2012-08-09 04:28:28 +00:00
|
|
|
PLUGIN_COMPONENTS = 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 }
|
2012-11-07 05:05:14 +00:00
|
|
|
|
|
|
|
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 }
|
2012-08-09 04:28:28 +00:00
|
|
|
end
|
2012-06-26 22:47:26 +00:00
|
|
|
|
2013-01-28 21:39:56 +00:00
|
|
|
# This returns a true/false showing whether we're running from the
|
|
|
|
# environment setup by the Vagrant installers.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def self.in_installer?
|
|
|
|
!!ENV["VAGRANT_INSTALLER_ENV"]
|
|
|
|
end
|
|
|
|
|
2010-08-24 18:18:29 +00:00
|
|
|
# The source root is the path to the root directory of
|
|
|
|
# the Vagrant gem.
|
|
|
|
def self.source_root
|
2010-09-04 02:25:48 +00:00
|
|
|
@source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
|
2010-08-24 04:33:14 +00:00
|
|
|
end
|
2011-12-12 07:22:44 +00:00
|
|
|
|
2012-05-22 04:47:01 +00:00
|
|
|
# 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
|
|
|
|
def self.configure(version, &block)
|
|
|
|
Config.run(version, &block)
|
|
|
|
end
|
|
|
|
|
2012-04-15 20:34:44 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2012-06-26 22:47:26 +00:00
|
|
|
# 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.
|
2012-04-15 20:34:44 +00:00
|
|
|
#
|
|
|
|
# @return [Class]
|
2012-06-26 22:47:26 +00:00
|
|
|
def self.plugin(version, component=nil)
|
|
|
|
# Build up the key and return a result
|
|
|
|
key = version.to_sym
|
|
|
|
key = [key, component.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}"
|
2012-04-15 20:34:44 +00:00
|
|
|
end
|
2012-05-06 21:01:10 +00:00
|
|
|
|
|
|
|
# This should be used instead of Ruby's built-in `require` in order to
|
|
|
|
# load a Vagrant plugin. This will load the given plugin by first doing
|
|
|
|
# a normal `require`, giving a nice error message if things go wrong,
|
|
|
|
# and second by verifying that a Vagrant plugin was actually defined in
|
|
|
|
# the process.
|
|
|
|
#
|
|
|
|
# @param [String] name Name of the plugin to load.
|
|
|
|
def self.require_plugin(name)
|
2013-03-01 19:55:06 +00:00
|
|
|
# Redirect stdout/stderr so that we can output it in our own way.
|
|
|
|
previous_stderr = $stderr
|
|
|
|
previous_stdout = $stdout
|
|
|
|
$stderr = StringIO.new
|
|
|
|
$stdout = StringIO.new
|
|
|
|
|
2012-05-06 21:01:10 +00:00
|
|
|
# Attempt the normal require
|
|
|
|
begin
|
|
|
|
require name
|
2013-02-03 22:13:27 +00:00
|
|
|
rescue Exception => e
|
2013-02-22 23:11:01 +00:00
|
|
|
# Since this is a rare case, we create a one-time logger here
|
|
|
|
# in order to output the error
|
|
|
|
logger = Log4r::Logger.new("vagrant::root")
|
|
|
|
logger.error("Failed to load plugin: #{name}")
|
|
|
|
logger.error(" -- Error: #{e.inspect}")
|
|
|
|
logger.error(" -- Backtrace:")
|
|
|
|
logger.error(e.backtrace.join("\n"))
|
|
|
|
|
2013-02-22 23:10:34 +00:00
|
|
|
# If it is a LoadError we first try to see if it failed loading
|
|
|
|
# the top-level entrypoint. If so, then we report a different error.
|
|
|
|
if e.is_a?(LoadError)
|
|
|
|
# Parse the message in order to get what failed to load, and
|
|
|
|
# add some extra protection around if the message is different.
|
|
|
|
parts = e.to_s.split(" -- ", 2)
|
|
|
|
if parts.length == 2 && parts[1] == name
|
|
|
|
raise Errors::PluginLoadError, :plugin => name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-01 19:55:06 +00:00
|
|
|
# Get the string data out from the stdout/stderr captures
|
|
|
|
stderr = $stderr.string
|
|
|
|
stdout = $stdout.string
|
|
|
|
if !stderr.empty? || !stdout.empty?
|
|
|
|
raise Errors::PluginLoadFailedWithOutput,
|
|
|
|
:plugin => name,
|
|
|
|
:stderr => stderr,
|
|
|
|
:stdout => stdout
|
|
|
|
end
|
|
|
|
|
2013-02-03 22:13:27 +00:00
|
|
|
# And raise an error itself
|
2013-03-01 19:55:06 +00:00
|
|
|
raise Errors::PluginLoadFailed,
|
|
|
|
:plugin => name
|
2012-05-06 21:01:10 +00:00
|
|
|
end
|
2013-03-01 19:55:06 +00:00
|
|
|
ensure
|
|
|
|
$stderr = previous_stderr
|
|
|
|
$stdout = previous_stdout
|
2012-05-06 21:01:10 +00:00
|
|
|
end
|
2010-08-24 04:33:14 +00:00
|
|
|
end
|
|
|
|
|
2012-06-24 21:24:52 +00:00
|
|
|
# Default I18n to load the en locale
|
2010-08-27 04:56:38 +00:00
|
|
|
I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root)
|
|
|
|
|
2012-04-20 23:53:01 +00:00
|
|
|
# A lambda that knows how to load plugins from a single directory.
|
|
|
|
plugin_load_proc = lambda do |directory|
|
2012-04-18 05:12:27 +00:00
|
|
|
# We only care about directories
|
2012-06-23 17:47:01 +00:00
|
|
|
next false if !directory.directory?
|
2012-04-18 05:12:27 +00:00
|
|
|
|
2012-04-20 23:53:01 +00:00
|
|
|
# If there is a plugin file in the top-level directory, then load
|
|
|
|
# that up.
|
2012-04-18 05:12:27 +00:00
|
|
|
plugin_file = directory.join("plugin.rb")
|
2012-04-20 23:53:01 +00:00
|
|
|
if plugin_file.file?
|
2012-06-24 21:24:52 +00:00
|
|
|
global_logger.debug("Loading core plugin: #{plugin_file}")
|
2012-04-20 23:53:01 +00:00
|
|
|
load(plugin_file)
|
2012-06-23 17:47:01 +00:00
|
|
|
next true
|
2012-04-20 23:53:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Go through the `plugins` directory and attempt to load any plugins. The
|
|
|
|
# plugins are allowed to be in a directory in `plugins` or at most one
|
|
|
|
# directory deep within the plugins directory. So a plugin can be at
|
|
|
|
# `plugins/foo` or also at `plugins/foo/bar`, but no deeper.
|
2012-06-23 03:47:43 +00:00
|
|
|
Vagrant.source_root.join("plugins").children(true).each do |directory|
|
2012-04-20 23:53:01 +00:00
|
|
|
# Ignore non-directories
|
|
|
|
next if !directory.directory?
|
|
|
|
|
|
|
|
# Load from this directory, and exit if we successfully loaded a plugin
|
|
|
|
next if plugin_load_proc.call(directory)
|
2012-04-18 05:12:27 +00:00
|
|
|
|
2012-04-20 23:53:01 +00:00
|
|
|
# Otherwise, attempt to load from sub-directories
|
2012-06-23 03:47:43 +00:00
|
|
|
directory.children(true).each(&plugin_load_proc)
|
2012-04-18 05:12:27 +00:00
|
|
|
end
|