Class: VBOX::VM

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ VM

Returns a new instance of VM.



5
6
7
8
9
10
# File 'lib/vbox/vm.rb', line 5

def initialize params = {}
  @metadata = params[:metadata]
  
  @name = params[:name] if params[:name]
  @uuid = params[:uuid] if params[:uuid]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/vbox/vm.rb', line 3

def name
  @name
end

#stateObject

Returns the value of attribute state.



3
4
5
# File 'lib/vbox/vm.rb', line 3

def state
  @state
end

#uuidObject

Returns the value of attribute uuid.



3
4
5
# File 'lib/vbox/vm.rb', line 3

def uuid
  @uuid
end

Class Method Details

.allObject



124
125
126
# File 'lib/vbox/vm.rb', line 124

def all
  VBOX.api.list_vms :include_state => true
end

.create!(*args) ⇒ Object



155
156
157
# File 'lib/vbox/vm.rb', line 155

def create! *args
  new(*args).create!
end

.expand_glob(glob, &block) ⇒ Object

expand globs like “d1-30” to d1,d2,d3,d4,…,d29,d30



145
146
147
148
149
150
151
152
153
# File 'lib/vbox/vm.rb', line 145

def expand_glob glob, &block
  if glob[/\{(\d+)-(\d+)\}/]
    $1.to_i.upto($2.to_i) do |i|
      expand_glob glob.sub($&,i.to_s), &block
    end
  else
    yield glob
  end
end

.find(name_or_uuid) ⇒ Object Also known as: []



132
133
134
135
# File 'lib/vbox/vm.rb', line 132

def find name_or_uuid
  r = VBOX.api.get_vm_details name_or_uuid
  r ? VM.new(:metadata => r) : nil
end

.find_all(glob) ⇒ Object



138
139
140
141
142
# File 'lib/vbox/vm.rb', line 138

def find_all glob
  all.keep_if do |vm|
    expand_glob(glob){ |glob1| File.fnmatch(glob1, vm.name) }
  end
end

.firstObject



128
129
130
# File 'lib/vbox/vm.rb', line 128

def first
  all.first
end

Instance Method Details

#clone!(params) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vbox/vm.rb', line 86

def clone! params
  raise "argument must be a Hash" unless params.is_a?(Hash)
  raise "no :snapshot key" unless params[:snapshot]
  r = VBOX.api.clone self.name, params
  case r
  when Array
    if r.first.is_a?(VM)
      r
    else
      r.map{ |name| VM.find(name) }
    end
  when String
    VM.find(r)
  when nil
    nil
  else
    r
  end
end

#create!Object



41
42
43
44
45
46
# File 'lib/vbox/vm.rb', line 41

def create!
  raise "cannot create VM w/o name" if self.name.to_s == ""
  VBOX.api.createvm(self) || raise("failed to create VM")
  
  self
end

#dir_sizeObject



106
107
108
109
110
111
112
113
# File 'lib/vbox/vm.rb', line 106

def dir_size
  @dir_size ||=
    begin
      return nil unless v=['CfgFile']
      dir = File.dirname(v)
      `du -sm "#{dir}"`.split("\t").first.tr("M","").to_i
    end
end

#memory_sizeObject



115
116
117
# File 'lib/vbox/vm.rb', line 115

def memory_size
  ['memory'].to_i
end

#metadataObject Also known as: fetch_metadata



27
28
29
30
31
32
# File 'lib/vbox/vm.rb', line 27

def 
  if !@metadata || @metadata.empty?
    
  end
  @metadata
end

#reload_metadataObject

reload all VM metadata info from VirtualBox



76
77
78
79
80
81
82
83
84
# File 'lib/vbox/vm.rb', line 76

def 
  raise "cannot reload metadata if name & uuid are NULL" unless name || uuid
  @metadata = VBOX.api.get_vm_details(self)

  # make a 'deep copy' of @metadata to detect changed vars
  # dup() or clone() does not fit here b/c they leave hash values linked to each other
  @metadata_orig = deep_copy(@metadata)
  
end

#saveObject

save modified metadata, if any



66
67
68
69
70
71
72
73
# File 'lib/vbox/vm.rb', line 66

def save
  return nil if @metadata == @metadata_orig
  vars = {}
  @metadata.each do |k,v|
    vars[k] = v if @metadata[k].to_s != @metadata_orig[k].to_s
  end
  VBOX.api.modify self, vars
end

#set_var(k, v = nil) ⇒ Object Also known as: set_vars

set some variable: change VM name, memory size, pae, etc



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vbox/vm.rb', line 49

def set_var k, v = nil
   unless @metadata
  @metadata_orig ||= deep_copy(@metadata)

  if k.is_a?(Hash) && v.nil?
    k.each do |kk,vv|
      @metadata[kk.to_s] = vv.to_s
    end
  elsif !k.is_a?(Hash) && !v.is_a?(Hash)
    @metadata[k.to_s] = v.to_s
  else
    raise "invalid params combination"
  end
end

#snapshotsObject



119
120
121
# File 'lib/vbox/vm.rb', line 119

def snapshots
  VBOX.api.get_snapshots(uuid||name)
end