class Koma::CLI
Public Instance Methods
exec()
click to toggle source
# File lib/koma/cli.rb, line 90 def exec backend = Koma::Backend::Exec.new(nil, options) gathered = backend.gather if options[:yaml] puts YAML.dump(gathered) elsif options[:csv] puts csv_dump(host, gathered) else puts JSON.pretty_generate(gathered) end end
help(command = nil)
click to toggle source
Calls superclass method
# File lib/koma/cli.rb, line 110 def help(command = nil) if options[:version] puts Koma::VERSION else super end end
keys()
click to toggle source
# File lib/koma/cli.rb, line 103 def keys Koma::HostInventory.all_inventory_keys.each do |key| key += ' (disabled)' if Koma::HostInventory.disabled_keys.include?(key) puts key end end
method_missing(command)
click to toggle source
# File lib/koma/cli.rb, line 118 def method_missing(command) message = <<-EOH (( )) (( _____ )) (U ● ● U) (( ● )) < Could not find command "#{command}". EOH puts message end
run_command(host = nil, *commands)
click to toggle source
# File lib/koma/cli.rb, line 48 def run_command(host = nil, *commands) if host.nil? STDERR.puts 'ERROR: "koma run-command" was called with no arguments' STDERR.puts 'Usage: "koma run-command <host1,host2,..> <command>"' return end if commands.empty? commands = [host] begin stdin = timeout(1) { $stdin.read } rescue Timeout::Error end if stdin.nil? STDERR.puts 'ERROR: "koma run-commands" was called with no arguments' STDERR.puts 'Usage: "koma run-commands <host1,host2,..> <commands>"' return end ret = stdin.split("\n").select { |line| line =~ /^Host ([^\s\*]+)/ }.map do |line| line =~ /^Host ([^\s]+)/ Regexp.last_match[1] end host = ret.join(',') end backend = Koma::Backend::Ssh.new(host, options) backend.stdin = stdin if stdin gathered = backend.run_commands(commands) if options[:yaml] puts YAML.dump(gathered) elsif options[:csv] puts csv_dump(host, gathered) else puts JSON.pretty_generate(gathered) end end
ssh(host = nil)
click to toggle source
# File lib/koma/cli.rb, line 19 def ssh(host = nil) if host.nil? begin stdin = timeout(1) { $stdin.read } rescue Timeout::Error end ret = stdin.split("\n").select { |line| line =~ /^Host ([^\s\*]+)/ }.map do |line| line =~ /^Host ([^\s]+)/ Regexp.last_match[1] end host = ret.join(',') end backend = Koma::Backend::Ssh.new(host, options) backend.stdin = stdin if stdin gathered = backend.gather if options[:yaml] puts YAML.dump(gathered) elsif options[:csv] puts csv_dump(host, gathered) else puts JSON.pretty_generate(gathered) end end
Private Instance Methods
csv_dump(hoststr, gathered)
click to toggle source
# File lib/koma/cli.rb, line 132 def csv_dump(hoststr, gathered) hosts = hoststr.split(',') if gathered.keys.sort == hosts.sort header = ['host'] + gathered.values.first.keys csv_data = CSV.generate() do |csv| csv << header gathered.each do |host, vals| results = [] vals.each do |_, v| if v.is_a?(Hash) && v.key?(:stdout) && v[:exit_status] == 0 results.push(v[:stdout]) else results.push(v) end end cols = [host] + results csv << cols end end else header = ['host'] + gathered.keys csv_data = CSV.generate() do |csv| csv << header results = [] gathered.values.each do |v| if v.is_a?(Hash) && v.key?(:stdout) && v[:exit_status] == 0 results.push(v[:stdout]) else results.push(v) end end cols = [hoststr] + results csv << cols end end return csv_data end