Created
February 21, 2014 00:46
-
-
Save johnskopis/9126664 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
DEFAULT_ATTRS = %w[ | |
name | |
fqdn | |
network.interfaces.eth1.addresses | |
network.interfaces.eth0.addresses | |
ec2.public_ipv4 | |
ec2.instance_id | |
run_list | |
] | |
# Default filter echos the string back | |
FILTERS = Hash.new do |h, k| | |
h[k] = Proc.new do |v| | |
val = Array(v).map(&:to_s) | |
val.size == 1 ? val.first : val | |
end | |
end | |
# Operate on { IPADDR => { DATA } } | |
NETWORK_FILTER = Proc.new do |addresses| | |
begin | |
ipa = addresses.select { |address, config| config[:family] == 'inet' }.keys | |
ipa.size == 1 ? ipa.first : ipa | |
rescue | |
'unknown' | |
end | |
end | |
# This is jank | |
FILTERS['network.interfaces.eth1.addresses'] = NETWORK_FILTER | |
FILTERS['network.interfaces.eth0.addresses'] = NETWORK_FILTER | |
DEFAULT_QUERY = 'chef_environment:*' | |
def data_for_csv | |
search(:node, @query).map do |node| | |
@attrs.map do |attr| | |
FILTERS[attr].call(lookup_attr(node, attr)) | |
end | |
end.sort | |
end | |
def lookup_attr(node, attr) | |
attr.split('.').inject(node) do |memo, meth| | |
memo.send meth.to_sym | |
end | |
rescue NoMethodError => e | |
warn "null data for #{node} #{e}" | |
end | |
def run | |
puts @attrs.to_csv | |
csv = CSV.generate do |csv| | |
data_for_csv.each do |data| | |
csv << data | |
end | |
end | |
puts csv | |
end | |
@query = ENV['QUERY'] || DEFAULT_QUERY | |
@attrs = ENV['ATTRS'].nil? ? DEFAULT_ATTRS : ENV['ATTRS'].split(",") | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment