Class: VM

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

Instance Method Summary collapse

Constructor Details

#initialize(ref, session, name) ⇒ VM

Returns a new instance of VM.



4
5
6
7
8
9
# File 'lib/VM.rb', line 4

def initialize ref, session, name
  @ref = ref
  @session = session
  @name = name
  @cached_state = Hash[ :state => [], :hw => [] ]
end

Instance Method Details

#cached_stateObject



101
102
103
# File 'lib/VM.rb', line 101

def cached_state
  @cached_state
end

#color(cached = nil) ⇒ Object



16
17
18
19
20
# File 'lib/VM.rb', line 16

def color cached=nil
  @color = @vm_on = @vm_off = @is_templ = nil unless cached.nil?
  return @color unless @color.nil?
  @color = is_templ ? 'blue' : vm_on ? 'green' : vm_off ? 'red' : 'yellow'
end

#draw {|[@name, self]| ... } ⇒ Object

Yields:



13
14
15
# File 'lib/VM.rb', line 13

def draw
  yield [@name, self]
end

#execute_task(task, *args) ⇒ Object



104
105
106
107
# File 'lib/VM.rb', line 104

def execute_task task, *args
  @ref.send task.to_sym, *args
  false
end

#get_hostObject



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

def get_host
  host = @ref['runtime']['host']
  return '' if host.nil? or host.kind_of? $mock.class
  host = @session.managed_object_wrapper_factory "HostSystem", host
  @host_cpu = host['summary']['hardware']['cpuMhz']
  host['name']
end

#get_hw_infoObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/VM.rb', line 43

def get_hw_info
  @ref['config']['hardware']['device'].collect do |d|
    if d.soap_type == 'VirtualCdrom'
      result = [d['deviceInfo']['label'].to_s,d['backing']['fileName'].to_s]
    end
    if d.soap_type == 'VirtualDisk'
      i = d['deviceInfo']
      result = [i['label'].to_s,i['summary'].to_s]
    end
    if ['VirtualE1000','VirtualPCNet32','VirtualVmxnet','VirtualEthernetCard'].include? d.soap_type
      i = d['deviceInfo']
      result = [i['label'].to_s,i['summary'].to_s]
    end
    result unless result.nil?
  end
end

#has_task?(task) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/VM.rb', line 33

def has_task? task
  @ref.respond_to? task
end

#is_templObject



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

def is_templ
  return @is_templ if !@is_templ.nil?
  @is_templ = @ref['summary']['config']['template']
end

#nameObject



10
11
12
# File 'lib/VM.rb', line 10

def name
  @name.strip
end

#stateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/VM.rb', line 59

def state
  snapshot = @ref['snapshot']
  snapshot = snapshot['currentSnapshot'].nil? ? 'no' : 'yes' unless snapshot.nil?
  runtime = @ref['runtime']
  summary = @ref['summary']
  config = @ref['config']
  num = summary['config']['numCpu'].to_s
  mem = summary['config']['memorySizeMB'].to_s
  qs = summary['quickStats']
  guest = @ref['guest']
  res = []
  res << ['power state',runtime['powerState'].to_s+' since '+runtime['bootTime'].to_s.sub(/T/,' ').sub(/\+.*/,'')]
  res << ['host',get_host]
  res << ['cpu usage',"#{qs['overallCpuUsage'].to_s} MHz / #{num} * #{@host_cpu.to_s} MHz"]
  res << ['guest memory usage',"#{qs['guestMemoryUsage'].to_s} MB / #{mem} MB"]
  res << ['host memory usage',qs['hostMemoryUsage'].to_s+' MB']
  res << ['memory overhead',(runtime['memoryOverhead'].to_i/1024/1024).to_s+' MB']
  res << ['console',runtime['numMksConnections'].to_s]
  res << ['snapshot',snapshot.to_s]
  unless guest.nil?
    res << ['host name',guest['hostName'].to_s]
    res << ['ip',guest['ipAddress'].to_s]
    res << ['tools version',guest['toolsVersion'].to_s]
    begin
    guest['disk'].each do |d|
      size = d['capacity']
      pc = d['freeSpace']*100/size
      size = size/1024/1024/1024
      res << [d['diskPath'], "#{size.round.to_s}GB #{pc.round.to_s}% free"]
    end unless guest['disk'].nil?
    rescue => ex
      res << ['exception',ex.to_s]
    end
  end
  res << ['store',config['files']['vmPathName'].to_s]
  first = true
  config['annotation'].to_s.split(/\n/).each do |a|
    res << [first ? 'notes' : '',a]
    first = false
  end
  @cached_state = Hash[ :state => res, :hw => get_hw_info.compact ]
end

#vm_offObject



25
26
27
28
# File 'lib/VM.rb', line 25

def vm_off
  return @vm_off if !@vm_off.nil?
  @vm_off = @ref['runtime']['powerState'].to_s == 'poweredOff'
end

#vm_onObject



21
22
23
24
# File 'lib/VM.rb', line 21

def vm_on
  return @vm_on if !@vm_on.nil?
  @vm_on = @ref['runtime']['powerState'].to_s == 'poweredOn'
end