Start server as vagrant command

This commit is contained in:
sophia 2020-11-30 15:31:01 -06:00 committed by Paul Hinze
parent c1dbc5efad
commit c02baa9219
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
8 changed files with 98 additions and 1 deletions

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"