noah 0.0.5-jruby → 0.1-jruby

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +10 -0
  3. data/LICENSE +201 -0
  4. data/README.md +68 -212
  5. data/Rakefile +65 -41
  6. data/TODO.md +65 -0
  7. data/bin/noah +2 -1
  8. data/bin/noah-watcher.rb +103 -0
  9. data/config.ru +6 -3
  10. data/config/warble.rb +18 -0
  11. data/examples/README.md +116 -0
  12. data/examples/cluster.ru +2 -0
  13. data/examples/custom-watcher.rb +10 -0
  14. data/examples/httpclient-server.rb +7 -0
  15. data/examples/httpclient.rb +12 -0
  16. data/examples/httpclient2.rb +28 -0
  17. data/examples/js/FABridge.js +1452 -0
  18. data/examples/js/WebSocketMain.swf +830 -0
  19. data/examples/js/swfobject.js +851 -0
  20. data/examples/js/web_socket.js +312 -0
  21. data/examples/logger.rb +11 -0
  22. data/examples/reconfiguring-sinatra-watcher.rb +11 -0
  23. data/examples/reconfiguring-sinatra.rb +33 -0
  24. data/examples/simple-post.rb +17 -0
  25. data/examples/websocket.html +24 -0
  26. data/examples/websocket.rb +41 -0
  27. data/lib/noah.rb +6 -8
  28. data/lib/noah/app.rb +20 -268
  29. data/lib/noah/application_routes.rb +70 -0
  30. data/lib/noah/ark.rb +0 -0
  31. data/lib/noah/configuration_routes.rb +81 -0
  32. data/lib/noah/custom_watcher.rb +79 -0
  33. data/lib/noah/ephemeral_routes.rb +47 -0
  34. data/lib/noah/helpers.rb +37 -14
  35. data/lib/noah/host_routes.rb +69 -0
  36. data/lib/noah/models.rb +86 -5
  37. data/lib/noah/models/applications.rb +41 -0
  38. data/lib/noah/models/configurations.rb +49 -0
  39. data/lib/noah/models/ephemerals.rb +54 -0
  40. data/lib/noah/models/hosts.rb +56 -0
  41. data/lib/noah/models/services.rb +54 -0
  42. data/lib/noah/models/watchers.rb +62 -0
  43. data/lib/noah/passthrough.rb +11 -0
  44. data/lib/noah/service_routes.rb +71 -0
  45. data/lib/noah/validations.rb +1 -0
  46. data/lib/noah/validations/watcher_validations.rb +48 -0
  47. data/lib/noah/version.rb +1 -1
  48. data/lib/noah/watcher_routes.rb +45 -0
  49. data/noah.gemspec +25 -17
  50. data/spec/application_spec.rb +30 -30
  51. data/spec/configuration_spec.rb +78 -14
  52. data/spec/ephemeral_spec.rb +59 -0
  53. data/spec/host_spec.rb +21 -21
  54. data/spec/noahapp_application_spec.rb +6 -6
  55. data/spec/noahapp_configuration_spec.rb +5 -5
  56. data/spec/noahapp_ephemeral_spec.rb +115 -0
  57. data/spec/noahapp_host_spec.rb +3 -3
  58. data/spec/noahapp_service_spec.rb +10 -10
  59. data/spec/noahapp_watcher_spec.rb +123 -0
  60. data/spec/service_spec.rb +27 -27
  61. data/spec/spec_helper.rb +13 -22
  62. data/spec/support/db/.keep +0 -0
  63. data/spec/support/test-redis.conf +8 -0
  64. data/spec/watcher_spec.rb +62 -0
  65. data/views/index.haml +21 -15
  66. metadata +189 -146
  67. data/Gemfile.lock +0 -83
  68. data/doc/coverage/index.html +0 -138
  69. data/doc/coverage/jquery-1.3.2.min.js +0 -19
  70. data/doc/coverage/jquery.tablesorter.min.js +0 -15
  71. data/doc/coverage/lib-helpers_rb.html +0 -393
  72. data/doc/coverage/lib-models_rb.html +0 -1449
  73. data/doc/coverage/noah_rb.html +0 -2019
  74. data/doc/coverage/print.css +0 -12
  75. data/doc/coverage/rcov.js +0 -42
  76. data/doc/coverage/screen.css +0 -270
  77. data/lib/noah/applications.rb +0 -46
  78. data/lib/noah/configurations.rb +0 -49
  79. data/lib/noah/hosts.rb +0 -54
  80. data/lib/noah/services.rb +0 -57
  81. data/lib/noah/watchers.rb +0 -18
