From 55c905b43b0bc427faafcafa27872f123772f91d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 4 Feb 2013 10:22:15 -0800 Subject: [PATCH] `vagrant plugin license` command. --- lib/vagrant/errors.rb | 8 +++ plugins/commands/plugin/action.rb | 8 +++ .../commands/plugin/action/license_plugin.rb | 53 +++++++++++++++++++ plugins/commands/plugin/command/license.rb | 31 +++++++++++ plugins/commands/plugin/command/root.rb | 5 ++ templates/locales/en.yml | 11 ++++ 6 files changed, 116 insertions(+) create mode 100644 plugins/commands/plugin/action/license_plugin.rb create mode 100644 plugins/commands/plugin/command/license.rb diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 179efd992..7de28415a 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -299,6 +299,10 @@ module Vagrant error_key(:plugin_install_bad_entry_point) end + class PluginInstallLicenseNotFound < VagrantError + error_key(:plugin_install_license_not_found) + end + class PluginInstallNotFound < VagrantError error_key(:plugin_install_not_found) end @@ -311,6 +315,10 @@ module Vagrant error_key(:plugin_load_failed) end + class PluginNotFound < VagrantError + error_key(:plugin_not_found) + end + class SCPPermissionDenied < VagrantError error_key(:scp_permission_denied) end diff --git a/plugins/commands/plugin/action.rb b/plugins/commands/plugin/action.rb index 4f14c2cf6..c628df3d7 100644 --- a/plugins/commands/plugin/action.rb +++ b/plugins/commands/plugin/action.rb @@ -14,6 +14,14 @@ module VagrantPlugins end end + # This middleware sequence licenses paid addons. + def self.action_license + Vagrant::Action::Builder.new.tap do |b| + b.use BundlerCheck + b.use LicensePlugin + end + end + # This middleware sequence will list all installed plugins. def self.action_list Vagrant::Action::Builder.new.tap do |b| diff --git a/plugins/commands/plugin/action/license_plugin.rb b/plugins/commands/plugin/action/license_plugin.rb new file mode 100644 index 000000000..3bbf63df6 --- /dev/null +++ b/plugins/commands/plugin/action/license_plugin.rb @@ -0,0 +1,53 @@ +require "fileutils" +require "pathname" +require "rubygems" +require "set" + +require "log4r" + +module VagrantPlugins + module CommandPlugin + module Action + # This middleware licenses a plugin by copying the license file to + # the proper place. + class LicensePlugin + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::plugins::plugincommand::license") + end + + def call(env) + # Get the list of installed plugins according to the state file + installed = Set.new(env[:plugin_state_file].installed_plugins) + + # If the plugin we're trying to license doesn't exist in the + # state file, then it is an error. + if !installed.include?(env[:plugin_name]) + raise Vagrant::Errors::PluginNotFound, :name => env[:plugin_name] + end + + # Verify the license file exists + license_file = Pathname.new(env[:plugin_license_path]) + if !license_file.file? + raise Vagrant::Errors::PluginInstallLicenseNotFound, + :name => env[:plugin_name], + :path => license_file.to_s + end + + # Copy it in. + final_path = env[:home_path].join("license-#{env[:plugin_name]}.lic") + @logger.info("Copying license from: #{license_file}") + @logger.info("Copying license to: #{final_path}") + env[:ui].info(I18n.t("vagrant.commands.plugin.installing_license", + :name => env[:plugin_name])) + FileUtils.cp(license_file, final_path) + + # Installed! + env[:ui].success(I18n.t("vagrant.commands.plugin.installed_license")) + + @app.call(env) + end + end + end + end +end diff --git a/plugins/commands/plugin/command/license.rb b/plugins/commands/plugin/command/license.rb new file mode 100644 index 000000000..81a0c9419 --- /dev/null +++ b/plugins/commands/plugin/command/license.rb @@ -0,0 +1,31 @@ +require 'optparse' + +require_relative "base" + +module VagrantPlugins + module CommandPlugin + module Command + class License < Base + def execute + opts = OptionParser.new do |o| + o.banner = "Usage: vagrant plugin license [-h]" + end + + # Parse the options + argv = parse_options(opts) + return if !argv + raise Vagrant::Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 2 + + # License the plugin + action(Action.action_license, { + :plugin_license_path => argv[1], + :plugin_name => argv[0] + }) + + # Success, exit status 0 + 0 + end + end + end + end +end diff --git a/plugins/commands/plugin/command/root.rb b/plugins/commands/plugin/command/root.rb index fe7f9b339..18e4749de 100644 --- a/plugins/commands/plugin/command/root.rb +++ b/plugins/commands/plugin/command/root.rb @@ -15,6 +15,11 @@ module VagrantPlugins Install end + @subcommands.register(:license) do + require_relative "license" + License + end + @subcommands.register(:list) do require_relative "list" List diff --git a/templates/locales/en.yml b/templates/locales/en.yml index b34e860c8..4064da644 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -175,6 +175,10 @@ en: the entry point doesn't exist. The entry point attempted was '%{entry_point}'. If this is not correct, please manually specify an `--entry-point` when installing the plugin. + plugin_install_license_not_found: |- + The license file to install could not be found. Please verify + the path you gave is correct. The path to the license file given + was: '%{path}' plugin_install_not_found: |- The plugin '%{name}' could not be found in local or remote repositories. Please check the name of the plugin and try again. @@ -183,6 +187,9 @@ en: properly installed via `vagrant plugin`. plugin_load_failed: |- Failed to load the "%{plugin}" plugin. View logs for more details. + plugin_not_found: |- + The plugin '%{name}' could not be found. Please install this plugin + prior to attempting to do anything with it. port_collision_resume: |- This VM cannot be resumed, because the forwarded ports would collide with another running virtual machine. Normally, Vagrant will attempt to fix this @@ -420,6 +427,10 @@ en: the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. plugin: + installed_license: |- + The license for '%{name}' was successfully installed! + installing_license: |- + Installing license for '%{name}'... no_plugins: |- No plugins installed. installed: |-