deputy 0.1.48 → 0.1.49
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/deputy.gemspec +1 -1
- data/lib/deputy.rb +21 -10
- data/spec/deputy_spec.rb +14 -5
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.49
|
data/deputy.gemspec
CHANGED
data/lib/deputy.rb
CHANGED
@@ -16,7 +16,8 @@ class Scout
|
|
16
16
|
OPTIONS = {}.to_yaml
|
17
17
|
|
18
18
|
def self.clean_class_name
|
19
|
-
to_s.split('::')
|
19
|
+
parts = to_s.split('::')
|
20
|
+
parts.size == 1 ? parts.first : parts[1..-1].join('::')
|
20
21
|
end
|
21
22
|
|
22
23
|
protected
|
@@ -84,13 +85,12 @@ class Scout
|
|
84
85
|
end
|
85
86
|
|
86
87
|
module Deputy
|
87
|
-
START_MINUTE = (Time.now.to_i + 30) / 60 # we could start at 58..02 seconds -> always in middle of minute
|
88
88
|
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
|
89
89
|
DEFAULT_VALUE = 'OK'
|
90
90
|
|
91
91
|
def self.install_cron
|
92
92
|
executable = `which deputy`.strip
|
93
|
-
unless (`crontab -l`).include?(executable)
|
93
|
+
unless (`crontab -l`).include?(executable)
|
94
94
|
`crontab -l | { cat; echo "* * * * * #{executable} --run-plugins >> /tmp/deputy.log 2>&1"; } | crontab -`
|
95
95
|
if executable !~ %r{^/usr/}
|
96
96
|
puts "make deputy globally available! or e.g. calls from inside cronjobs do not know deputy"
|
@@ -100,20 +100,20 @@ module Deputy
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def self.run_plugins(options={})
|
103
|
+
start_time = Time.now.to_i
|
103
104
|
sleep_random_interval unless options[:no_wait]
|
104
105
|
|
105
106
|
content = get("/plugins.rb")
|
106
107
|
|
107
108
|
exceptions = []
|
108
109
|
Scout.plugins(content).each do |interval, plugin|
|
109
|
-
|
110
|
-
|
111
|
-
if minutes_to_wait == run_every_n_minutes
|
110
|
+
wait = minutes_to_wait(start_time, interval)
|
111
|
+
if wait == 0
|
112
112
|
puts "#{plugin.clean_class_name}: running"
|
113
113
|
begin
|
114
114
|
plugin.new.build_report
|
115
115
|
rescue Object => e # catch and report plugin-specific errors
|
116
|
-
e.message[0..0] = plugin.
|
116
|
+
e.message[0..0] = plugin.clean_class_name
|
117
117
|
puts e
|
118
118
|
exceptions << e
|
119
119
|
end
|
@@ -131,15 +131,14 @@ module Deputy
|
|
131
131
|
|
132
132
|
def self.send_report(group, value, options = {})
|
133
133
|
raise "separate #{group} with a ." unless group.split('.',2).size == 2
|
134
|
-
|
135
|
-
|
136
|
-
get "/notify?group=#{CGI.escape group}&value=#{CGI.escape value.to_s}&hostname=#{host}#{'&forced_host=true' if options[:host]}"
|
134
|
+
get "/notify?group=#{CGI.escape group}&value=#{CGI.escape value.to_s}", options
|
137
135
|
end
|
138
136
|
|
139
137
|
def self.get(path, options = {})
|
140
138
|
url = "#{sheriff_url}#{path}"
|
141
139
|
url = "http://#{url}" unless url =~ %r{://}
|
142
140
|
options[:http_basic_authentication] = extract_auth_from_url!(url)
|
141
|
+
url = add_host_to_url(url, options.delete(:host))
|
143
142
|
|
144
143
|
Timeout.timeout(config['timeout']||10) do
|
145
144
|
open(url, options).read
|
@@ -161,6 +160,12 @@ module Deputy
|
|
161
160
|
raise "No deputy.yml found in /etc or #{home}"
|
162
161
|
end
|
163
162
|
|
163
|
+
def self.minutes_to_wait(start_time, interval)
|
164
|
+
start_minute = start_time / 60
|
165
|
+
run_every_n_minutes = interval / 60
|
166
|
+
start_minute % run_every_n_minutes
|
167
|
+
end
|
168
|
+
|
164
169
|
def self.sleep_random_interval
|
165
170
|
if max = config['max_random_start_delay']
|
166
171
|
constant_number = Socket.gethostname.sum{|x| x[0]}
|
@@ -202,4 +207,10 @@ module Deputy
|
|
202
207
|
auth = [$1, $2].compact
|
203
208
|
auth.empty? ? nil : auth
|
204
209
|
end
|
210
|
+
|
211
|
+
def self.add_host_to_url(url, host=nil)
|
212
|
+
query = "hostname=#{host || Socket.gethostname}#{'&forced_host=true' if host}"
|
213
|
+
separator = (url.include?('?') ? "&" : "?")
|
214
|
+
url + separator + query
|
215
|
+
end
|
205
216
|
end
|
data/spec/deputy_spec.rb
CHANGED
@@ -146,6 +146,16 @@ describe Deputy do
|
|
146
146
|
defined?(FasterCSV).should == "constant"
|
147
147
|
end
|
148
148
|
end
|
149
|
+
|
150
|
+
describe :clean_class_name do
|
151
|
+
it "is simple name for simple e.g. from irb" do
|
152
|
+
FooPlugin.clean_class_name.should == 'FooPlugin'
|
153
|
+
end
|
154
|
+
|
155
|
+
it "is last parts for namespaced" do
|
156
|
+
FooPlugin::NestedPlugin.clean_class_name.should == 'NestedPlugin'
|
157
|
+
end
|
158
|
+
end
|
149
159
|
end
|
150
160
|
|
151
161
|
describe :run_plugins do
|
@@ -155,7 +165,7 @@ describe Deputy do
|
|
155
165
|
|
156
166
|
it "executes all plugins" do
|
157
167
|
$notify = 0
|
158
|
-
FakeWeb.register_uri(:get, "http://sheri.ff/plugins.rb", :body => klass('C', :code => '$notify=1'))
|
168
|
+
FakeWeb.register_uri(:get, "http://sheri.ff/plugins.rb?hostname=my_host", :body => klass('C', :code => '$notify=1'))
|
159
169
|
FakeWeb.register_uri(:get, "http://sheri.ff/notify?group=Deputies.finished&value=OK&hostname=my_host", :body => 'OK')
|
160
170
|
Deputy.run_plugins
|
161
171
|
$notify.should == 1
|
@@ -172,16 +182,15 @@ describe Deputy do
|
|
172
182
|
end
|
173
183
|
|
174
184
|
it "does not sleeps no_wait given" do
|
175
|
-
FakeWeb.register_uri(:get, "http://sheri.ff/plugins.rb", :body => '')
|
185
|
+
FakeWeb.register_uri(:get, "http://sheri.ff/plugins.rb?hostname=my_host", :body => '')
|
176
186
|
FakeWeb.register_uri(:get, "http://sheri.ff/notify?group=Deputies.finished&value=OK&hostname=my_host", :body => 'OK')
|
177
|
-
Socket.stub!(:gethostname).and_return 'foo'
|
178
187
|
Deputy.stub!(:send_report)
|
179
188
|
Deputy.should_not_receive(:sleep)
|
180
189
|
Deputy.run_plugins
|
181
190
|
end
|
182
191
|
|
183
192
|
it "fails with nice backtrace" do
|
184
|
-
FakeWeb.register_uri(:get, "http://sheri.ff/plugins.rb", :body => klass('FooBar', :code => 'raise'))
|
193
|
+
FakeWeb.register_uri(:get, "http://sheri.ff/plugins.rb?hostname=my_host", :body => klass('FooBar', :code => 'raise'))
|
185
194
|
FakeWeb.register_uri(:get, "http://sheri.ff/notify?group=Deputies.finished&value=Error&hostname=my_host", :body => 'OK')
|
186
195
|
FakeWeb.register_uri(:get, "http://sheri.ff/notify?group=Deputies.Error&value=FooBar&hostname=my_host", :body => 'OK')
|
187
196
|
lambda{
|
@@ -191,7 +200,7 @@ describe Deputy do
|
|
191
200
|
|
192
201
|
it "continues to run plugins when one fails" do
|
193
202
|
$notify = 0
|
194
|
-
FakeWeb.register_uri(:get, "http://sheri.ff/plugins.rb", :body => klass('FooBar', :code => 'raise') + klass('C', :code => '$notify = 1'))
|
203
|
+
FakeWeb.register_uri(:get, "http://sheri.ff/plugins.rb?hostname=my_host", :body => klass('FooBar', :code => 'raise') + klass('C', :code => '$notify = 1'))
|
195
204
|
FakeWeb.register_uri(:get, "http://sheri.ff/notify?group=Deputies.finished&value=Error&hostname=my_host", :body => 'OK')
|
196
205
|
FakeWeb.register_uri(:get, "http://sheri.ff/notify?group=Deputies.Error&value=&hostname=my_host", :body => 'OK')
|
197
206
|
lambda{
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deputy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 121
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 49
|
10
|
+
version: 0.1.49
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael Grosser
|