diff --git a/Gemfile b/Gemfile index 565ca7959..69495bdbb 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,5 @@ if File.exist?(File.expand_path("../../vagrant-spec", __FILE__)) else gem 'vagrant-spec', git: "https://github.com/hashicorp/vagrant-spec.git", branch: :main end + +gem "pry-byebug" diff --git a/bin/vagrant-server.rb b/bin/vagrant-server.rb index 07036d3d5..66a0d841b 100755 --- a/bin/vagrant-server.rb +++ b/bin/vagrant-server.rb @@ -9,12 +9,12 @@ require 'grpc/health/v1/health_services_pb' class PluginService < Hashicorp::Vagrant::RubyVagrant::Service def get_plugins(req, _unused_call) + plugins = [Hashicorp::Vagrant::Plugin.new(name: "test")] Hashicorp::Vagrant::GetPluginsResponse.new( plugins: plugins ) end - end def main diff --git a/plugins/commands/serve/command.rb b/plugins/commands/serve/command.rb new file mode 100644 index 000000000..79284bb0f --- /dev/null +++ b/plugins/commands/serve/command.rb @@ -0,0 +1,59 @@ +require_relative "./service/plugin_service" +require "optparse" +require 'grpc' +require 'grpc/health/checker' +require 'grpc/health/v1/health_services_pb' + +module VagrantPlugins + module CommandServe + class Command < Vagrant.plugin("2", :command) + + DEFAULT_PORT = 10001 + + def self.synopsis + "start Vagrant server" + end + + def execute + options = {} + + opts = OptionParser.new do |o| + o.banner = "Usage: vagrant serve" + o.separator "" + o.separator "Options:" + o.separator "" + + o.on("--port PORT", "Port to start the GRPC server on, defaults to 10001") do |port| + options[:port] = port + end + end + + # Parse the options + argv = parse_options(opts) + return if !argv + options[:port] ||= DEFAULT_PORT + serve(options[:port]) + end + + private + + def serve(port=10001) + s = GRPC::RpcServer.new + # Listen on port 10001 on all interfaces. Update for production use. + s.add_http2_port("[::]:#{port}", :this_port_is_insecure) + + s.handle(VagrantPlugins::CommandServe::Serve::PluginService.new) + + health_checker = Grpc::Health::Checker.new + health_checker.add_status( + "Service::PluginService", + Grpc::Health::V1::HealthCheckResponse::ServingStatus::SERVING) + s.handle(health_checker) + + STDOUT.puts "1|1|tcp|127.0.0.1:#{port}|grpc" + STDOUT.flush + s.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT', 'SIGINT']) + end + end + end +end diff --git a/plugins/commands/serve/plugin.rb b/plugins/commands/serve/plugin.rb new file mode 100644 index 000000000..e12a42d97 --- /dev/null +++ b/plugins/commands/serve/plugin.rb @@ -0,0 +1,17 @@ +require "vagrant" + +module VagrantPlugins + module CommandServe + class Plugin < Vagrant.plugin("2") + name "start Vagrant server" + description <<-DESC + Start Vagrant in server mode + DESC + + command("serve") do + require File.expand_path("../command", __FILE__) + Command + end + end + end +end diff --git a/plugins/commands/serve/service/plugin_service.rb b/plugins/commands/serve/service/plugin_service.rb new file mode 100644 index 000000000..871b35b6c --- /dev/null +++ b/plugins/commands/serve/service/plugin_service.rb @@ -0,0 +1,18 @@ +require_relative 'proto/gen/ruby-server_pb' +require_relative 'proto/gen/ruby-server_services_pb' + +module VagrantPlugins + module CommandServe + module Serve + class PluginService < Hashicorp::Vagrant::RubyVagrant::Service + def get_plugins(req, _unused_call) + + plugins = [Hashicorp::Vagrant::Plugin.new(name: "test")] + Hashicorp::Vagrant::GetPluginsResponse.new( + plugins: plugins + ) + end + end + end + end +end diff --git a/proto/gen/ruby-server_pb.rb b/plugins/commands/serve/service/proto/gen/ruby-server_pb.rb similarity index 100% rename from proto/gen/ruby-server_pb.rb rename to plugins/commands/serve/service/proto/gen/ruby-server_pb.rb diff --git a/proto/gen/ruby-server_services_pb.rb b/plugins/commands/serve/service/proto/gen/ruby-server_services_pb.rb similarity index 100% rename from proto/gen/ruby-server_services_pb.rb rename to plugins/commands/serve/service/proto/gen/ruby-server_services_pb.rb diff --git a/vagrant.gemspec b/vagrant.gemspec index 992b48f2f..62415a7ae 100644 --- a/vagrant.gemspec +++ b/vagrant.gemspec @@ -19,6 +19,7 @@ Gem::Specification.new do |s| s.add_dependency "childprocess", "~> 4.1.0" s.add_dependency "ed25519", "~> 1.2.4" s.add_dependency "erubi" + s.add_dependency "grpc" s.add_dependency "hashicorp-checkpoint", "~> 0.1.5" s.add_dependency "i18n", "~> 1.8" s.add_dependency "listen", "~> 3.6"