Class: Lab::Controllers::VmController

Inherits:
Object
  • Object
show all
Includes:
Enumerable, DynagenController, FogController, RemoteEsxiController, RemoteWorkstationController, VirtualBoxController, VsphereDriver, WorkstationController
Defined in:
lib/lab/vm_controller.rb

Constant Summary

Constants included from RemoteEsxiController

RemoteEsxiController::VIM_CMD

Instance Method Summary collapse

Methods included from RemoteEsxiController

dir_list, running_list

Methods included from DynagenController

dir_list, running_list

Methods included from VirtualBoxController

config_list, config_list_uuid, dir_list, running_list

Methods included from RemoteWorkstationController

dir_list, running_list

Methods included from WorkstationController

dir_list, running_list

Constructor Details

#initialize(labdef = nil) ⇒ VmController

include Lab::Controllers::QemuController include Lab::Controllers::QemudoController



41
42
43
44
45
46
47
48
# File 'lib/lab/vm_controller.rb', line 41

def initialize (labdef=nil)

  # Start with an empty array of vm objects
  @vms = [] 

  # labdef is a just a big array of hashes
  load_vms(labdef) if labdef
end

Instance Method Details

#[](x) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/lab/vm_controller.rb', line 54

def [](x)
  # Support indexing by both names and number
  if x.class == String
    find_by_vmid(x)
  else
    return @vms[x]
  end
end

#add_vm(vmid, location = nil, os = nil, tools = nil, credentials = nil, user = nil, host = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/lab/vm_controller.rb', line 79

def add_vm(vmid, location=nil, os=nil, tools=nil, credentials=nil, user=nil, host=nil)
  @vms << Vm.new( {
    'vmid' => vmid, 
    'driver' => type, 
    'location' => location, 
    'credentials' => credentials,
    'user' => user,
    'host' => host
    })
end

#build_from_config(driver_type = nil, user = nil, host = nil, clear = false) ⇒ Object

Applicable only to virtualbox. Reads the config file & parses / creates VM objects for each vm.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/lab/vm_controller.rb', line 242

def build_from_config(driver_type=nil, user=nil, host=nil, clear=false)
  if clear
    @vms = []
  end

  case driver_type.intern
    when :virtualbox
      vm_list = ::Lab::Controllers::VirtualBoxController::config_list
      
      vm_list.each do |item|
        # Add it to the vm list
        @vms << Vm.new( {  
          'vmid' => "#{item}",
          'driver' => driver_type, 
          'location' => nil, 
          'user' => user,
          'host' => host } )
      end
        
    else
      raise TypeError, "Unsupported VM Type"
    end

end

#build_from_dir(driver_type, dir, clear = false) ⇒ Object

Build a vm lab from a directory of files. Really only useful for file-based vm hosts. (vmware workstation)



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/lab/vm_controller.rb', line 128

def build_from_dir(driver_type, dir, clear=false)

  if clear
    @vms = []
  end

  if driver_type.downcase == "workstation"
    vm_list = ::Lab::Controllers::WorkstationController::dir_list(dir)
  elsif driver_type.downcase == "remote_workstation"  
    vm_list = ::Lab::Controllers::RemoteWorkstationController::dir_list(dir)
  elsif driver_type.downcase == "virtualbox"  
    vm_list = ::Lab::Controllers::VirtualBoxController::dir_list(dir)
  elsif driver_type.downcase == "fog"
    vm_list = ::Lab::Controllers::FogController::dir_list(dir)
  elsif driver_type.downcase == "Dynagen"  
    vm_list = ::Lab::Controllers::DynagenController::dir_list(dir)
  elsif driver_type.downcase == "remote_esxi"
    vm_list =::Lab::Controllers::RemoteEsxiController::dir_list(dir)
  elsif driver_type.downcase == "vsphere"
    vm_list =::Lab::Controllers::VsphereController::dir_list(dir)
  else
    raise TypeError, "Unsupported VM Type"
  end
  
  vm_list.each_index do |index|
    @vms << Vm.new( {'vmid' => "vm_#{index}", 'driver' => driver_type, 'location' => vm_list[index]} )
  end
end

#build_from_running(driver_type = nil, user = nil, host = nil, clear = false, pass = nil) ⇒ Object

Builds a vm lab from all running vms. Handy for connecting and saving out a config or just managing the currently running vms



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/lab/vm_controller.rb', line 162

