class Object
Constants
- CONFIG_FILE
Load config file
- LOGGED_IN_USER
- MAX_SSH_HOSTS
If you’re sshing into too many hosts, you might be doing something wrong
- PRISM_URL
Public Instance Methods
display_results(matching, options, noun)
click to toggle source
# File lib/marauder/commands.rb, line 146 def display_results(matching, options, noun) if matching.empty? STDERR.puts "No #{noun} found" else STDERR.puts "#{matching.length} results" field_name = options.field || nil if options.short matching.map { |host| get_field(host, field_name) }.compact.each{ |value| puts value } else puts table(matching.map { |host| app = host['app'].join(',') app = host['mainclasses'].join(',') if app.length == 0 [host['stage'], host['stack'], app, get_field(host, field_name), host['createdAt']] }) end end end
find_hardware(filter)
click to toggle source
# File lib/marauder/commands.rb, line 111 def find_hardware(filter) begin data = prism_query('/hardware', filter) hardware = data["data"]["hardware"] token_filter(hardware, filter){ |h| [h["dnsName"], h["stage"], h["stack"]] + h["app"] } rescue StandardError => error STDERR.puts 'WARNING: The prism server you are using no longer has the hardware endpoint - ignoring request' # return an empty list [] end end
find_hosts(filter)
click to toggle source
# File lib/marauder/commands.rb, line 99 def find_hosts(filter) find_instances(filter) + find_hardware(filter) end
find_instances(filter)
click to toggle source
# File lib/marauder/commands.rb, line 103 def find_instances(filter) data = prism_query('/instances', filter) hosts = data["data"]["instances"] token_filter(hosts, filter){ |host| host["mainclasses"].map{|mc| tokenize(mc)}.flatten + host["mainclasses"] + [host["stage"], host["stack"]] + host["app"] } end
get_field(object, field)
click to toggle source
# File lib/marauder/commands.rb, line 135 def get_field(object, field) if field.nil? if object['addresses'] && object['addresses']['public'].nil? field = 'ip' else field = 'dnsName' end end get_field_rec(object, field.split('.')) end
get_field_rec(object, fieldList)
click to toggle source
# File lib/marauder/commands.rb, line 127 def get_field_rec(object, fieldList) if !object || fieldList.empty? object else get_field_rec(object[fieldList[0]], fieldList.drop(1)) end end
prism_query(path, filter)
click to toggle source
# File lib/marauder/commands.rb, line 61 def prism_query(path, filter) prism_filters = filter.select{ |f| f =~ /=/ } api_query = Hash[prism_filters.map { |f| param = f.split('=') [param[0], param[1]] }.group_by { |pair| pair[0] }.map { |key, kvs| [key, kvs.map{|v| v[1]}] }] data = Api.get("#{PRISM_URL}#{path}", :query => {:_expand => true}.merge(api_query)) if data.code != 200 raise StandardError, "Prism API returned status code #{data.code} in response to #{data.request.last_uri} - check that your configuration file is correct" end if data["stale"] update_time = data["lastUpdated"] STDERR.puts "WARNING: Prism reports that the data returned from #{path} is stale, it was last updated at #{update_time}" end data end
table(rows)
click to toggle source
# File lib/marauder/commands.rb, line 44 def table(rows) lengths = rows.map { |row| row.map { |value| value.nil? ? 0 : value.size } } col_widths = lengths.transpose.map { |column| column.max } rows.map { |row| col_widths.each_with_index.map { |width, index| (row[index] || "").ljust(width) }.join("\t") }.join("\n") end
token_filter(things_to_filter, filter) { |thing| ... }
click to toggle source
# File lib/marauder/commands.rb, line 87 def token_filter(things_to_filter, filter) dumb_filters = filter.reject{ |f| f =~ /=/ } query = dumb_filters.map(&:downcase).map{|s| Regexp.new("^#{s}.*")} things_to_filter.select do |thing| query.all? do |phrase| tokens = yield thing tokens.compact.any? {|token| phrase.match(token.downcase)} end end end
tokenize(s)
click to toggle source
# File lib/marauder/commands.rb, line 54 def tokenize(s) separators = ['-', '_', '::'] separators.inject([s]) do |tokens, sep| tokens.map {|t| t.split(sep)}.flatten end end
user_for_host(hostname)
click to toggle source
# File lib/marauder/commands.rb, line 123 def user_for_host(hostname) Net::SSH.configuration_for(hostname)[:user] end