From fb2a102c717ea4d1d30b7864d3c477eb31e3a2d8 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Wed, 29 Jun 2022 15:39:47 -0500 Subject: [PATCH 1/2] Fix init and other commands that run without a project The Basis needs to be able to respond to Vagrantfile() and DefaultProvider() to make it through Vagrant::Environment initialization. Depends on https://github.com/hashicorp/vagrant-plugin-sdk/pull/178 --- internal/core/basis.go | 96 +++++++++++++++++++ lib/vagrant/environment/remote.rb | 2 +- .../proto/vagrant_plugin_sdk/plugin_pb.rb | 4 + .../vagrant_plugin_sdk/plugin_services_pb.rb | 8 +- plugins/commands/serve/client/basis.rb | 9 ++ 5 files changed, 115 insertions(+), 4 deletions(-) diff --git a/internal/core/basis.go b/internal/core/basis.go index 3ee1651a5..4bdee0244 100644 --- a/internal/core/basis.go +++ b/internal/core/basis.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "sort" "strings" "sync" @@ -12,6 +13,7 @@ import ( "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-multierror" goplugin "github.com/hashicorp/go-plugin" + "github.com/pkg/errors" "google.golang.org/protobuf/proto" "github.com/hashicorp/vagrant-plugin-sdk/component" @@ -404,6 +406,96 @@ func (b *Basis) DefaultPrivateKey() (path path.Path, err error) { return b.dir.DataDir().Join("insecure_private_key"), nil } +// DefaultProvider implements core.Basis +// This is a subset of the Project.DefaultProvider() algorithm, just the parts +// that make sense when you don't have a Vagrantfile +func (b *Basis) DefaultProvider() (string, error) { + logger := b.logger.Named("default-provider") + logger.Debug("Searching for default provider") + + defaultProvider := os.Getenv("VAGRANT_DEFAULT_PROVIDER") + if defaultProvider != "" { + logger.Debug("Using VAGRANT_DEFAULT_PROVIDER", "provider", defaultProvider) + return defaultProvider, nil + } + + usableProviders := []*core.NamedPlugin{} + pluginProviders, err := b.plugins.ListPlugins("provider") + if err != nil { + return "", err + } + for _, pp := range pluginProviders { + logger.Debug("considering plugin", "provider", pp.Name) + + plug, err := b.plugins.GetPlugin(pp.Name, pp.Type) + if err != nil { + return "", err + } + + plugOpts := plug.Options.(*component.ProviderOptions) + logger.Debug("got provider options", "options", fmt.Sprintf("%#v", plugOpts)) + + // Skip providers that can't be defaulted. + if !plugOpts.Defaultable { + logger.Debug("skipping non-defaultable provider", "provider", pp.Name) + continue + } + + // Skip the providers that aren't usable. + logger.Debug("Checking usable on provider", "provider", pp.Name) + pluginImpl := plug.Plugin.(core.Provider) + usable, err := pluginImpl.Usable() + if err != nil { + return "", err + } + if !usable { + logger.Debug("Skipping unusable provider", "provider", pp.Name) + continue + } + + // If we made it here we have a candidate usable provider + usableProviders = append(usableProviders, plug) + } + logger.Debug("Initial usable provider list", "usableProviders", usableProviders) + + // Sort by plugin priority, higher is first + sort.SliceStable(usableProviders, func(i, j int) bool { + iPriority := usableProviders[i].Options.(*component.ProviderOptions).Priority + jPriority := usableProviders[j].Options.(*component.ProviderOptions).Priority + return iPriority > jPriority + }) + logger.Debug("Priority sorted usable provider list", "usableProviders", usableProviders) + + preferredProviders := strings.Split(os.Getenv("VAGRANT_PREFERRED_PROVIDERS"), ",") + k := 0 + for _, pp := range preferredProviders { + spp := strings.TrimSpace(pp) // .map { s.strip } + if spp != "" { // .select { !s.empty? } + preferredProviders[k] = spp + k++ + } + } + preferredProviders = preferredProviders[:k] + + for _, pp := range preferredProviders { + for _, up := range usableProviders { + if pp == up.Name { + logger.Debug("Using preffered provider found in usable list", + "provider", pp) + return pp, nil + } + } + } + + if len(usableProviders) > 0 { + logger.Debug("Using the first provider from the usable list", + "provider", usableProviders[0]) + return usableProviders[0].Name, nil + } + + return "", errors.New("No default provider.") +} + // Implements core.Basis // Returns all the registered plugins of the types specified func (b *Basis) Plugins(types ...string) (plugins []*core.NamedPlugin, err error) { @@ -801,6 +893,10 @@ func (b *Basis) TargetIndex() (core.TargetIndex, error) { return b.index, nil } +func (b *Basis) Vagrantfile() (core.Vagrantfile, error) { + return b.vagrantfile, nil +} + // Returns the list of all known components func (b *Basis) Components(ctx context.Context) ([]*Component, error) { return b.components(b.ctx) diff --git a/lib/vagrant/environment/remote.rb b/lib/vagrant/environment/remote.rb index 95c91af6c..3d6bee44e 100644 --- a/lib/vagrant/environment/remote.rb +++ b/lib/vagrant/environment/remote.rb @@ -102,7 +102,7 @@ module Vagrant end def default_provider(**opts) - client.respond_to?(:default_provider) && client.default_provider(opts) + client.default_provider(**opts) end # Gets a target (machine) by name diff --git a/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb b/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb index 28a2e092b..3f66327c9 100644 --- a/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb +++ b/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb @@ -660,6 +660,9 @@ Google::Protobuf::DescriptorPool.generated_pool.build do add_message "hashicorp.vagrant.sdk.Basis.ResourceIdResponse" do optional :resource_id, :string, 1 end + add_message "hashicorp.vagrant.sdk.Basis.DefaultProviderResponse" do + optional :provider_name, :string, 1 + end add_message "hashicorp.vagrant.sdk.Target" do end add_message "hashicorp.vagrant.sdk.Target.ResourceIdResponse" do @@ -1164,6 +1167,7 @@ module Hashicorp Ref::Machine = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Ref.Machine").msgclass Basis = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Basis").msgclass Basis::ResourceIdResponse = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Basis.ResourceIdResponse").msgclass + Basis::DefaultProviderResponse = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Basis.DefaultProviderResponse").msgclass Target = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Target").msgclass Target::ResourceIdResponse = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Target.ResourceIdResponse").msgclass Target::RecordResponse = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("hashicorp.vagrant.sdk.Target.RecordResponse").msgclass diff --git a/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb.rb b/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb.rb index 5e320af6d..79d0b4ee9 100644 --- a/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb.rb +++ b/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb.rb @@ -356,14 +356,16 @@ module Hashicorp self.unmarshal_class_method = :decode self.service_name = 'hashicorp.vagrant.sdk.BasisService' + rpc :Boxes, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::BoxCollection rpc :CWD, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::Path rpc :DataDir, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::DataDir::Basis rpc :DefaultPrivateKey, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::Path - rpc :UI, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::TerminalUI + rpc :DefaultProvider, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Basis::DefaultProviderResponse rpc :Host, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::Host - rpc :Boxes, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::BoxCollection - rpc :TargetIndex, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::TargetIndex rpc :ResourceId, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Basis::ResourceIdResponse + rpc :TargetIndex, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::TargetIndex + rpc :Vagrantfile, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::Vagrantfile + rpc :UI, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::TerminalUI rpc :Seed, ::Hashicorp::Vagrant::Sdk::Args::Seeds, ::Google::Protobuf::Empty rpc :Seeds, ::Google::Protobuf::Empty, ::Hashicorp::Vagrant::Sdk::Args::Seeds end diff --git a/plugins/commands/serve/client/basis.rb b/plugins/commands/serve/client/basis.rb index 221171397..05270f46e 100644 --- a/plugins/commands/serve/client/basis.rb +++ b/plugins/commands/serve/client/basis.rb @@ -30,6 +30,11 @@ module VagrantPlugins resp.path end + def default_provider(**opts) + resp = client.default_provider(Empty.new) + resp.provider_name.to_sym + end + def target_index TargetIndex.load( client.target_index(Empty.new), @@ -37,6 +42,10 @@ module VagrantPlugins ) end + def vagrantfile + client.vagrantfile(Empty.new).to_ruby + end + # @return [Terminal] def ui begin From 17be9ba354f2f5f474e0c3235cf2dfdc148c61b3 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Fri, 1 Jul 2022 12:23:15 -0500 Subject: [PATCH 2/2] bump sdk --- go.mod | 4 ++-- go.sum | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index ad39ef19d..61724d825 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,7 @@ require ( github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/hashicorp/hcl/v2 v2.11.1 github.com/hashicorp/nomad/api v0.0.0-20200814140818-42de70466a9d - github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220627195327-e6d45c1cb0af + github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220701171937-4cbbeaea0e70 github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect github.com/imdario/mergo v0.3.11 github.com/improbable-eng/grpc-web v0.13.0 @@ -78,7 +78,7 @@ require ( github.com/sirupsen/logrus v1.6.0 // indirect github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace // indirect - github.com/stretchr/testify v1.7.0 + github.com/stretchr/testify v1.7.5 github.com/ulikunitz/xz v0.5.10 // indirect github.com/zclconf/go-cty v1.10.0 github.com/zclconf/go-cty-yaml v1.0.2 diff --git a/go.sum b/go.sum index 1e202f1e8..0a064c2b8 100644 --- a/go.sum +++ b/go.sum @@ -361,10 +361,8 @@ github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJb github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg= github.com/hashicorp/nomad/api v0.0.0-20200814140818-42de70466a9d h1:afuZ/KNbxwUgjEzq2NXO2bRKZgsIJQgFxgIRGETF0/A= github.com/hashicorp/nomad/api v0.0.0-20200814140818-42de70466a9d/go.mod h1:DCi2k47yuUDzf2qWAK8E1RVmWgz/lc0jZQeEnICTxmY= -github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220627190203-ee2c7a069525 h1:2TOiNL+Eue6x7n682MmfVfgISi1mc3huTYr+JTKzaZw= -github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220627190203-ee2c7a069525/go.mod h1:0Xm6Bo60edIQffVSamvUvl2ecy+Ikb2dLO4CyX68wfw= -github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220627195327-e6d45c1cb0af h1:XktfLJMCW35AvGGbV+Z0tCj12Dqi3ALGAgyJiIwOq8s= -github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220627195327-e6d45c1cb0af/go.mod h1:0Xm6Bo60edIQffVSamvUvl2ecy+Ikb2dLO4CyX68wfw= +github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220701171937-4cbbeaea0e70 h1:Pqn/v5JGkP20UNz8gyq/bm4OBdhitEqxs5y/+CmaeFY= +github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220701171937-4cbbeaea0e70/go.mod h1:bdjvCJEaP+EFbg9vKdYuT5WuCfnFbemm0FEn4KtZaqU= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce h1:7UnVY3T/ZnHUrfviiAgIUjg2PXxsQfs5bphsG8F7Keo= github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= @@ -534,15 +532,18 @@ github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace h1:9PNP1jnUjRhfmGMlk github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q= +github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/tj/go-spin v1.1.0 h1:lhdWZsvImxvZ3q1C5OIB7d72DuOwP4O2NdBg9PyzNds= github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4= github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= @@ -952,8 +953,9 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=