From 3bda8b24efd472c0c8b5037a0159c9207e7b03fa Mon Sep 17 00:00:00 2001 From: sophia Date: Tue, 3 Aug 2021 15:38:20 -0500 Subject: [PATCH] Save point: Get a target successfully from target index --- internal/core/target.go | 1 + lib/vagrant/machine_index.rb | 4 ++++ lib/vagrant/machine_index/remote.rb | 18 ++++++++++++++++-- lib/vagrant/plugin/v2/command.rb | 5 +++++ .../proto/vagrant_plugin_sdk/plugin_pb.rb | 1 + plugins/commands/serve/client/machine_index.rb | 2 +- 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/internal/core/target.go b/internal/core/target.go index aa4b5583a..2147d8cd4 100644 --- a/internal/core/target.go +++ b/internal/core/target.go @@ -59,6 +59,7 @@ func (t *Target) Ref() interface{} { return &vagrant_plugin_sdk.Ref_Target{ ResourceId: t.target.ResourceId, Project: t.target.Project, + Name: t.target.Name, } } diff --git a/lib/vagrant/machine_index.rb b/lib/vagrant/machine_index.rb index a289d622e..73757b826 100644 --- a/lib/vagrant/machine_index.rb +++ b/lib/vagrant/machine_index.rb @@ -407,6 +407,10 @@ module Vagrant # The parameter given should be nil if this is being created # publicly. def initialize(id=nil, raw=nil) + @logger = Log4r::Logger.new("vagrant::machine_index::entry") + @logger.debug("got id: #{id}") + @logger.debug("got raw: #{raw}") + @extra_data = {} # Do nothing if we aren't given a raw value. Otherwise, parse it. diff --git a/lib/vagrant/machine_index/remote.rb b/lib/vagrant/machine_index/remote.rb index 0e78ed45d..f53dc050f 100644 --- a/lib/vagrant/machine_index/remote.rb +++ b/lib/vagrant/machine_index/remote.rb @@ -39,7 +39,11 @@ module Vagrant name: name, project: @project_ref ) - @client.get(ref) + get_response = @client.get(ref) + @logger.debug("got machine #{get_response} for #{name}") + entry = machine_to_entry(get_response.target, get_response.provider) + @logger.debug("entry: #{entry.to_json_struct}") + entry end # Tests if the index has the given UUID. @@ -85,7 +89,17 @@ module Vagrant # # @param [Hashicorp::Vagrant::Sdk::Args::Target] # @return [Vagrant::MachineIndex::Entry] - def machine_to_entry(machine) + def machine_to_entry(machine, provider) + @logger.debug("machine name: #{machine.name}") + raw = { + "name" => machine.name, + "vagrantfile_path" => machine.project.path, + "provider" => provider, + } + entry = Vagrant::MachineIndex::Entry.new( + id=machine.name, raw=raw + ) + return entry end end end diff --git a/lib/vagrant/plugin/v2/command.rb b/lib/vagrant/plugin/v2/command.rb index f61a926ed..d2cbdfb7e 100644 --- a/lib/vagrant/plugin/v2/command.rb +++ b/lib/vagrant/plugin/v2/command.rb @@ -123,12 +123,16 @@ module Vagrant # provider if it exists, otherwise use the default provider. # get_machine = lambda do |name| + @logger.debug("getting machine #{name}") + # Check for an active machine with the same name provider_to_use = options[:provider] provider_to_use = provider_to_use.to_sym if provider_to_use # If we have this machine in our index, load that. entry = @env.machine_index.get(name.to_s) + @logger.debug("initial entry get #{entry.to_json_struct}") + if entry @env.machine_index.release(entry) @@ -147,6 +151,7 @@ module Vagrant raise end + @logger.debug("command: entry #{entry.to_json_struct}") next env.machine(entry.name.to_sym, entry.provider.to_sym) end 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 36abb36df..d69d6c313 100644 --- a/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb +++ b/lib/vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb.rb @@ -697,6 +697,7 @@ Google::Protobuf::DescriptorPool.generated_pool.build do end add_message "hashicorp.vagrant.sdk.TargetIndex.GetResponse" do optional :target, :message, 1, "hashicorp.vagrant.sdk.Ref.Target" + optional :provider, :string, 2 end add_message "hashicorp.vagrant.sdk.TargetIndex.IncludesResponse" do optional :exists, :bool, 1 diff --git a/plugins/commands/serve/client/machine_index.rb b/plugins/commands/serve/client/machine_index.rb index a805b9c11..a5d774dfe 100644 --- a/plugins/commands/serve/client/machine_index.rb +++ b/plugins/commands/serve/client/machine_index.rb @@ -31,7 +31,7 @@ module VagrantPlugins def get(ref) @logger.debug("getting machine with ref #{ref} from index") resp = @client.get(ref) - return resp.target + return resp end # @param [Hashicorp::Vagrant::Sdk::Ref::Target]