torquebox-stompbox 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/stompbox ADDED
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env jruby
2
+ #-*-ruby-*-
3
+ #
4
+ # Copyright 2011 Red Hat, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'thor'
19
+ require 'torquebox-rake-support'
20
+
21
+ class StompBoxCommand < Thor
22
+
23
+ DEPLOYMENT_NAME = 'torquebox-stompbox-knob.yml'
24
+ DATABASE_URL = 'postgres://stompbox:stompbox@localhost/stompbox'
25
+ DEPLOYMENTS = '/opt/deployments'
26
+
27
+ desc "deploy [--secure=username:password[,username:password]*] [--setup-db] [--auto-migrate] [--db-url=url] [--deployments=/path/to/deployments] [--api-key=yourapikey]", "Deploys StompBox to $TORQUEBOX_HOME/apps"
28
+ method_option :secure, :type => :hash, :default => nil
29
+ method_option :'setup-db', :type => :boolean, :default => false
30
+ method_option :'auto-migrate', :type => :boolean, :default => false
31
+ method_option :'db-url', :type => :string, :default => DATABASE_URL
32
+ method_option :'deployments', :type => :string, :default => DEPLOYMENTS
33
+ method_option :'api-key', :type => :string
34
+ def deploy
35
+ check
36
+ root = File.expand_path( File.join( File.dirname( __FILE__ ), '..' ) )
37
+ descriptor = TorqueBox::DeployUtils.basic_deployment_descriptor( :root => root,
38
+ :env => 'production' )
39
+ descriptor['environment'] = {}
40
+ descriptor['web'] = {'context'=>'/stompbox'}
41
+
42
+ if options[:secure]
43
+ descriptor['environment']['REQUIRE_AUTHENTICATION'] = true
44
+ credentials = []
45
+ options[:secure].each do |user, pass|
46
+ credentials << [user, pass]
47
+ end
48
+ prop_file = TorqueBox::DeployUtils.write_credentials( credentials )
49
+ puts ">> INFO: Wrote user/password entries to #{prop_file}"
50
+ else
51
+ puts ">> WARNING: deploying StompBox with no security - use the --secure=username:password option to secure it"
52
+ end
53
+
54
+ if options[:'setup-db']
55
+ setup
56
+ puts ">> INFO: Setting up database."
57
+ end
58
+
59
+ if options[:'auto-migrate']
60
+ descriptor['environment']['AUTO_MIGRATE'] = true
61
+ puts ">> INFO: Configuring database for auto-migrations."
62
+ else
63
+ puts ">> WARNING: database not configured for auto-migration."
64
+ end
65
+
66
+ if options[:'db-url']
67
+ descriptor['environment']['DATABASE_URL'] = options[:'db-url']
68
+ puts ">> INFO: Using database URL: #{options[:'db-url']}"
69
+ else
70
+ puts ">> INFO: Using default database URL: #{DATABASE_URL}"
71
+ end
72
+
73
+ if options[:deployments]
74
+ descriptor['environment']['DEPLOYMENTS'] = options[:deployments]
75
+ puts ">> INFO: Using deployments path: #{options[:deployments]}"
76
+ else
77
+ puts ">> INFO: Using default deployments path: #{DEPLOYMENTS}"
78
+ end
79
+
80
+ if options[:'api-key']
81
+ descriptor['environment']['API_KEY'] = options[:'api-key']
82
+ puts ">> INFO: Using user-supplied API Key"
83
+ else
84
+ descriptor['environment']['API_KEY'] = generate_api_key
85
+ puts ">> INFO: Generated random API key."
86
+ end
87
+
88
+ name, dir = TorqueBox::DeployUtils.deploy_yaml( descriptor, DEPLOYMENT_NAME )
89
+
90
+ puts ">> Deployed #{name} to #{dir}"
91
+ end
92
+
93
+ desc "setup", "Sets up the StompBox database. Use on a new installation before deploying."
94
+ def setup
95
+ check
96
+ setup_script = File.expand_path( File.join( File.dirname( __FILE__ ), '..', 'bin', 'dbsetup' ) )
97
+ sh "#{setup_script} install"
98
+ end
99
+
100
+ desc "undeploy", "Removes StompBox from $TORQUEBOX_HOME/apps"
101
+ def undeploy
102
+ check
103
+ name, dir = TorqueBox::DeployUtils.undeploy( DEPLOYMENT_NAME )
104
+ puts ">> Undeployed #{name} from #{dir}"
105
+ end
106
+
107
+ desc "info", "Prints info about StompBox's status"
108
+ def info
109
+ check
110
+ path = File.join( ENV['TORQUEBOX_HOME'], 'apps', DEPLOYMENT_NAME )
111
+ if File.exists?( path )
112
+ puts ">> StompBox deployed to #{path}"
113
+ else
114
+ puts ">> StompBox is not deployed"
115
+ end
116
+ end
117
+
118
+ protected
119
+ def check
120
+ raise Exception.new("$TORQUEBOX_HOME must be set") unless ENV['TORQUEBOX_HOME']
121
+ end
122
+
123
+ def generate_api_key(size = 40)
124
+ charset = %w{ 2 3 4 6 7 9 A C D E F G H J K L M N P Q R T V W X Y Z}
125
+ (0...size).map{ charset.to_a[rand(charset.size)] }.join
126
+ end
127
+
128
+ end
129
+
130
+ StompBoxCommand.start
data/config.ru ADDED
@@ -0,0 +1,5 @@
1
+ require 'stompbox'
2
+
3
+ require 'sinatra/reloader'
4
+
5
+ run StompBox::Application
data/spec/auth_spec.rb ADDED
@@ -0,0 +1,50 @@
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
+ ENV['REQUIRE_AUTHENTICATION'] = 'true'
18
+ ENV['RACK_ENV'] = 'test'
19
+
20
+ require 'spec_helper'
21
+
22
+ module StompBox
23
+
24
+ describe 'application with authentication' do
25
+
26
+ it "should redirect to login if credentials are not supplied" do
27
+ get '/'
28
+ last_response.should be_redirect
29
+ end
30
+
31
+ it "should redirect to login if credentials are incorrect" do
32
+ authenticator = Object.new
33
+ TorqueBox::Authentication.stub!(:default).and_return(authenticator)
34
+ authenticator.stub!(:authenticate).and_return(false)
35
+ post '/login', {:user=>:foo, :password=>:bar}
36
+ last_response.should be_redirect
37
+ end
38
+
39
+ it "should allow access when authorized" do
40
+ rack_mock_session.stub!('authenticated?').and_return(true)
41
+ get "/"
42
+ # last_response.should be_ok
43
+ puts "TODO: This test is incorrect"
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+
@@ -0,0 +1,30 @@
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 'rack/test'
18
+ require 'stompbox'
19
+
20
+ ENV['DATABASE_URL'] = 'postgres://stompbox:stompbox@localhost/stompbox'
21
+
22
+ def app
23
+ @app ||= StompBox::Application.new
24
+ end
25
+
26
+ RSpec.configure do |conf|
27
+ conf.include Rack::Test::Methods
28
+ end
29
+
30
+
@@ -0,0 +1,50 @@
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 'spec_helper'
18
+
19
+ module StompBox
20
+
21
+ describe 'routes' do
22
+
23
+ it "should respond to GET /" do
24
+ get '/'
25
+ last_response.should be_ok
26
+ end
27
+
28
+ it "should respond to GET /repositories" do
29
+ get '/repositories'
30
+ last_response.should be_ok
31
+ end
32
+
33
+ it "should respond to POST /repositories" do
34
+ post '/repositories'
35
+ last_response.should be_redirect
36
+ end
37
+
38
+ it "should respond to PUT /repositories" do
39
+ put '/repositories/1'
40
+ last_response.should be_redirect
41
+ end
42
+
43
+ it "should respond to DELETE /repositories/:id" do
44
+ delete '/repositories/1'
45
+ last_response.should be_redirect
46
+ end
47
+
48
+ end
49
+
50
+ end
data/stompbox.rb ADDED
@@ -0,0 +1,148 @@
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
+ $: << File.join(File.dirname(__FILE__), 'app')
18
+
19
+ require 'rubygems'
20
+ require 'sinatra/base'
21
+ require 'rack-flash'
22
+
23
+ require 'haml'
24
+ require 'sass'
25
+ require 'yaml'
26
+
27
+ require 'authentication'
28
+ require 'deployer'
29
+ require 'helpers'
30
+ require 'models'
31
+
32
+
33
+ if ENV['RACK_ENV'].to_s == 'development'
34
+ puts "StompBox: DATABASE_URL = #{ENV['DATABASE_URL']}"
35
+ puts "StompBox: DEPLOYMENTS = #{ENV['DEPLOYMENTS']}"
36
+ puts "StompBox: AUTO_MIGRATE = #{ENV['AUTO_MIGRATE']}"
37
+ puts "StompBox: API_KEY = #{ENV['API_KEY']}"
38
+ end
39
+
40
+ module StompBox
41
+
42
+ class Application < Sinatra::Base
43
+ include StompBox::Authentication
44
+ use Rack::Flash
45
+
46
+
47
+ enable :sessions, :logging, :method_override, :static
48
+ set :public, Proc.new { File.join(root, "public") }
49
+ set :root, Proc.new { File.expand_path(File.dirname(__FILE__)) }
50
+ set :views, Proc.new { File.join(File.dirname(__FILE__), "app", "views") }
51
+
52
+ if ENV['REQUIRE_AUTHENTICATION']
53
+ ['*/push/*','*/login','*/logout', '*.js', '/*.css' ].each do |p|
54
+ before p do
55
+ skip_authentication
56
+ end
57
+ end
58
+
59
+ before do
60
+ require_authentication
61
+ end
62
+ else
63
+ puts "StompBox: REQUIRE_AUTHENTICATION is not set, *disabling* authentication" if ENV['REQUIRE_AUTHENTICATION'].nil?
64
+ end
65
+
66
+ post '/deploy' do
67
+ if (params[:id] && (push = Push.get(params[:id])))
68
+ Deployer.deploy(push)
69
+ end
70
+ redirect to('/')
71
+ end
72
+
73
+ post '/undeploy' do
74
+ if (params[:id] && (push = Push.get(params[:id])))
75
+ Deployer.undeploy(push)
76
+ end
77
+ redirect to('/')
78
+ end
79
+
80
+ # Post a deployment
81
+ post '/push/:api_key' do
82
+ if params[:payload] && (params[:api_key] == config('API_KEY'))
83
+ push = Push.create(:payload=>params[:payload], :created_at=>Time.now)
84
+ push.save if push
85
+ end
86
+ redirect to('/')
87
+ end
88
+
89
+ # List all deployments
90
+ get '/' do
91
+ @pushes = Push.all(:order => [ :created_at.desc ])
92
+ haml :'pushes/index'
93
+ end
94
+
95
+ get '/repositories' do
96
+ repositories
97
+ haml :'repositories/index'
98
+ end
99
+
100
+ post '/repositories' do
101
+ if params[:repository]
102
+ repo = Repository.create(params[:repository])
103
+ flash[:error] = repo.errors unless repo.save
104
+ end
105
+ redirect to("/repositories")
106
+ end
107
+
108
+ delete '/repositories/:id' do
109
+ if repo = Repository.get(params[:id])
110
+ repo.destroy
111
+ end
112
+ redirect to("/repositories")
113
+ end
114
+
115
+ put '/repositories/:id' do
116
+ if repo = Repository.get(params[:id])
117
+ repo.update(params[:repository])
118
+ repo.save
119
+ end
120
+ redirect to("repositories")
121
+ end
122
+
123
+ # Stylesheets - reset
124
+ get '/css/html5reset.css' do
125
+ sass :'/css/html5reset'
126
+ end
127
+
128
+ # App stylesheet
129
+ get '/css/styles.css' do
130
+ scss :'/css/styles'
131
+ end
132
+
133
+ get '/login' do
134
+ haml :'sessions/new'
135
+ end
136
+
137
+ post '/login' do
138
+ flash[:notice] = "Bad credentials. Try again?" unless authenticate( params[:user], params[:password] )
139
+ redirect to('/')
140
+ end
141
+
142
+ get '/logout' do
143
+ logout
144
+ end
145
+
146
+ run! if app_file == $0
147
+ end
148
+ end