Class: VMC::Micro::VMrun

Inherits:
Object show all
Defined in:
lib/vmc/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.



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

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.



3
4
5
# File 'lib/vmc/micro/vmrun.rb', line 3

def vmrun
  @vmrun
end

#vmxObject (readonly)

Returns the value of attribute vmx.



3
4
5
# File 'lib/vmc/micro/vmrun.rb', line 3

def vmx
  @vmx
end

Class Method Details

.locate(platform) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/vmc/micro/vmrun.rb', line 148

def self.locate(platform)
  paths = YAML.load_file(VMC::Micro.config_file('paths.yml'))
  vmrun_paths = paths[platform.to_s]['vmrun']
  vmrun_exe = @platform == :windows ? 'vmrun.exe' : 'vmrun'
  vmrun = VMC::Micro.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)


32
33
34
# File 'lib/vmc/micro/vmrun.rb', line 32

def bridged?
  connection_type == "bridged"
end

#connection_typeObject



20
21
22
# File 'lib/vmc/micro/vmrun.rb', line 20

def connection_type
  read_variable('ethernet0.connectionType')
end

#connection_type=(type) ⇒ Object



24
25
26
# File 'lib/vmc/micro/vmrun.rb', line 24

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

#domainObject



36
37
38
39
40
41
42
# File 'lib/vmc/micro/vmrun.rb', line 36

def domain
  # switch to Dir.mktmpdir
  state_config = VMC::Micro.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



44
45
46
47
48
49
50
51
52
# File 'lib/vmc/micro/vmrun.rb', line 44

def ip
  # switch to Dir.mktmpdir
  path = VMC::Micro.escape_path(VMC::Micro.config_file('refresh_ip.rb'))
  ip_file = VMC::Micro.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



54
55
56
57
58
# File 'lib/vmc/micro/vmrun.rb', line 54

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

#nat?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/vmc/micro/vmrun.rb', line 28

def nat?
  connection_type == "nat"
end

#offline!Object



75
76
77
78
79
80
# File 'lib/vmc/micro/vmrun.rb', line 75

def offline!
  path = VMC::Micro.escape_path(VMC::Micro.config_file('offline.conf'))
  run('CopyFileFromHostToGuest', "#{path} /etc/dnsmasq.d/offline.conf")
  run('runProgramInGuest', '/usr/bin/touch /var/vcap/micro/offline')
  restart_dnsmasq
end

#offline?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vmc/micro/vmrun.rb', line 60

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 "failed to execute vmrun:\n#{result}"
  end
end

#online!Object



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

def online!
  run('runProgramInGuest', '/bin/rm -f /etc/dnsmasq.d/offline.conf')
  run('runProgramInGuest', '/bin/rm -f /var/vcap/micro/offline')
  restart_dnsmasq
end

#read_variable(var) ⇒ Object



104
105
106
107
# File 'lib/vmc/micro/vmrun.rb', line 104

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)


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vmc/micro/vmrun.rb', line 90

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 "failed to execute vmrun:\n#{result}"
  end
end

#resetObject



113
114
115
# File 'lib/vmc/micro/vmrun.rb', line 113

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

#restart_dnsmasqObject



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

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



123
124
125
126
127
128
# File 'lib/vmc/micro/vmrun.rb', line 123

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

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  vms = list
  if @platform == :windows
    vms.map! { |x| x.downcase }
    vms.include?(@vmx.downcase)
  else
    vms.include?(@vmx)
  end
end

#startObject



140
141
142
# File 'lib/vmc/micro/vmrun.rb', line 140

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

#stopObject



144
145
146
# File 'lib/vmc/micro/vmrun.rb', line 144

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

#write_variable(var, value) ⇒ Object



109
110
111
# File 'lib/vmc/micro/vmrun.rb', line 109

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