Created
April 15, 2010 21:19
-
-
Save jasonnoble/367675 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
# Ops middleware, provides /ops/version that gives you information about your deployed application | |
# Use: | |
# Dump this code in lib/middleware/ops.rb | |
# Add the following line to your environment.rb: | |
# config.middleware.use Rack::Ops | |
# | |
module Rack | |
class Ops | |
def initialize(app) | |
@app = app | |
yield self if block_given? | |
end | |
def call(env) | |
return @app.call(env) unless env['PATH_INFO'] =~ %r{^/ops/version/?$} | |
@status = 200 | |
@headers = {} | |
version | |
@body = version_template | |
@headers['Content-Type'] = "text/html" | |
@headers['Content-Length'] = @body.length.to_s | |
[@status, @headers, @body] | |
end | |
private | |
def version | |
begin | |
@version = ::File.read('./VERSION').chomp.gsub('^{}', '') | |
@deploy_date = ::File.stat('./VERSION').mtime | |
rescue Errno::ENOENT | |
ensure | |
@version ||= "Unknown (VERSION file is missing)" | |
@deploy_date ||= "Unknown (VERSION file is missing)" | |
end | |
begin | |
@git_version = ::File.read('./REVISION').chomp | |
rescue Errno::ENOENT | |
ensure | |
@git_version ||= "Unknown (REVISION file is missing)" | |
end | |
app_name | |
@hostname = %x(/bin/hostname) | |
@hostname = @hostname.blank? ? "Unknown" : @hostname | |
end | |
def app_name | |
@app_name ||= Dir.pwd.split('/').last | |
end | |
def headers | |
hh = @headers.select{|k,v| k.match(/^HTTP.*/) } | |
return if hh.blank? | |
ret = "<table border='1' style='width:100%'>" | |
hh.each { |k, v| ret += "<tr><td>#{k}</td><td>#{v}</td></tr>" } | |
ret += "</table>" | |
end | |
def version_link | |
return @version if @version =~ /^Unknown/ | |
"<a href='https://github.com/primedia/#{@app_name}/tree/#{@version}' style='font-size: 20px;'>#{@version}</a>" | |
end | |
def git_version_link | |
return @git_version if @git_version =~ /^Unknown/ | |
"<a href='https://github.com/primedia/#{@app_name}/commit/#{@git_version}' style='font-size: 20px;'>#{@git_version}</a>" | |
end | |
def version_template | |
<<-HTML | |
<div class='container' style=' width: 1000px;'> | |
<div class='spacer'></div> | |
<div id='version'> | |
<div class='label'>#{@app_name} Version</div> | |
<div class='value'>#{version_link}</div> | |
</div> | |
<div class='spacer'></div> | |
<div id='date'> | |
<div class='label'>Date Deployed:</div> | |
<div class='value'>#{@deploy_date}</div> | |
</div> | |
<div class='spacer'></div> | |
<div id='commit'> | |
<div class='label'> | |
Last Commit: | |
</div> | |
<div class='value'> | |
#{git_version_link} | |
</div> | |
</div> | |
<div class='spacer'></div> | |
<div id='host'> | |
<div class='label'> | |
Host: | |
</div> | |
<div class='value'> | |
#{@hostname} | |
</div> | |
</div> | |
<div class='spacer'></div> | |
<div id='headers'> | |
<div class='label'> | |
Headers: | |
</div> | |
<div class='value'> | |
#{headers} | |
</div> | |
</div> | |
<div class='spacer'></div> | |
<div id='environment'> | |
<div class='label'>Environment:</div> | |
<div class='value'>#{ENV['RAILS_ENV']}</div> | |
</div> | |
</div> | |
<style> | |
.container {font-size: 20px;} | |
.label {float: left; width: 400px;} | |
.spacer {clear: both; padding: 10px;} | |
.value {float: left; width: 500px; text-align: right;} | |
.clear {clear: both;} | |
</style> | |
HTML | |
end | |
end | |
end |
I believe rack middle ware was added in 2.3.4 or 2.3.5. I know this works with 2.3.5.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great script! I like the idea, but what version of rails is this good for?