Class: Fusion

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

Constant Summary collapse

DEFAULT_VMRUN =
"/Applications/VMware\\ Fusion.app/Contents/Library/vmrun"
DEFAULT_VMPATH =
"#{ENV['HOME']}/Documents/Virtual\\ Machines*/*.vmwarevm"

Instance Method Summary collapse

Constructor Details

#initialize(vmpath = DEFAULT_VMPATH, nogui = 'nogui', force = 'hard') ⇒ Fusion

Returns a new instance of Fusion.



9
10
11
12
13
14
# File 'lib/fusion.rb', line 9

def initialize(vmpath=DEFAULT_VMPATH, nogui='nogui', force='hard')
  @vmpath = vmpath
  @vmrun  = DEFAULT_VMRUN
  @nogui  = nogui
  @force  = force
end

Instance Method Details

#generate_startup_scriptObject



91
92
93
94
# File 'lib/fusion.rb', line 91

def generate_startup_script
  # TODO: Generate a startup script that calls pauper vm start/suspend at the
  # right times.
end

#ipObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fusion.rb', line 113

def ip
  # switch mac to the format that arp likes
  arpmac = mac.gsub(/0([0-9a-fA-F])/, '\1')

  match = `arp -a | grep -i #{arpmac}`.match(/\(([0-9.]+)\)/)
  unless match
    puts "Could not find any running VM with MAC addr: #{arpmac}"
    puts " >> Manually SSH into the VM then try again <<"
    exit
  end

  return match.captures[0]
end


82
83
84
85
86
87
88
89
# File 'lib/fusion.rb', line 82

def link_vmrun(to='/user/local/bin/vmrun')
  cmd = "ln -s #{@vmrun} #{to}"
  if  File.exists? to
    puts "Something already exists at #{to}"
  else
    system(cmd)
  end
end

#listObject



40
41
42
# File 'lib/fusion.rb', line 40

def list
  vmrun('list')
end

#macObject



127
128
129
130
# File 'lib/fusion.rb', line 127

def mac
  vmx = VMX.new(self.vmx)
  return vmx.data['ethernet0.generatedAddress']
end

#pauseObject



32
33
34
# File 'lib/fusion.rb', line 32

def pause
  vmrun('pause')
end

#resetObject



24
25
26
# File 'lib/fusion.rb', line 24

def reset
  vmrun('reset')
end

#set_vmrun_to(vmrun) ⇒ Object



78
79
80
# File 'lib/fusion.rb', line 78

def set_vmrun_to(vmrun)
  @vmrun = vmrun
end

#setupObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fusion.rb', line 65

def setup
  # this really just adds a few things to the vmx file for better headless
  # operations.
  v = VMX.new vmx
  v.data['msg.autoAnswer'] = "TRUE" # Attempt to answer gui boxes you no longer get
  puts "  msg.autoAnswer=TRUE : auto answer gui boxes for you"
  v.data['usb.generic.pluginAction'] = "host" # Always plug usb devices into host
  puts "  usb.generic.pluginAction='host' : never ask to plug usb things into vm"
  v.data['mks.enable3d'] = "FALSE" # Turn of accelerated 3d... we dont need it
  puts "  mks.enable3d=FALSE : there is no need for 3d when headless..."
  v.save vmx
end

#startObject



16
17
18
# File 'lib/fusion.rb', line 16

def start
  vmrun('start')
end

#stopObject



20
21
22
# File 'lib/fusion.rb', line 20

def stop
  vmrun('stop')
end

#suspendObject



28
29
30
# File 'lib/fusion.rb', line 28

def suspend
  vmrun('suspend')
end

#unpauseObject



36
37
38
# File 'lib/fusion.rb', line 36

def unpause
  vmrun('unpause')
end

#vmrun(cmd) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fusion.rb', line 44

def vmrun(cmd)
  case cmd
  when 'start'
    cmd = "#{@vmrun} -T fusion #{cmd} #{vmx.shellescape} #{@nogui}"
  when 'stop', 'reset', 'suspend'
    cmd = "#{@vmrun} -T fusion #{cmd} #{vmx.shellescape} #{@force}"
  when 'pause', 'unpause'
    cmd = "#{@vmrun} -T fusion #{cmd} #{vmx.shellescape}"
  else
    cmd = "#{@vmrun} #{cmd}"
  end
  Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
    while line = stdout.gets
      puts "> #{line}"
    end
    while line = stderr.gets
      puts "err> #{line}"
    end
  end
end

#vmxObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fusion.rb', line 96

def vmx
  vms = Dir[@vmpath]
  if vms.size == 0
    puts "I can't find your VM. Try passing in a path to the <name>.vmwarevm dir."
    exit
  elsif vms.size > 1
    puts "I'm not sure which VM I should use! Try adding one of these to your Pauperfile:"
    vms.each do |vm|
      puts "\tvm_path \"#{vm}\""
    end
    exit
  end

  # Return .vmx file
  Dir[vms[0] + '/*.vmx'][0]
end