Isolate loading dependencies for serve command

Only load the dependencies needed for the serve command if the serve
command is being run. This allows Vagrant to properly load on platforms
where some of the dependency libraries may not be available due to
incompatibilities or being EOL.
This commit is contained in:
Chris Roberts 2022-12-07 15:35:00 -08:00
parent 1b68be9d52
commit 1cd6ac3b31
2 changed files with 27 additions and 16 deletions

View File

@ -235,6 +235,19 @@ module Vagrant
# @return [true] # @return [true]
def self.enable_server_mode! def self.enable_server_mode!
if !server_mode? if !server_mode?
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs").to_s
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs/proto").to_s
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs/proto/vagrant_plugin_sdk").to_s
require 'vagrant/protobufs/proto/vagrant_server/server_pb'
require 'vagrant/protobufs/proto/vagrant_server/server_services_pb'
require 'vagrant/protobufs/proto/ruby_vagrant/ruby-server_pb'
require 'vagrant/protobufs/proto/ruby_vagrant/ruby-server_services_pb'
require 'vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb'
require 'vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb'
require 'vagrant/protobufs/proto/plugin/grpc_broker_pb'
require 'vagrant/protobufs/proto/plugin/grpc_broker_services_pb'
SERVER_MODE_CALLBACKS.each(&:call) SERVER_MODE_CALLBACKS.each(&:call)
Util::HCLogOutputter.new("hclog") Util::HCLogOutputter.new("hclog")
Log4r::Outputter["hclog"].formatter = Util::HCLogFormatter.new Log4r::Outputter["hclog"].formatter = Util::HCLogFormatter.new

View File

@ -1,20 +1,4 @@
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs").to_s
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs/proto").to_s
$LOAD_PATH << Vagrant.source_root.join("lib/vagrant/protobufs/proto/vagrant_plugin_sdk").to_s
require 'vagrant/protobufs/proto/vagrant_server/server_pb'
require 'vagrant/protobufs/proto/vagrant_server/server_services_pb'
require 'vagrant/protobufs/proto/ruby_vagrant/ruby-server_pb'
require 'vagrant/protobufs/proto/ruby_vagrant/ruby-server_services_pb'
require 'vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_pb'
require 'vagrant/protobufs/proto/vagrant_plugin_sdk/plugin_services_pb'
require 'vagrant/protobufs/proto/plugin/grpc_broker_pb'
require 'vagrant/protobufs/proto/plugin/grpc_broker_services_pb'
require "optparse" require "optparse"
require 'grpc'
require 'grpc/health/checker'
require 'grpc/health/v1/health_services_pb'
module VagrantPlugins module VagrantPlugins
module CommandServe module CommandServe
@ -34,6 +18,17 @@ module VagrantPlugins
attr_accessor :broker attr_accessor :broker
attr_accessor :server attr_accessor :server
attr_reader :cache attr_reader :cache
# Loads the required dependencies for this command. This is isolated
# into a method so that the dependencies can be loaded just in time when
# the command is actually executed.
def load_dependencies!
return if @dependencies_loaded
require 'grpc'
require 'grpc/health/checker'
require 'grpc/health/v1/health_services_pb'
@dependencies_loaded = true
end
end end
@cache = Util::Cacher.new @cache = Util::Cacher.new
@ -49,6 +44,9 @@ module VagrantPlugins
end end
def execute def execute
# Load dependencies before we start
CommandServe.load_dependencies!
options = { options = {
bind: DEFAULT_BIND, bind: DEFAULT_BIND,
min_port: DEFAULT_PORT_RANGE.first, min_port: DEFAULT_PORT_RANGE.first,