Class: CFMicro::VMrun

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ VMrun

Returns a new instance of VMrun.


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/micro/vmrun.rb', line 7

def initialize(config)
  @platform = config[:platform]
  @user = 'root' # must use root as we muck around with system settings
  @password = config[:password]
  @vmrun = config[:vmrun]
  @vmx = config[:vmx]

  # TODO honor TMPDIR
  if @platform == :windows
    @temp_dir = ENV['temp']
  else
    @temp_dir = '/tmp'
  end
end

Instance Attribute Details

#vmrunObject (readonly)

Returns the value of attribute vmrun


5
6
7
# File 'lib/micro/vmrun.rb', line 5

def vmrun
  @vmrun
end

#vmxObject (readonly)

Returns the value of attribute vmx


5
6
7
# File 'lib/micro/vmrun.rb', line 5

def vmx
  @vmx
end

Class Method Details

.locate(platform) ⇒ Object


165
166
167
168
169
170
171
172
# File 'lib/micro/vmrun.rb', line 165

def self.locate(platform)
  paths = YAML.load_file(CFMicro.config_file('paths.yml'))
  vmrun_paths = paths[platform.to_s]['vmrun']
  vmrun_exe = @platform == :windows ? 'vmrun.exe' : 'vmrun'
  vmrun = CFMicro.locate_file(vmrun_exe, "VMware", vmrun_paths)
  err "Unable to locate vmrun, please supply --vmrun option" unless vmrun
  vmrun
end

Instance Method Details

#bridged?Boolean

Returns:

  • (Boolean)

38
39
40
# File 'lib/micro/vmrun.rb', line 38

def bridged?
  connection_type == "bridged"
end

#connection_typeObject


26
27
28
# File 'lib/micro/vmrun.rb', line 26

def connection_type
  read_variable('ethernet0.connectionType')
end

#connection_type=(type) ⇒ Object


30
31
32
# File 'lib/micro/vmrun.rb', line 30

def connection_type=(type)
  write_variable("ethernet0.connectionType", type)
end

#domainObject


42
43
44
45
46
47
48
# File 'lib/micro/vmrun.rb', line 42

def domain  # switch to Dir.mktmpdir

  state_config = CFMicro.escape_path(File.join(@temp_dir, 'state.yml'))
  run('CopyFileFromGuestToHost', "/var/vcap/bosh/state.yml #{state_config}")
  bosh_config = YAML.load_file(state_config)
  bosh_config['properties']['domain']
end

#ipObject


50
51
52
53
54
55
56
57
58
# File 'lib/micro/vmrun.rb', line 50

def ip  # switch to Dir.mktmpdir

  path = CFMicro.escape_path(CFMicro.config_file('refresh_ip.rb'))
  ip_file = CFMicro.escape_path(File.join(@temp_dir, 'ip.txt'))
  run('CopyFileFromHostToGuest', "#{path} /tmp/refresh_ip.rb")
  run('runProgramInGuest', '/tmp/refresh_ip.rb')
  run('CopyFileFromGuestToHost', "/tmp/ip.txt #{ip_file}")
  File.open(ip_file, 'r') { |file| file.read }
end

#listObject


60
61
62
63
64
# File 'lib/micro/vmrun.rb', line 60

def list
  vms = run("list")
  vms.delete_if { |line| line =~ /^Total/ }
  vms.map { |line| CFMicro.escape_path(File.expand_path(line)) }
end

#nat?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/micro/vmrun.rb', line 34

def nat?
  connection_type == "nat"
end

#offline!Object


81
82
83
84
85
86
87
88
# File 'lib/micro/vmrun.rb', line 81

def offline!
  path = CFMicro.escape_path(CFMicro.config_file('offline.conf'))
  run('CopyFileFromHostToGuest', "#{path} /etc/dnsmasq.d/offline.conf")
  run('runProgramInGuest', '/usr/bin/touch /var/vcap/micro/offline')
  run('runProgramInGuest',
    "/bin/sed -i -e 's/^[^#]/# &/g' /etc/dnsmasq.d/server || true")
  restart_dnsmasq
