Skip to content

Instantly share code, notes, and snippets.

@k9ert
Created December 31, 2012 13:46
Show Gist options
  • Save k9ert/4419852 to your computer and use it in GitHub Desktop.
Save k9ert/4419852 to your computer and use it in GitHub Desktop.
A very hackish and basic frontend. Can be used to embed version-information gathered by report_mongo.sh https://github.com/example42/puppi/blob/master/files/scripts/report_mongo.sh into a wiki with the help of iframes, for confluence e.g. (per environment) {iframe:width=950|height=95|scroling=false|marginheight=1|frameborder=0|src=http://check.s…
getVersions = function(query) {
var map = function() {
emit( this.value.project, {project:this.value.project, fqdn:this.value.fqdn, ts:this.value.ts,version:this.value.version,environment:this.value.environment} );
};
var reduce = function(k,vals) {
version = vals[0].version;
vals.forEach(function(val) { if (val.version != version) return "inconsistent" } ) ;
return version;
};
return db.versions.mapReduce(
map,
reduce,
{query:query,out:{inline:1}})
}
// Need to define query before!
versions = getVersions(query).results.map(function(result) { return result._id + ":" + result.value.version;}).join(" \t ")
print(versions)
require 'rubygems'
require 'sinatra'
set :protection, :except => :frame_options
get '/server/:sname/summary' do
servername = params[:sname]
serverrole = getServerroles(servername)
if serverrole == nil
next "us" # unknown server
else
"<!DOCTYPE html><html><body> " + serversummary(servername) + "</body></html>"
end
end
get '/server/:sname/versions' do
servername = params[:sname]
getVersions("{\"value.fqdn\":\"#{servername}\"}")
end
get '/server/:sname' do
servername = params[:sname]
print "lets search for server #{servername}"
serverroles = getServerroles(servername)
output = ""
if serverroles == nil
next "us"
else
serverroles.split(/;/).each { |serverrole|
output += "<h2>"+servername + " : " + serverrole+"</h2>"
output += `NODENAME=#{params[:sname]} cd ../vagrants/#{serverrole} && NODENAME=#{params[:sname]} ./sanity-check.sh`.split("\n").join("<br>\n")
}
end
output
end
def serversummary(server)
serverroles = getServerroles(server)
output = ""
if (server == getOldestServer())
beard = "<img src=\"https://wiki.somedomain.com/download/attachments/134809333/beard.jpg\"/>"
end
if serverroles == nil
return "us"
else
serverroles.split(/;/).each { |serverrole|
system("cd ../vagrants/#{serverrole} && NODENAME=#{params[:sname]} ./sanity-check.sh")
result = $?.exitstatus
if (result == 0)
output += "<a href=\"/server/#{server}\" target=\"_blank\"><img src=\"https://wiki.somedomain.com/images/icons/emoticons/star_green.gif\"/></a>#{beard}"
elsif (result == 1)
output += "(*y)#{beard}"
elsif (result == 2)
output += "<a href=\"/server/#{server}\" target=\"_blank\"><img src=\"https://wiki.somedomain.com/images/icons/emoticons/star_red.gif\"/></a>#{beard}"
else
output += "?"
end
}
return output
end
end
def getServerroles(server)
print "search for #{server}"
mycommand = "echo \"db.nodes.findOne({fqdn : \\\"#{server}\\\"}).facts.serverroles\" | mongo mymongo.somedomain.com/puppet --quiet | head -1 "
print "command is #{mycommand}"
serverroles = `#{mycommand}`.chomp
if /TypeError/.match(serverroles)
return nil
end
return serverroles
end
# Simple caching-logic
$ts = Time.now
$oldestServer = nil
def getOldestServer()
if ((!($oldestServer)) || $ts+500 < Time.now )
mycommand = "echo \"db.nodes.mapReduce(function() {if (this.lastseen > Math.round(new Date().getTime()/1000.0)-65) emit( this.fqdn, parseInt(this.facts.uptime_seconds) )},function(k,vals) { return vals; }, {out:{inline:1}}).results.sort(function(a,b){return a.value - b.value}).pop()._id\" | mongo mymongo.somedomain.com/puppet --quiet | head -1 "
$oldestServer = `#{mycommand}`.chomp
$ts = Time.now
end
return $oldestServer
end
get '/environment/:sname/versions' do
environment = params[:sname]
print "lets search for environment #{environment}"
getVersions("{\"value.environment\":\"#{environment}\"}")
end
def getVersions(query)
mycommand = "mongo mymongo.somedomain.com/deployments --eval \'query=#{query}\' recentVersions.js | tail -1"
print mycommand
versions = `#{mycommand}`.chomp
return versions
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment