virtualman 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/virtualman.rb +88 -0
  2. metadata +46 -0
data/lib/virtualman.rb ADDED
@@ -0,0 +1,88 @@
1
+ # Implement a way to interact with the VirtualBox command line tool.
2
+ # Each #Vm is a Class that contains the #name of the VM. With that
3
+ # name you can then interact with it through VBoxManage for example
4
+
5
+ class Vm
6
+
7
+ # This attribute contains the name of the VM in VirtualBox.
8
+ attr_reader :name
9
+
10
+ # A #Vm is just described by it's name.
11
+ # *type is for further needs
12
+ def initialize(vm_name, *type)
13
+ @name = vm_name
14
+ end
15
+
16
+ # Returns a boolean whether the VM is running or not
17
+ def running?
18
+ !`VBoxManage showvminfo #{name} | grep State | grep running`.empty?
19
+ end
20
+
21
+ # A general method to interact with the VM.
22
+ # action is the kind of action to request to VBoxManage
23
+ # *param is a list of options
24
+ def manage(action, *param)
25
+ puts "VBoxManage #{action} #{@name} #{param.join(" ")}"
26
+ puts `VBoxManage #{action} #{@name} #{param.join(" ")}`
27
+ end
28
+
29
+ # A method to automatically Export a VM as an "importable" appliance
30
+ # /!\ be careful, this operation requires to poweroff the VM.
31
+ def backup!(folder)
32
+ self.manage("controlvm","poweroff")
33
+
34
+ filename = Time.now().strftime("#{@name.delete "\""}_%Y%m%dT%H%M")
35
+
36
+ self.manage("export","-o #{folder}/#{filename}.ova")
37
+
38
+ self.manage("startvm", "--type headless")
39
+ end
40
+ end
41
+
42
+ # Create an array tht contains all the #Vm from VirtualBox
43
+ # And implement methods to play with it as a whole.
44
+ class VmLister < Array
45
+
46
+ # Return an array with the list returned by the command "VBoxManage list vms"
47
+ # The argument *type will be used later (to specify a remote host for instance)
48
+ def perform_list(*type)
49
+ vm_list = `VBoxManage list vms`.split(/\n/).collect {|e| e[/".*"/]}
50
+ return vm_list
51
+ end
52
+
53
+ # Populate the array with the method perform_list
54
+ # The argument *type will be used later (to specify a remote host for instance)
55
+ def populate!(*type)
56
+ self.perform_list.each {|vm| self << Vm.new(vm)}
57
+ end
58
+
59
+ # A simple lister
60
+ def list
61
+ self.each {|vm| puts "#{vm.name}"}
62
+ end
63
+
64
+ # This methods returns a #VmLister object but just with the running VMs of the list.
65
+ def running?
66
+ running_vms = VmLister.new
67
+ self.collect {|vm| running_vms << vm if vm.running?}
68
+ return running_vms
69
+ end
70
+
71
+ # A general method to interact with the VBoxManage tool
72
+ # the block param could be useful for advanced links between your script and this class
73
+ def manage(action, *param, &block)
74
+ self.each do |vm|
75
+ block_param = block.call(vm.name) if block_given?
76
+
77
+ puts "action: #{action} for the VM: #{vm.name} with options :"
78
+ puts " #{param} #{block_param}"
79
+
80
+ vm.act(action, param, block_param)
81
+ end
82
+ end
83
+
84
+ # A method to automatically export the list of VMs
85
+ def backup!(folder)
86
+ self.each {|vm| vm.backup!(folder)}
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtualman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pierre Ozoux
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: It is for writing scripts for UNIX-like systems to handle your VirtualBox
15
+ appliance.
16
+ email: pierre.ozoux@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/virtualman.rb
22
+ homepage: http://rubygems.org/gems/virtualman
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: A sinmple way to manage your Virtual Machines under VirtualBox.
46
+ test_files: []