Top Level Namespace

Defined Under Namespace

Classes: Api, Marauder

Constant Summary collapse

CONFIG_FILE =

Load config file

"#{ENV['HOME']}/.config/marauder/defaults.yaml"
MAX_SSH_HOSTS =

If you’re sshing into too many hosts, you might be doing something wrong

4
LOGGED_IN_USER =
ENV['USER']

Instance Method Summary collapse

Instance Method Details

#display_results(matching, options, noun) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# 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) ⇒ Object



99
100
101
# File 'lib/marauder/commands.rb', line 99

def find_hosts(filter)
  find_instances(filter) + find_hardware(filter)
end

#find_instances(filter) ⇒ Object



103
104
105
106
107
108
109
# 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) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# 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) ⇒ Object



127
128
129
130
131
132
133
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



54
55
56
57
58
59
# 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) ⇒ Object



123
124
125
# File 'lib/marauder/commands.rb', line 123

def user_for_host(hostname)
  Net::SSH.configuration_for(hostname)[:user]
end