From 1ea5b2ba9150f38f544fee7ba502aa4c561c4efe Mon Sep 17 00:00:00 2001 From: sophia Date: Tue, 3 Aug 2021 12:08:14 -0500 Subject: [PATCH] Save point: use target ref to retrieve info from target index --- internal/core/target_index.go | 19 +++--------- lib/vagrant/environment/remote.rb | 5 +++- lib/vagrant/machine_index/remote.rb | 29 ++++++++++++------- .../commands/serve/client/machine_index.rb | 25 +++++++--------- plugins/commands/serve/client/project.rb | 5 ++++ .../commands/serve/service/command_service.rb | 14 ++++++--- 6 files changed, 51 insertions(+), 46 deletions(-) diff --git a/internal/core/target_index.go b/internal/core/target_index.go index 843dc87f5..04dce300b 100644 --- a/internal/core/target_index.go +++ b/internal/core/target_index.go @@ -30,31 +30,20 @@ func (t *TargetIndex) Delete(target core.Target) (err error) { return } -func (t *TargetIndex) Get(uuid string) (entry core.Target, err error) { - target, err := t.client.GetTarget( - t.ctx, - &vagrant_server.GetTargetRequest{ - Target: &vagrant_plugin_sdk.Ref_Target{ResourceId: uuid}, - }, - ) - refTarget := &vagrant_plugin_sdk.Ref_Target{ - ResourceId: target.Target.ResourceId, - Project: target.Target.Project, - Name: target.Target.Name, - } +func (t *TargetIndex) Get(ref *vagrant_plugin_sdk.Ref_Target) (entry core.Target, err error) { // TODO: check if this actually gets back a full target entry, err = NewTarget( t.ctx, - WithTargetRef(refTarget), + WithTargetRef(ref), ) return } -func (t *TargetIndex) Includes(uuid string) (exists bool, err error) { +func (t *TargetIndex) Includes(ref *vagrant_plugin_sdk.Ref_Target) (exists bool, err error) { resp, err := t.client.GetTarget( t.ctx, &vagrant_server.GetTargetRequest{ - Target: &vagrant_plugin_sdk.Ref_Target{ResourceId: uuid}, + Target: ref, }, ) if err != nil { diff --git a/lib/vagrant/environment/remote.rb b/lib/vagrant/environment/remote.rb index 218ee6bde..e45ae3486 100644 --- a/lib/vagrant/environment/remote.rb +++ b/lib/vagrant/environment/remote.rb @@ -35,7 +35,10 @@ module Vagrant if !@client.nil? machine_index_client = @client.machine_index @machine_index ||= Vagrant::MachineIndex.new() - @machine_index.set_client(machine_index_client) + @logger.debug("setting machine index client") + @machine_index.client = machine_index_client + @logger.debug("setting machine index project ref") + @machine_index.project_ref = @client.ref end @machine_index end diff --git a/lib/vagrant/machine_index/remote.rb b/lib/vagrant/machine_index/remote.rb index 2ee3bbf14..0e78ed45d 100644 --- a/lib/vagrant/machine_index/remote.rb +++ b/lib/vagrant/machine_index/remote.rb @@ -3,6 +3,10 @@ module Vagrant # This module enables the MachineIndex for server mode module Remote + attr_accessor :client + + attr_accessor :project_ref + # Add an attribute reader for the client # when applied to the MachineIndex class def self.prepended(klass) @@ -17,11 +21,6 @@ module Vagrant @machines = {} end - def set_client(client) - @logger.debug("setting machine index client") - @client = client - end - # Deletes a machine by UUID. # # @param [Entry] entry The entry to delete. @@ -33,18 +32,26 @@ module Vagrant # Accesses a machine by UUID # - # @param [String] uuid UUID for the machine to access. + # @param [String] name for the machine to access. # @return [MachineIndex::Entry] - def get(uuid) - @client.get(machine) + def get(name) + ref = Hashicorp::Vagrant::Sdk::Ref::Target.new( + name: name, + project: @project_ref + ) + @client.get(ref) end # Tests if the index has the given UUID. # - # @param [String] uuid + # @param [String] name # @return [Boolean] - def include?(uuid) - @client.include?(uuid) + def include?(name) + ref = Hashicorp::Vagrant::Sdk::Ref::Target.new( + name: name, + project: @project_ref + ) + @client.include?(ref) end def release(entry) diff --git a/plugins/commands/serve/client/machine_index.rb b/plugins/commands/serve/client/machine_index.rb index 3a1e8e55c..a805b9c11 100644 --- a/plugins/commands/serve/client/machine_index.rb +++ b/plugins/commands/serve/client/machine_index.rb @@ -26,24 +26,19 @@ module VagrantPlugins true end - # @param [String] uuid UUID for the machine to access. - # @return [Hashicorp::Vagrant::Sdk::Args::Target] - def get(uuid) - @logger.debug("getting machine with uuid #{uuid} from index") - req = TargetIndex::GetRequest.new( - uuid: uuid - ) - @client.get(req) + # @param [Hashicorp::Vagrant::Sdk::Ref::Target] a ref for the machine to access. + # @return [Hashicorp::Vagrant::Sdk::Ref::Target] + def get(ref) + @logger.debug("getting machine with ref #{ref} from index") + resp = @client.get(ref) + return resp.target end - # @param [String] uuid + # @param [Hashicorp::Vagrant::Sdk::Ref::Target] # @return [Boolean] - def include?(uuid) - @logger.debug("checking for machine with uuid #{uuid} in index") - req = TargetIndex::IncludesRequest.new( - uuid: uuid - ) - @client.includes(req).exists + def include?(ref) + @logger.debug("checking for machine with ref #{ref} in index") + @client.includes(red).exists end # @param [Hashicorp::Vagrant::Sdk::Args::Target] target diff --git a/plugins/commands/serve/client/project.rb b/plugins/commands/serve/client/project.rb index e978506ad..3522d8af7 100644 --- a/plugins/commands/serve/client/project.rb +++ b/plugins/commands/serve/client/project.rb @@ -4,6 +4,7 @@ module VagrantPlugins class Project attr_reader :client + attr_reader :resource_id def initialize(conn, broker=nil) @logger = Log4r::Logger.new("vagrant::command::serve::client::project") @@ -17,6 +18,10 @@ module VagrantPlugins self.new(conn.to_s, broker) end + def ref + SDK::Ref::Project.new(resource_id: resource_id) + end + # Returns a machine client for the given name # return [VagrantPlugins::CommandServe::Client::Machine] def target(name) diff --git a/plugins/commands/serve/service/command_service.rb b/plugins/commands/serve/service/command_service.rb index 15a089ed9..19d8455f8 100644 --- a/plugins/commands/serve/service/command_service.rb +++ b/plugins/commands/serve/service/command_service.rb @@ -76,10 +76,16 @@ module VagrantPlugins raise "Failed to locate command plugin for: #{plugin_name}" end - cmd_klass = plugin.call - cmd_args = req.command_args.to_a[1..] + arguments.args.to_a - cmd = cmd_klass.new(cmd_args, env) - result = cmd.execute + begin + cmd_klass = plugin.call + cmd_args = req.command_args.to_a[1..] + arguments.args.to_a + cmd = cmd_klass.new(cmd_args, env) + result = cmd.execute + rescue => err + LOGGER.error(err) + LOGGER.debug("#{err.class}: #{err}\n#{err.backtrace.join("\n")}") + raise + end if !result.is_a?(Integer) result = 1