health_check 0.2.4 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +18 -0
- data/Gemfile.lock +34 -0
- data/MIT-LICENSE +1 -1
- data/README.rdoc +38 -16
- data/Rakefile +48 -29
- data/VERSION +1 -1
- data/config/routes.rb +4 -11
- data/health_check.gemspec +50 -47
- data/lib/health_check.rb +16 -4
- data/lib/health_check/add_23_routes.rb +5 -2
- data/lib/health_check/health_check_controller.rb +85 -56
- data/lib/health_check/utils.rb +103 -0
- data/test/migrate/nine/9_create_countries.rb +11 -0
- data/test/migrate/twelve/012_create_users.rb +2 -1
- data/test/{helper.rb → test_helper.rb} +0 -1
- data/test/test_with_railsapp +164 -0
- data/test/testurl +33 -0
- data/test/{test_health_check_controller.rb → unit/health_check_controller_test.rb} +31 -31
- data/test/{test_routes.rb → unit/routes_test.rb} +1 -1
- metadata +92 -42
- data/.gitignore +0 -21
- data/lib/health_check/add_3x_routes.rb +0 -13
- data/lib/health_check/health_check_class.rb +0 -99
@@ -1,70 +1,99 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
layout nil
|
1
|
+
# Copyright (c) 2010-2013 Ian Heggie, released under the MIT license.
|
2
|
+
# See MIT-LICENSE for details.
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
end
|
4
|
+
module HealthCheck
|
5
|
+
class HealthCheckController < ActionController::Base
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
7
|
+
# turn sessions off if we can
|
8
|
+
session(:off) rescue nil
|
12
9
|
|
13
|
-
|
10
|
+
layout nil
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
errors = process_checks(checks)
|
18
|
-
rescue Exception => e
|
19
|
-
errors = e.message
|
20
|
-
end
|
21
|
-
if errors.blank?
|
22
|
-
render :text => HealthCheck.success, :content_type => 'text/plain'
|
23
|
-
else
|
24
|
-
msg = "health_check failed: #{errors}"
|
25
|
-
render :text => msg, :status => 500, :content_type => 'text/plain'
|
26
|
-
# Log a single line as some uptime checkers only record that it failed, not the text returned
|
27
|
-
silence_level, logger.level = logger.level, @old_logger_level
|
28
|
-
logger.info msg
|
29
|
-
logger.level = silence_level
|
12
|
+
def index
|
13
|
+
do_check('standard')
|
30
14
|
end
|
31
|
-
end
|
32
15
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
errors << "Current database version (#{database_version}) does not match latest migration (#{migration_version}). "
|
48
|
-
end
|
49
|
-
when "standard"
|
50
|
-
errors << process_checks("database_migrations")
|
51
|
-
errors << process_checks("email") unless HealthCheck.default_action_mailer_configuration?
|
52
|
-
when "all", "full"
|
53
|
-
errors << process_checks("database_migrations_email")
|
16
|
+
def check
|
17
|
+
do_check(params[:checks])
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def do_check(checks)
|
23
|
+
begin
|
24
|
+
errors = process_checks(checks)
|
25
|
+
rescue Exception => e
|
26
|
+
errors = e.message
|
27
|
+
end
|
28
|
+
if errors.blank?
|
29
|
+
render :text => HealthCheck::Utils.success, :content_type => 'text/plain'
|
54
30
|
else
|
55
|
-
|
31
|
+
msg = "health_check failed: #{errors}"
|
32
|
+
render :text => msg, :status => 500, :content_type => 'text/plain'
|
33
|
+
# Log a single line as some uptime checkers only record that it failed, not the text returned
|
34
|
+
if logger
|
35
|
+
silence_level, logger.level = logger.level, @old_logger_level
|
36
|
+
logger.info msg
|
37
|
+
logger.level = silence_level
|
38
|
+
end
|
56
39
|
end
|
57
40
|
end
|
58
|
-
return errors
|
59
|
-
end
|
60
41
|
|
42
|
+
def process_checks(checks)
|
43
|
+
errors = ''
|
44
|
+
checks.split('_').each do |check|
|
45
|
+
case check
|
46
|
+
when 'and', 'site'
|
47
|
+
# do nothing
|
48
|
+
when "database"
|
49
|
+
HealthCheck::Utils.get_database_version
|
50
|
+
when "email"
|
51
|
+
errors << HealthCheck::Utils.check_email
|
52
|
+
when "migrations", "migration"
|
53
|
+
database_version = HealthCheck::Utils.get_database_version
|
54
|
+
migration_version = HealthCheck::Utils.get_migration_version
|
55
|
+
if database_version.to_i != migration_version.to_i
|
56
|
+
errors << "Current database version (#{database_version}) does not match latest migration (#{migration_version}). "
|
57
|
+
end
|
58
|
+
when "standard"
|
59
|
+
errors << process_checks("database_migrations")
|
60
|
+
errors << process_checks("email") unless HealthCheck::Utils.default_action_mailer_configuration?
|
61
|
+
when "all", "full"
|
62
|
+
errors << process_checks("database_migrations_email")
|
63
|
+
else
|
64
|
+
return "invalid argument to health_test. "
|
65
|
+
end
|
66
|
+
end
|
67
|
+
return errors
|
68
|
+
end
|
69
|
+
|
70
|
+
protected
|
61
71
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
process_without_silence(*args)
|
72
|
+
# turn cookies for CSRF off
|
73
|
+
def protect_against_forgery?
|
74
|
+
false
|
66
75
|
end
|
67
|
-
end
|
68
76
|
|
69
|
-
|
77
|
+
# Silence logger as much as we can
|
78
|
+
|
79
|
+
def process_with_silent_log(method_name, *args)
|
80
|
+
if logger
|
81
|
+
@old_logger_level = logger.level
|
82
|
+
if Rails.version >= '3.2'
|
83
|
+
silence do
|
84
|
+
process_without_silent_log(method_name, *args)
|
85
|
+
end
|
86
|
+
else
|
87
|
+
logger.silence do
|
88
|
+
process_without_silent_log(method_name, *args)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
else
|
92
|
+
process_without_silent_log(method_name, *args)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
alias_method_chain :process, :silent_log
|
97
|
+
|
98
|
+
end
|
70
99
|
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# Copyright (c) 2010-2013 Ian Heggie, released under the MIT license.
|
2
|
+
# See MIT-LICENSE for details.
|
3
|
+
|
4
|
+
module HealthCheck
|
5
|
+
class Utils
|
6
|
+
|
7
|
+
@@success = "success"
|
8
|
+
|
9
|
+
cattr_accessor :success
|
10
|
+
|
11
|
+
@@smtp_timeout = 30.0
|
12
|
+
|
13
|
+
cattr_accessor :smtp_timeout
|
14
|
+
|
15
|
+
@@default_smtp_settings =
|
16
|
+
{
|
17
|
+
:address => "localhost",
|
18
|
+
:port => 25,
|
19
|
+
:domain => 'localhost.localdomain',
|
20
|
+
:user_name => nil,
|
21
|
+
:password => nil,
|
22
|
+
:authentication => nil,
|
23
|
+
:enable_starttls_auto => true,
|
24
|
+
}
|
25
|
+
|
26
|
+
cattr_accessor :default_smtp_settings
|
27
|
+
|
28
|
+
def self.db_migrate_path
|
29
|
+
# Lazy initialisation so Rails.root will be defined
|
30
|
+
@@db_migrate_path ||= File.join(Rails.root, 'db', 'migrate')
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.db_migrate_path=(value)
|
34
|
+
@@db_migrate_path = value
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.default_action_mailer_configuration?
|
38
|
+
ActionMailer::Base.delivery_method == :smtp && HealthCheck::Utils.default_smtp_settings == ActionMailer::Base.smtp_settings
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.get_database_version
|
42
|
+
ActiveRecord::Migrator.current_version
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.get_migration_version(dir = self.db_migrate_path)
|
46
|
+
latest_migration = nil
|
47
|
+
Dir[File.join(dir, "[0-9]*_*.rb")].each do |f|
|
48
|
+
l = f.scan(/0*([0-9]+)_[_a-zA-Z0-9]*.rb/).first.first
|
49
|
+
latest_migration = l if !latest_migration || l.to_i > latest_migration.to_i
|
50
|
+
end
|
51
|
+
latest_migration
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.check_email
|
55
|
+
case ActionMailer::Base.delivery_method
|
56
|
+
when :smtp
|
57
|
+
HealthCheck::Utils.check_smtp(ActionMailer::Base.smtp_settings, HealthCheck::Utils.smtp_timeout)
|
58
|
+
when :sendmail
|
59
|
+
HealthCheck::Utils.check_sendmail(ActionMailer::Base.sendmail_settings)
|
60
|
+
else
|
61
|
+
''
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
def self.check_sendmail(settings)
|
67
|
+
File.executable?(settings[:location]) ? '' : 'no sendmail executable found. '
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.check_smtp(settings, timeout)
|
71
|
+
status = ''
|
72
|
+
begin
|
73
|
+
if @skip_external_checks
|
74
|
+
status = '221'
|
75
|
+
else
|
76
|
+
Timeout::timeout(timeout) do |timeout_length|
|
77
|
+
t = TCPSocket.new(settings[:address], settings[:port])
|
78
|
+
begin
|
79
|
+
status = t.gets
|
80
|
+
while status != nil && status !~ /^2/
|
81
|
+
status = t.gets
|
82
|
+
end
|
83
|
+
t.puts "HELO #{settings[:domain]}"
|
84
|
+
while status != nil && status !~ /^250/
|
85
|
+
status = t.gets
|
86
|
+
end
|
87
|
+
t.puts "QUIT"
|
88
|
+
status = t.gets
|
89
|
+
ensure
|
90
|
+
t.close
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
rescue Errno::EBADF => ex
|
95
|
+
status = "Unable to connect to service"
|
96
|
+
rescue Exception => ex
|
97
|
+
status = ex.to_s
|
98
|
+
end
|
99
|
+
(status =~ /^221/) ? '' : "SMTP: #{status || 'unexpected EOF on socket'}. "
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
# Any failure causes exit
|
4
|
+
set -e
|
5
|
+
|
6
|
+
base_dir=`pwd`
|
7
|
+
|
8
|
+
testurl=$base_dir/test/testurl
|
9
|
+
|
10
|
+
test_dir=`pwd`/tmp
|
11
|
+
rm -rf tmp/Gemfile* tmp/railsapp tmp/bin tmp/gems
|
12
|
+
mkdir -p tmp/gems
|
13
|
+
|
14
|
+
echo Installing health_check as a gem into tmp/gems
|
15
|
+
env GEM_HOME=$test_dir/gems rake install
|
16
|
+
|
17
|
+
export GEM_PATH="$test_dir/gems:`gem environment gempath`"
|
18
|
+
echo Set GEM_PATH=$GEM_PATH
|
19
|
+
|
20
|
+
echo Gems in tmp/gems:
|
21
|
+
ls tmp/gems
|
22
|
+
|
23
|
+
echo GEM LIST
|
24
|
+
gem list
|
25
|
+
|
26
|
+
echo Environment:
|
27
|
+
env | egrep 'TRAVIS|RAILS|RUBY|_ENV'
|
28
|
+
|
29
|
+
server_pid=''
|
30
|
+
|
31
|
+
finish()
|
32
|
+
{
|
33
|
+
set +e
|
34
|
+
set +x
|
35
|
+
echo ========================================================
|
36
|
+
echo TEST ${1:-FAILED}
|
37
|
+
echo ========================================================
|
38
|
+
if [ -s $test_dir/railsapp/log/test.log ]
|
39
|
+
then
|
40
|
+
echo Last 300 lines of test log
|
41
|
+
tail -300 $test_dir/railsapp/log/test.log
|
42
|
+
fi
|
43
|
+
case "$server_pid" in
|
44
|
+
[0-9]*)
|
45
|
+
echo "Killing rails server [pid: $server_pid]"
|
46
|
+
kill -9 $server_pid
|
47
|
+
wait
|
48
|
+
;;
|
49
|
+
esac
|
50
|
+
trap "" 0
|
51
|
+
exit ${2:-2}
|
52
|
+
}
|
53
|
+
|
54
|
+
trap "finish FAILED 1" 0
|
55
|
+
|
56
|
+
actual_rails_version=`rails -v`
|
57
|
+
echo "Creating $actual_rails_version app in $test_dir/railsapp"
|
58
|
+
|
59
|
+
cd $test_dir
|
60
|
+
|
61
|
+
case "$actual_rails_version" in
|
62
|
+
*' '[12].*)
|
63
|
+
rails railsapp
|
64
|
+
;;
|
65
|
+
*' '[34].*)
|
66
|
+
rails new railsapp
|
67
|
+
;;
|
68
|
+
*)
|
69
|
+
echo "Unknown rails version"
|
70
|
+
;;
|
71
|
+
esac
|
72
|
+
|
73
|
+
cd railsapp
|
74
|
+
echo "Changed current directory to railsapp root: `pwd`"
|
75
|
+
|
76
|
+
echo "Fixing rdoc require in Rakefile if needed"
|
77
|
+
ruby -pi.bak -e "gsub(/rake.rdoctask/, 'rdoc/task')" Rakefile
|
78
|
+
|
79
|
+
if [ -f Gemfile ]
|
80
|
+
then
|
81
|
+
echo Installing health_check as gem ...
|
82
|
+
echo "gem 'health_check', :path => '$base_dir'" >> Gemfile
|
83
|
+
case "$BUNDLE_GEMFILE" in
|
84
|
+
?*)
|
85
|
+
echo Unsetting BUNDLE_GEMFILE '(so Gemfile will be used)'
|
86
|
+
unset BUNDLE_GEMFILE
|
87
|
+
;;
|
88
|
+
esac
|
89
|
+
bundle
|
90
|
+
else
|
91
|
+
echo Installing health_check as plugin ...
|
92
|
+
dest=`pwd`/vendor/plugins/health_check
|
93
|
+
mkdir -p $dest
|
94
|
+
(
|
95
|
+
cd $base_dir
|
96
|
+
find . | egrep -v '/tmp|/\.git|\.gem$' | cpio -pdl $dest
|
97
|
+
)
|
98
|
+
fi
|
99
|
+
|
100
|
+
port=3456
|
101
|
+
echo Starting server on port $port ...
|
102
|
+
|
103
|
+
export RAILS_ENV=test
|
104
|
+
|
105
|
+
if [ -x script/server ]
|
106
|
+
then
|
107
|
+
script/server webrick -p $port &
|
108
|
+
else
|
109
|
+
rails s webrick -p $port &
|
110
|
+
fi
|
111
|
+
server_pid=$!
|
112
|
+
|
113
|
+
echo STATIC-FILE > public/static.txt
|
114
|
+
|
115
|
+
host=http://localhost:${port}
|
116
|
+
|
117
|
+
# get a static file
|
118
|
+
echo
|
119
|
+
echo 'TESTING can get a static file ...'
|
120
|
+
$testurl ${host}/static.txt STATIC-FILE
|
121
|
+
echo
|
122
|
+
|
123
|
+
echo 'TESTING health_check should pass with no database migrations ...'
|
124
|
+
mkdir -p db/migrate
|
125
|
+
ls db/migrate
|
126
|
+
$testurl ${host}/health_check success
|
127
|
+
echo
|
128
|
+
|
129
|
+
echo 'TESTING health_check should fail without initial database migration ...'
|
130
|
+
cp $base_dir/test/migrate/nine/* db/migrate
|
131
|
+
ls db/migrate
|
132
|
+
$testurl ${host}/health_check failed
|
133
|
+
echo
|
134
|
+
|
135
|
+
echo 'TESTING health_check should pass after initial database migration ...'
|
136
|
+
rake db:migrate
|
137
|
+
$testurl ${host}/health_check success
|
138
|
+
echo
|
139
|
+
|
140
|
+
echo 'TESTING health_check should fail without all migrations ...'
|
141
|
+
cp $base_dir/test/migrate/twelve/* db/migrate
|
142
|
+
ls db/migrate
|
143
|
+
$testurl ${host}/health_check failed
|
144
|
+
echo
|
145
|
+
|
146
|
+
echo 'TESTING health_check should pass after both database migrations ...'
|
147
|
+
rake db:migrate
|
148
|
+
$testurl ${host}/health_check success
|
149
|
+
echo
|
150
|
+
|
151
|
+
echo "Killing rails server [pid: $server_pid]"
|
152
|
+
kill -9 $server_pid
|
153
|
+
server_pid=''
|
154
|
+
|
155
|
+
#if [ -d vendor/plugins/health_check ]
|
156
|
+
#then
|
157
|
+
# echo "RUNNING UNIT TESTS...."
|
158
|
+
# rake test:plugins
|
159
|
+
#fi
|
160
|
+
|
161
|
+
finish PASSED 0
|
162
|
+
exit 0
|
163
|
+
|
164
|
+
# vi: sw=4 ai sm:
|