From fe293dfd1f242fd10a53f6cb31d6e584284885df Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Thu, 5 Aug 2021 10:52:41 -0700 Subject: [PATCH] Require client to be set and provide stub on initial startup When running in server mode, make the Environment validate a client option is provided on instantiation. If no client option is provided, raise an exception since it is required in server mode. Since an initial Environment is required during start, include a stub value for the client so the initial Environment can be instantiated successfully and the GRPC service can be started. --- bin/vagrant | 6 ++++++ lib/vagrant/environment/remote.rb | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/bin/vagrant b/bin/vagrant index adeba4f32..989fe2d3d 100755 --- a/bin/vagrant +++ b/bin/vagrant @@ -197,6 +197,12 @@ begin # Check if we are running the server if sub_cmd == "serve" Vagrant.enable_server_mode! + # NOTE: We stub the client option to allow the environment to + # be initialized for startup since it will raise an + # exception if it is unset. We don't care about the + # initially created environment, we just need it long + # enough for our serve command to get executed. + opts[:client] = :stub end # Create the environment, which is the cwd of wherever the diff --git a/lib/vagrant/environment/remote.rb b/lib/vagrant/environment/remote.rb index b4c53e059..b1695d26e 100644 --- a/lib/vagrant/environment/remote.rb +++ b/lib/vagrant/environment/remote.rb @@ -11,6 +11,10 @@ module Vagrant def initialize(opts={}) super @client = opts[:client] + if @client.nil? + raise ArgumentError, + "Remote client is required for `#{self.class.name}'" + end end # Gets a target (machine) by name @@ -18,7 +22,7 @@ module Vagrant # @param [String] machine name # return [VagrantPlugins::CommandServe::Client::Machine] def get_target(name) - return @client.target(name) + client.target(name) end end end