Module: OpsRoutes

Defined in:
lib/ops_routes.rb,
lib/ops_routes/middleware.rb

Defined Under Namespace

Classes: Middleware

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_nameObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/ops_routes.rb', line 110

def app_name
  @app_name ||= begin
    dirs = Dir.pwd.split('/')
    if dirs.last =~ /^\d+$/
      dirs[-3]
    else
      dirs.last
    end.sub(/\.com$/, '')
  end
end

.file_rootObject

Returns the value of attribute file_root.



8
9
10
# File 'lib/ops_routes.rb', line 8

def file_root
  @file_root
end

Class Method Details

.add_configuration_section(name, &block) ⇒ Object



139
140
141
# File 'lib/ops_routes.rb', line 139

def add_configuration_section(name, &block)
  configuration[name] = block
end

.add_heartbeat(name, &block) ⇒ Object



19
20
21
# File 'lib/ops_routes.rb', line 19

def add_heartbeat(name, &block)
  heartbeats[name] = block
end

.check_configurationObject



150
151
152
153
# File 'lib/ops_routes.rb', line 150

def check_configuration
  config_template = File.join(File.dirname(__FILE__), 'views', 'config.html.haml')
  Haml::Engine.new(File.read(config_template)).render(self)
end

.check_heartbeat(name = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ops_routes.rb', line 23

def check_heartbeat(name = nil)
  if name
    begin
      heartbeats[name.to_sym].call
      return { :status => 200, :text => "#{name} is OK" }
    rescue
      return { :status => 500, :text => "#{name} does not have a heartbeat" }
    end
  else
    return { :status => 200, :text => 'OK' }
  end
end

.check_version(headers = {}) ⇒ Object

Version page



38
39
40
41
42
# File 'lib/ops_routes.rb', line 38

def check_version(headers={})
  @headers = headers
  version_template = File.join(File.dirname(__FILE__), 'views', 'version.html.haml')
  Haml::Engine.new(File.read(version_template)).render(self)
end


129
130
131
# File 'lib/ops_routes.rb', line 129

def commit_link(commit)
  "https://github.com/primedia/#{app_name}/commit/#{commit}" unless commit =~ /^Unknown/
end

.config {|OpsRoutes| ... } ⇒ Object

Yields:



10
11
12
# File 'lib/ops_routes.rb', line 10

def config
  yield OpsRoutes if block_given?
end

.configurationObject

Configuration page



135
136
137
# File 'lib/ops_routes.rb', line 135

def configuration
  @configuration ||= {}
end

.current_configObject



143
144
145
146
147
148
# File 'lib/ops_routes.rb', line 143

def current_config
  configuration.inject({}) do |current, (section, block)|
    current[section] = block.call
    current
  end
end

.deploy_dateObject



82
83
84
85
86
87
88
89
90
# File 'lib/ops_routes.rb', line 82

def deploy_date
  @deploy_date ||= if File.exists?(version_file)
    File.stat(version_file).mtime
  elsif environment == 'development'
    'Live'
  else
    'Unknown (VERSION file is missing)'
  end
end

.environmentObject



78
79
80
# File 'lib/ops_routes.rb', line 78

def environment
  ENV['RAILS_ENV']
end

.headersObject



121
122
123
# File 'lib/ops_routes.rb', line 121

def headers
  @headers.select{|k,v| k.match(/^[-A-Z_].*$/) }
end

.heartbeatsObject

Heartbeats



15
16
17
# File 'lib/ops_routes.rb', line 15

def heartbeats
  @heartbeats ||= {}
end

.hostnameObject



106
107
108
# File 'lib/ops_routes.rb', line 106

def hostname
  @hostname ||= `/bin/hostname` || 'Unknown'
end

.last_commitObject



92
93
94
95
96
97
98
99
100
# File 'lib/ops_routes.rb', line 92

def last_commit
  @last_commit ||= if File.exists?(revision_file)
    File.read(revision_file).chomp
  elsif environment == 'development' && `git show` =~ /^commit (.*)$/
    $1
  else
    'Unknown (REVISION file is missing)'
  end
end

.previous_versionsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ops_routes.rb', line 54

def previous_versions
  return @previous_versions if @previous_versions
  @previous_versions = []
  dirs = Dir.pwd.split('/')
  if dirs.last =~ /^\d+$/
    Dir["../*"].each do |dir|
      next if dir =~ /#{dirs.last}$/
      version = File.join(dir, 'VERSION')
      revision = File.join(dir, 'REVISION')
      if File.exists?(version) && File.exists?(revision)
        @previous_versions << { :version => File.read(version).chomp.gsub('^{}', ''),
          :revision => File.read(revision).chomp,
          :time => File.stat(revision).mtime }
      end
    end
    @previous_versions.sort!{ |a, b| a[:time] <=> b[:time] }
  end
  @previous_versions
end


155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ops_routes.rb', line 155

def print_detail( object, indent = 0 )
  output = ""
  if object.kind_of? Hash
   output << "{\n"
   output << object.collect { |key, value|
     "  " * indent + "  #{print_detail key} => #{print_detail value, indent+1}"
   }.join(",\n") << "\n"
   output << "  " * indent + "}"
  else
   output << object.inspect
  end
  output
end

.revision_fileObject



102
103
104
# File 'lib/ops_routes.rb', line 102

def revision_file
  @revision_file ||= File.join(file_root, 'REVISION')
end

.version_fileObject



74
75
76
# File 'lib/ops_routes.rb', line 74

def version_file
  @version_file ||= File.join(file_root, 'VERSION')
end


125
126
127
# File 'lib/ops_routes.rb', line 125

def version_link(version)
  "https://github.com/primedia/#{app_name}/tree/#{version}" unless version_or_branch =~ /^Unknown/
end

.version_or_branchObject



44
45
46
47
48
49
50
51
52
# File 'lib/ops_routes.rb', line 44

def version_or_branch
  @version ||= if File.exists?(version_file)
    File.read(version_file).chomp.gsub('^{}', '')
  elsif environment == 'development' && `git branch` =~ /^\* (.*)$/
    $1
  else
    'Unknown (VERSION file is missing)'
  end
end