Class: Virtualman::Vm
- Inherits:
-
Object
- Object
- Virtualman::Vm
- Defined in:
- lib/virtualman/vm.rb
Overview
Implement a way to interact with the VirtualBox command line tool. Each #Vm is a Class that contains the #name of the VM. With that name you can then interact with it through VBoxManage for example
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
This attribute contains the name of the VM in VirtualBox.
Instance Method Summary collapse
-
#initialize(vm_name, *type) ⇒ Vm
constructor
A #Vm is just described by it’s name.
-
#ip ⇒ Object
return the ip of the VM if it is running.
-
#manage(action, *param) ⇒ Object
A general method to interact with the VM.
-
#running? ⇒ Boolean
Returns a boolean whether the VM is running or not.
-
#stop! ⇒ Object
A method to stop properly a vm It assumes that you can access to the VM with root and ssh_keys.
Constructor Details
#initialize(vm_name, *type) ⇒ Vm
A #Vm is just described by it’s name. *type is for further needs
13 14 15 |
# File 'lib/virtualman/vm.rb', line 13 def initialize(vm_name, *type) @name = vm_name end |
Instance Attribute Details
#name ⇒ Object (readonly)
This attribute contains the name of the VM in VirtualBox.
9 10 11 |
# File 'lib/virtualman/vm.rb', line 9 def name @name end |
Instance Method Details
#ip ⇒ Object
return the ip of the VM if it is running. O if it is not or if the ip is not correct.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/virtualman/vm.rb', line 36 def ip if self.running? ip = self.manage("guestproperty enumerate").split("\n").grep(/IP/)[0].split(",")[1].sub(/^ value: /, '') if ip.match /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/ return ip else return false end else puts "The VM is not running, cannot determine IP" return false end end |
#manage(action, *param) ⇒ Object
A general method to interact with the VM. action is the kind of action to request to VBoxManage *param is a list of options
20 21 22 23 24 25 26 27 28 |
# File 'lib/virtualman/vm.rb', line 20 def manage(action, *param) output = `VBoxManage #{action} \"#{@name}\" #{param.join(" ")} 2>&1` if $?.exitstatus != 0 Kernel.abort(output) else return output end end |
#running? ⇒ Boolean
Returns a boolean whether the VM is running or not
31 32 33 |
# File 'lib/virtualman/vm.rb', line 31 def running? return !self.manage("showvminfo").split("\n").grep(/running/).empty? end |
#stop! ⇒ Object
A method to stop properly a vm It assumes that you can access to the VM with root and ssh_keys
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/virtualman/vm.rb', line 52 def stop! if self.running? `ssh root@#{self.ip} "shutdown -h now"` print "Waiting for complete shutdown of #{self.name}" while self.running? print "." sleep (1) end sleep (5) print("\n") end end |