Created
February 15, 2011 19:52
-
-
Save antonlindstrom/828104 to your computer and use it in GitHub Desktop.
Simplifies the management of vms in virtualbox. 2> /dev/null moahaha..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the Virtual machines | |
def vms | |
vm = Hash.new | |
vm["vm-name"] = { :port => "2030", :natif => "3", :user => "root" } | |
vm["centos-test"] = { :port => "2031", :natif => "1", :user => "root" } | |
vm["gentoo-dev"] = { :port => "2032", :natif => "1", :user => "root" } | |
vm | |
end | |
desc "Setup correct portforwarding" | |
task :setup do | |
vms.each_pair do |name, cf| | |
system("VBoxManage modifyvm \"#{name}\" --natpf#{cf[:natif]} \"guestssh,tcp,,#{cf[:port]},,22\" 2> /dev/null") | |
unless $? == 0 | |
puts "There was an error completing the request" | |
else | |
puts "#{name} is listening on #{cf[:port]}" | |
end | |
end | |
end | |
desc "Cleanup portforwarding" | |
task :cleanup do | |
vms.each_pair do |name, cf| | |
system("VBoxManage modifyvm \"#{name}\" --natpf#{cf[:natif]} delete \"guestssh\"") | |
end | |
end | |
desc "Start all defined virtual machines" | |
task :startup do | |
vms.each_pair do |name, cf| | |
system("VBoxManage startvm \"#{name}\" --type headless > /dev/null 2>&1") | |
unless $? == 0 | |
puts "Starting #{name} returned an error, maybe it is running?" | |
else | |
puts "#{name} at #{cf[:port]} up." | |
end | |
end | |
puts "=> done." | |
end | |
desc "Start all virtual machines without headless option" | |
task :startup_nh do | |
vms.each_pair do |name, cf| | |
system("VBoxManage startvm \"#{name}\"") | |
unless $? == 0 | |
puts "Starting #{name} returned an error, maybe it is running?" | |
else | |
puts "#{name} at #{cf[:port]} up." | |
end | |
end | |
puts "=> done." | |
end | |
desc "Shutdown defined virtual machines" | |
task :destroy do | |
vms.each_pair do |name, cf| | |
system("VBoxManage controlvm \"#{name}\" poweroff > /dev/null 2>&1") | |
puts "#{name} at #{cf[:port]} down." | |
end | |
puts "=> done." | |
end | |
desc "Create a snapshot" | |
task :snap do | |
unless ENV["S_VM"] && ENV["S_NAME"] | |
puts 'Usage: S_VM="vm_name" S_NAME="snapshot_name"' | |
end | |
system("VBoxManage snapshot #{ENV["S_VM"]} take #{ENV["S_NAME"]} > /dev/null") | |
puts "=> done." | |
end | |
desc "List VMs" | |
task :list do | |
puts "Virtual machines:" | |
all = `VBoxManage list vms` | |
all.each { |a| puts "\t#{a}"} | |
puts "Running machines:" | |
run = `VBoxManage list runningvms` | |
run.each { |r| puts "\t#{r}"} | |
end | |
desc "Choose which system to log into" | |
task :ssh do | |
puts "=================================" | |
puts "Machines:" | |
vms.each_pair { |k,v| puts "\t* #{k}" } | |
puts "=================================" | |
puts "Type machine to enter:" | |
print "vm> " | |
vm = STDIN.gets.chomp | |
unless vms.has_key?(vm) | |
puts "Type name of the machine to enter it." | |
exit | |
end | |
system("ssh #{vms[vm][:user]}@localhost -p #{vms[vm][:port]}") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment