heaven 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem "god", ">= 0"
9
+ group :development do
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ god (0.11.0)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.9.2)
11
+ rcov (0.9.10)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ god
19
+ jeweler (~> 1.6.4)
20
+ rcov
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Eugen Minciu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = heaven
2
+
3
+ A set of utilities to make it easier to work with god.
4
+
5
+ == Contributing to heaven
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Eugen Minciu. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "heaven"
18
+ gem.homepage = "http://github.com/minciue/heaven"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A set of utilities to make it easier to work with god.}
21
+ gem.description = %Q{A set of utilities to make it easier to work with god.}
22
+ gem.email = "eugen@lesseverything.com"
23
+ gem.authors = ["Eugen Minciu"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "heaven #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.12
@@ -0,0 +1,2 @@
1
+ require 'heaven/heaven'
2
+ require 'heaven/god_watcher'
@@ -0,0 +1,143 @@
1
+ class HeavenConfigurationError < StandardError; end
2
+ class GodWatcher # :nodoc:all
3
+ def initialize(name, options = {})
4
+ @name = name
5
+ @start = options[:start]
6
+ @stop = options[:stop]
7
+ @restart = options[:restart]
8
+ @pid_file = options[:pid_file]
9
+ @log_file = options[:log_file]
10
+ @uid = options[:uid]
11
+ @gid = options[:gid]
12
+
13
+ @interval = options[:interval] || 15.seconds
14
+ @grace = options[:grace] || 15.seconds
15
+
16
+ @behaviors = options[:behaviors] || [:clean_pid_file]
17
+
18
+ @max_memory = options[:max_memory] || 250.megabytes
19
+ @max_cpu = options[:max_cpu] || 50.percent
20
+ @max_times = options[:max_times] || 5
21
+
22
+ @check_flapping = options[:check_flapping].nil? ? true : options[:check_flapping]
23
+ @check_memory = options[:check_memory].nil? ? true : options[:check_memory]
24
+ @check_cpu = options[:check_cpu].nil? ? true : options[:check_cpu]
25
+
26
+ @email_notifications = options[:email_notifications] || false
27
+ @from_email = options[:from_email]
28
+ @from_name = options[:from_name] || "God"
29
+ @delivery_method = options[:delivery_method] || :sendmail
30
+ @to_emails = options[:to_emails] || []
31
+
32
+ @campfire_notifications = options[:campfire_notifications] || false
33
+ @campfire_subdomain = options[:campfire_subdomain]
34
+ @campfire_token = options[:campfire_token]
35
+ @campfire_room = options[:campfire_room]
36
+ end
37
+
38
+ def watch
39
+ raise HeavenConfigurationError, "start command not specified" if @start.nil?
40
+ raise HeavenConfigurationError, "stop command not specified" if @stop.nil?
41
+ set_god_defaults unless @god_defaults_set
42
+
43
+ God.watch do |w|
44
+ w.name = @name
45
+ w.interval = @interval
46
+ w.start = @start
47
+ w.stop = @stop
48
+ w.restart = @restart if @restart # not mandatory, if we don't specify it god will do a stop and a start
49
+ w.pid_file = @pid_file if @pid_file
50
+ w.log = @log_file if @log_file
51
+
52
+ w.start_grace = @grace
53
+ w.restart_grace = @grace
54
+
55
+ @behaviors.each { |b| w.behavior(b) }
56
+
57
+ # determine the state on startup
58
+ w.transition(:init, { true => :up, false => :start }) do |on|
59
+ on.condition(:process_running) do |c|
60
+ c.running = true
61
+ end
62
+ end
63
+
64
+ # determine when process has finished starting
65
+ w.transition([:start, :restart], :up) do |on|
66
+ on.condition(:process_running) do |c|
67
+ c.running = true
68
+ end
69
+
70
+ # failsafe
71
+ on.condition(:tries) do |c|
72
+ c.times = @max_times
73
+ c.transition = :start
74
+ end
75
+ end
76
+
77
+ # start if process is not running
78
+ w.transition(:up, :start) do |on|
79
+ on.condition(:process_exits)
80
+ end
81
+
82
+ if @check_memory || @check_cpu
83
+ # restart if memory or cpu is too high
84
+ w.transition(:up, :restart) do |on|
85
+ if @check_memory
86
+ on.condition(:memory_usage) do |c|
87
+ c.above = @max_memory
88
+ c.times = @max_times
89
+ end
90
+ end
91
+
92
+ if @check_cpu
93
+ on.condition(:cpu_usage) do |c|
94
+ c.above = @max_cpu
95
+ c.times = @max_times
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ w.lifecycle do |check|
102
+ check.condition(:flapping) do |c|
103
+ c.to_state = [:start, :restart]
104
+ c.times = 5
105
+ c.within = 5.minutes
106
+ c.transition = :unmonitored
107
+ c.retry_in = 10.minutes
108
+ c.retry_times = 5
109
+ c.retry_within = 2.hours
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ def set_god_defaults
116
+ raise HeavenConfigurationError, "You must specify at least one email address" if @email_notifications == true && @to_emails.empty?
117
+
118
+ if @email_notifications
119
+ God::Contacts::Email.defaults do |d|
120
+ d.from_email = @from_email
121
+ d.from_name = @from_name
122
+ d.delivery_method = @delivery_method
123
+ end
124
+
125
+ @to_emails.each do |email|
126
+ God.contact(:email) do |c|
127
+ c.name = email
128
+ c.to_email = email
129
+ end
130
+ end
131
+ end
132
+
133
+ if @campfire_notifications
134
+ God::Contacts::Campfire.defaults do |d|
135
+ d.subdomain = @campfire_subdomain
136
+ d.token = @campfire_token
137
+ d.room = @campfire_room
138
+ d.ssl = true
139
+ end
140
+ end
141
+ @god_defaults_set = true
142
+ end
143
+ end
@@ -0,0 +1,47 @@
1
+ class Heaven
2
+ class << self
3
+ # Set the default options for all subsequent watch calls
4
+ #
5
+ # Available options are:
6
+ #
7
+ # start:: start command;mandatory
8
+ # stop:: stop command; mandatory
9
+ # restart:: restart command; optional, if none is specified then god will execute a stop and a start
10
+ #
11
+ # pid_file:: the full path of the pid file the service uses
12
+ # log_file:: the full path of the log file where all the daemon's output will be dumped
13
+ #
14
+ # uid:: the user this service needs to be run as
15
+ # gid:: the group this service needs to be run as
16
+ #
17
+ # interval:: interval to poll the service and check against the conditions (default: 15s)
18
+ # grace:: wait interval while starting or restarting the service before beginning to monitor it again (default: 15s)
19
+ # behaviors:: god behaviors to enforce for this service, see god documentation (default: [:clean_pid_file])
20
+ #
21
+ # check_flapping:: check to see if the process keeps being restarted ad-infinitum and stop trying to restart it
22
+ # check_memory:: check memory usage (default: true)
23
+ # check_cpu:: check cpu usage (default: true)
24
+ #
25
+ # max_memory:: restart the service if its memory usage exceeds this much on [max_times] consecutive polls (default: 250.megabytes)
26
+ # max_cpu:: restart the service if its cpu usage exceeds this much on [max_times] consecutive polls (default: 50.percent)
27
+ # max_times:: see the above (default: 5)
28
+ def defaults(options)
29
+ @defaults = options
30
+ end
31
+
32
+ # Watch a run-of-the-mill UNIX daemon
33
+ #
34
+ # It has all the options available to defaults *and*:
35
+ # script:: name of the daemon's command script (it must take options stop, start and restart)
36
+ def watch_daemon(name, options = {})
37
+ script = options.delete(:script)
38
+ if script
39
+ options[:start] = "#{script} start"
40
+ options[:stop] = "#{script} stop"
41
+ options[:restart] = "#{script} restart"
42
+ end
43
+
44
+ GodWatcher.new(name, @defaults.merge(options)).watch
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'heaven'
15
+
16
+ class Test::Unit::TestCase
17
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestHeaven < Test::Unit::TestCase
4
+ def test_something_for_real
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heaven
3
+ version: !ruby/object:Gem::Version
4
+ hash: 7
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 12
10
+ version: 0.0.12
11
+ platform: ruby
12
+ authors:
13
+ - Eugen Minciu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-18 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ requirement: *id001
33
+ prerelease: false
34
+ name: god
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 23
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ requirement: *id002
49
+ prerelease: false
50
+ name: bundler
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 1
61
+ - 6
62
+ - 4
63
+ version: 1.6.4
64
+ requirement: *id003
65
+ prerelease: false
66
+ name: jeweler
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirement: *id004
79
+ prerelease: false
80
+ name: rcov
81
+ description: A set of utilities to make it easier to work with god.
82
+ email: eugen@lesseverything.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.rdoc
95
+ - Rakefile
96
+ - VERSION
97
+ - lib/heaven.rb
98
+ - lib/heaven/god_watcher.rb
99
+ - lib/heaven/heaven.rb
100
+ - test/helper.rb
101
+ - test/test_heaven.rb
102
+ has_rdoc: true
103
+ homepage: http://github.com/minciue/heaven
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ hash: 3
126
+ segments:
127
+ - 0
128
+ version: "0"
129
+ requirements: []
130
+
131
+ rubyforge_project:
132
+ rubygems_version: 1.4.2
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: A set of utilities to make it easier to work with god.
136
+ test_files: []
137
+