Start server as vagrant command
This commit is contained in:
parent
c1dbc5efad
commit
c02baa9219
2
Gemfile
2
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"
|
||||
|
||||
@ -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
|
||||
|
||||
59
plugins/commands/serve/command.rb
Normal file
59
plugins/commands/serve/command.rb
Normal 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
|
||||
17
plugins/commands/serve/plugin.rb
Normal file
17
plugins/commands/serve/plugin.rb
Normal 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
|
||||
18
plugins/commands/serve/service/plugin_service.rb
Normal file
18
plugins/commands/serve/service/plugin_service.rb
Normal 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
|
||||
@ -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"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user