vagrant/bin/vagrant

85 lines
2.5 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# Trap interrupts to quit cleanly. This will be overriden at some point
# by Vagrant. This is made to catch any interrupts while Vagrant is
# initializing which have historically resulted in stack traces.
Signal.trap("INT") { exit 1 }
require 'log4r'
require 'vagrant'
require 'vagrant/cli'
require 'vagrant/util/platform'
# Create a logger right away
logger = Log4r::Logger.new("vagrant::bin::vagrant")
logger.info("`vagrant` invoked: #{ARGV.inspect}")
# Stdout/stderr should not buffer output
$stdout.sync = true
$stderr.sync = true
# These will be the options that are passed to initialze the Vagrant
# environment.
opts = {}
# Disable color if the proper argument was passed or if we're
# on Windows since the default Windows terminal doesn't support
# colors.
if ARGV.include?("--no-color") || !$stdout.tty? || !Vagrant::Util::Platform.terminal_supports_colors?
# Delete the argument from the list so that it doesn't cause any
# invalid arguments down the road.
ARGV.delete("--no-color")
opts[:ui_class] = Vagrant::UI::Basic
else
opts[:ui_class] = Vagrant::UI::Colored
end
# This is kind of hacky, and I'd love to find a better way to do this, but
# if we're accessing the plugin interface, we want to NOT load plugins
# for this run, because they can actually interfere with the function
# of the plugin interface.
ARGV.each do |arg|
if !arg.start_with?("-")
ENV["VAGRANT_NO_PLUGINS"] = "1" if arg == "plugin"
break
end
end
env = nil
begin
# Create the environment, which is the cwd of wherever the
# `vagrant` command was invoked from
logger.debug("Creating Vagrant environment")
env = Vagrant::Environment.new(opts)
# If we're not in the installer, warn.
env.ui.warn(I18n.t("vagrant.general.not_in_installer")) if !Vagrant.in_installer?
begin
# Execute the CLI interface, and exit with the proper error code
exit_status = env.cli(ARGV)
ensure
# Unload the environment so cleanup can be done
env.unload
end
# Exit with the exit status from our CLI command
exit(exit_status)
rescue Vagrant::Errors::VagrantError => e
logger.error("Vagrant experienced an error! Details:")
logger.error(e.inspect)
logger.error(e.message)
logger.error(e.backtrace.join("\n"))
if env
opts = { :prefix => false }
env.ui.error e.message, opts if e.message
else
$stderr.puts "Vagrant failed to initialize at a very early stage:\n\n"
$stderr.puts e.message
end
exit e.status_code if e.respond_to?(:status_code)
exit 999 # An error occurred with no status code defined
end