Created
November 26, 2009 09:43
-
-
Save kates/243368 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'highline' | |
require 'optparse' | |
require 'etc' | |
current_user = Etc.getpwuid | |
OPTIONS = { | |
:host => "localhost", | |
:port => 8222, | |
:user => current_user.name, | |
:password => "" | |
} | |
op = OptionParser.new do |opts| | |
script_name = File.basename($0) | |
opts.banner = "Usage: #{script_name} start|stop|list [options]" | |
opts.separator "" | |
opts.on("-h", "--host HOST", String, "Host hostname or IP", "Default: localhost"){|OPTIONS[:host]|} | |
opts.on("-p", "--port PORT", Integer, "Port number vmware sdk listens to", "Default: 8222"){|OPTIONS[:port]|} | |
opts.on("-u", "--user USER", String, "VM administrator", "Defaults to the current user"){|OPTIONS[:user]|} | |
opts.separator "" | |
opts.on_tail("-h", "--help", "Show this help message."){ puts opts; exit} | |
end.parse! | |
if ARGV.empty? || !["start","stop","list"].include?(ARGV.first) | |
puts op.options | |
exit | |
end | |
OPTIONS[:password] = HighLine.new.ask("Password: "){|q| q.echo = false } | |
class Command | |
def self.invoke(cmd) | |
self.new.send(cmd) | |
end | |
def start | |
perform("start") | |
end | |
def stop | |
perform("stop") | |
end | |
def list | |
result = get_list | |
puts result | |
end | |
def perform(action) | |
vms = action == "start" ? get_list_registered.split("\n") : get_list_running.split("\n") | |
vms.shift | |
running_vms = get_list_running.split("\n") | |
running_vms.shift | |
HighLine.new.choose do |menu| | |
menu.prompt = "Choose VM to #{action}: " | |
vms.each_with_index do |vm, i| | |
vm += " [running]" if running_vms.include?(vm) && action == "start" | |
menu.choice(vm) do | |
%x[vmrun -T server -h http://#{OPTIONS[:host]}:#{OPTIONS[:port]}/sdk -u #{OPTIONS[:user]} -p #{OPTIONS[:password]} #{action} '#{vm}'] | |
end | |
end | |
menu.choice("cancel"){exit;} | |
end | |
puts get_list_running | |
end | |
def get_list(action='list') | |
%x[vmrun -T server -h http://#{OPTIONS[:host]}:#{OPTIONS[:port]}/sdk -u #{OPTIONS[:user]} -p #{OPTIONS[:password]} #{action}] | |
end | |
def get_list_registered | |
get_list("listRegisteredVM") | |
end | |
def get_list_running | |
get_list("list") | |
end | |
end | |
Command.invoke(ARGV.first) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment