diff --git a/plugins/commands/serve/client.rb b/plugins/commands/serve/client.rb index 4aa2c2e6e..f4447a4e0 100644 --- a/plugins/commands/serve/client.rb +++ b/plugins/commands/serve/client.rb @@ -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 diff --git a/plugins/commands/serve/client/capability_platform.rb b/plugins/commands/serve/client/capability_platform.rb new file mode 100644 index 000000000..f5dee511c --- /dev/null +++ b/plugins/commands/serve/client/capability_platform.rb @@ -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 diff --git a/plugins/commands/serve/client/guest.rb b/plugins/commands/serve/client/guest.rb index 870374ec2..ab8db423c 100644 --- a/plugins/commands/serve/client/guest.rb +++ b/plugins/commands/serve/client/guest.rb @@ -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 diff --git a/plugins/commands/serve/client/host.rb b/plugins/commands/serve/client/host.rb new file mode 100644 index 000000000..a40a142f8 --- /dev/null +++ b/plugins/commands/serve/client/host.rb @@ -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 [] 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 diff --git a/plugins/commands/serve/client/project.rb b/plugins/commands/serve/client/project.rb index 9a62adf3e..f9130661e 100644 --- a/plugins/commands/serve/client/project.rb +++ b/plugins/commands/serve/client/project.rb @@ -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 []