Use Hash Arg to pass around communincator args

This commit is contained in:
sophia 2021-12-08 13:59:27 -06:00 committed by Paul Hinze
parent ba1dd33fff
commit 73b6c7c6d6
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 29 additions and 14 deletions

View File

@ -4,6 +4,7 @@ module VagrantPlugins
module Client
class Communicator
prepend Util::HasMapper
extend Util::Connector
include Util::HasSeeds::Client
@ -177,10 +178,7 @@ module VagrantPlugins
protected
def generate_execution_request(machine, cmd, opts={})
opts.transform_values! { |v| v.is_a?(Symbol) ? v.to_s : v}
opts_struct = Google::Protobuf::Struct.from_hash(
opts.transform_keys(&:to_s)
)
opts_proto = mapper.map(opts, to: SDK::Args::Hash)
SDK::FuncSpec::Args.new(
args: [
@ -194,8 +192,8 @@ module VagrantPlugins
value: Google::Protobuf::Any.pack(SDK::Communicator::Command.new(command: cmd)),
),
SDK::FuncSpec::Value.new(
type: "google.protobuf.Struct",
value: Google::Protobuf::Any.pack(opts_struct),
type: "hashicorp.vagrant.sdk.Args.Hash",
value: Google::Protobuf::Any.pack(opts_proto),
),
]
)

View File

@ -51,6 +51,24 @@ module VagrantPlugins
end
end
class HashProtoFromSpec < Mapper
def initialize
super(
inputs: [Input.new(type: SDK::FuncSpec::Value) { |arg|
arg.type == "hashicorp.vagrant.sdk.Args.Hash" &&
!arg&.value&.value.nil?
}
],
output: SDK::Args::Hash,
func: method(:converter),
)
end
def converter(fv)
SDK::Args::Hash.decode(fv.value.value)
end
end
class HashFromProto < Mapper
def initialize
super(
@ -65,7 +83,7 @@ module VagrantPlugins
def converter(proto, mapper)
Hash.new.tap do |result|
proto.fields.each_pair do |k, v|
proto.fields.each do |k, v|
r = mapper.map(v)
result[k.to_s] = r
end

View File

@ -260,7 +260,7 @@ module VagrantPlugins
name: "",
),
SDK::FuncSpec::Value.new(
type: "google.protobuf.Struct",
type: "hashicorp.vagrant.sdk.Args.Hash",
name: "",
)
],
@ -274,15 +274,14 @@ module VagrantPlugins
def privileged_execute(req, ctx)
with_info(ctx) do |info|
plugin_name = info.plugin_name
target, cmd, opts = mapper.funcspec_map(req, mapper, broker)
logger.debug("Got machine client #{target}")
machine, cmd, opts = mapper.funcspec_map(
req, mapper, broker,
expect: [Vagrant::Machine, SDK::Communicator::Command, Hash]
)
logger.debug("Got machine client #{machine}")
logger.debug("Got opts #{opts}")
logger.debug("Got cmd #{cmd}")
project = target.project
env = Vagrant::Environment.new({client: project})
machine = env.machine(target.name.to_sym, target.provider_name.to_sym)
plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym]
communicator = plugin.new(machine)
exit_code = communicator.sudo(cmd.command, opts)