virtualman 1.0.0 → 1.1.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.
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ####Required
4
+ require 'virtualman'
5
+
6
+ cloned_vms = load_conf["cloned_vms"]
7
+
8
+ cloned_vms.each do |vm|
9
+ list << vm["name"]
10
+ end
11
+
12
+ puts "----Clone_Cooking----"
13
+ puts "Which VM do you want to cook?"
14
+
15
+ chocie = Menu.unic_run(list)
16
+
17
+ selected_vm = Vm.new(choice)
18
+
19
+ if !selected_vm.running?
20
+ puts "#{selected_vm.name} is not running..."
21
+ start_vm(selected_vm)
22
+ if !selected_vm.running?
23
+ puts "As you want.."
24
+ return 1
25
+ end
26
+ end
27
+
28
+ cmd = "ssh #{user}@#{selected_vm.ip}"
29
+ exec (cmd)
30
+
31
+ puts "Thanks!"
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ####Required
4
+ require 'virtualman'
5
+
6
+ source_vms = load_conf["source_vms"]
7
+
8
+ cloned_vm = clone_vm(source_vms)
9
+
10
+ start_vm(cloned_vm)
11
+
12
+ record_conf(cloned_vm)
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ####Required
4
+ require 'virtualman'
5
+
6
+ cloned_vms = load_conf["cloned_vms"]
7
+
8
+ cloned_vms.each do |vm|
9
+ list << vm["name"]
10
+ end
11
+
12
+ puts "----SSH_cloned_VM----"
13
+ puts "Which VM do you want to ssh?"
14
+
15
+ chocie = Menu.unic_run(list)
16
+
17
+ selected_vm = Vm.new(choice)
18
+
19
+ puts "With which user? [root]"
20
+ user = Menu.ask
21
+
22
+ if user.empty?
23
+ user = "root"
24
+ end
25
+
26
+ if !selected_vm.running?
27
+ puts "#{selected_vm.name} is not running..."
28
+ puts "Do you want to start it?"
29
+ puts "yes/[no]"
30
+ chocie = Menu.ask
31
+ if choice == "yes"
32
+ start_vm(selected_vm)
33
+ else
34
+ puts "As you want!"
35
+ return 1
36
+ end
37
+ end
38
+
39
+ cmd = "ssh #{user}@#{selected_vm.ip}"
40
+ exec (cmd)
41
+
42
+ puts "Thanks!"
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ####Required
4
+ require 'virtualman'
5
+
6
+ menu = ["clone","cook","ssh_cloned","exit"]
7
+
8
+ puts "----VirtualMan----"
9
+ puts "Please select an action to do with your VM."
10
+
11
+ choice = Menu.unic_run(menu)
12
+
13
+ while choice != "exit"
14
+ send(choice)
15
+ puts "----VirtualMan----"
16
+ puts "Please select an action to do with your VM."
17
+ choice = Menu.unic_run(menu)
18
+ end
19
+
20
+ puts "Thank you come again!"
@@ -1,98 +1,5 @@
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 stop properly a vm
30
- # It assumes that you have the name of the VM on your personnal hosts file or DNS
31
- # It can be modified to use GuestAdditions instead
32
- def stop!
33
- $stdout.sync = true
34
- if self.running?
35
- `ssh root@#{self.name.delete "\""} "shutdown -h now"`
36
- puts "Waiting for complete shutdown of #{self.name}"
37
- while self.running?
38
- print "."
39
- sleep (1)
40
- end
41
- end
42
-
43
- end
44
- end
45
-
46
- # Create an array tht contains all the #Vm from VirtualBox
47
- # And implement methods to play with it as a whole.
48
- class VmLister < Array
49
-
50
- # Return an array with the list returned by the command "VBoxManage list vms"
51
- # The argument *type will be used later (to specify a remote host for instance)
52
- def perform_list(*type)
53
- vm_list = `VBoxManage list vms`.split(/\n/).collect {|e| e[/".*"/]}
54
- return vm_list
55
- end
56
-
57
- # Populate the array with the method perform_list
58
- # The argument *type will be used later (to specify a remote host for instance)
59
- def populate!(*type)
60
- self.perform_list.each {|vm| self << Vm.new(vm)}
61
- end
62
-
63
- # A simple lister
64
- def list
65
- self.each {|vm| puts "#{vm.name}"}
66
- end
67
-
68
- # This methods returns a #VmLister object but just with the running VMs of the list.
69
- def running?
70
- running_vms = VmLister.new
71
- self.collect {|vm| running_vms << vm if vm.running?}
72
- return running_vms
73
- end
74
-
75
- # A general method to interact with the VBoxManage tool
76
- # the block param could be useful for advanced links between your script and this class
77
- def manage(action, *param, &block)
78
- self.each do |vm|
79
- block_param = block.call(vm.name) if block_given?
80
-
81
- vm.manage(action, param, block_param)
82
- end
83
- end
84
-
85
- # A method to automatically export the list of VMs
86
- def backup!(folder)
87
- self.each {|vm| vm.stop!}
88
- sleep (5)
89
-
90
- self.each do |vm|
91
- filename = Time.now().strftime("#{vm.name.delete "\""}_%Y%m%dT%H%M")
92
- vm.manage("export","-o #{folder}/#{filename}.ova")
93
- sleep(5)
94
- end
95
-
96
- self.each {|vm| vm.manage("startvm", "--type headless")}
97
- end
98
- end
1
+ require 'virtualman/menu'
2
+ require 'virtualman/vm'
3
+ require 'virtualman/vmlister'
4
+ require 'virtualman/conf'
5
+ require 'virtualman/interractive'
@@ -0,0 +1,37 @@
1
+ require 'yaml'
2
+
3
+ CONFIG_FILE = File.join(ENV['HOME'],'.clone_vm.rc.yaml')
4
+
5
+ def load_conf
6
+ if File.exists? CONFIG_FILE
7
+ config_options = YAML.load_file(CONFIG_FILE)
8
+ else
9
+ STDERR.puts "No configuration file found here ~/.vm_to_clone.rc.yaml"
10
+ STDERR.puts "Config file example:"
11
+ STDERR.puts "---"
12
+ STDERR.puts "source_vm:"
13
+ STDERR.puts " - name: Debian"
14
+ STDERR.puts " snapshot: ready to clone!"
15
+ STDERR.puts " - name: Gentoo"
16
+ STDERR.puts " snapshot: almost ready"
17
+ STDERR.puts "role_path: http://path/to/your/role/fodler"
18
+ STDERR.puts "cookbook_path: http://path/to/your/cookbooks.tar.gz"
19
+ STDERR.puts "roles:"
20
+ STDERR.puts "- devtest"
21
+ STDERR.puts "- devtest_jenkins"
22
+ STDERR.puts "- build_pkg_devtest_jenkins"
23
+ end
24
+ return config_options
25
+ end
26
+
27
+ def record_conf(vm_cloned)
28
+ conf = load_conf()
29
+ if conf["cloned_vms"]
30
+ conf["cloned_vms"] << {"name" => vm_cloned.name}
31
+ else
32
+ conf.merge!({"cloned_vms" => {"name" => vm_cloned.name}})
33
+ end
34
+
35
+ File.open(CONFIG_FILE, "w") {|f| f.write(conf.to_yaml) }
36
+ puts "VM #{vm_cloned.name} saved in your configuration file"
37
+ end
@@ -0,0 +1,124 @@
1
+ #require 'lib/virtualman'
2
+
3
+ def choosed_option(array,choice,attribute_to_check)
4
+ array.each_with_index do |option,index|
5
+ if option[attribute_to_check] == choice
6
+ return array[index]
7
+ end
8
+ end
9
+ end
10
+
11
+ def choose options, *attribute_to_check
12
+ options_array = load_conf[options]
13
+ if attribute_to_check.empty?
14
+ return Menu.unic_run(options_array)
15
+ else
16
+ list = Array.new
17
+ options_array.each do |option|
18
+ list << option[attribute_to_check[0]]
19
+ end
20
+ choice = Menu.unic_run(list)
21
+ return choosed_option(options_array,choice,attribute_to_check[0])
22
+ end
23
+ end
24
+
25
+ def clone_vm
26
+ puts "----Clone_VM----"
27
+ puts "Which VM do you want to clone?"
28
+
29
+ vm_choice = choose "source_vms", "name"
30
+ vm_to_clone = Vm.new("\"#{vm_choice["name"]}\"")
31
+
32
+ puts "How do you want to name your freshly spawned VM?"
33
+ vm_name = Menu.ask
34
+
35
+ options = "--snapshot '#{vm_choice["snapshot"]}' --name #{vm_name} --register"
36
+ vm_to_clone.manage("clonevm", options)
37
+
38
+ vm_cloned = Vm.new("\"#{vm_name}\"")
39
+ return vm_cloned
40
+ end
41
+
42
+ def start_vm(vm)
43
+ if !vm.running?
44
+ puts "Do you want to start the VM? headless?"
45
+ puts "(default is no)"
46
+ puts "yes/headless/[no]"
47
+
48
+ answer = Menu.ask
49
+
50
+ if answer == "headless"
51
+ option = "--type headless"
52
+ puts "The VM is booting headless"
53
+ elsif answer == "yes"
54
+ option = ""
55
+ puts "The VM is booting"
56
+ else
57
+ puts "The vm will not be started"
58
+ return vm
59
+ end
60
+
61
+ vm.manage("startvm", option)
62
+
63
+ print "Waiting for the VM to get an IP"
64
+ while !vm.ip
65
+ print "."
66
+ sleep (1)
67
+ end
68
+ print "\n"
69
+ end
70
+
71
+ return vm.running?
72
+ end
73
+
74
+ def clone
75
+ cloned_vm = clone_vm
76
+ start_vm(cloned_vm)
77
+ record_conf(cloned_vm)
78
+ end
79
+
80
+ def cook
81
+ puts "----Cook a clone!----"
82
+ puts "Which VM do you want to cook?"
83
+ role_path = load_conf["role_path"]
84
+ cookbook_path = load_conf["cookbook_path"]
85
+
86
+ vm_choice = choose "cloned_vms", "name"
87
+
88
+ selected_vm = Vm.new(vm_choice["name"])
89
+
90
+ puts "Which role do you want to give?"
91
+
92
+ role = choose "roles"
93
+
94
+ user="root"
95
+
96
+ if start_vm(selected_vm)
97
+ cmd = "ssh #{user}@#{selected_vm.ip} '/usr/local/bin/chef-solo -j #{role_path}#{role}.json -r #{cookbook_path}'"
98
+ p cmd
99
+ `#{cmd}`
100
+ end
101
+
102
+ puts "Thanks!"
103
+ end
104
+
105
+ def ssh_cloned
106
+ puts "----SSH to cloned VM----"
107
+ puts "Which VM do you want to ssh?"
108
+
109
+ vm_choice = choose "cloned_vms", "name"
110
+
111
+ selected_vm = Vm.new(vm_choice["name"])
112
+
113
+ puts "With which user? [root]"
114
+ user = Menu.ask
115
+
116
+ if user.empty?
117
+ user = "root"
118
+ end
119
+
120
+ if start_vm(selected_vm)
121
+ cmd = "ssh #{user}@#{selected_vm.ip}"
122
+ exec (cmd)
123
+ end
124
+ end
@@ -0,0 +1,58 @@
1
+ #The MIT LICENSE
2
+
3
+ #Copyright (c) 2010 Gabriel Horner
4
+
5
+ #Permission is hereby granted, free of charge, to any person obtaining
6
+ #a copy of this software and associated documentation files (the
7
+ #"Software"), to deal in the Software without restriction, including
8
+ #without limitation the rights to use, copy, modify, merge, publish,
9
+ #distribute, sublicense, and/or sell copies of the Software, and to
10
+ #permit persons to whom the Software is furnished to do so, subject to
11
+ #the following conditions:
12
+
13
+ #The above copyright notice and this permission notice shall be
14
+ #included in all copies or substantial portions of the Software.
15
+
16
+ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ #MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ #NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ #LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ #WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ #Forked from https://github.com/cldwalker/menu
25
+
26
+ module Menu
27
+ extend self
28
+
29
+ def ask
30
+ $stdin.reopen '/dev/tty'
31
+ $stdin.gets.chomp
32
+ end
33
+
34
+ def unic_run(array)
35
+ unic_prompt(array)
36
+ answer = ask
37
+ return unic_answer(array, answer)
38
+ end
39
+
40
+ def unic_answer(array, input)
41
+ if input[/(\d+)/]
42
+ index = $1.to_i - 1
43
+ return array[index] if array[index]
44
+ else
45
+ abort("`#{input}' is an invalid choice.")
46
+ end
47
+ end
48
+
49
+ def unic_prompt(lines)
50
+ ljust_size = lines.size.to_s.size + 1
51
+ lines.each_with_index {|obj,i|
52
+ puts "#{i+1}.".ljust(ljust_size) + " " +obj
53
+ }
54
+ print "\nSpecify your choice\nChoose: "
55
+ end
56
+
57
+
58
+ end
@@ -0,0 +1,61 @@
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 stop properly a vm
30
+ # It assumes that you have the name of the VM on your personnal hosts file or DNS
31
+ # It can be modified to use GuestAdditions instead
32
+ def stop!
33
+ $stdout.sync = true
34
+ if self.running?
35
+ `ssh root@#{self.name.delete "\""} "shutdown -h now"`
36
+ puts "Waiting for complete shutdown of #{self.name}"
37
+ while self.running?
38
+ print "."
39
+ sleep (1)
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ # return the ip of the VM if it is running. O if it is not.
46
+ def ip
47
+ if self.running?
48
+ options = "enumerate #{self.name} | grep IP | cut -d , -f 2 | cut -d : -f 2"
49
+ cmd_ip = "VBoxManage guestproperty #{options}"
50
+ ip = `#{cmd_ip}`.strip
51
+ if ip.match /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/
52
+ return ip
53
+ else
54
+ return false
55
+ end
56
+ else
57
+ puts "The VM is not running, cannot determine IP"
58
+ return false
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,58 @@
1
+ # Create an array that contains all the #Vm from VirtualBox
2
+ # And implement methods to play with it as a whole.
3
+ class VmLister < Array
4
+
5
+ # Return an array with the list returned by the command "VBoxManage list vms"
6
+ # The argument *type will be used later (to specify a remote host for instance)
7
+ def perform_list(*type)
8
+ vm_list = `VBoxManage list vms`.split(/\n/).collect {|e| e[/".*"/]}
9
+ return vm_list
10
+ end
11
+
12
+ # Populate the array with the method perform_list
13
+ # The argument *type will be used later (to specify a remote host for instance)
14
+ def populate!(*type)
15
+ self.perform_list.each {|vm| self << Vm.new(vm)}
16
+ end
17
+
18
+ # A simple lister
19
+ def list
20
+ self.each {|vm| puts "#{vm.name}"}
21
+ end
22
+
23
+ # Get the IP of each running vm.
24
+ def ip
25
+ self.each {|vm| puts vm.ip}
26
+ end
27
+
28
+ # This methods returns a #VmLister object but just with the running VMs of the list.
29
+ def running?
30
+ running_vms = VmLister.new
31
+ self.collect {|vm| running_vms << vm if vm.running?}
32
+ return running_vms
33
+ end
34
+
35
+ # A general method to interact with the VBoxManage tool
36
+ # the block param could be useful for advanced links between your script and this class
37
+ def manage(action, *param, &block)
38
+ self.each do |vm|
39
+ block_param = block.call(vm.name) if block_given?
40
+
41
+ vm.manage(action, param, block_param)
42
+ end
43
+ end
44
+
45
+ # A method to automatically export the list of VMs
46
+ def backup!(folder)
47
+ self.each {|vm| vm.stop!}
48
+ sleep (5)
49
+
50
+ self.each do |vm|
51
+ filename = Time.now().strftime("#{vm.name.delete "\""}_%Y%m%dT%H%M")
52
+ vm.manage("export","-o #{folder}/#{filename}.ova")
53
+ sleep(5)
54
+ end
55
+
56
+ self.each {|vm| vm.manage("startvm", "--type headless")}
57
+ end
58
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virtualman
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,16 +9,29 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-16 00:00:00.000000000 Z
12
+ date: 2012-09-06 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: It is for writing scripts for UNIX-like systems to handle your VirtualBox
15
15
  appliance.
16
16
  email: pierre.ozoux@gmail.com
17
- executables: []
17
+ executables:
18
+ - clone_vm
19
+ - virtualman
20
+ - ssh_cloned
21
+ - clone_cooking
18
22
  extensions: []
19
23
  extra_rdoc_files: []
20
24
  files:
21
25
  - lib/virtualman.rb
26
+ - lib/virtualman/conf.rb
27
+ - lib/virtualman/interractive.rb
28
+ - lib/virtualman/menu.rb
29
+ - lib/virtualman/vm.rb
30
+ - lib/virtualman/vmlister.rb
31
+ - bin/clone_vm
32
+ - bin/virtualman
33
+ - bin/ssh_cloned
34
+ - bin/clone_cooking
22
35
  homepage: http://rubygems.org/gems/virtualman
23
36
  licenses: []
24
37
  post_install_message:
@@ -42,5 +55,5 @@ rubyforge_project:
42
55
  rubygems_version: 1.8.23
43
56
  signing_key:
44
57
  specification_version: 3
45
- summary: A sinmple way to manage your Virtual Machines under VirtualBox.
58
+ summary: A simple way to manage your Virtual Machines under VirtualBox.
46
59
  test_files: []