Class: Tenderloin::FusionVM

Inherits:
Object
  • Object
show all
Defined in:
lib/tenderloin/fusion_vm.rb

Constant Summary collapse

VMRUN =
"/Applications/VMware\\ Fusion.app/Contents/Library/vmrun"

Instance Method Summary collapse

Constructor Details

#initialize(vmx) ⇒ FusionVM

Returns a new instance of FusionVM.



5
6
7
# File 'lib/tenderloin/fusion_vm.rb', line 5

def initialize(vmx)
  @vmx = vmx
end

Instance Method Details

#deleteObject



44
45
46
# File 'lib/tenderloin/fusion_vm.rb', line 44

def delete()
  run 'deleteVM'
end

#dhcp_leasesObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tenderloin/fusion_vm.rb', line 74

def dhcp_leases
  mac_ip = {}
  curLeaseIp = nil
  Dir['/var/db/vmware/vmnet-dhcpd*.leases'].each do |f|
    File.open(f).each do |line|
      case line
      when /lease (.*) \{/
        curLeaseIp = $1
      when /hardware ethernet (.*);/
        mac_ip[$1] = curLeaseIp
      end
    end
  end
  mac_ip
end

#enable_shared_foldersObject



61
62
63
# File 'lib/tenderloin/fusion_vm.rb', line 61

def enable_shared_folders
  run 'enableSharedFolders'
end

#get_guest_var(var) ⇒ Object



48
49
50
# File 'lib/tenderloin/fusion_vm.rb', line 48

def get_guest_var(var)
  run 'readVariable', 'guestVar ' + var
end

#ipObject



52
53
54
55
56
57
58
59
# File 'lib/tenderloin/fusion_vm.rb', line 52

def ip
  ip = get_guest_var('ip').strip
  unless ip =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
    mac_address = VMXFile.load(@vmx)["ethernet0.generatedAddress"]
    ip = dhcp_leases[mac_address]
  end
  ip
end

#run(cmd, opts = '') ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tenderloin/fusion_vm.rb', line 9

def run(cmd, opts='')
  retrycount = 0
  while true
    res = `#{VMRUN} #{cmd} #{@vmx} #{opts}`
    if $? == 0
      return res
    else
      if res =~ /VMware Tools are not running/
        sleep 1; next unless retrycount > 10
      end
      raise "Error running vmrun command #{cmd}: " + res
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/tenderloin/fusion_vm.rb', line 29

def running?()
  `#{VMRUN} list | grep "#{@vmx}"`
  $? == 0 ? true : false
end

#share_folder(name, hostpath) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/tenderloin/fusion_vm.rb', line 65

def share_folder(name, hostpath)
  # Try and clean up first, to handle path changes.
  begin
    run 'removeSharedFolder', "#{name}"
  rescue
  end
  run 'addSharedFolder', "#{name} #{hostpath}"
end

#start(opts = {}) ⇒ Object



34
35
36
37
# File 'lib/tenderloin/fusion_vm.rb', line 34

def start(opts = {})
  gui_opt = opts[:headless] == true ? "nogui" : "gui"
  run('start', gui_opt)
end

#start_fusionObject



24
25
26
27
# File 'lib/tenderloin/fusion_vm.rb', line 24

def start_fusion
  # Ensure fusion is running.
  `if [[ -z $(pgrep 'VMware Fusion') ]]; then open /Applications/VMware\\ Fusion.app ; sleep 5 ; fi`
end

#stop(opts = {}) ⇒ Object



39
40
41
42
# File 'lib/tenderloin/fusion_vm.rb', line 39

def stop(opts = {})
  hard_opt = opts[:force] == true ? "hard" : "soft"
  run 'stop', hard_opt
end