Class: Lab::Controllers::VmController
- Inherits:
-
Object
- Object
- Lab::Controllers::VmController
- Includes:
- Enumerable, DynagenController, FogController, RemoteEsxiController, RemoteWorkstationController, VirtualBoxController, VsphereController, WorkstationController
- Defined in:
- lib/lab/vm_controller.rb
Constant Summary
Constants included from RemoteEsxiController
Instance Method Summary collapse
- #[](x) ⇒ Object
- #add_vm(vmid, location = nil, os = nil, tools = nil, credentials = nil, user = nil, host = nil) ⇒ Object
-
#build_from_config(driver_type = nil, user = nil, host = nil, clear = false) ⇒ Object
Applicable only to virtualbox.
-
#build_from_dir(driver_type, dir, clear = false) ⇒ Object
Build a vm lab from a directory of files.
-
#build_from_running(driver_type = nil, user = nil, host = nil, clear = false, pass = nil) ⇒ Object
Builds a vm lab from all running vms.
- #clear! ⇒ Object
- #each(&block) ⇒ Object
- #find_by_hostname(search) ⇒ Object
- #find_by_tag(search) ⇒ Object
- #find_by_vmid(search) ⇒ Object
- #from_file(file) ⇒ Object
- #includes?(specified_vm) ⇒ Boolean
- #includes_hostname?(hostname) ⇒ Boolean
- #includes_vmid?(vmid) ⇒ Boolean
-
#initialize(labdef = nil) ⇒ VmController
constructor
include Lab::Controllers::QemuController include Lab::Controllers::QemudoController.
- #load_vms(vms) ⇒ Object
- #remove_by_vmid(vmid) ⇒ Object
- #running?(vmid) ⇒ Boolean
- #to_file(file) ⇒ Object
Methods included from VsphereController
Methods included from RemoteEsxiController
Methods included from DynagenController
Methods included from VirtualBoxController
config_list, config_list_uuid, dir_list, running_list
Methods included from RemoteWorkstationController
Methods included from WorkstationController
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
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/lab/vm_controller.rb', line 83 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.
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/lab/vm_controller.rb', line 253 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)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/lab/vm_controller.rb', line 139 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
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 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/lab/vm_controller.rb', line 173 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
113 114 115 |
# File 'lib/lab/vm_controller.rb', line 113 def each &block @vms.each { |vm| yield vm } end |
#find_by_hostname(search) ⇒ Object
70 71 72 |
# File 'lib/lab/vm_controller.rb', line 70 def find_by_hostname(search) self.find_by_vmid search end |
#find_by_tag(search) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/lab/vm_controller.rb', line 74 def find_by_tag(search) @vms.each do |vm| vm..each do |tag| return vm if tag == search 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 == search end nil end |
#from_file(file) ⇒ Object
98 99 100 |
# File 'lib/lab/vm_controller.rb', line 98 def from_file(file) load_vms(YAML::load_file(file)) end |
#includes?(specified_vm) ⇒ Boolean
117 118 119 |
# File 'lib/lab/vm_controller.rb', line 117 def includes?(specified_vm) @vms.each { |vm| if (vm == specified_vm) then return true end } end |
#includes_hostname?(hostname) ⇒ Boolean
128 129 130 131 132 133 |
# File 'lib/lab/vm_controller.rb', line 128 def includes_hostname?(hostname) @vms.each do |vm| return true if (vm.hostname == hostname) end false end |
#includes_vmid?(vmid) ⇒ Boolean
121 122 123 124 125 126 |
# File 'lib/lab/vm_controller.rb', line 121 def includes_vmid?(vmid) @vms.each do |vm| return true if (vm.vmid == vmid) end false end |
#load_vms(vms) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/lab/vm_controller.rb', line 102 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
94 95 96 |
# File 'lib/lab/vm_controller.rb', line 94 def remove_by_vmid(vmid) @vms.delete(self.find_by_vmid(vmid)) end |
#running?(vmid) ⇒ Boolean
278 279 280 281 282 283 |
# File 'lib/lab/vm_controller.rb', line 278 def running?(vmid) if includes_vmid?(vmid) return self.find_by_vmid(vmid).running? end return false end |
#to_file(file) ⇒ Object
109 110 111 |
# File 'lib/lab/vm_controller.rb', line 109 def to_file(file) File.open(file, 'w') { |f| @vms.each { |vm| f.puts vm.to_yaml } } end |