health_check 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,30 +4,54 @@
4
4
  module HealthCheck
5
5
  class Utils
6
6
 
7
- @@success = "success"
8
-
9
- cattr_accessor :success
10
-
11
- @@smtp_timeout = 30.0
12
-
13
- cattr_accessor :smtp_timeout
7
+ @@default_smtp_settings =
8
+ {
9
+ :address => "localhost",
10
+ :port => 25,
11
+ :domain => 'localhost.localdomain',
12
+ :user_name => nil,
13
+ :password => nil,
14
+ :authentication => nil,
15
+ :enable_starttls_auto => true,
16
+ }
14
17
 
15
- @@error_status_code = 500
18
+ cattr_accessor :default_smtp_settings
16
19
 
17
- cattr_accessor :error_status_code
18
20
 
19
- @@default_smtp_settings =
20
- {
21
- :address => "localhost",
22
- :port => 25,
23
- :domain => 'localhost.localdomain',
24
- :user_name => nil,
25
- :password => nil,
26
- :authentication => nil,
27
- :enable_starttls_auto => true,
28
- }
21
+ def self.process_checks(checks)
22
+ errors = ''
23
+ checks.split('_').each do |check|
24
+ case check
25
+ when 'and', 'site'
26
+ # do nothing
27
+ when "database"
28
+ HealthCheck::Utils.get_database_version
29
+ when "email"
30
+ errors << HealthCheck::Utils.check_email
31
+ when "migrations", "migration"
32
+ database_version = HealthCheck::Utils.get_database_version
33
+ migration_version = HealthCheck::Utils.get_migration_version
34
+ if database_version.to_i != migration_version.to_i
35
+ errors << "Current database version (#{database_version}) does not match latest migration (#{migration_version}). "
36
+ end
37
+ when 'cache'
38
+ errors << HealthCheck::Utils.check_cache
39
+ when "standard"
40
+ errors << HealthCheck::Utils.process_checks("database_migrations_custom")
41
+ errors << HealthCheck::Utils.process_checks("email") unless HealthCheck::Utils.default_action_mailer_configuration?
42
+ when "custom"
43
+ HealthCheck.custom_checks.each do |custom_check|
44
+ errors << custom_check.call(self)
45
+ end
46
+ when "all", "full"
47
+ errors << HealthCheck::Utils.process_checks("database_migrations_custom_email_cache")
48
+ else
49
+ return "invalid argument to health_test. "
50
+ end
51
+ end
52
+ return errors
53
+ end
29
54
 
30
- cattr_accessor :default_smtp_settings
31
55
 
32
56
  def self.db_migrate_path
33
57
  # Lazy initialisation so Rails.root will be defined
@@ -57,12 +81,12 @@ module HealthCheck
57
81
 
58
82
  def self.check_email
59
83
  case ActionMailer::Base.delivery_method
60
- when :smtp
61
- HealthCheck::Utils.check_smtp(ActionMailer::Base.smtp_settings, HealthCheck::Utils.smtp_timeout)
62
- when :sendmail
63
- HealthCheck::Utils.check_sendmail(ActionMailer::Base.sendmail_settings)
64
- else
65
- ''
84
+ when :smtp
85
+ HealthCheck::Utils.check_smtp(ActionMailer::Base.smtp_settings, HealthCheck.smtp_timeout)
86
+ when :sendmail
87
+ HealthCheck::Utils.check_sendmail(ActionMailer::Base.sendmail_settings)
88
+ else
89
+ ''
66
90
  end
67
91
  end
68
92
 
@@ -95,7 +119,7 @@ module HealthCheck
95
119
  end
96
120
  end
97
121
  end
98
- rescue Errno::EBADF => ex
122
+ rescue Errno::EBADF => ex
99
123
  status = "Unable to connect to service"
100
124
  rescue Exception => ex
101
125
  status = ex.to_s
@@ -1,4 +1,4 @@
1
1
  module HealthCheck
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.0"
3
3
  end
4
4
 
@@ -0,0 +1,60 @@
1
+ #!/bin/bash
2
+
3
+ # Any failure causes exit
4
+ set -e
5
+
6
+ echo Setting RAILS_ENV=test RACK_ENV=test
7
+ export RAILS_ENV=test RACK_ENV=test
8
+
9
+ base_dir=$PWD
10
+ tmp_dir=$base_dir/tmp
11
+ railsapp=$tmp_dir/railsapp
12
+ custom_file="$railsapp/tmp/custom_check.ok"
13
+ success=successful
14
+
15
+ rehash=''
16
+ rbenv_which='which'
17
+
18
+ if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
19
+ echo "Detected user installed rvm"
20
+ elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
21
+ echo "Detected root installed rvm"
22
+ elif [[ -d "$HOME/.rbenv" ]] ; then
23
+ echo "Detected rbenv: `rbenv version`"
24
+ rehash='rbenv rehash'
25
+ rbenv_which='rbenv which'
26
+ else
27
+ printf "Note: Neither rvm nor rbenv was not found.\n"
28
+ fi
29
+
30
+ echo "Checking required commands exist:"
31
+ for cmd in bash gem egrep ls tail kill find cpio
32
+ do
33
+ echo -n " "
34
+ which $cmd || ( echo "Aborting setup_railsapp: Missing $cmd command!" && exit 2 )
35
+ done
36
+ for cmd in ruby gem
37
+ do
38
+ echo -n " "
39
+ $rbenv_which $cmd || ( echo "Aborting setup_railsapp: Missing $cmd command!" && exit 2 )
40
+ done
41
+
42
+ rails="rails"
43
+ rake="rake"
44
+
45
+ if [ -x $base_dir/test/bin/rails ]
46
+ then
47
+ rails="$base_dir/test/bin/rails"
48
+ rake="$base_dir/test/bin/rake"
49
+ export PATH="$base_dir/test/bin:$PATH"
50
+ fi
51
+
52
+ if [ -x $railsapp/bin/rails ]
53
+ then
54
+ rails="$railsapp/bin/rails"
55
+ rake="$railsapp/bin/rake"
56
+ export PATH="$railsapp/bin:$PATH"
57
+ fi
58
+
59
+ echo "Using rails=$rails, rake=$rake"
60
+
@@ -2,9 +2,17 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
+ # Last rubygems version before 2.0.0 (which breaks rails 2.3)
6
+ #rubygems 1.8.25
7
+
8
+ ruby "1.8.7"
9
+
5
10
  gem 'rails', "~> 2.3.15"
6
11
  # rails requires rake >= 0.8.3
7
12
 
13
+ gem 'rdoc'
14
+ gem 'rdoc-data'
15
+
8
16
  group :development, :test do
9
17
  if defined?(JRUBY_VERSION)
10
18
  gem 'jruby-openssl'
@@ -12,6 +20,5 @@ group :development, :test do
12
20
  else
13
21
  gem 'sqlite3', "~> 1.3.7"
14
22
  end
15
- gem 'jeweler', '~> 1.8.4'
16
23
  gem 'shoulda', "~> 2.11.0"
17
24
  end
@@ -12,6 +12,5 @@ group :development, :test do
12
12
  else
13
13
  gem 'sqlite3', "~> 1.3.7"
14
14
  end
15
- gem 'jeweler', '~> 1.8.4'
16
15
  gem 'shoulda', "~> 2.11.0"
17
16
  end
@@ -12,6 +12,5 @@ group :development, :test do
12
12
  else
13
13
  gem 'sqlite3', "~> 1.3.7"
14
14
  end
15
- gem 'jeweler', '~> 1.8.4'
16
15
  gem 'shoulda', "~> 2.11.0"
17
16
  end
@@ -12,6 +12,5 @@ group :development, :test do
12
12
  else
13
13
  gem 'sqlite3', "~> 1.3.7"
14
14
  end
15
- gem 'jeweler', '~> 1.8.4'
16
15
  gem 'shoulda', "~> 2.11.0"
17
16
  end
@@ -4,11 +4,11 @@ source 'https://rubygems.org'
4
4
 
5
5
  # Bundle edge Rails instead:
6
6
 
7
- gem 'rails', :git => 'git://github.com/rails/rails.git'
7
+ ruby "1.9.3" if RUBY_VERSION < '1.9'
8
8
 
9
- gem 'arel', :git => 'git://github.com/rails/arel.git'
10
-
11
- gem "rack", '~> 1.4.3'
9
+ gem 'rails', "~> 4.0.0.beta1"
10
+ gem 'rake', '>= 0.8.3'
11
+ gem "rack", '~> 1.5.2'
12
12
 
13
13
  group :development, :test do
14
14
  if defined?(JRUBY_VERSION)
@@ -17,6 +17,5 @@ group :development, :test do
17
17
  else
18
18
  gem 'sqlite3', "~> 1.3.7"
19
19
  end
20
- gem 'jeweler'
21
20
  gem 'shoulda'
22
21
  end
@@ -0,0 +1,258 @@
1
+ #!/bin/bash
2
+
3
+ # Any failure causes exit
4
+ set -e
5
+
6
+ while :
7
+ do
8
+ case "$BUNDLE_GEMFILE" in
9
+ */test/rails_[Qq].gemfile)
10
+ echo "Aborting..."
11
+ exit 2
12
+ ;;
13
+ */test/rails_[0-9].[0-9]*.gemfile)
14
+ if [ -f "$BUNDLE_GEMFILE" ]; then
15
+ break
16
+ fi
17
+ ;;
18
+ esac
19
+ echo "== SELECT GEMFILE =="
20
+ echo
21
+ echo "Please select the gemfile for the required rails series:"
22
+ (cd test ; ls rails*gemfile | ruby -p -e '$_.sub!(/rails_(.*).gemfile/, " \\1")' )
23
+ echo
24
+ echo -n "Enter choice (or q to quit): "
25
+ read x
26
+ export BUNDLE_GEMFILE=$PWD/test/rails_$x.gemfile
27
+ done
28
+
29
+ rm -rf tmp/Gemfile* tmp/railsapp tmp/bin tmp/gems test/bin test/rails*.gemfile.lock
30
+
31
+ mkdir -p tmp/gems
32
+
33
+ . test/init_variables
34
+
35
+ if $rbenv_which bundle ; then
36
+ echo Bundler is installed
37
+ else
38
+ gem install bundler
39
+ $rehash
40
+ fi
41
+
42
+ echo "Running bundle --binstubs with BUNDLE_GEMFILE=$BUNDLE_GEMFILE ..."
43
+ if ! bundle --binstubs ; then
44
+ echo "Test aborted (missing required gems)"
45
+ exit 2
46
+ fi
47
+ $rehash
48
+
49
+ rails="$base_dir/test/bin/rails"
50
+ rake="$base_dir/test/bin/rake"
51
+
52
+ actual_rails_version=`$rails -v`
53
+
54
+ [ -d lib/health_check ] || exec echo setup_railsapp MUST be executed in the base of the health_check gem/clone of git repository
55
+
56
+ echo Installing health_check as a gem into tmp/gems
57
+ env GEM_HOME=$tmp_dir/gems rake install
58
+
59
+ export GEM_PATH="$tmp_dir/gems:`gem environment gempath`"
60
+ echo Set GEM_PATH=$GEM_PATH
61
+
62
+ echo Gems in tmp/gems:
63
+ ls tmp/gems
64
+
65
+ echo Environment:
66
+ env | egrep 'TRAVIS|RAILS|RUBY|_ENV|GEM|BUNDLE'
67
+
68
+ cd $tmp_dir
69
+
70
+ case `ruby -e 'puts JRUBY_VERSION' 2> /dev/null` in
71
+ [0-9]*)
72
+ db=jdbcsqlite3
73
+ # Appears to need a bit extra time
74
+ ;;
75
+ *)
76
+ db=sqlite3
77
+ ;;
78
+ esac
79
+
80
+ echo "Creating $actual_rails_version app in $tmp_dir/railsapp using adapter $db"
81
+ case "$actual_rails_version" in
82
+ *' '[12].*)
83
+ $rails railsapp -d $db
84
+ ;;
85
+ *' '[34].*)
86
+ $rails new railsapp -d $db
87
+ ;;
88
+ *)
89
+ echo "Unknown rails version"
90
+ ;;
91
+ esac
92
+
93
+ cd $railsapp
94
+
95
+ [ -z "$rehash" ] || rbenv local `rbenv version-name`
96
+
97
+ echo "Changed current directory to railsapp root: $railsapp"
98
+
99
+ echo "Fixing rdoc require in Rakefile if needed"
100
+ ruby -p -i.bak -e '$_.gsub!(/rake.rdoctask/, "rdoc/task")' Rakefile
101
+
102
+ echo "Configuring mailer to point to fake_smtp_server port 3555"
103
+ cat >> config/environment.rb <<'!EOF!'
104
+
105
+ ActionMailer::Base.delivery_method = :smtp
106
+ ActionMailer::Base.smtp_settings = { :address => "localhost", :port => 3555 }
107
+
108
+ !EOF!
109
+
110
+ echo Adding an initializer for health_check gem ...
111
+ mkdir -p config/initializers
112
+ tee config/initializers/health_check.rb <<!
113
+ HealthCheck.setup do |config|
114
+ config.success = "$success"
115
+ config.smtp_timeout = 60.0
116
+ config.http_status_for_error_text = 550
117
+ config.http_status_for_error_object = 555
118
+
119
+ config.add_custom_check do
120
+ File.exists?("$custom_file") ? '' : '$custom_file is missing!'
121
+ end
122
+ end
123
+ !
124
+
125
+ echo "Last ten lines of config/environment.rb:"
126
+ tail -10 config/environment.rb
127
+ echo
128
+
129
+ echo Unsetting BUNDLE_GEMFILE '(so Gemfile for rails application will be used)'
130
+ unset BUNDLE_GEMFILE
131
+
132
+ if [ -s Gemfile ]
133
+ then
134
+ echo Adding health_check as gem to Gemfile...
135
+ echo "gem 'health_check', :path => '$base_dir'" >> Gemfile
136
+
137
+ echo
138
+ echo Gemfile contents:
139
+ cat Gemfile
140
+ echo
141
+ echo running bundle install --binstubs
142
+ bundle install --binstubs
143
+ case "$actual_rails_version" in
144
+ *' '4.*)
145
+ echo "Running rake rails:update:binstubs to fix up binstubs bundle has overwritten"
146
+ echo a | rake rails:update:bin
147
+ ;;
148
+ esac
149
+ $rehash
150
+ echo "Using binstubs in $railsapp/bin for rails and rake commands"
151
+ rails="$railsapp/bin/rails"
152
+ rake="$railsapp/bin/rake"
153
+ # Fix for rvm, otherwise bundle run from rails create fails
154
+ export PATH="`pwd`/bin:$PATH"
155
+ else
156
+ dest=$railsapp/vendor/plugins/health_check
157
+ echo Installing health_check as plugin in $dest ...
158
+ mkdir -p $dest
159
+ (
160
+ cd $base_dir
161
+
162
+ git ls-files | cpio -pdl $dest
163
+
164
+ cd $dest
165
+
166
+ echo Files installed as plugin:
167
+ find . -type f
168
+ echo
169
+ )
170
+ fi
171
+
172
+ echo Setting RAILS_ENV=test RACK_ENV=test
173
+ export RAILS_ENV=test RACK_ENV=test
174
+
175
+ case $db in
176
+ jdbcsqlite3)
177
+ echo
178
+ echo 'Jruby requires the database to be created before the server is started: running rake db:migrate'
179
+ $rake db:migrate
180
+ echo
181
+ ;;
182
+ esac
183
+
184
+ cat > public/ajax_example.html <<'EOF'
185
+ <html>
186
+ <head>
187
+ <title>Example static and dynamic calls to health_check</title>
188
+ <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
189
+ <script>
190
+ function parse_result(result, callback) {
191
+ $("#html_status_"+callback).text(result.status);
192
+ $("#html_body_"+callback).text(result.responseText);
193
+ alert(callback + " callback called");
194
+ };
195
+
196
+ function dynamic_call(dataType, url) {
197
+ $.ajax({
198
+ dataType: dataType,
199
+ url: url,
200
+ success: function(data, textStatus, result) {
201
+ $("#data_success").text(data);
202
+ parse_result(result, "success");
203
+ },
204
+ error: function(result, textStatus) {
205
+ parse_result(result, "error");
206
+ },
207
+ complete: function(result, textStatus) {
208
+ parse_result(result, "complete");
209
+ }
210
+ });
211
+ };
212
+ </script>
213
+ </head>
214
+ <body>
215
+ <h1>Static calls</h1>
216
+ <ul>
217
+ <li><a href="http://localhost:3000/health_check/site" target="_blank">Minimal health check should always work</a>
218
+ (<a href="http://localhost:3000/health_check/site.json" target="_blank">json</a>,
219
+ <a href="http://localhost:3000/health_check/site.xml" target="_blank">xml</a>,
220
+ <a href="http://localhost:3000/health_check/site.html" target="_blank">html</a>)
221
+ <li><a href="http://localhost:3000/health_check/fail" target="_blank">Force health check to fail!</a>
222
+ (<a href="http://localhost:3000/health_check/fail.json" target="_blank">json</a>,
223
+ <a href="http://localhost:3000/health_check/fail.xml" target="_blank">xml</a>,
224
+ <a href="http://localhost:3000/health_check/fail.html" target="_blank">html</a>)
225
+ </ul>
226
+ <h1>Dynamic calls</h1>
227
+ <ul>
228
+ <li><a href="#" onclick="dynamic_call('text', 'http://localhost:3000/health_check/site');">Minimal health check should always work</a>
229
+ (<a href="#" onclick="dynamic_call('json', 'http://localhost:3000/health_check/site.json');">json</a>,
230
+ <a href="#" onclick="dynamic_call('site', 'http://localhost:3000/health_check/site.xml');">xml</a>,
231
+ <a href="#" onclick="dynamic_call('text', 'http://localhost:3000/health_check/site.html');">html</a>)
232
+
233
+ <li><a href="#" onclick="dynamic_call('text', 'http://localhost:3000/health_check/fail');">Force health check to fail!</a>
234
+ (<a href="#" onclick="dynamic_call('json', 'http://localhost:3000/health_check/fail.json');">json</a>,
235
+ <a href="#" onclick="dynamic_call('site', 'http://localhost:3000/health_check/fail.xml');">xml</a>,
236
+ <a href="#" onclick="dynamic_call('text', 'http://localhost:3000/health_check/fail.html');">html</a>)
237
+ <li>Last results sent to success:<ul>
238
+ <li><b>Data:</b><span id=data_success></span>
239
+ <li><b>result.status:</b><span id=html_status_success></span>
240
+ <li><b>result.responseText:</b><span id=html_body_success></span>
241
+ </ul>
242
+ <li>Last results sent to error:<ul>
243
+ <li><b>result.status:</b><span id=html_status_error></span>
244
+ <li><b>result.responseText:</b><span id=html_body_error></span>
245
+ </ul>
246
+ <li>Last results sent to complete:<ul>
247
+ <li><b>result.status:</b><span id=html_status_complete></span>
248
+ <li><b>result.responseText:</b><span id=html_body_complete></span>
249
+ </ul>
250
+ </ul>
251
+ </body>
252
+ </html>
253
+ EOF
254
+
255
+ echo
256
+ echo "Created $actual_rails_version app in $railsapp using adapter $db"
257
+ echo -n "Using "
258
+ ruby --version