netvbox 0.0.3 → 0.0.4

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.
data/README.rdoc CHANGED
@@ -1,5 +1,6 @@
1
1
  = netvbox
2
2
 
3
+
3
4
  == Description
4
5
  netvbox is a {ruby gem}[https://rubygems.org/gems/netvbox] designed for limited
5
6
  administration of VirtualBox VMs on networked machines in a Linux environment.
@@ -7,12 +8,50 @@ administration of VirtualBox VMs on networked machines in a Linux environment.
7
8
  The original use case was for testing {Chef}[http://www.opscode.com/chef/]
8
9
  recipes without needing (or wanting) to provision (and pay for) cloud resources
9
10
  (e.g. Amazon EC2). Take a few spare machines, install VirtualBox on each, create
10
- some snapshots and you're ready to go.
11
+ some snapshots, and you're ready to go.
11
12
 
12
13
  netvbox maintains a list of snapshots for VirtualBox VMs, and can easily load
13
14
  all snapshots, power off all VMs, and display VM status information. VMs can
14
15
  be added and removed from the list via the add and remove subcommands.
15
16
 
17
+ === VM sets
18
+ netvbox can manage sets of VirtualBox VMs. You can think of these sets much
19
+ like {rvm}[http://beginrescueend.com/] gemsets. Say, for example, that you have
20
+ 5 VirtualBox VMs on various hosts on the network. Sometimes you might want to
21
+ clean and reinstall things on all 5 VMs. Other times you wish to only reload
22
+ snapshots on 3 VMs. You can create VM sets to manage sets of VirtualBox VMs, and
23
+ switch between sets on the fly. See VM set sub-commands in the sub-commands
24
+ section.
25
+
26
+
16
27
  == Sub-commands
17
- netvbox uses sub-comamnds. Type netvbox with no arguments to see the commands
28
+ netvbox uses sub-commands. Below is the help listing.
29
+
30
+ SUB-COMMANDS
31
+ add <ssh host> <username> <password> <vm name> <snapshot name>
32
+ adds a vm snapshot on the specified host
33
+ list
34
+ lists vm snapshots
35
+ load
36
+ loads vm snapshots
37
+ poweroff
38
+ powers off all vms
39
+ remove <ssh host> <username> <vm name> <snapshot name>
40
+ removes a vm snapshot from management
41
+ status
42
+ shows status of vms
43
+
44
+ VM SET SUB-COMMANDS
45
+ createset <set name>
46
+ creates a vm set with the specified name
47
+ currentset
48
+ shows the current vm set in use
49
+ listsets
50
+ lists all vm sets
51
+ removeset <set name>
52
+ removes the vm set with the specified name
53
+ usedefaultset
54
+ uses the default vm set
55
+ useset <set name>
56
+ uses the vm set with the specified name
18
57
 
data/bin/netvbox CHANGED
@@ -1,64 +1,97 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'netvbox/vm_manager'
4
- require 'netvbox/config_manager'
3
+ require 'netvbox/vm_set_config'
4
+ require 'netvbox/vm_set_manager'
5
+ require 'netvbox/printing_delegator'
5
6
  require 'netvbox/version'
6
7
 
7
- NETVBOX_CONFIG = "#{ENV['HOME']}/.netvbox"
8
+ NETVBOX_HOME = "#{ENV['HOME']}/.netvbox"
8
9
 
9
10
  def print_help
10
- puts "NetVbox version #{NetVbox::VERSION}"
11
- puts "Current config file: #{NETVBOX_CONFIG}"
12
- puts 'sub-commands:'
13
- puts ' add [ssh host] [username] [password] [vm name] [snapshot name]'
14
- puts ' adds a vm to manage'
11
+ puts "NetVbox #{NetVbox::VERSION}"
12
+ puts "Config path: #{NETVBOX_HOME}"
13
+ puts
14
+ puts 'SUB-COMMANDS'
15
+ puts ' add <ssh host> <username> <password> <vm name> <snapshot name>'
16
+ puts ' adds a vm snapshot on the specified host'
15
17
  puts ' list'
16
- puts ' lists vms under management'
18
+ puts ' lists vm snapshots'
17
19
  puts ' load'
18
20
  puts ' loads vm snapshots'
19
21
  puts ' poweroff'
20
22
  puts ' powers off all vms'
21
- puts ' remove [ssh host] [username] [vm name] [snapshot name]'
22
- puts ' removes a vm from management'
23
+ puts ' remove <ssh host> <username> <vm name> <snapshot name>'
24
+ puts ' removes a vm snapshot from management'
23
25
  puts ' status'
24
26
  puts ' shows status of vms'
27
+ puts
28
+ puts 'VM SET SUB-COMMANDS'
29
+ puts ' createset <set name>'
30
+ puts ' creates a vm set with the specified name'
31
+ puts ' currentset'
32
+ puts ' shows the current vm set in use'
33
+ puts ' listsets'
34
+ puts ' lists all vm sets'
35
+ puts ' removeset <set name>'
36
+ puts ' removes the vm set with the specified name'
37
+ puts ' usedefaultset'
38
+ puts ' uses the default vm set'
39
+ puts ' useset <set name>'
40
+ puts ' uses the vm set with the specified name'
25
41
  end
26
42
 
27
- def parse_args
43
+ def expect_args(num_expected_args)
44
+ if ARGV.length == num_expected_args
45
+ yield ARGV
46
+ else
47
+ puts 'Wrong number of arguments'
48
+ print_help
49
+ end
50
+ end
51
+
52
+ def process_args
28
53
  command = ARGV[0]
29
- config_manager = NetVbox::ConfigManager.new(NETVBOX_CONFIG)
30
- vm_manager = NetVbox::VmManager.new(config_manager)
54
+ vm_set_manager = NetVbox::VmSetManager.new(NETVBOX_HOME)
55
+ vm_set = vm_set_manager.current_set
56
+ delegator = NetVbox::PrintingDelegator.new(vm_set_manager, vm_set)
31
57
  case command
32
58
  when 'add'
33
- if ARGV.length >= 6
59
+ expect_args(6) do
34
60
  hostname, username, password, vm_name, snapshot_name = ARGV[1..5]
35
- vm_manager.add_vm(hostname, username, password, vm_name, snapshot_name)
36
- else
37
- print_help
61
+ delegator.add_vm(hostname, username, password, vm_name, snapshot_name)
38
62
  end
39
63
  when 'list'
40
- vm_manager.list_vms
64
+ delegator.list_vms
41
65
  when 'load'
42
- vm_manager.load_snapshots
66
+ delegator.load_snapshots
43
67
  when 'poweroff'
44
- vm_manager.poweroff_all
68
+ delegator.poweroff_all
45
69
  when 'remove'
46
- if ARGV.length >= 5
70
+ expect_args(5) do
47
71
  hostname, username, vm_name, snapshot_name = ARGV[1..4]
48
- vm_manager.remove_vm(hostname, username, vm_name, snapshot_name)
49
- else
50
- print_help
72
+ delegator.remove_vm(hostname, username, vm_name, snapshot_name)
51
73
  end
52
74
  when 'status'
53
- vm_manager.print_status
75
+ delegator.print_status
76
+ when 'createset'
77
+ expect_args(2) {delegator.create_set ARGV[1]}
78
+ when 'currentset'
79
+ delegator.print_current_set
80
+ when 'listsets'
81
+ delegator.list_sets
82
+ when 'removeset'
83
+ expect_args(2) {delegator.remove_set ARGV[1]}
84
+ when 'usedefaultset'
85
+ delegator.use_default_set
86
+ when 'useset'
87
+ expect_args(2) {delegator.use_set ARGV[1]}
54
88
  else
55
- puts "Unknown command: #{command}"
56
- print_help
89
+ puts "Unknown command: #{command}. See help."
57
90
  end
58
91
  end
59
92
 
60
93
  if ARGV.empty?
61
94
  print_help
62
95
  else
63
- parse_args
96
+ process_args
64
97
  end
@@ -0,0 +1,99 @@
1
+ require 'netvbox/vm_set'
2
+ require 'netvbox/vm_set_manager'
3
+
4
+ module NetVbox
5
+ class PrintingDelegator
6
+ def initialize(vm_set_manager, vm_set)
7
+ @vm_set = vm_set
8
+ @vm_set_manager = vm_set_manager
9
+ end
10
+
11
+ def add_vm(hostname, username, password, vm_name, snapshot_name)
12
+ begin
13
+ @vm_set.add_vm(hostname, username, password, vm_name, snapshot_name)
14
+ puts "Added (#{vm_name}, #{snapshot_name}) on host, #{hostname}"
15
+ rescue => e
16
+ puts "Could not add VM snapshot: #{e.message}"
17
+ end
18
+ end
19
+
20
+ def list_vms
21
+ all_vm_info = @vm_set.config.all_vm_info
22
+ all_vm_info.each do |vm_info|
23
+ ssh_info = vm_info.ssh_connection_info
24
+ printf "vm: %-36s host: %s\n", "#{vm_info.vm_name} (#{vm_info.snapshot_name})", "#{ssh_info.username}@#{ssh_info.hostname}"
25
+ end
26
+ puts 'No vms' if all_vm_info.empty?
27
+ end
28
+
29
+ def load_snapshots
30
+ puts 'Loading snapshots...'
31
+ @vm_set.load_snapshots
32
+ print_status
33
+ end
34
+
35
+ def poweroff_all
36
+ puts 'Powering off VMs...'
37
+ @vm_set.poweroff_all
38
+ print_status
39
+ end
40
+
41
+ def remove_vm(hostname, username, vm_name, snapshot_name)
42
+ begin
43
+ @vm_set.remove_vm(hostname, username, vm_name, snapshot_name)
44
+ puts "Removed (#{vm_name}, #{snapshot_name}) on host, #{hostname}"
45
+ rescue => e
46
+ puts "Could not remove VM snapshot: #{e.message}"
47
+ end
48
+ end
49
+
50
+ def print_status
51
+ puts 'Retrieving status info...'
52
+ all_status = @vm_set.all_status
53
+ all_status.each do |vm_info, status|
54
+ printf "%-40s %s\n", "#{vm_info.vm_name} on #{vm_info.ssh_connection_info.hostname}", "[#{status}]"
55
+ end
56
+ puts 'There are no vms' if all_status.empty?
57
+ end
58
+
59
+ def create_set(set_name)
60
+ begin
61
+ @vm_set_manager.create_set set_name
62
+ puts "Created set: #{set_name}"
63
+ rescue => e
64
+ puts "Could not create set: #{e.message}"
65
+ end
66
+ end
67
+
68
+ def print_current_set
69
+ puts @vm_set_manager.current_set_name
70
+ end
71
+
72
+ def list_sets
73
+ @vm_set_manager.vm_set_names.each {|vm_set_name| puts vm_set_name}
74
+ end
75
+
76
+ def remove_set(set_name)
77
+ begin
78
+ @vm_set_manager.remove_set set_name
79
+ puts "Removed set: #{set_name}"
80
+ rescue => e
81
+ puts "Could not remove set: #{e.message}"
82
+ end
83
+ end
84
+
85
+ def use_default_set
86
+ @vm_set_manager.use_default_set
87
+ puts "Now using set: #{@vm_set_manager.current_set_name}"
88
+ end
89
+
90
+ def use_set(set_name)
91
+ begin
92
+ @vm_set_manager.use_set set_name
93
+ rescue => e
94
+ puts "Could not use set: #{e.message}"
95
+ end
96
+ puts "Now using set: #{@vm_set_manager.current_set_name}"
97
+ end
98
+ end
99
+ end
@@ -1,3 +1,3 @@
1
1
  module NetVbox
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/netvbox/vm.rb CHANGED
@@ -69,41 +69,35 @@ module NetVbox
69
69
 
70
70
  def poweroff
71
71
  if showvminfo('VMState') == 'running'
72
- my_ssh do |ssh|
73
- ssh.exec!("VBoxManage controlvm \"#{@vm_info.vm_name}\" poweroff").strip
74
- end
75
- else
76
- "#{@vm_info.vm_name} is not running"
72
+ my_ssh {|ssh| ssh.exec!("VBoxManage controlvm \"#{@vm_info.vm_name}\" poweroff").strip}
77
73
  end
78
74
  end
79
75
 
80
76
  def load_snapshot
81
- return "ERROR: you must disable 3d acceleration for #{@vm_info.vm_name}" if showvminfo('accelerate3d') == 'on'
77
+ # VMs with 3D acceleration cannot be started remotely
78
+ # TODO: propogate this info to a log or some output to notify user
79
+ return if showvminfo('accelerate3d') == 'on'
82
80
  poweroff
83
- my_ssh do |ssh|
84
- ssh.exec!("VBoxManage snapshot \"#{@vm_info.vm_name}\" restore \"#{@vm_info.snapshot_name}\" && VBoxManage startvm \"#{@vm_info.vm_name}\" --type headless")
85
- end
81
+ command = "VBoxManage snapshot \"#{@vm_info.vm_name}\" restore \"#{@vm_info.snapshot_name}\" && VBoxManage startvm \"#{@vm_info.vm_name}\" --type headless"
82
+ my_ssh {|ssh| ssh.exec!(command)}
86
83
  end
87
84
 
88
85
  private
89
86
 
90
87
  def showvminfo(var_name)
91
- my_ssh do |ssh|
92
- ssh.exec!("VBoxManage showvminfo \"#{@vm_info.vm_name}\" --machinereadable | grep ^#{var_name}= | cut -d '\"' -f 2").strip
93
- end
88
+ command = "VBoxManage showvminfo \"#{@vm_info.vm_name}\" --machinereadable | grep ^#{var_name}= | cut -d '\"' -f 2"
89
+ my_ssh {|ssh| ssh.exec!(command).strip}
94
90
  end
95
91
 
96
92
  def my_ssh
97
93
  ssh_info = @vm_info.ssh_connection_info
98
94
  begin
99
95
  # can raise SocketError or Net::SSH::AuthenticationFailed
100
- Net::SSH.start(ssh_info.hostname, ssh_info.username, :password => ssh_info.password) do |ssh|
101
- return yield ssh
102
- end
96
+ Net::SSH.start(ssh_info.hostname, ssh_info.username, :password => ssh_info.password) {|ssh| return yield ssh}
103
97
  rescue SocketError
104
- return 'Connection Error'
98
+ return 'connection error'
105
99
  rescue Net::SSH::AuthenticationFailed
106
- return 'Authentication Error'
100
+ return 'authentication error'
107
101
  end
108
102
  end
109
103
  end
@@ -0,0 +1,57 @@
1
+ require 'netvbox/vm'
2
+ require 'netvbox/vm_set_config'
3
+
4
+ module NetVbox
5
+ class VmSet
6
+ def initialize(vm_set_config)
7
+ @vm_set_config = vm_set_config
8
+ end
9
+
10
+ def config
11
+ @vm_set_config
12
+ end
13
+
14
+ def load_snapshots
15
+ threads = []
16
+ get_vms.each do |vm|
17
+ threads << Thread.new(vm) {|vm| vm.load_snapshot}
18
+ end
19
+ threads.each(&:join)
20
+ end
21
+
22
+ def poweroff_all
23
+ threads = []
24
+ get_vms.each do |vm|
25
+ threads << Thread.new(vm) {|vm| vm.poweroff}
26
+ end
27
+ threads.each(&:join)
28
+ end
29
+
30
+ def add_vm(hostname, username, password, vm_name, snapshot_name)
31
+ vm_info = VmInfo.new(SshConnectionInfo.new(hostname, username, password), vm_name, snapshot_name)
32
+ @vm_set_config.add_vm(vm_info)
33
+ end
34
+
35
+ def remove_vm(hostname, username, vm_name, snapshot_name)
36
+ vm_info = VmInfo.new(SshConnectionInfo.new(hostname, username, nil), vm_name, snapshot_name)
37
+ @vm_set_config.remove_vm(vm_info)
38
+ end
39
+
40
+ # return Hash of VmInfo to status
41
+ def all_status
42
+ status_map = {}
43
+ threads = []
44
+ get_vms.each do |vm|
45
+ threads << Thread.new(vm) {|vm| status_map[vm.vm_info] = vm.status}
46
+ end
47
+ threads.each(&:join)
48
+ status_map
49
+ end
50
+
51
+ private
52
+
53
+ def get_vms
54
+ @vm_set_config.all_vm_info.collect {|vm_info| Vm.new(vm_info)}
55
+ end
56
+ end
57
+ end
@@ -2,30 +2,38 @@ require 'csv'
2
2
  require 'netvbox/vm'
3
3
 
4
4
  module NetVbox
5
- class ConfigManager
5
+ class VmSetConfig
6
6
  def initialize(config_file_path)
7
7
  @config_file_path = config_file_path
8
8
  end
9
9
 
10
- def get_all_vm_info
10
+ def all_vm_info
11
11
  all_vm_info = []
12
12
  if File.readable?(@config_file_path)
13
- CSV.foreach(@config_file_path) do |row|
14
- all_vm_info << a_to_vm_info(row)
15
- end
13
+ CSV.foreach(@config_file_path) {|row| all_vm_info << a_to_vm_info(row)}
14
+ else
15
+ raise "Could not read file: #{@config_file_path}"
16
16
  end
17
17
  all_vm_info
18
18
  end
19
19
 
20
20
  def add_vm(vm_info)
21
- all_vm_info = get_all_vm_info
22
- all_vm_info.index(vm_info).nil? ? write_vm_info(all_vm_info << vm_info) : false
21
+ all = all_vm_info
22
+ if all.index(vm_info).nil?
23
+ write_vm_info(all << vm_info)
24
+ else
25
+ raise "(#{vm_info.vm_name}, #{vm_info.snapshot_name}) is already under management"
26
+ end
23
27
  end
24
28
 
25
29
  def remove_vm(vm_info)
26
- all_vm_info = get_all_vm_info
27
- updated_vm_info = all_vm_info.select {|i| !(i === vm_info)}
28
- all_vm_info != updated_vm_info ? write_vm_info(updated_vm_info) : false
30
+ all = all_vm_info
31
+ updated = all.select {|i| !(i === vm_info)}
32
+ if all != updated
33
+ write_vm_info(updated)
34
+ else
35
+ raise "(#{vm_info.vm_name}, #{vm_info.snapshot_name}) is not under management"
36
+ end
29
37
  end
30
38
 
31
39
  private
@@ -49,10 +57,8 @@ module NetVbox
49
57
  CSV.open(@config_file_path, "w") do |csv|
50
58
  all_vm_info.each {|vm_info| csv << vm_info_to_a(vm_info)}
51
59
  end
52
- true
53
60
  rescue IOError
54
- puts "ERROR: Could not write vm info to #{@config_file_path}"
55
- return false
61
+ raise "Could not write vm info to #{@config_file_path}"
56
62
  end
57
63
  end
58
64
  end
@@ -0,0 +1,75 @@
1
+ require 'fileutils'
2
+ require 'netvbox/vm_set_config'
3
+ require 'netvbox/vm_set'
4
+
5
+ module NetVbox
6
+ class VmSetManager
7
+ DEFAULT_VM_SET_NAME = 'default'
8
+ VM_SETS_FOLDER_NAME = 'vm_sets'
9
+ CURRENT_VM_SET_FILENAME = 'current_vm_set'
10
+ VM_SET_EXTENSION = 'vmset'
11
+
12
+ def initialize(netvbox_home)
13
+ @netvbox_home = netvbox_home
14
+ FileUtils.mkdir_p "#{@netvbox_home}/#{VM_SETS_FOLDER_NAME}"
15
+ FileUtils.touch vm_set_config_file(DEFAULT_VM_SET_NAME)
16
+ current_vm_file = "#{@netvbox_home}/#{CURRENT_VM_SET_FILENAME}"
17
+ IO.write(current_vm_file, DEFAULT_VM_SET_NAME) unless File.exists?(current_vm_file)
18
+ end
19
+
20
+ def exists?(vm_set_name)
21
+ File.exists? vm_set_config_file(vm_set_name)
22
+ end
23
+
24
+ def vm_set_names
25
+ Dir.entries("#{@netvbox_home}/#{VM_SETS_FOLDER_NAME}")
26
+ .select {|e| e.end_with?(VM_SET_EXTENSION)}
27
+ .collect {|e| e[0...(-VM_SET_EXTENSION.length - 1)]}
28
+ end
29
+
30
+ def current_set_name
31
+ current_vm_file = "#{@netvbox_home}/#{CURRENT_VM_SET_FILENAME}"
32
+ File.readable?(current_vm_file) ? IO.read(current_vm_file) : DEFAULT_VM_SET_NAME
33
+ end
34
+
35
+ def current_set
36
+ VmSet.new(VmSetConfig.new(vm_set_config_file(current_set_name)))
37
+ end
38
+
39
+ def use_default_set
40
+ FileUtils.touch vm_set_config_file(DEFAULT_VM_SET_NAME)
41
+ IO.write("#{@netvbox_home}/#{CURRENT_VM_SET_FILENAME}", DEFAULT_VM_SET_NAME)
42
+ end
43
+
44
+ def use_set(vm_set_name)
45
+ if File.exists? vm_set_config_file(vm_set_name)
46
+ IO.write("#{@netvbox_home}/#{CURRENT_VM_SET_FILENAME}", vm_set_name)
47
+ else
48
+ raise "VM set #{vm_set_name} does not exist"
49
+ end
50
+ end
51
+
52
+ def create_set(vm_set_name)
53
+ raise "Set: #{vm_set_name} already exists" if exists? vm_set_name
54
+ begin
55
+ FileUtils.touch vm_set_config_file(vm_set_name)
56
+ rescue
57
+ raise "Cannot create set with name: #{vm_set_name}"
58
+ end
59
+ end
60
+
61
+ def remove_set(vm_set_name)
62
+ raise "Set: #{vm_set_name} does not exist" unless exists? vm_set_name
63
+ raise "Cannot remove default set" if vm_set_name == DEFAULT_VM_SET_NAME
64
+ use_default_set if current_set_name == vm_set_name
65
+ File.delete vm_set_config_file(vm_set_name)
66
+ end
67
+
68
+ private
69
+
70
+ def vm_set_config_file(vm_set_name)
71
+ "#{@netvbox_home}/#{VM_SETS_FOLDER_NAME}/#{vm_set_name}.#{VM_SET_EXTENSION}"
72
+ end
73
+ end
74
+ end
75
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netvbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-23 00:00:00.000000000 Z
12
+ date: 2011-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: net-ssh
16
- requirement: &10050840 !ruby/object:Gem::Requirement
16
+ requirement: &13997300 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *10050840
24
+ version_requirements: *13997300
25
25
  description:
26
26
  email:
27
27
  - hsu.dennis@gmail.com
@@ -36,10 +36,12 @@ files:
36
36
  - Rakefile
37
37
  - bin/netvbox
38
38
  - lib/netvbox.rb
39
- - lib/netvbox/config_manager.rb
39
+ - lib/netvbox/printing_delegator.rb
40
40
  - lib/netvbox/version.rb
41
41
  - lib/netvbox/vm.rb
42
- - lib/netvbox/vm_manager.rb
42
+ - lib/netvbox/vm_set.rb
43
+ - lib/netvbox/vm_set_config.rb
44
+ - lib/netvbox/vm_set_manager.rb
43
45
  - netvbox.gemspec
44
46
  homepage: https://github.com/dhsu/netvbox
45
47
  licenses: []
@@ -1,73 +0,0 @@
1
- require 'netvbox/vm'
2
- require 'netvbox/config_manager'
3
-
4
- module NetVbox
5
- class VmManager
6
- def initialize(config_manager)
7
- @config_manager = config_manager
8
- end
9
-
10
- def print_status
11
- puts 'Getting status info...'
12
- threads = []
13
- get_vms.each do |vm|
14
- threads << Thread.new(vm) {|vm| puts "#{vm.vm_info.vm_name} on #{vm.vm_info.ssh_connection_info.hostname}... #{vm.status}"}
15
- end
16
- threads.each(&:join)
17
- puts 'There are no vms' if threads.empty?
18
- end
19
-
20
- def load_snapshots
21
- puts 'Loading snapshots...'
22
- threads = []
23
- get_vms.each do |vm|
24
- threads << Thread.new(vm) {|vm| vm.load_snapshot}
25
- end
26
- threads.each(&:join)
27
- print_status
28
- end
29
-
30
- def poweroff_all
31
- puts 'Powering off vms...'
32
- threads = []
33
- get_vms.each do |vm|
34
- threads << Thread.new(vm) {|vm| vm.poweroff}
35
- end
36
- threads.each(&:join)
37
- print_status
38
- end
39
-
40
- def list_vms
41
- all_vm_info = @config_manager.get_all_vm_info
42
- all_vm_info.each do |vm_info|
43
- ssh_info = vm_info.ssh_connection_info
44
- puts "#{ssh_info.username}@#{ssh_info.hostname} - vm: #{vm_info.vm_name}, snapshot: #{vm_info.snapshot_name}"
45
- end
46
- puts 'There are no vms' if all_vm_info.empty?
47
- end
48
-
49
- def add_vm(hostname, username, password, vm_name, snapshot_name)
50
- vm_info = VmInfo.new(SshConnectionInfo.new(hostname, username, password), vm_name, snapshot_name)
51
- if @config_manager.add_vm(vm_info)
52
- puts "Successfully added vm (#{vm_name}, #{snapshot_name})"
53
- else
54
- puts "Failed to add vm (#{vm_name}, #{snapshot_name})"
55
- end
56
- end
57
-
58
- def remove_vm(hostname, username, vm_name, snapshot_name)
59
- vm_info = VmInfo.new(SshConnectionInfo.new(hostname, username, nil), vm_name, snapshot_name)
60
- if @config_manager.remove_vm(vm_info)
61
- puts "Successfully removed vm (#{vm_name}, #{snapshot_name})"
62
- else
63
- puts "Failed to remove vm (#{vm_name}, #{snapshot_name})"
64
- end
65
- end
66
-
67
- private
68
-
69
- def get_vms
70
- @config_manager.get_all_vm_info.collect {|vm_info| Vm.new(vm_info)}
71
- end
72
- end
73
- end