From df53588de5566c76656123cbca8952b76be4e8ff Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 17 Aug 2021 14:03:40 -0700 Subject: [PATCH] Store machine client within index entry for use in generating environment --- lib/vagrant/machine_index/remote.rb | 69 ++++++++++++++++++++++++----- lib/vagrant/plugin/v2/command.rb | 4 +- lib/vagrant/shared_helpers.rb | 3 +- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/lib/vagrant/machine_index/remote.rb b/lib/vagrant/machine_index/remote.rb index a6ce39db2..4c9994c66 100644 --- a/lib/vagrant/machine_index/remote.rb +++ b/lib/vagrant/machine_index/remote.rb @@ -2,17 +2,64 @@ module Vagrant class MachineIndex class Entry module Remote - def load(machine) - raw = Vagrant::Util::HashWithIndifferentAccess.new({ - name: machine.name, - local_data_path: machine.project.local_data_path, - provider: machine.provider_name, - full_state: machine.machine_state, - state: machine.machine_state.id, - vagrantfile_name: machine.project.vagrantfile_name, - vagrantfile_path: machine.project.vagrantfile_path, - }) - self.new(machine.get_uuid, raw) + module ClassMethods + def load(machine) + raw = Vagrant::Util::HashWithIndifferentAccess.new({ + name: machine.name, + local_data_path: machine.project.local_data_path, + provider: machine.provider_name, + full_state: machine.machine_state, + state: machine.machine_state.id, + vagrantfile_name: machine.project.vagrantfile_name, + vagrantfile_path: machine.project.vagrantfile_path, + machine: machine + }) + self.new(machine.get_uuid, raw) + end + end + + module InstanceMethods + def initialize(id, raw=nil) + @logger = Log4r::Logger.new("vagrant::machine_index::entry") + + @extra_data = {} + @id = id + # Do nothing if we aren't given a raw value. Otherwise, parse it. + return if !raw + + @local_data_path = raw["local_data_path"] + @name = raw["name"] + @provider = raw["provider"] + @state = raw["state"] + @full_state = raw["full_state"] + @vagrantfile_name = raw["vagrantfile_name"] + @vagrantfile_path = raw["vagrantfile_path"] + # TODO(mitchellh): parse into a proper datetime + @updated_at = raw["updated_at"] + @extra_data = raw["extra_data"] || {} + + @machine_client = raw["machine_client"] + + # Be careful with the paths + @local_data_path = nil if @local_data_path == "" + @vagrantfile_path = nil if @vagrantfile_path == "" + + # Convert to proper types + @local_data_path = Pathname.new(@local_data_path) if @local_data_path + @vagrantfile_path = Pathname.new(@vagrantfile_path) if @vagrantfile_path + end + + def vagrant_env(home_path, opts={}) + Vagrant::Util::SilenceWarnings.silence! do + Environment.new({ + cwd: @vagrantfile_path, + home_path: home_path, + local_data_path: @local_data_path, + vagrantfile_name: @vagrantfile_name, + client: @machine_client&.project, + }.merge(opts)) + end + end end end end diff --git a/lib/vagrant/plugin/v2/command.rb b/lib/vagrant/plugin/v2/command.rb index 194cd82ef..f61a926ed 100644 --- a/lib/vagrant/plugin/v2/command.rb +++ b/lib/vagrant/plugin/v2/command.rb @@ -137,10 +137,8 @@ module Vagrant # Vagrantfiles often have constants, so people would otherwise # constantly (heh) get "already initialized constant" warnings. begin - # TODO: Remove reference to client here. This won't work - # running legacy vagrant directly. env = entry.vagrant_env( - @env.home_path, ui_class: @env.ui_class, client: @env.client) + @env.home_path, ui_class: @env.ui_class) rescue Vagrant::Errors::EnvironmentNonExistentCWD # This means that this environment working directory # no longer exists, so delete this entry. diff --git a/lib/vagrant/shared_helpers.rb b/lib/vagrant/shared_helpers.rb index 1f2f0bc94..7b8c51339 100644 --- a/lib/vagrant/shared_helpers.rb +++ b/lib/vagrant/shared_helpers.rb @@ -244,6 +244,7 @@ module Vagrant ->{ Vagrant::Machine.prepend(Vagrant::Machine::Remote) }, ->{ Vagrant::Environment.prepend(Vagrant::Environment::Remote) }, ->{ Vagrant::MachineIndex.prepend(Vagrant::MachineIndex::Remote) }, - ->{ Vagrant::MachineIndex::Entry.extend(Vagrant::MachineIndex::Entry::Remote) }, + ->{ Vagrant::MachineIndex::Entry.prepend(Vagrant::MachineIndex::Entry::Remote::InstanceMethods) }, + ->{ Vagrant::MachineIndex::Entry.extend(Vagrant::MachineIndex::Entry::Remote::ClassMethods) }, ].freeze end