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



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

def delete()
  run 'deleteVM'
end

#dhcp_leasesObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tenderloin/fusion_vm.rb', line 79

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



66
67
68
# File 'lib/tenderloin/fusion_vm.rb', line 66

def enable_shared_folders
  run 'enableSharedFolders'
end

#get_guest_var(var) ⇒ Object



51
52
53
# File 'lib/tenderloin/fusion_vm.rb', line 51

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

#ipObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/tenderloin/fusion_vm.rb', line 55

def ip
  ip = get_guest_var('ip')
  if ip && ip.strip =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
    ip = ip.strip
  else
    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
23
24
25
# File 'lib/tenderloin/fusion_vm.rb', line 9

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

#running?Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/tenderloin/fusion_vm.rb', line 32

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

#share_folder(name, hostpath) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/tenderloin/fusion_vm.rb', line 70

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



37
38
39
40
# File 'lib/tenderloin/fusion_vm.rb', line 37

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

#start_fusionObject



27
28
29
30
# File 'lib/tenderloin/fusion_vm.rb', line 27

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



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

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

#to_hashObject



95
96
97
# File 'lib/tenderloin/fusion_vm.rb', line 95

def to_hash
  {:ip => ip, :running => running?}
end