Add guest client

This commit is contained in:
sophia 2021-08-20 16:54:57 -05:00 committed by Paul Hinze
parent f6a6ef8c79
commit 34e7105193
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 71 additions and 0 deletions

View File

@ -7,6 +7,7 @@ module VagrantPlugins
SRV = Service::SRV
ServiceInfo = Service::ServiceInfo
autoload :Guest, Vagrant.source_root.join("plugins/commands/serve/client/guest").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,70 @@
require "google/protobuf/well_known_types"
module VagrantPlugins
module CommandServe
module Client
class Guest
extend Util::Connector
attr_reader :client
def initialize(conn, broker=nil)
@logger = Log4r::Logger.new("vagrant::command::serve::client::guest")
@logger.debug("connecting to guest service on #{conn}")
@client = SDK::GuestService::Stub.new(conn, :this_channel_is_insecure)
@broker = broker
end
def self.load(raw_guest, broker:)
g = raw_guest.is_a?(String) ? SDK::Args::Guest.decode(raw_guest) : raw_guest
self.new(connect(proto: g, broker: broker), broker)
end
# @param [Symbol] cap_name Capability name
# @return [Boolean]
def 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 resutl #{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