end

#offline?Boolean

Returns:

  • (Boolean)

66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/micro/vmrun.rb', line 66

def offline?
  command = "-gu #{@user} -gp #{@password} runProgramInGuest"
  args =  '/usr/bin/test -e /var/vcap/micro/offline'  # why not use run_command?

  result = %x{#{@vmrun} #{command} #{@vmx} #{args}}

  if result.include?('Guest program exited with non-zero exit code: 1')
    return false
  elsif $?.exitstatus == 0
    return true
  else
    raise CFMicro::MCFError, "failed to execute vmrun:\n#{result}"
  end
end

#online!Object


90
91
92
93
94
95
96
# File 'lib/micro/vmrun.rb', line 90

def online!
  run('runProgramInGuest', '/bin/rm -f /etc/dnsmasq.d/offline.conf')
  run('runProgramInGuest', '/bin/rm -f /var/vcap/micro/offline')
  run('runProgramInGuest',
    "/bin/sed -i -e 's/^# //g' /etc/dnsmasq.d/server || true")
  restart_dnsmasq
end

#read_variable(var) ⇒ Object


114
115
116
117
# File 'lib/micro/vmrun.rb', line 114

def read_variable(var)
  # TODO deal with non-ok return
  run("readVariable", "runtimeConfig #{var}").first
end

#ready?Boolean

check to see if the micro cloud has been configured uses default password to check

Returns:

  • (Boolean)

100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/micro/vmrun.rb', line 100

def ready?
  command = "-gu root -gp 'ca$hc0w' runProgramInGuest"
  args =  '/usr/bin/test -e /var/vcap/micro/micro.json'
  result = %x{#{@vmrun} #{command} #{@vmx} #{args}}

  if result.include?('Invalid user name or password for the guest OS') || $?.exitstatus == 0
    return true
  elsif $?.exitstatus == 1
    return false
  else
    raise CFMicro::MCFError, "failed to execute vmrun:\n#{result}"
  end
end

#resetObject


123
124
125
# File 'lib/micro/vmrun.rb', line 123

def reset
  run('reset', 'soft')
end

#restart_dnsmasqObject


127
128
129
130
131
# File 'lib/micro/vmrun.rb', line 127

def restart_dnsmasq  # restart command doesn't always work, start and stop seems to be more reliable

  run('runProgramInGuest', '/etc/init.d/dnsmasq stop')
  run('runProgramInGuest', '/etc/init.d/dnsmasq start')
end

#run(command, args = nil) ⇒ Object


133
134
135
136
137
138
# File 'lib/micro/vmrun.rb', line 133

def run(command, args=nil)
  if command.include?('Guest')
    command = "-gu #{@user} -gp #{@password} #{command}"
  end
  CFMicro.run_command(@vmrun, "#{command} #{@vmx} #{args}")
end

#running?Boolean

Returns:

  • (Boolean)

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/micro/vmrun.rb', line 140

def running?
  vms = list
  if @platform == :windows
    vms.any? { |x|
      x.downcase == @vmx.downcase
    }
  else
    # Handle vmx being in a symlinked dir.
    real_path = nil
    begin
      real_path = File.realpath(@vmx)
    rescue
    end
    vms.include?(@vmx) || (real_path && vms.include?(real_path))
  end
end

#start!Object


157
158
159
# File 'lib/micro/vmrun.rb', line 157

def start!
  run('start') unless running?
end

#stop!Object


161
162
163
# File 'lib/micro/vmrun.rb', line 161

def stop!
  run('stop') if running?
end

#write_variable(var, value) ⇒ Object


119
120
121
# File 'lib/micro/vmrun.rb', line 119

def write_variable(var, value)
  run('writeVariable', "runtimeConfig #{var} #{value}")
end