Class: HashiCorp::VagrantVMwareDesktop::Action::BaseMacConfigWarning

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/vagrant-vmware-desktop/action/base_mac_config_warning.rb

Overview

This inspects the Vagrantfile provided by the box (if one was provided) and checks if it set the ‘base_mac` value. If it did, a warning will be generated to the user.

NOTE: This action is merely a “best effort” at providing the warning. It is using non-public Vagrant internals to inspect box Vagrantfile. As such, any errors encountered will be ignored with only a debug log.

Instance Method Summary collapse

Methods included from Common

#to_s

Constructor Details

#initialize(app, env) ⇒ BaseMacConfigWarning

Returns a new instance of BaseMacConfigWarning.



20
21
22
23
# File 'lib/vagrant-vmware-desktop/action/base_mac_config_warning.rb', line 20

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new("hashicorp::provider::vmware::basemacwarning")
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vagrant-vmware-desktop/action/base_mac_config_warning.rb', line 25

def call(env)
  catch(:complete) do
    begin
      # Attempt to extract the vagrantfile loader
      loader = env[:machine].vagrantfile.instance_variable_get(:@loader)
      if !loader
        @logger.debug("base_mac check in box vagrantfile failed - cannot access loader")
        throw :complete
      end

      # Attempt to get the Vagrantfile source for the box
      # provided Vagrantfile
      source = loader.instance_variable_get(:@sources)&.keys&.last
      if !source
        @logger.debug("base_mac check in box vagrantfile failed - cannot get box source")
        throw :complete
      end

      begin
        # Attempt to load the partial config
        partial = loader.partial_load(source)

        # Only proceed to display warning if the base_mac value
        # in the partial load matches the base_mac in the final
        # config
        throw :complete if partial.vm.base_mac != env[:machine].config.vm.base_mac

        # Display the warning message
        env[:ui].warn(
          I18n.t(
            "hashicorp.vagrant_vmware_desktop.box_base_mac_warning",
            base_mac: env[:machine].config.vm.base_mac
          )
        )
      rescue KeyError => err
        @logger.debug("base_mac check in box vagrantfile failed - partial load failure #{err.class}: #{err}")
      end
    rescue => err
      @logger.debug("base_mac check in box vagrantfile failed - #{err.class}: #{err}")
    end
  end

  @app.call(env)
end