def build_from_running(driver_type=nil, user=nil, host=nil, clear=false, pass=nil)

  if clear
    @vms = []
  end

  case driver_type.intern
    when :workstation
      vm_list = ::Lab::Controllers::WorkstationController::running_list
      vm_list.each do |item|
        # Name the VM
        index = @vms.count + 1
        # Add it to the vm list
        @vms << Vm.new({
          'vmid' => "vm_#{index}",
          'driver' => driver_type, 
          'location' => item
          })
      end
    when :remote_workstation
      vm_list = ::Lab::Controllers::RemoteWorkstationController::running_list(user, host)
      vm_list.each do |item|
        # Name the VM
        index = @vms.count + 1
        # Add it to the VM list
        @vms << Vm.new({
          'vmid' => "vm_#{index}",
          'driver' => driver_type, 
          'location' => item, 
          'user' => user,
          'host' => host 
          })
      end
    when :virtualbox
      vm_list = ::Lab::Controllers::VirtualBoxController::running_list
      vm_list.each do |item|
        # Add it to the vm list
        @vms << Vm.new( {  
          'vmid' => "#{item}",
          'driver' => driver_type,
          'location' => nil 
          })
      end
    when :fog
      raise "Unsupported"
    when :dynagen
      raise "Unsupported"
    when :remote_esxi
      vm_list = ::Lab::Controllers::RemoteEsxiController::running_list(user,host)
      vm_list.each do |item|
        @vms << Vm.new( {  
          'vmid' => "#{item[:id]}",
          'name' => "#{item[:name]}",
          'driver' => driver_type, 
          'user' => user,
          'host' => host 
          })
      end
    when :vsphere
      vm_list = ::Lab::Controllers::VsphereController::running_list(user,host,pass)
      vm_list.each do |item|
        @vms << Vm.new( {  
          'vmid' => "#{item[:id]}",
          'name' => "#{item[:name]}",
          'driver' => driver_type, 
          'user' => user,
          'host' => host,
          'pass' => pass
          })
      end
    else
      raise TypeError, "Unsupported VM Type"
    end

end

#clear!Object



50
51
52
# File 'lib/lab/vm_controller.rb', line 50

def clear!
  @vms = []
end

#each(&block) ⇒ Object



109
110
111
# File 'lib/lab/vm_controller.rb', line 109

def each &block
  @vms.each { |vm| yield vm }
end

#find_by_tag(search) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/lab/vm_controller.rb', line 70

def find_by_tag(search)
  @vms.each do |vm|
    vm.tags.each do |tag| 
      return vm if tag.downcase == search.to_s.downcase
    end
  end
  return nil
end

#find_by_vmid(search) ⇒ Object



63
64
65
66
67
68
# File 'lib/lab/vm_controller.rb', line 63

def find_by_vmid(search)
  @vms.each do |vm|
    return vm if vm.hostname.to_s.downcase == search.to_s.downcase
  end
  return nil
end

#from_file(file) ⇒ Object



94
95
96
# File 'lib/lab/vm_controller.rb', line 94

def from_file(file)
  load_vms(YAML::load_file(file))
end

#includes?(specified_vm) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/lab/vm_controller.rb', line 113

def includes?(specified_vm)
  @vms.each { |vm| if (vm == specified_vm) then return true end  }
end

#includes_vmid?(vmid) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
# File 'lib/lab/vm_controller.rb', line 117

def includes_vmid?(vmid)
  @vms.each do |vm|
    return true if (vm.vmid == vmid)
  end
false
end

#load_vms(vms) ⇒ Object



98
99
100
101
102
103
# File 'lib/lab/vm_controller.rb', line 98

def load_vms(vms)
  vms.each do |item|
    vm = Vm.new(item)
    @vms << vm unless includes_vmid? vm.vmid
  end
end

#remove_by_vmid(vmid) ⇒ Object



90
91
92
# File 'lib/lab/vm_controller.rb', line 90

def remove_by_vmid(vmid)
  @vms.delete(self.find_by_vmid(vmid))
end

#running?(vmid) ⇒ Boolean

Returns:

  • (Boolean)


267
268
269
270
271
272
# File 'lib/lab/vm_controller.rb', line 267

def running?(vmid)
  if includes_vmid?(vmid)
    return self.find_by_vmid(vmid).running?
  end
  return false 
end

#to_file(file) ⇒ Object



105
106
107
# File 'lib/lab/vm_controller.rb', line 105

def to_file(file)
  File.open(file, 'w') { |f| @vms.each { |vm| f.puts vm.to_yaml } } 
end