torquebox-backstage 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/Gemfile +18 -0
  2. data/Gemfile.lock +69 -0
  3. data/README.md +164 -0
  4. data/Rakefile +32 -0
  5. data/TODO +9 -0
  6. data/TORQUEBOX_VERSION +1 -0
  7. data/VERSION +1 -0
  8. data/backstage.rb +84 -0
  9. data/bin/backstage +78 -0
  10. data/config.ru +3 -0
  11. data/config/torquebox.yml +14 -0
  12. data/lib/apps.rb +18 -0
  13. data/lib/apps/models/app.rb +33 -0
  14. data/lib/apps/routes.rb +18 -0
  15. data/lib/authentication.rb +64 -0
  16. data/lib/destinations.rb +21 -0
  17. data/lib/destinations/models/destination.rb +80 -0
  18. data/lib/destinations/models/message.rb +67 -0
  19. data/lib/destinations/models/queue.rb +33 -0
  20. data/lib/destinations/models/topic.rb +59 -0
  21. data/lib/destinations/routes.rb +44 -0
  22. data/lib/has_mbean.rb +59 -0
  23. data/lib/helpers.rb +142 -0
  24. data/lib/jobs.rb +19 -0
  25. data/lib/jobs/models/job.rb +35 -0
  26. data/lib/jobs/routes.rb +18 -0
  27. data/lib/message_processors.rb +18 -0
  28. data/lib/message_processors/models/message_processor.rb +40 -0
  29. data/lib/message_processors/routes.rb +18 -0
  30. data/lib/pools.rb +18 -0
  31. data/lib/pools/models/pool.rb +51 -0
  32. data/lib/pools/routes.rb +41 -0
  33. data/lib/resource.rb +53 -0
  34. data/lib/resource_helpers.rb +63 -0
  35. data/lib/runtimes.rb +19 -0
  36. data/lib/runtimes/models/job.rb +35 -0
  37. data/lib/runtimes/routes.rb +18 -0
  38. data/lib/services.rb +18 -0
  39. data/lib/services/models/service.rb +35 -0
  40. data/lib/services/routes.rb +17 -0
  41. data/lib/torquebox_managed.rb +36 -0
  42. data/lib/util.rb +33 -0
  43. data/spec/api_spec.rb +136 -0
  44. data/spec/auth_spec.rb +70 -0
  45. data/spec/spec_helper.rb +39 -0
  46. data/views/apps/index.haml +15 -0
  47. data/views/apps/show.haml +12 -0
  48. data/views/css/_mixins.sass +51 -0
  49. data/views/css/html5reset.sass +82 -0
  50. data/views/css/style.sass +152 -0
  51. data/views/destinations/index.haml +37 -0
  52. data/views/destinations/show.haml +18 -0
  53. data/views/jobs/index.haml +23 -0
  54. data/views/jobs/show.haml +13 -0
  55. data/views/layout.haml +31 -0
  56. data/views/message_processors/index.haml +28 -0
  57. data/views/message_processors/show.haml +12 -0
  58. data/views/messages/index.haml +20 -0
  59. data/views/messages/properties.haml +3 -0
  60. data/views/messages/show.haml +19 -0
  61. data/views/pools/index.haml +32 -0
  62. data/views/pools/show.haml +44 -0
  63. data/views/services/index.haml +21 -0
  64. data/views/services/show.haml +13 -0
  65. metadata +289 -0