data/lib/noah.rb CHANGED
@@ -1,5 +1,6 @@
1
- require 'ohm'
2
- require 'ohm/contrib'
1
+ module Noah
2
+ PROTECTED_PATHS = %w[a c h s w]
3
+ end
3
4
  begin
4
5
  require 'yajl'
5
6
  rescue LoadError
@@ -8,11 +9,8 @@ end
8
9
  require 'haml'
9
10
  require 'yaml'
10
11
  require 'sinatra/base'
11
- require 'sinatra/namespace'
12
12
 
13
- require File.join(File.dirname(__FILE__), 'noah','hosts')
14
- require File.join(File.dirname(__FILE__), 'noah','services')
15
- require File.join(File.dirname(__FILE__), 'noah','applications')
16
- require File.join(File.dirname(__FILE__), 'noah','configurations')
17
- require File.join(File.dirname(__FILE__), 'noah','watchers')
13
+ require File.join(File.dirname(__FILE__), 'noah', 'custom_watcher')
14
+ require File.join(File.dirname(__FILE__), 'noah','validations')
15
+ require File.join(File.dirname(__FILE__), 'noah','models')
18
16
  require File.join(File.dirname(__FILE__), 'noah','app')
data/lib/noah/app.rb CHANGED
@@ -1,15 +1,9 @@
1
- require 'sinatra/base'
2
- require 'sinatra/namespace'
3
- require 'ohm'
4
- require 'ohm/contrib'
5
-
1
+ require File.join(File.dirname(__FILE__), 'version')
6
2
  require File.join(File.dirname(__FILE__), 'helpers')
7
3
  require File.join(File.dirname(__FILE__), 'models')
8
4
 
9
5
  module Noah
10
6
  class App < Sinatra::Base
11
- register ::Sinatra::Namespace
12
-
13
7
  helpers Noah::SinatraHelpers
14
8
 
15
9
  configure do
@@ -19,6 +13,8 @@ module Noah
19
13
  set :logging, true
20
14
  set :raise_errors, false
21
15
  set :show_exceptions, false
16
+ set :run, false
17
+ set :redis_url, ENV['REDIS_URL'] || 'redis://localhost:6379/0'
22
18
  end
23
19
 
24
20
  configure(:development) do
@@ -32,14 +28,24 @@ module Noah
32
28
  end
33
29
  end
34
30
 
31
+ # Need to move this out to configuration.
32
+ # Maybe bootstrap them from itself?
33
+ @content_type_mapping = {
34
+ :yaml => "text/x-yaml",
35
+ :json => "application/json",
36
+ :xml => "text/xml",
37
+ :string => "text/plain"
38
+ }
39
+
35
40
  before do
36
41
  content_type "application/json"
37
42
  end
38
43
 
44
+ # Displays an overview page of all registered objects
39
45
  get '/' do
40
46
  content_type "text/html"
41
47
 
42
- haml :index, :format => :html5
48
+ haml :index, :format => :html5, :locals => {:redis_url => settings.redis_url, :noah_version => Noah::VERSION}
43
49
  end
44
50
 
45
51
  not_found do
@@ -52,266 +58,12 @@ module Noah
52
58
  erb :'500'
53
59
  end
54
60
 
55
- namespace "/h" do
56
-
57
- get '/:hostname/:servicename/?' do |hostname, servicename|
58
- h = host_service(hostname, servicename)
59
- if h.nil?
60
- halt 404
61
- else
62
- h.to_json
63
- end
64
- end
65
-
66
- get '/:hostname/?' do |hostname|
67
- h = host(:name => hostname)
68
- if h.nil?
69
- halt 404
70
- else
71
- h.to_json
72
- end
73
- end
74
-
75
- get '/?' do
76
- hosts.map {|h| h.to_hash}
77
- if hosts.size == 0
78
- halt 404
79
- else
80
- hosts.to_json
81
- end
82
- end
83
-
84
- put '/:hostname/?' do |hostname|
85
- required_params = ["name", "status"]
86
- data = JSON.parse(request.body.read)
87
- (data.keys.sort == required_params.sort && data['name'] == hostname) ? (host = ::Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters")
88
- if host.valid?
89
- r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}", "new_record" => host.is_new?}
90
- r.to_json
91
- else
92
- raise "#{host.errors}"
93
- end
94
- end
95
-
96
- delete '/:hostname/?' do |hostname|
97
- host = ::Host.find(:name => hostname).first
98
- if host
99
- services = []
100
- Service.find(:host_id => host.id).sort.each {|x| services << x; x.delete} if host.services.size > 0
101
- host.delete
102
- r = {"result" => "success", "id" => "#{host.id}", "name" => "#{hostname}", "service_count" => "#{services.size}"}
103
- r.to_json
104
- else
105
- halt 404
106
- end
107
- end
108
-
109
- end
110
-
111
- namespace "/s" do
112
-
113
- get '/:servicename/:hostname/?' do |servicename, hostname|
114
- hs = host_service(hostname, servicename)
115
- if hs.nil?
116
- halt 404
117
- else
118
- hs.to_json
119
- end
120
- end
121
-
122
- get '/:servicename/?' do |servicename|
123
- s = services(:name => servicename)
124
- s.map {|x| x.to_hash}
125
- if s.empty?
126
- halt 404
127
- else
128
- s.to_json
129
- end
130
- end
131
-
132
- get '/?' do
133
- if services.empty?
134
- halt 404
135
- else
136
- services.map {|s| s.to_hash}
137
- services.to_json
138
- end
139
- end
140
-
141
- put '/:servicename/?' do |servicename|
142
- required_params = ["status", "host", "name"]
143
- data = JSON.parse(request.body.read)
144
- if data.keys.sort == required_params.sort
145
- h = ::Host.find(:name => data['host']).first || (raise "Invalid Host")
146
- service = ::Service.find_or_create(:name => servicename, :status => data['status'], :host => h)
147
- if service.valid?
148
- action = service.is_new? ? "create" : "update"
149
- service.save
150
- r = {"action" => action, "result" => "success", "id" => service.id, "host" => h.name, "name" => service.name}
151
- r.to_json
152
- else
153
- raise "#{service.errors}"
154
- end
155
- else
156
- raise "Missing Parameters"
157
- end
158
- end
159
-
160
- delete '/:servicename/:hostname/?' do |servicename, hostname|
161
- host = ::Host.find(:name => hostname).first || (halt 404)
162
- service = ::Service.find(:name => servicename, :host_id => host.id).first || (halt 404)
163
- if host && service
164
- service.delete
165
- r = {"action" => "delete", "result" => "success", "id" => service.id, "host" => host.name, "service" => servicename}
166
- r.to_json
167
- else
168
- halt 404
169
- end
170
- end
171
-
172
- end
61
+ load File.join(File.dirname(__FILE__), 'host_routes.rb')
62
+ load File.join(File.dirname(__FILE__), 'service_routes.rb')
63
+ load File.join(File.dirname(__FILE__), 'application_routes.rb')
64
+ load File.join(File.dirname(__FILE__), 'configuration_routes.rb')
65
+ load File.join(File.dirname(__FILE__), 'watcher_routes.rb')
66
+ load File.join(File.dirname(__FILE__), 'ephemeral_routes.rb')
173
67
 
