From 6903f9bf09df24cff74b08919213bf9920b5a128 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 25 Oct 2021 15:36:17 -0500 Subject: [PATCH] Setup communincator service --- plugins/commands/serve/command.rb | 1 + plugins/commands/serve/service.rb | 1 + .../serve/service/communicator_service.rb | 404 ++++++++++++++++++ 3 files changed, 406 insertions(+) create mode 100644 plugins/commands/serve/service/communicator_service.rb diff --git a/plugins/commands/serve/command.rb b/plugins/commands/serve/command.rb index e2c65b763..3e2999f5e 100644 --- a/plugins/commands/serve/command.rb +++ b/plugins/commands/serve/command.rb @@ -98,6 +98,7 @@ module VagrantPlugins [Service::InternalService, Service::ProviderService, Service::GuestService, Service::HostService, Service::CommandService, Service::SyncedFolderService, + Service::CommunicatorService, Broker::Streamer].each do |service_klass| service = service_klass.new(broker: broker) s.handle(service) diff --git a/plugins/commands/serve/service.rb b/plugins/commands/serve/service.rb index 6310c86f6..cd37a53a0 100644 --- a/plugins/commands/serve/service.rb +++ b/plugins/commands/serve/service.rb @@ -3,6 +3,7 @@ module VagrantPlugins module Service autoload :CapabilityPlatformService, Vagrant.source_root.join("plugins/commands/serve/service/capability_platform_service").to_s autoload :CommandService, Vagrant.source_root.join("plugins/commands/serve/service/command_service").to_s + autoload :CommunicatorService, Vagrant.source_root.join("plugins/commands/serve/service/communicator_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 diff --git a/plugins/commands/serve/service/communicator_service.rb b/plugins/commands/serve/service/communicator_service.rb new file mode 100644 index 000000000..e495d62d1 --- /dev/null +++ b/plugins/commands/serve/service/communicator_service.rb @@ -0,0 +1,404 @@ +require "google/protobuf/well_known_types" + +module VagrantPlugins + module CommandServe + module Service + class CommunicatorService < Hashicorp::Vagrant::Sdk::CommunicatorService::Service + include Util::ServiceInfo + + prepend Util::HasMapper + prepend Util::HasBroker + prepend Util::ExceptionLogger + prepend Util::HasLogger + + def initialize(*args, **opts, &block) + super() + end + + def ready_spec(*_) + logger.debug("generating ready spec") + SDK::FuncSpec.new( + name: "ready_spec", + args: [ + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Target.Machine", + name: "", + ) + ], + result: [ + type: "hashicorp.vagrant.sdk.Communicator.ReadyResp", + name: "", + ] + ) + end + + def ready(req, ctx) + logger.debug("Checking if ready") + with_info(ctx) do |info| + plugin_name = info.plugin_name + logger.debug("Got plugin #{plugin_name}") + target = mapper.funcspec_map(req) + + machine = mapper.map(target, to: Vagrant::Machine) + logger.debug("Got machine #{machine}") + + plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym] + logger.debug("Got plugin #{plugin}") + + ready = plugin.new(machine).ready? + logger.debug("is ready: #{ready}") + SDK::Communicator::ReadyResp.new( + ready: ready + ) + end + end + + def wait_for_ready_spec(*_) + SDK::FuncSpec.new( + name: "ready_spec", + args: [ + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Target.Machine", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.TimeDuration", + name: "", + ) + ], + result: [ + type: "hashicorp.vagrant.sdk.Communicator.ReadyResp", + name: "", + ] + ) + end + + def wait_for_ready(req, ctx) + with_info(ctx) do |info| + plugin_name = info.plugin_name + logger.debug("Got plugin #{plugin_name}") + target, wait_duration = mapper.funcspec_map(req) + logger.debug("Got target #{target}") + logger.debug("Got duration #{wait_duration}") + + machine = mapper.map(target, to: Vagrant::Machine) + logger.debug("Got machine #{machine}") + + plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym] + logger.debug("Got plugin #{plugin}") + + begin + ready = plugin.new(machine).wait_for_ready(wait_duration) + rescue => err + logger.error(err) + logger.debug("#{err.class}: #{err}\n#{err.backtrace.join("\n")}") + raise + end + logger.debug("ready? #{ready}") + SDK::Communicator::ReadyResp.new( + ready: ready + ) + end + end + + def download_spec(*_) + SDK::FuncSpec.new( + name: "download_spec", + args: [ + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Target.Machine", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Path", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Path", + name: "", + ) + ], + result: [ + type: "hashicorp.vagrant.sdk.Communicator.FileTransferResp", + name: "", + ] + ) + end + + def download(req, ctx) + logger.debug("Uploading") + with_info(ctx) do |info| + plugin_name = info.plugin_name + logger.debug("Got plugin #{plugin_name}") + + target, from, to = mapper.funcspec_map(req) + logger.debug("Got target #{target}") + logger.debug("Got from #{from}") + logger.debug("Got to #{to}") + + logger.info("mapping received arguments to guest machine") + machine = mapper.map(target, to: Vagrant::Machine) + logger.debug("Got machine #{machine}") + + plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym] + logger.debug("Got plugin #{plugin}") + + communicator = plugin.new(machine) + logger.debug("communicator: #{communicator}") + + communicator.download(from, to) + + SDK::Communicator::FileTransferResp.new() + end + end + + def upload_spec(*_) + SDK::FuncSpec.new( + name: "upload_spec", + args: [ + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Target.Machine", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Path", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Path", + name: "", + ) + ], + result: [ + type: "hashicorp.vagrant.sdk.Communicator.FileTransferResp", + name: "", + ] + ) + end + + def upload(req, ctx) + logger.debug("Uploading") + with_info(ctx) do |info| + plugin_name = info.plugin_name + logger.debug("Got plugin #{plugin_name}") + + target, from, to = mapper.funcspec_map(req) + logger.debug("Got target #{target}") + logger.debug("Got from #{from}") + logger.debug("Got to #{to}") + + logger.info("mapping received arguments to guest machine") + machine = mapper.map(target, to: Vagrant::Machine) + logger.debug("Got machine #{machine}") + + plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym] + logger.debug("Got plugin #{plugin}") + + communicator = plugin.new(machine) + logger.debug("communicator: #{communicator}") + + communicator.upload(from, to) + + SDK::Communicator::FileTransferResp.new() + end + end + + def execute_spec(*_) + SDK::FuncSpec.new( + name: "execute_spec", + args: [ + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Target.Machine", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Command", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "", # TODO: get opts + name: "", + ) + ], + result: [ + type: "hashicorp.vagrant.sdk.Communicator.ExecuteResp", + name: "", + ] + ) + end + + def execute(req, ctx) + with_info(ctx) do |info| + plugin_name = info.plugin_name + logger.debug("Got plugin #{plugin_name}") + + target, cmd, opts = mapper.funcspec_map(req) + logger.debug("Got machine #{target}") + logger.debug("Got opts #{opts}") + logger.debug("Got cmd #{cmd}") + + logger.info("mapping received arguments to guest machine") + machine = mapper.map(target, to: Vagrant::Machine) + logger.debug("Got machine #{machine}") + + plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym] + logger.debug("Got plugin #{plugin}") + + communicator = plugin.new(machine) + logger.debug("communicator: #{communicator}") + + exit_code = communicator.execute(cmd, opts) + logger.debug("command exit code: #{exit_code}") + + SDK::Communicator::ExecuteResp.new( + exit_code: exit_code + ) + end + end + + def privileged_execute_spec(*_) + SDK::FuncSpec.new( + name: "privileged_execute_spec", + args: [ + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Target.Machine", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Command", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "", # TODO: get opts + name: "", + ) + ], + result: [ + type: "hashicorp.vagrant.sdk.Communicator.ExecuteResp", + name: "", + ] + ) + end + + def privileged_execute(req, ctx) + with_info(ctx) do |info| + plugin_name = info.plugin_name + logger.debug("Got plugin #{plugin_name}") + + target, cmd, opts = mapper.funcspec_map(req) + logger.debug("Got machine #{target}") + logger.debug("Got opts #{opts}") + logger.debug("Got cmd #{cmd}") + + logger.info("mapping received arguments to guest machine") + machine = mapper.map(target, to: Vagrant::Machine) + logger.debug("Got machine #{machine}") + + plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym] + logger.debug("Got plugin #{plugin}") + + communicator = plugin.new(machine) + logger.debug("communicator: #{communicator}") + + exit_code = communicator.sudo(cmd, opts) + logger.debug("command exit code: #{exit_code}") + + SDK::Communicator::ExecuteResp.new( + exit_code: exit_code + ) + end + end + + def test_spec(*_) + SDK::FuncSpec.new( + name: "test_spec", + args: [ + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Target.Machine", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Command", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "", # TODO: get opts + name: "", + ) + ], + result: [ + type: "hashicorp.vagrant.sdk.Communicator.TestResp", + name: "", + ] + ) + end + + def test(req, ctx) + with_info(ctx) do |info| + plugin_name = info.plugin_name + logger.debug("Got plugin #{plugin_name}") + + target, cmd, opts = mapper.funcspec_map(req) + logger.debug("Got machine #{target}") + logger.debug("Got opts #{opts}") + logger.debug("Got cmd #{cmd}") + + logger.info("mapping received arguments to guest machine") + machine = mapper.map(target, to: Vagrant::Machine) + logger.debug("Got machine #{machine}") + + plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym] + logger.debug("Got plugin #{plugin}") + + communicator = plugin.new(machine) + logger.debug("communicator: #{communicator}") + + valid = communicator.test(cmd, opts) + logger.debug("command is valid?: #{valid}") + + SDK::Communicator::TestResp.new( + valid: valid + ) + end + end + + def reset_spec(*_) + SDK::FuncSpec.new( + name: "reset_spec", + args: [ + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Target.Machine", + name: "", + ), + ], + result: [ + type: "hashicorp.vagrant.sdk.Communicator.ResetResp", + name: "", + ] + ) + end + + def reset(req, ctx) + with_info(ctx) do |info| + plugin_name = info.plugin_name + logger.debug("Got plugin #{plugin_name}") + target = mapper.funcspec_map(req) + + machine = mapper.map(target, to: Vagrant::Machine) + logger.debug("Got machine #{machine}") + + plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym] + logger.debug("Got plugin #{plugin}") + + communicator = plugin.new(machine) + logger.debug("communicator: #{communicator}") + + communicator.reset() + + SDK::Communicator::ResetResp.new() + end + end + end + end + end +end