#!/usr/bin/env ruby # Copyright (c) 2011 VMware, Inc. All Rights Reserved. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'readline' require 'pp' require 'trollop' require 'rbvmomi' require 'rbvmomi/trollop' require 'shellwords' require 'yaml' require 'backports' require 'rvc' VIM = RbVmomi::VIM include RVC::Util Thread.abort_on_exception = true CMD = Module.new $opts = Trollop.options do banner <<-EOS Ruby vSphere Console. Usage: rvc [options] [username[:password]@]hostname where [options] are: EOS opt :insecure, "don't verify ssl certificate", :short => 'k', :default => (ENV['RBVMOMI_INSECURE'] == '1') opt :path, "Initial directory", :short => :none, :default => ENV['RVC_PATH'], :type => :string opt :create_directory, "Create the initial directory if it doesn't exist", :short => :none opt :cmd, "command to evaluate", :short => 'c', :multi => true, :type => :string end Trollop.die "At least 1 argument expected" unless ARGV.length >= 1 RVC.reload_modules false $shell = RVC::Shell.new ARGV.each do |uri| begin puts "Connecting to #{uri}..." if ARGV.size > 1 CMD.vim.connect uri, :insecure => $opts[:insecure] rescue UserError puts "Failed to connect to #{uri}: #{$!.message}" exit 1 end end RVC::Completion.install history_fn = "#{ENV['HOME']}/.rvc-history" IO.foreach(history_fn) { |l| Readline::HISTORY << l.chomp } rescue puts "Welcome to RVC. Try the 'help' command." history = File.open(history_fn, 'a') if $opts[:path] begin CMD.basic.cd $opts[:path] rescue UserError raise unless $opts[:create_directory] parent_path = File.dirname($opts[:path]) lookup(parent_path).CreateFolder(:name => File.basename($opts[:path])) CMD.basic.cd $opts[:path] end elsif ARGV.size == 1 CMD.basic.cd $shell.connections.keys.first end unless CMD.vmrc.find_vmrc $stderr.puts "VMRC is not installed. You will be unable to view virtual machine consoles. Use the vmrc.install command to install it." end CMD.basic.ls '.' while true begin input = $opts[:cmd].shift || Readline.readline($shell.prompt, false) or break input = input.strip next if input.empty? (history.puts input; Readline::HISTORY << input) unless input == Readline::HISTORY.to_a[-1] $shell.eval_input input rescue Interrupt puts end end