Setup host client so that a golang based host may be used

This commit is contained in:
sophia 2021-08-27 15:25:49 -05:00 committed by Paul Hinze
parent 0016baef36
commit a2ade36a74
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
5 changed files with 102 additions and 47 deletions

View File

@ -1,7 +1,9 @@
module VagrantPlugins
module CommandServe
module Client
autoload :CapabilityPlatform, Vagrant.source_root.join("plugins/commands/serve/client/capability_platform").to_s
autoload :Guest, Vagrant.source_root.join("plugins/commands/serve/client/guest").to_s
autoload :Host, Vagrant.source_root.join("plugins/commands/serve/client/host").to_s
autoload :Machine, Vagrant.source_root.join("plugins/commands/serve/client/machine").to_s
autoload :TargetIndex, Vagrant.source_root.join("plugins/commands/serve/client/target_index").to_s
autoload :Project, Vagrant.source_root.join("plugins/commands/serve/client/project").to_s

View File

@ -0,0 +1,56 @@
require "google/protobuf/well_known_types"
module VagrantPlugins
module CommandServe
module Client
module CapabilityPlatform
attr_reader :client
# @param [Symbol] cap_name Capability name
# @return [Boolean]
def has_capability?(cap_name)
@logger.debug("checking for capability #{cap_name}")
val = SDK::Args::NamedCapability.new(Capability: cap_name.to_s)
req = SDK::FuncSpec::Args.new(
args: [SDK::FuncSpec::Value.new(
name: "",
type: "hashicorp.vagrant.sdk.Args.NamedCapability",
value: Google::Protobuf::Any.pack(val)
)]
)
res = client.has_capability(req)
@logger.debug("got result #{res}")
res.has_capability
end
# @param [Symbol] cap_name Name of the capability
def capability(cap_name, *args)
arg_protos = []
args.each do |a|
if a.class.ancestors.include?(Google::Protobuf::MessageExts)
val = a
else
val = Google::Protobuf::Value.new
val.from_ruby(a)
end
arg_protos << SDK::FuncSpec::Value.new(
name: "",
type: "",
value: Google::Protobuf::Any.pack(val)
)
end
req = SDK::Platform::Capability::NamedRequest.new(
name: cap_name.to_s,
func_args: SDK::FuncSpec::Args.new(
args: arg_protos
)
)
@client.capability(req)
end
end
end
end
end

View File

@ -4,6 +4,7 @@ module VagrantPlugins
module CommandServe
module Client
class Guest
include CapabilityPlatform
extend Util::Connector
@ -34,50 +35,6 @@ module VagrantPlugins
@logger.debug("got parents #{res}")
res.parents
end
# @param [Symbol] cap_name Capability name
# @return [Boolean]
def has_capability?(cap_name)
@logger.debug("checking for capability #{cap_name}")
val = SDK::Args::NamedCapability.new(Capability: cap_name.to_s)
req = SDK::FuncSpec::Args.new(
args: [SDK::FuncSpec::Value.new(
name: "",
type: "hashicorp.vagrant.sdk.Args.NamedCapability",
value: Google::Protobuf::Any.pack(val)
)]
)
res = client.has_capability(req)
@logger.debug("got result #{res}")
res.has_capability
end
# @param [Symbol] cap_name Name of the capability
def capability(cap_name, *args)
arg_protos = []
args.each do |a|
if a.class.ancestors.include?(Google::Protobuf::MessageExts)
val = a
else
val = Google::Protobuf::Value.new
val.from_ruby(a)
end
arg_protos << SDK::FuncSpec::Value.new(
name: "",
type: "",
value: Google::Protobuf::Any.pack(val)
)
end
req = SDK::Platform::Capability::NamedRequest.new(
name: cap_name.to_s,
func_args: SDK::FuncSpec::Args.new(
args: arg_protos
)
)
@client.capability(req)
end
end
end
end

View File

@ -0,0 +1,41 @@
require "google/protobuf/well_known_types"
module VagrantPlugins
module CommandServe
module Client
class Host
include CapabilityPlatform
extend Util::Connector
attr_reader :broker
attr_reader :client
attr_reader :proto
def initialize(conn, proto, broker=nil)
@logger = Log4r::Logger.new("vagrant::command::serve::client::host")
@logger.debug("connecting to host service on #{conn}")
@client = SDK::HostService::Stub.new(conn, :this_channel_is_insecure)
@broker = broker
@proto = proto
end
def self.load(raw_host, broker:)
g = raw_host.is_a?(String) ? SDK::Args::Host.decode(raw_host) : raw_host
self.new(connect(proto: g, broker: broker), g, broker)
end
# @return [<String>] parents
def parents
@logger.debug("getting parents")
req = SDK::FuncSpec::Args.new(
args: []
)
res = client.parents(req)
@logger.debug("got parents #{res}")
res.parents
end
end
end
end
end

View File

@ -68,9 +68,8 @@ module VagrantPlugins
# TODO
def host
@client.host(Google::Protobuf::Empty.new)
# TODO load the remote host plugin.
nil
h = @client.host(Google::Protobuf::Empty.new)
Host.load(h, broker: @broker)
end
# return [<String>]