class VagrantVbguest::Machine

Attributes

env[R]
installer[R]
options[R]
vm[R]

Public Class Methods

new(vm, options) click to toggle source
# 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

Public Instance Methods

info() click to toggle source
# 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
install() click to toggle source
# 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?() click to toggle source
# File lib/vagrant-vbguest/machine.rb, line 51
def installation_ran?; guest_additions_state.state == :installed end
reboot() click to toggle source
# File lib/vagrant-vbguest/machine.rb, line 55
def reboot;  box_state.trigger :reboot end
reboot?() click to toggle source
# File lib/vagrant-vbguest/machine.rb, line 56
def reboot?; box_state.state == :rebooted end
rebuild() click to toggle source
# 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?() click to toggle source
# File lib/vagrant-vbguest/machine.rb, line 53
def rebuilt?; guest_additions_state.state == :rebuilt end
run() click to toggle source
# 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
start() click to toggle source
# File lib/vagrant-vbguest/machine.rb, line 47
def start
  guest_additions_state.trigger :start
end
started?() click to toggle source
# File lib/vagrant-vbguest/machine.rb, line 52
def started?; guest_additions_state.state == :started end
state() click to toggle source
# 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) click to toggle source
# 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

Protected Instance Methods

box_state() click to toggle source
# File lib/vagrant-vbguest/machine.rb, line 112
def box_state
  @box_state ||= MicroMachine.new(:first_boot).tap { |m|
    m.when :reboot, :first_boot => :rebooted
  }
end
guest_additions_state() click to toggle source
# File lib/vagrant-vbguest/machine.rb, line 100
def guest_additions_state
  @guest_additions_state ||= MicroMachine.new(:pending).tap { |m|
    m.when :install, :pending => :installed
    m.when :start,   :pending => :started
    m.when :rebuild, :pending => :rebuilt, :started => :rebuilt

    m.on(:installed) { installer.install }
    m.on(:started)   { installer.start }
    m.on(:rebuilt)   { installer.rebuild }
  }
end