Last active
August 29, 2015 14:00
-
-
Save irvingpop/477345871c4c8c48d38b to your computer and use it in GitHub Desktop.
Orgmapper snippet to output all nodes with client versions in all organizations to a JSON file
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
# Orgmapper snippet to output all nodes with client versions in all organizations to a JSON file | |
# To Use: | |
# 1) Start orgmapper | |
# 2) Load the script | |
# eval(::File.read("/path/to/orgmapper_all_client_versions.rb")) | |
# 3) output will be printed to the screen and also written to JSON_OUTPUT_FILE | |
JSON_OUTPUT_FILE = 'chef_versions.json' | |
require 'zlib' | |
require 'stringio' | |
def gunzip(s) | |
reader = Zlib::GzipReader.new(StringIO.new(s)) | |
reader.read | |
end | |
def guid_to_name(guid) | |
begin | |
orgname = ORGS.find {|o| o.guid == guid }.name | |
rescue Exception | |
orgname = guid | |
end | |
orgname | |
end | |
def doit | |
chef_versions = {} | |
skipped_nodes = 0 | |
node_count = sql.select(:name).from(:nodes).count | |
puts "Parsing chef-client versions for #{node_count} nodes" | |
sql.select(:name, :org_id, :updated_at, :environment, :serialized_object).from(:nodes).each do |n| | |
begin | |
n_obj = JSON.parse(gunzip(n[:serialized_object])) | |
rescue Exception | |
puts "skipping parse of node #{n[:name]} due to JSON parse error" | |
skipped_nodes += 1 | |
next | |
end | |
if n_obj['chef_packages'] && n_obj['chef_packages']['chef'] | |
version = n_obj['chef_packages']['chef']['version'] | |
else | |
version = 'UNKNOWN' | |
end | |
org_name = guid_to_name(n[:org_id]) || n[:org_id] | |
chef_versions[org_name] = {} unless chef_versions[org_name] | |
chef_versions[org_name][n[:environment]] = {} unless chef_versions[org_name][n[:environment]] | |
chef_versions[org_name][n[:environment]][version] = {} unless chef_versions[org_name][n[:environment]][version] | |
chef_versions[org_name][n[:environment]][version].store(n[:name], n[:updated_at]) | |
end | |
File.open(JSON_OUTPUT_FILE, 'w') {|file| file.write(JSON.pretty_generate(chef_versions))} | |
puts JSON.pretty_generate(chef_versions) | |
puts "Skipped #{skipped_nodes} nodes out of #{node_count}" | |
end | |
doit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment