From d4ddb3c2f3356b4dd7392ee7d177da409545fea3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 4 Nov 2015 14:20:48 -0800 Subject: [PATCH] commands/provider --- lib/vagrant/errors.rb | 4 ++ plugins/commands/provider/command.rb | 69 ++++++++++++++++++++++++++++ plugins/commands/provider/plugin.rb | 18 ++++++++ templates/locales/en.yml | 5 ++ 4 files changed, 96 insertions(+) create mode 100644 plugins/commands/provider/command.rb create mode 100644 plugins/commands/provider/plugin.rb diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 906b8d9bc..f46491604 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -520,6 +520,10 @@ module Vagrant error_key(:requires_directory, "vagrant.actions.general.package") end + class ProviderCantInstall < VagrantError + error_key(:provider_cant_install) + end + class ProviderNotFound < VagrantError error_key(:provider_not_found) end diff --git a/plugins/commands/provider/command.rb b/plugins/commands/provider/command.rb new file mode 100644 index 000000000..791a82246 --- /dev/null +++ b/plugins/commands/provider/command.rb @@ -0,0 +1,69 @@ +require 'optparse' + +module VagrantPlugins + module CommandProvider + class Command < Vagrant.plugin("2", :command) + def self.synopsis + "show provider for this environment" + end + + def execute + options = {} + options[:install] = false + options[:usable] = false + + opts = OptionParser.new do |o| + o.banner = "Usage: vagrant provider [options] [args]" + o.separator "" + o.separator "This command interacts with the provider for this environment." + o.separator "With no arguments, it'll output the default provider for this" + o.separator "environment." + o.separator "" + o.separator "Options:" + o.separator "" + + o.on("--install", "Installs the provider if possible") do |f| + options[:install] = f + end + + o.on("--usable", "Checks if the named provider is usable") do |f| + options[:usable] = f + end + end + + # Parse the options + argv = parse_options(opts) + return if !argv + + # Get the machine + machine = nil + with_target_vms(argv, single_target: true) do |m| + machine = m + end + + # Output some machine readable stuff + @env.ui.machine("provider-name", machine.provider_name, target: machine.name.to_s) + + # Check if we're just doing a usability check + if options[:usable] + puts machine.provider_name.to_s + return 0 if machine.provider.class.usable?(false) + return 1 + end + + # Check if we're requesting installation + if options[:install] + if !machine.provider.capability?(:install) + raise Vagrant::Errors::ProviderCantInstall, + provider: machine.provider_name.to_s + end + + machine.provider.capability(:install) + end + + # Success, exit status 0 + 0 + end + end + end +end diff --git a/plugins/commands/provider/plugin.rb b/plugins/commands/provider/plugin.rb new file mode 100644 index 000000000..173088864 --- /dev/null +++ b/plugins/commands/provider/plugin.rb @@ -0,0 +1,18 @@ +require "vagrant" + +module VagrantPlugins + module CommandProvider + class Plugin < Vagrant.plugin("2") + name "provider command" + description <<-DESC + The `provider` command is used to interact with the various providers + that are installed with Vagrant. + DESC + + command("provider", primary: false) do + require_relative "command" + Command + end + end + end +end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 9e8c680aa..eee943b1c 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1001,6 +1001,11 @@ en: A file or directory you're attempting to include with your packaged box has symlinks in it. Vagrant cannot include symlinks in the resulting package. Please remove the symlinks and try again. + provider_cant_install: |- + The provider '%{provider}' doesn't support automatic installation. + This is a limitation of this provider. Please report this as a feature + request to the provider in question. To install this provider, you'll + have to do so manually. provider_not_found: |- The provider '%{provider}' could not be found, but was requested to back the machine '%{machine}'. Please use a provider that exists.