174
- namespace "/a" do
175
-
176
- get '/:appname/:config/?' do |appname, config|
177
- app = ::Application.find(:name => appname).first
178
- if app.nil?
179
- halt 404
180
- else
181
- c = ::Configuration.find(:name => config, :application_id => app.id).first
182
- c.to_json
183
- end
184
- end
185
-
186
- get '/:appname/?' do |appname|
187
- app = ::Application.find(:name => appname).first
188
- if app.nil?
189
- halt 404
190
- else
191
- app.to_json
192
- end
193
- end
194
-
195
- put '/:appname/?' do |appname|
196
- required_params = ["name"]
197
- data = JSON.parse(request.body.read)
198
- if data.keys.sort == required_params.sort && data['name'] == appname
199
- app = ::Application.find_or_create(:name => appname)
200
- else
201
- raise "Missing Parameters"
202
- end
203
- if app.valid?
204
- action = app.is_new? ? "create" : "update"
205
- app.save
206
- r = {"result" => "success","id" => app.id, "action" => action, "name" => app.name }
207
- r.to_json
208
- else
209
- raise "#{app.errors}"
210
- end
211
- end
212
-
213
- delete '/:appname/?' do |appname|
214
- app = ::Application.find(:name => appname).first
215
- if app.nil?
216
- halt 404
217
- else
218
- configurations = []
219
- ::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
220
- app.delete
221
- r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
222
- r.to_json
223
- end
224
- end
225
-
226
- get '/?' do
227
- apps = []
228
- ::Application.all.sort.each {|a| apps << a.to_hash}
229
- if apps.empty?
230
- halt 404
231
- else
232
- apps.to_json
233
- end
234
- end
235
-
236
- end
237
-
238
- namespace '/c' do
239
-
240
- # Need to move this out to configuration.
241
- # Maybe bootstrap them from itself?
242
- content_type_mapping = {
243
- :yaml => "text/x-yaml",
244
- :json => "application/json",
245
- :xml => "text/xml",
246
- :string => "text/plain"
247
- }
248
-
249
- get '/:appname/:element/?' do |appname, element|
250
- a = ::Application.find(:name => appname).first
251
- if a.nil?
252
- halt 404
253
- else
254
- c = ::Configuration.find(:name => element, :application_id => a.id).first
255
- content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym]
256
- c.body
257
- end
258
- end
259
-
260
- get '/:appname/?' do |appname|
261
- config = []
262
- a = ::Application.find(:name => appname).first
263
- if a.nil?
264
- halt 404
265
- else
266
- ::Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
267
- config.to_json
268
- end
269
- end
270
-
271
- get '/?' do
272
- configs = []
273
- ::Configuration.all.sort.each {|c| configs << c.to_hash}
274
- if configs.empty?
275
- halt 404
276
- else
277
- configs.to_json
278
- end
279
- end
280
-
281
- put '/:appname/:element?' do |appname, element|
282
- app = ::Application.find_or_create(:name => appname)
283
- config = ::Configuration.find_or_create(:name => element, :application_id => app.id)
284
- required_params = ["format", "body"]
285
- data = JSON.parse(request.body.read)
286
- data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters")
287
- if config.valid?
288
- config.save
289
- action = config.is_new? ? "create" : "update"
290
- dependency_action = app.is_new? ? "created" : "updated"
291
- r = {"result" => "success","id" => "#{config.id}", "action" => action, "dependencies" => dependency_action, "application" => app.name, "item" => config.name}
292
- r.to_json
293
- else
294
- raise "#{config.errors}"
295
- end
296
- end
297
-
298
- delete '/:appname/:element?' do |appname, element|
299
- app = ::Application.find(:name => appname).first
300
- if app
301
- config = ::Configuration.find(:name=> element, :application_id => app.id).first
302
- if config
303
- config.delete
304
- r = {"result" => "success", "id" => "#{config.id}", "action" => "delete", "application" => "#{app.name}", "item" => "#{element}"}
305
- r.to_json
306
- else
307
- halt 404
308
- end
309
- else
310
- halt 404
311
- end
312
- end
313
-
314
- end
315
- # run! if app_file == $0
316
68
  end
317
69
  end
