Class: VagrantVbguest::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vbguest/machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vm, options) ⇒ Machine

Returns a new instance of Machine.



8
9
10
11
12
13
14
15
16
17
# File 'lib/vagrant-vbguest/machine.rb', line 8

def initialize vm, options
  @vm       = vm
  @env      = vm.env
  @options  = options

  @logger = Log4r::Logger.new("vagrant::plugins::vbguest-machine")
  @logger.debug("initialize vbguest machine for VM '#{vm.name}' (#{vm.to_s})")

  @installer = Installer.new(vm, options)
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/vagrant-vbguest/machine.rb', line 6

def env
  @env
end

#installerObject (readonly)

Returns the value of attribute installer.



6
7
8
# File 'lib/vagrant-vbguest/machine.rb', line 6

def installer
  @installer
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/vagrant-vbguest/machine.rb', line 6

def options
  @options
end

#vmObject (readonly)

Returns the value of attribute vm.



6
7
8
# File 'lib/vagrant-vbguest/machine.rb', line 6

def vm
  @vm
end

Instance Method Details

#infoObject



90
91
92
93
94
95
96
# File 'lib/vagrant-vbguest/machine.rb', line 90

def info
  {
    :vm_name => vm.name,
    :host_version => installer.host_version,
    :guest_version => installer.guest_version
  }
end

#installObject



37
38
39
40
# File 'lib/vagrant-vbguest/machine.rb', line 37

def install
  return env.ui.warn(I18n.t("vagrant_vbguest.skipped_installation")) if options[:no_install] && !options[:force]
  guest_additions_state.trigger :install
end

#installation_ran?Boolean

Returns:

  • (Boolean)


51
# File 'lib/vagrant-vbguest/machine.rb', line 51

def installation_ran?; guest_additions_state.state == :installed end

#rebootObject



55
# File 'lib/vagrant-vbguest/machine.rb', line 55

def reboot;  box_state.trigger :reboot end

#reboot?Boolean

Returns:

  • (Boolean)


56
# File 'lib/vagrant-vbguest/machine.rb', line 56

def reboot?; box_state.state == :rebooted end

#rebuildObject



42
43
44
45
# File 'lib/vagrant-vbguest/machine.rb', line 42

def rebuild
  return env.ui.warn(I18n.t("vagrant_vbguest.skipped_rebuild")) if options[:no_install] && !options[:force]
  guest_additions_state.trigger :rebuild
end

#rebuilt?Boolean

Returns:

  • (Boolean)


53
# File 'lib/vagrant-vbguest/machine.rb', line 53

def rebuilt?; guest_additions_state.state == :rebuilt end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vagrant-vbguest/machine.rb', line 19

def run
  current_state = state
  runlist = steps(current_state)
  @logger.debug("Runlist for state #{current_state} is: #{runlist}")
  while (command = runlist.shift)
    @logger.debug("Running command #{command} from runlist")
    case self.send(command)
    when nil # skipped
      return false
    when false # machine state change error
      env.ui.error(I18n.t('vagrant_vbguest.machine_loop_guard', :command => command, :state => guest_additions_state.state))
      return false
    end
    return run if current_state != state
  end
  true
end

#startObject



47
48
49
# File 'lib/vagrant-vbguest/machine.rb', line 47

def start
  guest_additions_state.trigger :start
end

#started?Boolean

Returns:

  • (Boolean)


52
# File 'lib/vagrant-vbguest/machine.rb', line 52

def started?; guest_additions_state.state == :started end

#stateObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant-vbguest/machine.rb', line 69

def state
  guest_version = installer.guest_version(true)
  host_version  = installer.host_version
  running = installer.running?
  @logger.debug("Current states for VM '#{vm.name}' are : guest_version=#{guest_version} : host_version=#{host_version} : running=#{running}")

  return :clean if !guest_version

  # some sort of distro installation bot no `vboxadd` tools to trigger rebuilds or manual starts
  return :dirty if installer.provides_vboxadd_tools? && !installer.vboxadd_tools_available?

  if host_version != guest_version
    return :unmatched if host_version > guest_version || options[:allow_downgrade] || options[:force]
    return :not_running if !running # still need to check this here, so we don't miss it before running into "skipped_downgrade"
    return :skipped_downgrade if host_version < guest_version
  end

  return :not_running if !running
  return :ok
end

#steps(state) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/vagrant-vbguest/machine.rb', line 58

def steps(state)
  case state
  when :clean, :dirty, :unmatched
    [:install].tap { |l| l << :reboot if installer.reboot_after_install? }
  when :not_running
    installation_ran? ? [:reboot] : [:start, :rebuild, :reboot]
  else
    []
  end
end