Add guest server for using Ruby based guest plugins

This commit is contained in:
sophia 2021-08-20 11:45:26 -05:00 committed by Paul Hinze
parent d327d70247
commit a1fca2fde8
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 141 additions and 1 deletions

View File

@ -88,7 +88,7 @@ module VagrantPlugins
health_checker = Grpc::Health::Checker.new
broker = Broker.new(bind: bind_addr, ports: ports)
[Service::InternalService, Service::ProviderService,
[Service::InternalService, Service::ProviderService, Service::GuestService,
Service::HostService, Service::CommandService, Broker::Streamer].each do |service_klass|
service = service_klass.new(broker: broker)
s.handle(service)

View File

@ -7,6 +7,7 @@ module VagrantPlugins
SRV = Hashicorp::Vagrant
autoload :CommandService, Vagrant.source_root.join("plugins/commands/serve/service/command_service").to_s
autoload :GuestService, Vagrant.source_root.join("plugins/commands/serve/service/guest_service").to_s
autoload :HostService, Vagrant.source_root.join("plugins/commands/serve/service/host_service").to_s
autoload :InternalService, Vagrant.source_root.join("plugins/commands/serve/service/internal_service").to_s
autoload :ProviderService, Vagrant.source_root.join("plugins/commands/serve/service/provider_service").to_s

View File

@ -0,0 +1,139 @@
require "google/protobuf/well_known_types"
module VagrantPlugins
module CommandServe
module Service
class GuestService < Hashicorp::Vagrant::Sdk::GuestService::Service
prepend Util::HasBroker
prepend Util::ExceptionLogger
LOGGER = Log4r::Logger.new("vagrant::command::serve::command")
def detect_spec(*_)
SDK::FuncSpec.new(
name: "detect_spec",
args: [
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Args.Target",
name: "",
)
],
result: [
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Platform.DetectResp",
name: "",
),
],
)
end
def detect(req, ctx)
ServiceInfo.with_info(ctx) do |info|
plugin_name = info.plugin_name
raw_target = req.args.detect { |a|
a.type == "hashicorp.vagrant.sdk.Args.Target"
}&.value&.value
target = Client::Target.load(raw_target, broker: broker)
machine_client = target.to_machine
# TODO: this machine should be a Remote::Machine
machine = machine_client
plugin = Vagrant.plugin("2").manager.guests[plugin_name.to_s.to_sym].to_a.first
if !plugin
LOGGER.debug("Failed to locate guest plugin for: #{plugin_name}")
raise "Failed to locate guest plugin for: #{plugin_name.inspect}"
end
guest = plugin.new
begin
detected = guest.detect?(machine)
rescue => err
LOGGER.debug("error detecting guest plugin!")
LOGGER.error(err)
LOGGER.debug("#{err.class}: #{err}\n#{err.backtrace.join("\n")}")
detected = false
end
LOGGER.debug("detected? #{detected}")
SDK::Platform::DetectResp.new(
detected: detected,
)
end
end
def parents_spec(*_)
SDK::FuncSpec.new(
name: "parents_spec",
result: [
type: "hashicorp.vagrant.sdk.Platform.ParentsResp",
name: "",
]
)
end
def parents(req, ctx)
ServiceInfo.with_info(ctx) do |info|
plugin_name = info.plugin_name
plugin = Vagrant.plugin("2").manager.guest[plugin_name.to_s.to_sym].to_a.first
if !plugin
raise "Failed to locate guest plugin for: #{plugin_name.inspect}"
end
SDK::Platform::ParentsResp.new(
parents: plugin.new.parents
)
end
end
def has_capability_spec(*_)
SDK::FuncSpec.new(
name: "has_capability_spec",
args: [
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Args.NamedCapability",
name: "",
)
],
result: [
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Platform.Capability.CheckResp",
name: "",
),
],
)
end
def has_capability(req, ctx)
# TODO
end
# TODO: Need to be able to specify all the arguments that are required
# for the capability
def capability_spec(*_)
SDK::FuncSpec.new(
name: "has_capability_spec",
args: [
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Args.NamedCapability",
name: "",
),
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Args.Target",
name: "",
),
],
result: [
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Platform.Capability.CheckResp",
name: "",
),
],
)
end
# TODO: Need to be able to specify all the arguments that are required
# for the capability
def capability(req, ctx)
# TODO
end
end
end
end
end