2013-02-03 02:42:04 +00:00
|
|
|
require "rubygems"
|
2013-02-03 21:59:43 +00:00
|
|
|
require "rubygems/dependency_installer"
|
2013-10-25 09:27:17 +00:00
|
|
|
begin
|
|
|
|
require "rubygems/format"
|
|
|
|
rescue LoadError
|
|
|
|
# rubygems 2.x
|
|
|
|
end
|
2013-02-03 02:42:04 +00:00
|
|
|
|
|
|
|
require "log4r"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module CommandPlugin
|
|
|
|
module Action
|
|
|
|
# This action takes the `:plugin_name` variable in the environment
|
|
|
|
# and installs it using the RubyGems API.
|
|
|
|
class InstallGem
|
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
@logger = Log4r::Logger.new("vagrant::plugins::plugincommand::installgem")
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
plugin_name = env[:plugin_name]
|
2013-03-21 05:30:31 +00:00
|
|
|
prerelease = env[:plugin_prerelease]
|
|
|
|
version = env[:plugin_version]
|
2013-02-03 02:42:04 +00:00
|
|
|
|
2013-07-29 04:38:48 +00:00
|
|
|
# Determine the plugin name we'll look for in the installed set
|
|
|
|
# in order to determine the version and all that.
|
|
|
|
find_plugin_name = plugin_name
|
|
|
|
if plugin_name =~ /\.gem$/
|
|
|
|
# If we're installing from a gem file, determine the name
|
|
|
|
# based on the spec in the file.
|
2013-10-25 09:27:17 +00:00
|
|
|
pkg = if defined?(Gem::Format)
|
|
|
|
Gem::Format.from_file_by_path(plugin_name)
|
|
|
|
else
|
|
|
|
Gem::Package.new(plugin_name)
|
|
|
|
end
|
2013-07-29 04:38:48 +00:00
|
|
|
find_plugin_name = pkg.spec.name
|
2013-10-25 09:27:17 +00:00
|
|
|
version = pkg.spec.version
|
2013-07-29 04:38:48 +00:00
|
|
|
end
|
|
|
|
|
2013-02-03 07:31:53 +00:00
|
|
|
# Install the gem
|
2013-03-20 06:02:32 +00:00
|
|
|
plugin_name_label = plugin_name
|
|
|
|
plugin_name_label += ' --prerelease' if prerelease
|
2013-03-21 05:30:31 +00:00
|
|
|
plugin_name_label += " --version '#{version}'" if version
|
2013-02-03 07:31:53 +00:00
|
|
|
env[:ui].info(I18n.t("vagrant.commands.plugin.installing",
|
2013-03-20 06:02:32 +00:00
|
|
|
:name => plugin_name_label))
|
2013-02-03 21:59:43 +00:00
|
|
|
installed_gems = env[:gem_helper].with_environment do
|
2013-03-21 05:33:50 +00:00
|
|
|
# Override the list of sources by the ones set as a parameter if given
|
|
|
|
if env[:plugin_sources]
|
|
|
|
@logger.info("Custom plugin sources: #{env[:plugin_sources]}")
|
|
|
|
Gem.sources = env[:plugin_sources]
|
|
|
|
end
|
2013-03-20 06:02:32 +00:00
|
|
|
|
|
|
|
installer = Gem::DependencyInstaller.new(:document => [], :prerelease => prerelease)
|
2013-02-03 22:29:29 +00:00
|
|
|
|
|
|
|
begin
|
2013-03-20 06:02:32 +00:00
|
|
|
installer.install(plugin_name, version)
|
2013-02-03 22:29:29 +00:00
|
|
|
rescue Gem::GemNotFoundException
|
|
|
|
raise Vagrant::Errors::PluginInstallNotFound,
|
|
|
|
:name => plugin_name
|
|
|
|
end
|
2013-02-03 21:59:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# The plugin spec is the last installed gem since RubyGems
|
|
|
|
# currently always installed the requested gem last.
|
|
|
|
@logger.debug("Installed #{installed_gems.length} gems.")
|
2013-07-29 04:38:48 +00:00
|
|
|
plugin_spec = installed_gems.find do |gem|
|
|
|
|
gem.name.downcase == find_plugin_name.downcase
|
|
|
|
end
|
2013-02-03 21:59:43 +00:00
|
|
|
|
|
|
|
# Store the installed name so we can uninstall it if things go
|
|
|
|
# wrong.
|
|
|
|
@installed_plugin_name = plugin_spec.name
|
2013-02-03 02:42:04 +00:00
|
|
|
|
|
|
|
# Mark that we installed the gem
|
2013-02-03 21:59:43 +00:00
|
|
|
@logger.info("Adding the plugin to the state file...")
|
|
|
|
env[:plugin_state_file].add_plugin(plugin_spec.name)
|
|
|
|
|
|
|
|
# Tell the user
|
|
|
|
env[:ui].success(I18n.t("vagrant.commands.plugin.installed",
|
2013-02-07 01:08:48 +00:00
|
|
|
:name => plugin_spec.name,
|
|
|
|
:version => plugin_spec.version.to_s))
|
2013-02-03 02:42:04 +00:00
|
|
|
|
2013-07-26 05:53:53 +00:00
|
|
|
# If the plugin's spec includes a post-install message display it
|
2013-08-09 18:26:39 +00:00
|
|
|
post_install_message = plugin_spec.post_install_message
|
|
|
|
if post_install_message
|
|
|
|
if post_install_message.is_a?(Array)
|
|
|
|
post_install_message = post_install_message.join(" ")
|
|
|
|
end
|
2013-07-26 05:53:53 +00:00
|
|
|
|
|
|
|
env[:ui].info(I18n.t("vagrant.commands.plugin.post_install",
|
|
|
|
:name => plugin_spec.name,
|
2013-08-09 18:26:39 +00:00
|
|
|
:message => post_install_message.to_s))
|
2013-07-26 05:53:53 +00:00
|
|
|
end
|
|
|
|
|
2013-02-03 02:42:04 +00:00
|
|
|
# Continue
|
|
|
|
@app.call(env)
|
|
|
|
end
|
2013-02-03 21:59:43 +00:00
|
|
|
|
|
|
|
def recover(env)
|
|
|
|
# If any error happens, we uninstall it and remove it from
|
|
|
|
# the state file. We can only do this if we successfully installed
|
|
|
|
# the gem in the first place.
|
|
|
|
if @installed_plugin_name
|
|
|
|
new_env = env.dup
|
|
|
|
new_env.delete(:interrupted)
|
|
|
|
new_env[:plugin_name] = @installed_plugin_name
|
|
|
|
new_env[:action_runner].run(Action.action_uninstall, new_env)
|
|
|
|
end
|
|
|
|
end
|
2013-02-03 02:42:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|