Store machine client within index entry for use in generating environment

This commit is contained in:
Chris Roberts 2021-08-17 14:03:40 -07:00 committed by Paul Hinze
parent 99aef42342
commit df53588de5
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 61 additions and 15 deletions

View File

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

View File

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

View File

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