@@ -0,0 +1,59 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ module Backstage
18
+ module HasMBean
19
+ java_import javax.management.ObjectName
20
+
21
+ def self.included(base)
22
+ base.send( :attr_accessor, :mbean_name, :mbean )
23
+ base.extend( ClassMethods )
24
+ end
25
+
26
+ def initialize(mbean_name, mbean)
27
+ self.mbean_name = mbean_name
28
+ self.mbean = mbean
29
+ end
30
+
31
+ def full_name
32
+ mbean_name.to_s
33
+ end
34
+
35
+ def method_missing(method, *args, &block)
36
+ mbean.send( method, *args, &block )
37
+ rescue NoMethodError => ex
38
+ super
39
+ end
40
+
41
+ module ClassMethods
42
+ def jmx_server
43
+ @jmx_server ||= JMX::MBeanServer.new
44
+ end
45
+
46
+ def all(filter_string = filter)
47
+ jmx_server.query_names( filter_string ).collect { |name| new( name, jmx_server[name] ) }
48
+ end
49
+
50
+ def find(name)
51
+ name = ObjectName.new( name ) unless name.is_a?( ObjectName )
52
+ new( name, jmx_server[name] )
53
+ rescue JMX::NoSuchBeanError => ex
54
+ nil
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,142 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'util'
18
+ require 'authentication'
19
+ require 'sinatra/url_for'
20
+
21
+ module Backstage
22
+ class Application < Sinatra::Base
23
+
24
+ helpers do
25
+ include Backstage::Authentication
26
+ include Sinatra::UrlForHelper
27
+
28
+ def json_url_for(fragment, options = { })
29
+ options[:format] = 'json'
30
+ url_for( fragment, :full, options )
31
+ end
32
+
33
+ def object_path(object)
34
+ object_action_or_collection_path(*(object.association_chain << nil))
35
+ end
36
+
37
+ def object_action_or_collection_path(*objects)
38
+ collection_or_action = objects.pop
39
+ paths = []
40
+ objects.each do |object|
41
+ paths << "#{simple_class_name( object )}/#{Util.encode_name( object.full_name )}"
42
+ end
43
+ paths << collection_or_action if collection_or_action
44
+ '/' + paths.join( '/' )
45
+ end
46
+ alias_method :object_action_path, :object_action_or_collection_path
47
+ alias_method :collection_path, :object_action_or_collection_path
48
+
49
+ def redirect_to(location)
50
+ redirect url_for(location, :full)
51
+ end
52
+
53
+ def link_to(path, text, options = {})
54
+ "<a href='#{url_for path}' class='#{options[:class]}'>#{text}</a>"
55
+ end
56
+
57
+ def data_row(name, value)
58
+ dom_class = ['value']
59
+ dom_class << 'status' << value.downcase if name.to_s.downcase == 'status' # hack
60
+ "<tr class='data-row'><td class='label'>#{name}</td><td class='#{dom_class.join(' ')}'>#{value}</td></tr>"
61
+ end
62
+
63
+ def simple_class_name(object)
64
+ object.class.name.split( "::" ).last.underscore
65
+ end
66
+
67
+ def truncate(text, length = 30)
68
+ text.length > length ? text[0...length] + '...' : text
69
+ end
70
+
71
+ def class_for_body
72
+ klass = request.path_info.split('/').reverse.select { |part| part =~ /^[A-Za-z_]*$/ }
73
+ klass.empty? ? 'root' : klass
74
+ end
75
+
76
+ def action_button(object, action, text=nil)
77
+ text ||= action.capitalize
78
+ accum = <<-EOF
79
+ <form method="post" action="#{url_for object_action_path(object, action)}">
80
+ <input type="submit" value="#{text}"/>
81
+ </form>
82
+ EOF
83
+ end
84
+
85
+ def html_requested?
86
+ params[:format] != 'json' && env['rack-accept.request'].media_type?( 'text/html' )
87
+ end
88
+
89
+ def collection_to_json( collection )
90
+ JSON.generate( collection.collect { |object| object_to_hash( object ) } )
91
+ end
92
+
93
+ def object_to_json(object)
94
+ JSON.generate( object_to_hash( object ) )
95
+ end
96
+
97
+ def object_to_hash(object)
98
+ response = object.to_hash
99
+ response[:actions] = object.available_actions.inject({}) do |actions, action|
100
+ actions[action] = json_url_for( object_action_path( response[:resource], action ) )
101
+ actions
102
+ end
103
+ response.each do |key, value|
104
+ if value.kind_of?( Resource )
105
+ response[key] = json_url_for( object_path( value ) )
106
+ end
107
+ end
108
+ response
109
+ end
110
+
111
+ end
112
+ end
113
+ end
114
+
115
+ class String
116
+ def classify
117
+ if self =~ %r{/}
118
+ split( '/' ).collect( &:classify ).join( '::' )
119
+ elsif self =~ %r{_}
120
+ split( '_' ).collect( &:classify ).join( '' )
121
+ else
122
+ capitalize
123
+ end
124
+ end
125
+
126
+ def constantize
127
+ eval( classify )
128
+ end
129
+
130
+ def underscore
131
+ gsub(/([a-zA-Z])([A-Z])/, '\1_\2').downcase
132
+ end
133
+
134
+ def humanize
135
+ split( '_' ).collect( &:capitalize ).join( ' ' )
136
+ end
137
+
138
+ #poor man's...
139
+ def pluralize
140
+ "#{self}s"
141
+ end
142
+ end
@@ -0,0 +1,19 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'jobs/models/job'
18
+ require 'jobs/routes'
19
+
@@ -0,0 +1,35 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ module Backstage
18
+ class Job
19
+ include HasMBean
20
+ include TorqueBoxManaged
21
+ include Resource
22
+
23
+ def self.filter
24
+ "torquebox.jobs:*"
25
+ end
26
+
27
+ def self.to_hash_attributes
28
+ super + [:name, :app, :app_name, :ruby_class_name, :status, :cron_expression]
29
+ end
30
+
31
+ def available_actions
32
+ status == 'Started' ? %w{stop} : %w{start}
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ Backstage::Application.resource :job, :actions => [:start, :stop]
18
+
@@ -0,0 +1,18 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'message_processors/models/message_processor'
18
+ require 'message_processors/routes'
@@ -0,0 +1,40 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ module Backstage
18
+ class MessageProcessor
19
+ include HasMBean
20
+ include TorqueBoxManaged
21
+ include Resource
22
+
23
+ def self.filter
24
+ "torquebox.messaging.processors:*"
25
+ end
26
+
27
+ def self.to_hash_attributes
28
+ super + [:name, :app, :app_name, :status, :destination_name, :message_selector, :concurrency]
29
+ end
30
+
31
+ def destination_name
32
+ name = super
33
+ name =~ /\[(.*)\]/ ? $1 : name
34
+ end
35
+
36
+ def available_actions
37
+ status == 'Started' ? %w{ stop } : %w{ start }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ Backstage::Application.resource :message_processor, :actions => [:start, :stop]
18
+
@@ -0,0 +1,18 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'pools/models/pool'
18
+ require 'pools/routes'
@@ -0,0 +1,51 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ module Backstage
18
+ class Pool
19
+ include HasMBean
20
+ include TorqueBoxManaged
21
+ include Resource
22
+
23
+ def self.filter
24
+ "torquebox.pools:*"
25
+ end
26
+
27
+ def self.to_hash_attributes
28
+ super + [:name, :app, :app_name, :pool_type, :size, :available, :borrowed, :minimum_instances, :maximum_instances]
29
+ end
30
+
31
+ def pool_type
32
+ full_name =~ /type=(.*?)(,|$)/ ? $1 : ''
33
+ end
34
+
35
+ def size
36
+ shared? ? 1 : mbean.size
37
+ end
38
+
39
+ %w{ available borrowed minimum_instances maximum_instances }.each do |meth|
40
+ define_method meth do
41
+ shared? ? 0 : mbean.__send__( meth )
42
+ end
43
+ end
44
+
45
+ def shared?
46
+ pool_type == 'shared'
47
+ end
48
+
49
+ end
50
+ end
51
+
@@ -0,0 +1,41 @@
1
+ #
2
+ # Copyright 2011 Red Hat, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ Backstage::Application.resource :pool
18
+
19
+
20
+ module Backstage
21
+ class Application < Sinatra::Base
22
+
23
+ post "/pool/:name/evaluate" do
24
+ content_type :json
25
+
26
+ object = Pool.find( Util.decode_name( params[:name] ) )
27
+ script = params[:script] || ''
28
+ response = { :script => script }
29
+ begin
30
+ result = object.evaluate( script )
31
+ response[:result] = result
32
+ rescue Exception => ex
33
+ response[:exception] = ex
34
+ response[:backtrace] = ex.backtrace
35
+ end
36
+
37
+ JSON.generate( response )
38
+ end
39
+
40
+ end
41
+ end