#!/usr/bin/env ruby require 'gli' begin # XXX: Remove this begin/rescue before distributing your app require 'vmesh' require 'logger' require 'highline/import' rescue LoadError STDERR.puts "In development, you need to use `bundle exec bin/vmesh` to run your app" STDERR.puts "At install-time, RubyGems will make sure lib, etc. are in the load path" STDERR.puts "Feel free to remove this message from bin/vmesh now" exit 64 end include GLI::App version Vmesh::VERSION Vmesh.logger = Logger.new(STDOUT) @logger = Vmesh.logger @logger.level = Logger::WARN config_filename = '.vmesh.rc' def check_migrate Dir.glob(File.join(File.expand_path(ENV['HOME']),'.mesh.*.rc')).each do |mesh_file| new_name = mesh_file.gsub('.mesh','.vmesh') Vmesh.logger.info "Migrating #{mesh_file} from #{File.basename(mesh_file)} to #{new_name}" File.rename mesh_file, new_name end end check_migrate if ARGV.count >= 2 host_alias = ARGV[0] init_config = (ARGV.last == 'initconfig') if init_config or File.exist? File.join(File.expand_path(ENV['HOME']),".vmesh.#{host_alias}.rc") config_filename = ".vmesh.#{host_alias}.rc" Vmesh.logger.info "Using host specific config file ~/.vmesh.#{host_alias}.rc" ARGV.shift else Vmesh.logger.info "No site specific config file found, using default ~/#{config_filename}." end end config_file config_filename program_desc 'VMWare VSphere command suite interface' program_long_desc 'A command suite to allow users to manage VMWare virtual machines remotely via the command line' desc 'Ignore SSL Certificate errors in connection' switch [:insecure] desc 'VCenter username' arg_name 'user' flag [:u,:user] desc 'VCenter password' arg_name 'password' flag [:p,:password] desc 'VCenter host' arg_name 'vcenter host' flag [:h,:host] desc 'Datacenter' arg_name 'datacenter' flag [:d,:datacenter] desc 'VCenter Resource Pool' arg_name 'vcenter resource pool' flag [:r,:resource_pool] desc 'Run in debug mode' switch [:debug] commands_from 'vmesh/commands' pre do |global,command,options,args| # Pre logic here # Return true to proceed; false to abort and not call the # chosen command # Use skips_pre before a command to skip this block # on that command only @logger.level = Logger::DEBUG if global[:debug] if global[:user].to_s == '' not_so_secret_username = ask("Enter Username please:") {|q| q.echo = true} global['u'] = global[:u] = global['user'] = global[:user] = not_so_secret_username end if global[:password].to_s == '' the_super_secret_password = ask("Enter Password for #{global[:user]} please:") {|q| q.echo = false} global['p'] = global[:p] = global['password'] = global[:password] = the_super_secret_password end true end post do |global,command,options,args| # Post logic here # Use skips_post before a command to skip this # block on that command only end on_error do |exception| # Error logic here # return false to skip default error handling true end exit run(ARGV)