Module: Statusz

Defined in:
lib/statusz.rb,
lib/statusz/version.rb

Overview

Statusz is a tool for displaying deploy-time and runtime server information.

Defined Under Namespace

Classes: Server

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.render_from_json(filename = "./statusz.json", format = :html, extra_fields = {}) ⇒ Object

If you wrote out a json file at deploy time, you can use this at runtime to turn the json file into any of statusz's supported formats (html, json, text) and add additional runtime values.

Parameters:

  • filename (String) (defaults to: "./statusz.json")

    the json statusz file written at deploy time. If filename is nil, then statusz will output an html file containing only the fields in extra_fields.

  • format (Symbol) (defaults to: :html)

    then output format (one of :html, :json, :text). Defaults to :html.

  • extra_fields (Hash) (defaults to: {})

    the extra key/value pairs to include in the output.



102
103
104
105
106
107
# File 'lib/statusz.rb', line 102

def self.render_from_json(filename = "./statusz.json", format = :html, extra_fields = {})
  raise "Bad format: #{format}" unless [:html, :text, :json].include? format
  fields = load_json_info(filename)
  fields.merge! extra_fields
  render(fields, format)
end

.write_file(filename = "./statusz.html", options = {}) ⇒ Object

Write out a statusz file. This should be done at deployment time.

Parameters:

  • filename (String) (defaults to: "./statusz.html")

    the output filename.

  • options (Hash) (defaults to: {})

    the options for the output.

Options Hash (options):

  • :commit (String)

    The git commit for which to to output deploy information (default: HEAD).

  • :format (Symbol)

    The output format (one of :html, :text, or :json). Default: `:html

  • :fields (Array)

    The fields to include in the output. Default: all fields.

  • :extra_fields (Hash)

    A hash of extra key/value pairs to include in the output.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/statusz.rb', line 32

def self.write_file(filename = "./statusz.html", options = {})
  options[:commit] ||= "HEAD"
  options[:format] ||= :html
  raise "Bad format: #{options[:format]}" unless [:html, :text, :json].include? options[:format]
  options[:fields] ||= FIELD_TO_SCRAPING_PROC.keys
  bad_options = options[:fields] - FIELD_TO_SCRAPING_PROC.keys
  raise "Bad options: #{bad_options.inspect}" unless bad_options.empty?
  extra_fields = options[:extra_fields] || {}
  unless extra_fields.is_a? Hash
    raise "Extra fields should be a hash, but #{extra_fields.inspect} (#{extra_fields.class}) was given."
  end

  results = {}
  options[:fields].each do |field|
    results[field] = FIELD_TO_SCRAPING_PROC[field].call(options[:commit])
  end
  extra_fields.each { |field, value| results[field.to_s] = value.to_s }

  File.open(filename, "w") { |file| file.puts(render(results, options[:format])) }
end