Created
December 13, 2013 00:30
-
-
Save johnskopis/7938140 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[ | |
network.interfaces.eth0.addresses | |
network.interfaces.eth0.addresses | |
] | |
DEFAULT_QUERY = 'role:base' | |
DEFAULT_FILTER = Proc.new do |addresses| | |
ipa = addresses.select { |address, config| config[:family] == 'inet' }.keys | |
ipa.size == 1 ? ipa.first : ipa | |
end | |
def data_for_csv(query = DEFAULT_FILTER, | |
attrs = DEFAULT_ATTRS, | |
filter = DEFAULT_FILTER | |
) | |
search(:node, query).map do |node| | |
[node.name] + | |
attrs.map do |attr| | |
filter.call(lookup_attr(node, attr)) | |
end | |
end | |
end | |
def lookup_attr(node, attr) | |
attr.split('.').inject(node) do |memo, meth| | |
memo.send meth.to_sym | |
end | |
end | |
puts DEFAULT_ATTRS.to_csv | |
csv = CSV.generate do |csv| | |
data_for_csv.each do |data| | |
csv << data | |
end | |
end | |
puts csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment