Class: Inprovise::VBox::VBoxScript
- Inherits:
-
Script
- Object
- Script
- Inprovise::VBox::VBoxScript
- Defined in:
- lib/inprovise/vbox/vbox.rb
Instance Method Summary collapse
-
#configure(cfg = nil, &definition) ⇒ Object
overload Script#configure.
-
#initialize(name) ⇒ VBoxScript
constructor
configuration :name :virt_type :arch :image :format :diskbus :memory :cdrom :cdrombus :cpu_model :cpu_sockets :cpu_cores :cpu_threads :os :network :netname :nic :autostart :kernel :kernel_args :install_opts.
- #setup ⇒ Object
- #vbox_arch(context) ⇒ Object
- #vbox_autostart(context) ⇒ Object
- #vbox_cdrom(context) ⇒ Object
- #vbox_cdrombus(context) ⇒ Object
- #vbox_config_hash(context) ⇒ Object
- #vbox_cpu_cores(context) ⇒ Object
- #vbox_cpu_model(context) ⇒ Object
- #vbox_cpu_sockets(context) ⇒ Object
- #vbox_cpu_threads(context) ⇒ Object
- #vbox_diskbus(context) ⇒ Object
- #vbox_format(context) ⇒ Object
- #vbox_graphics(context) ⇒ Object
- #vbox_image(context) ⇒ Object
- #vbox_install_opts(context) ⇒ Object
- #vbox_kernel(context) ⇒ Object
- #vbox_kernel_args(context) ⇒ Object
- #vbox_memory(context) ⇒ Object
- #vbox_name(context) ⇒ Object
- #vbox_netname(context) ⇒ Object
- #vbox_network(context) ⇒ Object
- #vbox_nic(context) ⇒ Object
- #vbox_no_node(context) ⇒ Object
- #vbox_no_sniff(context) ⇒ Object
- #vbox_os(context) ⇒ Object
- #vbox_source(context) ⇒ Object
- #vbox_user(context) ⇒ Object
- #vbox_virt_type(context) ⇒ Object
Constructor Details
#initialize(name) ⇒ VBoxScript
configuration
:name
:virt_type
:arch
:image
:format
:diskbus
:memory
:cdrom
:cdrombus
:cpu_model
:cpu_sockets
:cpu_cores
:cpu_threads
:os
:network
:netname
:nic
:autostart
:kernel
:kernel_args
:install_opts
102 103 104 105 106 |
# File 'lib/inprovise/vbox/vbox.rb', line 102 def initialize(name) super(name) @vm_script = nil @node_script = nil end |
Instance Method Details
#configure(cfg = nil, &definition) ⇒ Object
overload Script#configure
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/inprovise/vbox/vbox.rb', line 299 def configure(cfg=nil, &definition) @configuration = Inprovise::Config.new.merge!(cfg) if cfg vbs = self config_block = block_given? ? definition : nil command(:configure).clear command(:configure) do self.instance_eval(&config_block) if config_block config['vbox'] = Inprovise::Infrastructure.find(config[vbs.name][:name]) end @configuration end |
#setup ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 156 157 158 159 160 161 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/inprovise/vbox/vbox.rb', line 108 def setup # take care of defaults @configuration ||= Inprovise::Config.new @configuration[:arch] ||= 'x86_64' @configuration[:memory] ||= 1024 @configuration[:cpu_model] ||= 'host-model' @configuration[:cpu_sockets] ||= 1 @configuration[:cpu_cores] ||= 2 @configuration[:cpu_threads] ||= 1 @configuration[:network] ||= :hostnet # generate name if none set @configuration[:name] ||= "#{name}_#{self.hash}_#{Time.now.to_f}" # create default configuration callback self.configure vbs = self # define the scripts that do the actual work as dependencies # 1. install the virtual machine @vm_script = Inprovise::DSL.script "#{name}#vbox_vm" do # verify VM validate do vmname = vbs.vbox_name(self) if trigger 'vbox:vbox-verify', vmname true # vm with this name already running else # look for existing target with VM name on :apply unless node creation is suppressed if command == :apply && !vbs.vbox_no_node(self) if tgt = Inprovise::Infrastructure.find(vmname) type = Inprovise::Infrastructure::Group === tgt ? 'group' : 'node' raise ArgumentError, "VBox #{vmname} clashes with existing #{type}" end end false end end # apply : installation apply do # 1. trigger virt-install trigger('vbox:vbox-install', vbs.vbox_name(self), { :virt_type => vbs.vbox_virt_type(self), :arch => vbs.vbox_arch(self), :autostart => vbs.vbox_autostart(self), :memory => vbs.vbox_memory(self), :cpu_model => vbs.vbox_cpu_model(self), :cpu_sockets => vbs.vbox_cpu_sockets(self), :cpu_cores => vbs.vbox_cpu_cores(self), :cpu_threads => vbs.vbox_cpu_threads(self), :os => vbs.vbox_os(self), :network => vbs.vbox_network(self) || :hostnet, :netname => vbs.vbox_netname(self), :source=> vbs.vbox_source(self), :nic => vbs.vbox_nic(self), :graphics => vbs.vbox_graphics(self), :image => vbs.vbox_image(self), :diskbus => vbs.vbox_diskbus(self), :format => vbs.vbox_format(self), :cdrom => vbs.vbox_cdrom(self), :cdrombus => vbs.vbox_cdrombus(self), :kernel => vbs.vbox_kernel(self), :kernel_args => vbs.vbox_kernel_args(self), :install_opts => vbs.vbox_install_opts(self) }) # wait to startup 10.times do sleep(1) break if trigger 'vbox:vbox-verify', vbs.vbox_name(self), true, vbs.vbox_autostart(self) end end # revert : uninstall revert do vmname = vbs.vbox_name(self) if trigger 'vbox:vbox-verify', vmname, true trigger 'vbox:vbox-shutdown', vmname msg = "Waiting for shutdown of VBox #{vmname}. Please wait ..." log.print("#{msg}|\r", :bold) 30.times do |n| sleep(1) log.print("#{msg}#{%w{| / - \\}[(n+1) % 4]}\r", :bold) break unless trigger 'vbox:vbox-verify', vmname, true end if trigger('vbox:vbox-verify', vmname, true) trigger('vbox:vbox-kill', vmname) sleep(1) end log.println("#{msg}done", :bold) end trigger('vbox:vbox-delete', vmname) unless trigger('vbox:vbox-verify', vmname, true) end end # 2. add an Inprovise node if the VM was installed successfully @node_script = Inprovise::DSL.script "#{name}#vbox_node" do validate do if vbs.vbox_no_node(self) config.command != :revert else vmname = vbs.vbox_name(self) if tgt = Inprovise::Infrastructure.find(vmname) raise ArgumentError, "VBox #{vmname} clashes with existing group" if Inprovise::Infrastructure::Group === tgt true else false end end end # add a node object for the new VM unless suppressed apply do vmname = vbs.vbox_name(self) unless vbs.vbox_no_node(self) # get MAC and IP for VM msg = "Determining IP address for VBox #{vmname}. Please wait ..." log.print("#{msg}|\r", :bold) mac = addr = nil 150.times do |n| sleep(2) log.print("#{msg}#{%w{| / - \\}[(n+1) % 4]}\r", :bold) mac, addr = trigger 'vbox:vbox-ifaddr', vmname if addr break end end log.println("#{msg}done", :bold) raise RuntimeError, "Failed to determine IP address for VBox #{vmname}" unless addr log("VBox #{vmname} : mac=#{mac}, addr=#{addr}") if Inprovise.verbosity > 0 vbox_opts = vbs.vbox_config_hash(self) vbox_opts.delete(:no_node) vbox_opts.delete(:no_sniff) vbox_opts[:mac] = mac # record mac-address node_opts = vbox_opts.delete(:node) || {} node_group = [node_opts.delete(:group)].flatten.compact node_opts[:host] ||= addr node_opts[:user] ||= vbs.vbox_user(self) node_opts[:vbox] = vbox_opts node = Inprovise::Infrastructure::Node.new(vmname, node_opts) Inprovise::Infrastructure.save node_group.each do |grpnm| grp = Inprovise::Infrastructure.find(grpnm) raise ArgumentError, "Invalid Group name '#{grpnm}'." unless grp.nil? || Inprovise::Infrastructure::Group === grp grp = Inprovise::Infrastructure::Group.new(grpnm) unless grp node.add_to(grp) end Inprovise::Infrastructure.save unless node_group.empty? unless vbs.vbox_no_sniff(self) (1..10).each do |i| begin Inprovise::Sniffer.run_sniffers_for(node) break rescue raise if i == 10 sleep(5) # maybe VM needs more time to start up SSH node.disconnect! # retry on (comm) failure end end Inprovise::Infrastructure.save end log("Added new node #{node}", :bold) config['vbox'] = node end end # remove the node object for the VM unless node creation suppressed revert do vmname = vbs.vbox_name(self) unless vbs.vbox_no_node(self) tgt = Inprovise::Infrastructure.find(vmname) if tgt && Inprovise::Infrastructure::Node === tgt Inprovise::Infrastructure.deregister(vmname) Inprovise::Infrastructure.save log("Removed node #{tgt}", :bold) else log("No existing node #{vmname} found!", :yellow) end end end end # add dependencies in correct order # MUST preceed any user defined dependencies dependencies.insert(0, @vm_script.name, @node_script.name) self end |
#vbox_arch(context) ⇒ Object
335 336 337 |
# File 'lib/inprovise/vbox/vbox.rb', line 335 def vbox_arch(context) value_for context, context.config[name.to_sym][:arch] end |
#vbox_autostart(context) ⇒ Object
319 320 321 |
# File 'lib/inprovise/vbox/vbox.rb', line 319 def vbox_autostart(context) value_for context, context.config[name.to_sym][:autostart] end |
#vbox_cdrom(context) ⇒ Object
395 396 397 |
# File 'lib/inprovise/vbox/vbox.rb', line 395 def vbox_cdrom(context) value_for context, context.config[name.to_sym][:cdrom] end |
#vbox_cdrombus(context) ⇒ Object
399 400 401 |
# File 'lib/inprovise/vbox/vbox.rb', line 399 def vbox_cdrombus(context) value_for context, context.config[name.to_sym][:cdrombus] end |
#vbox_config_hash(context) ⇒ Object
415 416 417 418 419 420 421 422 423 |
# File 'lib/inprovise/vbox/vbox.rb', line 415 def vbox_config_hash(context) context.config[name.to_sym].to_h.reduce({}) do |h, (k,v)| case h[k] = value_for(context, v) when Inprovise::Config h[k] = h[k].to_h end h end end |
#vbox_cpu_cores(context) ⇒ Object
351 352 353 |
# File 'lib/inprovise/vbox/vbox.rb', line 351 def vbox_cpu_cores(context) value_for context, context.config[name.to_sym][:cpu_cores] end |
#vbox_cpu_model(context) ⇒ Object
343 344 345 |
# File 'lib/inprovise/vbox/vbox.rb', line 343 def vbox_cpu_model(context) value_for context, context.config[name.to_sym][:cpu_model] end |
#vbox_cpu_sockets(context) ⇒ Object
347 348 349 |
# File 'lib/inprovise/vbox/vbox.rb', line 347 def vbox_cpu_sockets(context) value_for context, context.config[name.to_sym][:cpu_sockets] end |
#vbox_cpu_threads(context) ⇒ Object
355 356 357 |
# File 'lib/inprovise/vbox/vbox.rb', line 355 def vbox_cpu_threads(context) value_for context, context.config[name.to_sym][:cpu_threads] end |
#vbox_diskbus(context) ⇒ Object
387 388 389 |
# File 'lib/inprovise/vbox/vbox.rb', line 387 def vbox_diskbus(context) value_for context, context.config[name.to_sym][:diskbus] end |
#vbox_format(context) ⇒ Object
391 392 393 |
# File 'lib/inprovise/vbox/vbox.rb', line 391 def vbox_format(context) value_for context, context.config[name.to_sym][:format] end |
#vbox_graphics(context) ⇒ Object
375 376 377 |
# File 'lib/inprovise/vbox/vbox.rb', line 375 def vbox_graphics(context) value_for context, context.config[name.to_sym][:graphics] end |
#vbox_image(context) ⇒ Object
331 332 333 |
# File 'lib/inprovise/vbox/vbox.rb', line 331 def vbox_image(context) value_for context, context.config[name.to_sym][:image] end |
#vbox_install_opts(context) ⇒ Object
411 412 413 |
# File 'lib/inprovise/vbox/vbox.rb', line 411 def vbox_install_opts(context) value_for context, context.config[name.to_sym][:install_opts] end |
#vbox_kernel(context) ⇒ Object
403 404 405 |
# File 'lib/inprovise/vbox/vbox.rb', line 403 def vbox_kernel(context) value_for context, context.config[name.to_sym][:kernel] end |
#vbox_kernel_args(context) ⇒ Object
407 408 409 |
# File 'lib/inprovise/vbox/vbox.rb', line 407 def vbox_kernel_args(context) value_for context, context.config[name.to_sym][:kernel_args] end |
#vbox_memory(context) ⇒ Object
339 340 341 |
# File 'lib/inprovise/vbox/vbox.rb', line 339 def vbox_memory(context) value_for context, context.config[name.to_sym][:memory] end |
#vbox_name(context) ⇒ Object
311 312 313 |
# File 'lib/inprovise/vbox/vbox.rb', line 311 def vbox_name(context) value_for context, context.config[name.to_sym][:name] end |
#vbox_netname(context) ⇒ Object
363 364 365 |
# File 'lib/inprovise/vbox/vbox.rb', line 363 def vbox_netname(context) value_for context, context.config[name.to_sym][:netname] end |
#vbox_network(context) ⇒ Object
359 360 361 |
# File 'lib/inprovise/vbox/vbox.rb', line 359 def vbox_network(context) value_for context, context.config[name.to_sym][:network] end |
#vbox_nic(context) ⇒ Object
371 372 373 |
# File 'lib/inprovise/vbox/vbox.rb', line 371 def vbox_nic(context) value_for context, context.config[name.to_sym][:nic] end |
#vbox_no_node(context) ⇒ Object
323 324 325 |
# File 'lib/inprovise/vbox/vbox.rb', line 323 def vbox_no_node(context) value_for context, context.config[name.to_sym][:no_node] end |
#vbox_no_sniff(context) ⇒ Object
327 328 329 |
# File 'lib/inprovise/vbox/vbox.rb', line 327 def vbox_no_sniff(context) value_for context, context.config[name.to_sym][:no_sniff] end |
#vbox_os(context) ⇒ Object
379 380 381 |
# File 'lib/inprovise/vbox/vbox.rb', line 379 def vbox_os(context) value_for context, context.config[name.to_sym][:os] end |
#vbox_source(context) ⇒ Object
367 368 369 |
# File 'lib/inprovise/vbox/vbox.rb', line 367 def vbox_source(context) value_for context, context.config[name.to_sym][:source] end |
#vbox_user(context) ⇒ Object
383 384 385 |
# File 'lib/inprovise/vbox/vbox.rb', line 383 def vbox_user(context) value_for context, context.config[name.to_sym][:user] end |
#vbox_virt_type(context) ⇒ Object
315 316 317 |
# File 'lib/inprovise/vbox/vbox.rb', line 315 def vbox_virt_type(context) value_for context, context.config[name.to_sym][:virt_type] end |