turnout 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ require 'rack'
2
+ require 'yaml'
3
+ require 'ipaddr'
4
+ require 'nokogiri'
5
+
6
+ class Rack::Turnout
7
+
8
+ def initialize(app, config={})
9
+ @app = app
10
+ @config = config
11
+ end
12
+
13
+ def call(env)
14
+ reload_settings
15
+
16
+ if on?(env)
17
+ [ 503, { 'Content-Type' => 'text/html', 'Content-Length' => content_length }, [content] ]
18
+ else
19
+ @app.call(env)
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ def on?(env)
26
+ request = Rack::Request.new(env)
27
+
28
+ return false if ip_allowed?(request.ip)
29
+ File.exists? settings_file
30
+ end
31
+
32
+ def ip_allowed?(ip)
33
+ ip = IPAddr.new(ip) unless ip.is_a? IPAddr
34
+ (settings['allowed_ips'] || []).each do |allowed_ip|
35
+ return true if IPAddr.new(allowed_ip).include? ip
36
+ end
37
+ false
38
+ end
39
+
40
+ def reload_settings
41
+ @settings = nil
42
+ settings
43
+ end
44
+
45
+ def settings
46
+ @settings ||= if File.exists? settings_file
47
+ YAML::load(File.open(settings_file)) || {}
48
+ else
49
+ {}
50
+ end
51
+ end
52
+
53
+ def app_root
54
+ @app_root ||= Pathname.new(
55
+ @config[:app_root] || @app.respond_to?(:root)? @app.root.to_s : '.'
56
+ )
57
+ end
58
+
59
+ def settings_file
60
+ app_root.join('tmp', 'maintenance.yml')
61
+ end
62
+
63
+ def maintenance_page
64
+ File.exists?(app_maintenance_page) ? app_maintenance_page : default_maintenance_page
65
+ end
66
+
67
+ def app_maintenance_page
68
+ @app_maintenance_page ||= app_root.join('public', 'maintenance.html')
69
+ end
70
+
71
+ def default_maintenance_page
72
+ @default_maintenance_page ||= File.expand_path('../../../public/maintenance.html', __FILE__)
73
+ end
74
+
75
+ def content_length
76
+ content.size.to_s
77
+ end
78
+
79
+ def content
80
+ content = File.open(maintenance_page, 'rb').read
81
+
82
+ if settings['reason']
83
+ html = Nokogiri::HTML(content)
84
+ html.at_css('#reason').inner_html = settings['reason']
85
+ content = html.to_s
86
+ end
87
+
88
+ content
89
+ end
90
+
91
+ end
@@ -0,0 +1,21 @@
1
+ namespace :maintenance do
2
+
3
+ desc 'Enable the maintenance mode page ("reason" and/or "allowed_ips" can be passed as environment variables)'
4
+ task :start do |t, args|
5
+ settings = { 'reason' => ENV['reason'], 'allowed_ips' => ENV['allowed_ips'].to_s.split(',') }
6
+
7
+ file = File.open settings_file, 'w'
8
+ file.write settings.to_yaml
9
+ file.close
10
+ end
11
+
12
+ desc 'Disable the maintenance mode page'
13
+ task :end do
14
+ File.delete settings_file
15
+ end
16
+
17
+ def settings_file
18
+ Rails.root.join('tmp', 'maintenance.yml')
19
+ end
20
+
21
+ end
data/lib/turnout.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Turnout
2
+ require 'turnout/engine' if defined?(Rails)
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'turnout'
2
+ require 'rails'
3
+ require 'active_record'
4
+ require 'rack/turnout'
5
+
6
+ module Turnout
7
+ class Engine < Rails::Engine
8
+ initializer 'turnout.add_to_middleware_stack' do |app|
9
+ app.config.middleware.use Rack::Turnout
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Turnout
2
+
3
+ VERSION = '0.1.0'
4
+
5
+ end
@@ -0,0 +1,69 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Down for Maintenance</title>
5
+
6
+ <style type="text/css">
7
+
8
+ *{
9
+ font-family: Arial, Helvetica, sans-serif;
10
+ }
11
+
12
+ body{
13
+ margin: 0;
14
+ background-color: #fff;
15
+ }
16
+
17
+ #page{
18
+ position: relative;
19
+ width: 550px;
20
+ margin: 200px auto;
21
+ padding: 75px 0;
22
+ text-align: center;
23
+ background-color: #eaeaea;
24
+ border: solid 1px #ccc;
25
+ border-top: solid 10px #666;
26
+ -moz-box-shadow: inset 0 2px 10px #ccc;
27
+ -webkit-box-shadow: inset 0 2px 10px #ccc;
28
+ box-shadow: inset 0 2px 10px #ccc;
29
+ }
30
+
31
+ header, #body{
32
+ width: 400px;
33
+ margin: 0 auto;
34
+ }
35
+
36
+ h1{
37
+ margin: 0;
38
+ color: #CC3601;
39
+ font-size: 26pt;
40
+ border-bottom: solid 4px #666;
41
+ }
42
+
43
+ #reason{
44
+ margin: 10px 0;
45
+ color: #333;
46
+ }
47
+
48
+ </style>
49
+
50
+ </head>
51
+ <body>
52
+
53
+ <section id="page">
54
+
55
+ <header>
56
+ <h1>Down for Maintenance</h1>
57
+ </header>
58
+
59
+ <section id="body">
60
+ <div id="reason">
61
+ <p>The site is temporarily down for maintenance.</p>
62
+ <p>Please check back soon.</p>
63
+ </div>
64
+ </section>
65
+
66
+ </section>
67
+
68
+ </body>
69
+ </html>
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turnout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Crownoble
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-15 00:00:00.000000000 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ requirement: &86563140 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *86563140
26
+ description: Turnout makes it easy to put your Rails application into maintenance
27
+ mode
28
+ email: adam.crownoble@biola.edu
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/turnout.rb
34
+ - lib/rack/turnout.rb
35
+ - lib/turnout/version.rb
36
+ - lib/turnout/engine.rb
37
+ - lib/tasks/maintenance.rake
38
+ - public/maintenance.html
39
+ has_rdoc: true
40
+ homepage: https://github.com/biola/turnout
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.6.2
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: A Rack based maintenance mode plugin for Rails
64
+ test_files: []