@@ -0,0 +1,70 @@
1
+ class Noah::App
2
+ # Application URIs
3
+ get '/a/:appname/:config/?' do |appname, config|
4
+ app = Noah::Application.find(:name => appname).first
5
+ if app.nil?
6
+ halt 404
7
+ else
8
+ c = Noah::Configuration.find(:name => config, :application_id => app.id).first
9
+ c.to_json
10
+ end
11
+ end
12
+
13
+ get '/a/:appname/?' do |appname|
14
+ app = Noah::Application.find(:name => appname).first
15
+ if app.nil?
16
+ halt 404
17
+ else
18
+ app.to_json
19
+ end
20
+ end
21
+
22
+ put '/a/:appname/watch' do |appname|
23
+ required_params = ["endpoint"]
24
+ data = JSON.parse(request.body.read)
25
+ (data.keys.sort == required_params.sort) ? (a = Noah::Application.find(:name => appname).first) : (raise "Missing Parameters")
26
+ a.nil? ? (halt 404) : (w = a.watch!(:endpoint => data['endpoint']))
27
+ w.to_json
28
+ end
29
+
30
+ put '/a/:appname/?' do |appname|
31
+ required_params = ["name"]
32
+ data = JSON.parse(request.body.read)
33
+ if data.keys.sort == required_params.sort && data['name'] == appname
34
+ app = Noah::Application.find_or_create(:name => appname)
35
+ else
36
+ raise "Missing Parameters"
37
+ end
38
+ if app.valid?
39
+ action = app.is_new? ? "create" : "update"
40
+ app.save
41
+ r = {"result" => "success","id" => app.id, "action" => action, "name" => app.name }
42
+ r.to_json
43
+ else
44
+ raise "#{format_errors(app)}"
45
+ end
46
+ end
47
+
48
+ delete '/a/:appname/?' do |appname|
49
+ app = Noah::Application.find(:name => appname).first
50
+ if app.nil?
51
+ halt 404
52
+ else
53
+ configurations = []
54
+ Noah::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
55
+ app.delete
56
+ r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
57
+ r.to_json
58
+ end
59
+ end
60
+
61
+ get '/a/?' do
62
+ apps = []
63
+ Noah::Application.all.sort.each {|a| apps << a.to_hash}
64
+ if apps.empty?
65
+ halt 404
66
+ else
67
+ apps.to_json
68
+ end
69
+ end
70
+ end
data/lib/noah/ark.rb ADDED
File without changes
@@ -0,0 +1,81 @@
1
+ class Noah::App
2
+ content_type_mapping = {
3
+ :yaml => "text/x-yaml",
4
+ :json => "application/json",
5
+ :xml => "text/xml",
6
+ :string => "text/plain"
7
+ }
8
+ # Configuration URIs
9
+ get '/c/:appname/:element/?' do |appname, element|
10
+ a = Noah::Application.find(:name => appname).first
11
+ if a.nil?
12
+ halt 404
13
+ else
14
+ c = Noah::Configuration.find(:name => element, :application_id => a.id).first
15
+ content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym]
16
+ c.body
17
+ end
18
+ end
19
+
20
+ get '/c/:appname/?' do |appname|
21
+ config = []
22
+ a = Noah::Application.find(:name => appname).first
23
+ if a.nil?
24
+ halt 404
25
+ else
26
+ Noah::Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
27
+ config.to_json
28
+ end
29
+ end
30
+
31
+ get '/c/?' do
32
+ configs = []
33
+ Noah::Configuration.all.sort.each {|c| configs << c.to_hash}
34
+ if configs.empty?
35
+ halt 404
36
+ else
37
+ configs.to_json
38
+ end
39
+ end
40
+
41
+ put '/c/:configname/watch' do |configname|
42
+ required_params = ["endpoint"]
43
+ data = JSON.parse(request.body.read)
44
+ (data.keys.sort == required_params.sort) ? (c = Noah::Configuration.find(:name => configname).first) : (raise "Missing Parameters")
45
+ c.nil? ? (halt 404) : (w = c.watch!(:endpoint => data['endpoint']))
46
+ w.to_json
47
+ end
48
+
49
+ put '/c/:appname/:element?' do |appname, element|
50
+ app = Noah::Application.find_or_create(:name => appname)
51
+ config = Noah::Configuration.find_or_create(:name => element, :application_id => app.id)
52
+ required_params = ["format", "body"]
53
+ data = JSON.parse(request.body.read)
54
+ data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters")
55
+ if config.valid?
56
+ config.save
57
+ action = config.is_new? ? "create" : "update"
58
+ dependency_action = app.is_new? ? "created" : "updated"
59
+ r = {"result" => "success","id" => "#{config.id}", "action" => action, "dependencies" => dependency_action, "application" => app.name, "item" => config.name}
60
+ r.to_json
61
+ else
62
+ raise "#{format_errors(config)}"
63
+ end
64
+ end
65
+
66
+ delete '/c/:appname/:element?' do |appname, element|
67
+ app = Noah::Application.find(:name => appname).first
68
+ if app
69
+ config = Noah::Configuration.find(:name=> element, :application_id => app.id).first
70
+ if config
71
+ config.delete
72
+ r = {"result" => "success", "id" => "#{config.id}", "action" => "delete", "application" => "#{app.name}", "item" => "#{element}"}
73
+ r.to_json
74
+ else
75
+ halt 404
76
+ end
77
+ else
78
+ halt 404
79
+ end
80
+ end
81
+ end