deep_thought 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +184 -0
  5. data/Rakefile +50 -0
  6. data/config.ru +5 -0
  7. data/db/migrate/20130414012805_create_projects.rb +15 -0
  8. data/db/migrate/20130426165010_create_states.rb +14 -0
  9. data/db/migrate/20130501003758_create_users.rb +14 -0
  10. data/db/migrate/20130508001711_add_api_key_to_users.rb +9 -0
  11. data/db/migrate/20130509014935_create_deploys.rb +27 -0
  12. data/db/migrate/20130514062148_create_delayed_jobs.rb +21 -0
  13. data/db/migrate/20130516042149_change_delayed_job_last_error_to_text.rb +9 -0
  14. data/db/migrate/20130517010210_add_ci_to_projects.rb +9 -0
  15. data/db/migrate/20130525001332_add_notification_url_to_users.rb +9 -0
  16. data/db/migrate/20131127022704_remove_deploy_type_and_ci_from_projects.rb +11 -0
  17. data/db/schema.rb +72 -0
  18. data/deep_thought.gemspec +45 -0
  19. data/lib/deep_thought.rb +62 -0
  20. data/lib/deep_thought/api.rb +156 -0
  21. data/lib/deep_thought/app.rb +342 -0
  22. data/lib/deep_thought/ci_service.rb +39 -0
  23. data/lib/deep_thought/ci_service/janky.rb +34 -0
  24. data/lib/deep_thought/deployer.rb +180 -0
  25. data/lib/deep_thought/deployer/deployer.rb +17 -0
  26. data/lib/deep_thought/deployer/shell.rb +45 -0
  27. data/lib/deep_thought/git.rb +76 -0
  28. data/lib/deep_thought/models/deploy.rb +27 -0
  29. data/lib/deep_thought/models/project.rb +32 -0
  30. data/lib/deep_thought/models/state.rb +6 -0
  31. data/lib/deep_thought/models/user.rb +45 -0
  32. data/lib/deep_thought/notifier.rb +13 -0
  33. data/lib/deep_thought/public/assets/javascripts/jquery.min.js +6 -0
  34. data/lib/deep_thought/public/assets/javascripts/main.js +36 -0
  35. data/lib/deep_thought/public/assets/stylesheets/main.css +438 -0
  36. data/lib/deep_thought/public/robots.txt +2 -0
  37. data/lib/deep_thought/scaler.rb +35 -0
  38. data/lib/deep_thought/tasks.rb +65 -0
  39. data/lib/deep_thought/version.rb +3 -0
  40. data/lib/deep_thought/views/history/index.haml +4 -0
  41. data/lib/deep_thought/views/history/show.haml +68 -0
  42. data/lib/deep_thought/views/layouts/layout.haml +32 -0
  43. data/lib/deep_thought/views/projects/edit.haml +15 -0
  44. data/lib/deep_thought/views/projects/index.haml +7 -0
  45. data/lib/deep_thought/views/projects/new.haml +8 -0
  46. data/lib/deep_thought/views/projects/show.haml +25 -0
  47. data/lib/deep_thought/views/sessions/login.haml +8 -0
  48. data/lib/deep_thought/views/users/index.haml +7 -0
  49. data/lib/deep_thought/views/users/new.haml +12 -0
  50. data/lib/deep_thought/views/users/show.haml +38 -0
  51. data/script/bootstrap +3 -0
  52. data/script/console +7 -0
  53. data/script/server +7 -0
  54. data/script/test +3 -0
  55. data/test/deep_thought_api_test.rb +114 -0
  56. data/test/deep_thought_app_test.rb +315 -0
  57. data/test/deep_thought_ci_service_test.rb +44 -0
  58. data/test/deep_thought_deployer_test.rb +72 -0
  59. data/test/deep_thought_git_test.rb +62 -0
  60. data/test/deep_thought_janky_test.rb +42 -0
  61. data/test/deep_thought_notifier_test.rb +35 -0
  62. data/test/deep_thought_project_test.rb +34 -0
  63. data/test/deep_thought_shell_test.rb +38 -0
  64. data/test/fixtures/project-test/HEAD +1 -0
  65. data/test/fixtures/project-test/config +8 -0
  66. data/test/fixtures/project-test/description +1 -0
  67. data/test/fixtures/project-test/hooks/applypatch-msg.sample +15 -0
  68. data/test/fixtures/project-test/hooks/commit-msg.sample +24 -0
  69. data/test/fixtures/project-test/hooks/post-update.sample +8 -0
  70. data/test/fixtures/project-test/hooks/pre-applypatch.sample +14 -0
  71. data/test/fixtures/project-test/hooks/pre-commit.sample +49 -0
  72. data/test/fixtures/project-test/hooks/pre-push.sample +54 -0
  73. data/test/fixtures/project-test/hooks/pre-rebase.sample +169 -0
  74. data/test/fixtures/project-test/hooks/prepare-commit-msg.sample +36 -0
  75. data/test/fixtures/project-test/hooks/update.sample +128 -0
  76. data/test/fixtures/project-test/info/exclude +6 -0
  77. data/test/fixtures/project-test/objects/13/8b8baba400e64fc384d468e80f10cf707e38d7 +1 -0
  78. data/test/fixtures/project-test/objects/6b/8fb16e1675767742afc70ac2ce038da46461f8 +0 -0
  79. data/test/fixtures/project-test/objects/7f/fc496d150b8a0015e3a16be8bacd47daed1a1c +0 -0
  80. data/test/fixtures/project-test/objects/8f/8606174a2aa11545d119fe3c4d6840fe0c4825 +0 -0
  81. data/test/fixtures/project-test/objects/9c/362942288a8ff6dc1addf7c5199bd9837e09de +0 -0
  82. data/test/fixtures/project-test/objects/b5/f7341de6145d3f8b95877cd2675d798fd172f1 +0 -0
  83. data/test/fixtures/project-test/objects/d1/f1e8664b0326316fb18796ac4ae1171e239cb6 +1 -0
  84. data/test/fixtures/project-test/objects/e6/d7ad6d61242936a2cd2bb9ae236adf43e59718 +0 -0
  85. data/test/fixtures/project-test/objects/fa/4cf5ff52462d17b6378a06fae8771c13b654b0 +1 -0
  86. data/test/fixtures/project-test/objects/fa/dda6bc20afa5cf93196bfb08a4989ee3b75ed7 +0 -0
  87. data/test/fixtures/project-test/packed-refs +4 -0
  88. data/test/fixtures/project-test/refs/heads/.gitkeep +0 -0
  89. data/test/fixtures/project-test/refs/tags/.gitkeep +0 -0
  90. data/test/test_helper.rb +42 -0
  91. metadata +462 -0
@@ -0,0 +1,45 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'deep_thought/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "deep_thought"
8
+ gem.version = DeepThought::VERSION
9
+ gem.authors = ["Ian Lollar"]
10
+ gem.email = ["rhv@redhotvengeance.com"]
11
+ gem.description = "Deploy smart, not hard."
12
+ gem.summary = "Deploy smart, not hard."
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split("\n") - %w[.gitignore]
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "rake", "~>10.1"
21
+ gem.add_dependency "sinatra", "~>1.4"
22
+ gem.add_dependency "sinatra-contrib", "~>1.4"
23
+ gem.add_dependency "json", "~>1.8"
24
+ gem.add_dependency "activerecord", "~>3.2"
25
+ gem.add_dependency "pg", "~>0.17"
26
+ gem.add_dependency "rugged", "~>0.19"
27
+ gem.add_dependency "httparty", "~>0.12"
28
+ gem.add_dependency "bcrypt-ruby", "~>3.1"
29
+ gem.add_dependency "rack-ssl", "~>1.3"
30
+ gem.add_dependency "rack_csrf", "~>2.4"
31
+ gem.add_dependency "haml", "~>4.0"
32
+ gem.add_dependency "delayed_job_active_record", "~>0.4"
33
+
34
+ # # development
35
+ gem.add_development_dependency "shotgun", "~>0.9"
36
+ gem.add_development_dependency "thin", "~>1.6"
37
+ gem.add_development_dependency "racksh", "~>1.0.0"
38
+
39
+ # # testing
40
+ gem.add_development_dependency "minitest", "~>4.7"
41
+ gem.add_development_dependency "mocha", "~>0.14"
42
+ gem.add_development_dependency "database_cleaner", "~>1.2"
43
+ gem.add_development_dependency "turn", "~>0.9"
44
+ gem.add_development_dependency "capybara", "~>2.2"
45
+ end
@@ -0,0 +1,62 @@
1
+ require 'active_record'
2
+ require 'delayed_job_active_record'
3
+
4
+ require 'deep_thought/app'
5
+ require 'deep_thought/api'
6
+ require 'deep_thought/models/deploy'
7
+ require 'deep_thought/models/project'
8
+ require 'deep_thought/models/state'
9
+ require 'deep_thought/models/user'
10
+ require 'deep_thought/deployer'
11
+ require 'deep_thought/deployer/shell'
12
+ require 'deep_thought/ci_service'
13
+ require 'deep_thought/ci_service/janky'
14
+ require 'deep_thought/notifier'
15
+ require 'deep_thought/scaler'
16
+ require 'deep_thought/version'
17
+
18
+ module DeepThought
19
+ def self.setup(settings)
20
+ env = settings['RACK_ENV']
21
+
22
+ if env != "production"
23
+ settings["DATABASE_URL"] ||= "postgres://deep_thought@localhost/deep_thought_#{env}"
24
+ end
25
+
26
+ database = URI(settings["DATABASE_URL"])
27
+
28
+ connection = {
29
+ :adapter => "postgresql",
30
+ :encoding => "unicode",
31
+ :database => database.path[1..-1],
32
+ :pool => 5,
33
+ :username => database.user,
34
+ :password => database.password,
35
+ :host => database.host,
36
+ :port => database.port
37
+ }
38
+
39
+ ActiveRecord::Base.establish_connection(connection)
40
+
41
+ BCrypt::Engine.cost = 12
42
+
43
+ if settings['CI_SERVICE']
44
+ DeepThought::CIService.setup(settings)
45
+ end
46
+ end
47
+
48
+ def self.app
49
+ @app ||= Rack::Builder.app {
50
+ map '/' do
51
+ run DeepThought::App
52
+ end
53
+
54
+ map '/deploy/' do
55
+ run DeepThought::Api
56
+ end
57
+ }
58
+ end
59
+
60
+ DeepThought::Deployer.register_adapter('shell', DeepThought::Deployer::Shell)
61
+ DeepThought::CIService.register_adapter('janky', DeepThought::CIService::Janky)
62
+ end
@@ -0,0 +1,156 @@
1
+ require 'sinatra'
2
+ require 'sinatra/json'
3
+ require 'rack/ssl'
4
+ require 'deep_thought/git'
5
+ require 'deep_thought/deployer'
6
+
7
+ module DeepThought
8
+ class Api < Sinatra::Base
9
+ helpers Sinatra::JSON
10
+
11
+ if ENV['RACK_ENV'] != 'development' && ENV['RACK_ENV'] != 'test'
12
+ use Rack::SSL
13
+ end
14
+
15
+ before '*' do
16
+ if !is_json?
17
+ halt 400, 'I prefer to converse in JSON.'
18
+ end
19
+
20
+ if request.env['HTTP_AUTHORIZATION'] =~ /Token token="[a-zA-Z0-9\+=]+"/
21
+ token = request.env['HTTP_AUTHORIZATION'].gsub(/Token token="/, '').gsub(/"/, '')
22
+
23
+ @user = DeepThought::User.find_by_api_key("#{token}")
24
+
25
+ if !@user
26
+ halt 401
27
+ end
28
+ else
29
+ halt 401
30
+ end
31
+ end
32
+
33
+ get '/status' do
34
+ if DeepThought::Deployer.is_deploying?
35
+ [500, "I'm currently in mid-deployment."]
36
+ else
37
+ [200, "I'm ready to ponder the infinitely complex questions of the universe."]
38
+ end
39
+ end
40
+
41
+ get '*' do
42
+ [401, "I don't got what you're trying to GET."]
43
+ end
44
+
45
+ post '/' do
46
+ [500, "Must supply app name."]
47
+ end
48
+
49
+ post '/:app' do
50
+ raw = request.body.read
51
+ data = JSON.parse(raw) if raw != ''
52
+
53
+ app = params[:app]
54
+
55
+ if data
56
+ branch = data['branch'] || 'master'
57
+ actions = data['actions'] if data['actions']
58
+ environment = data['environment'] if data['environment']
59
+ box = data['box'] if data['box']
60
+ on_behalf_of = data['on_behalf_of'] if data['on_behalf_of']
61
+ variables = data['variables'] if data['variables']
62
+ else
63
+ branch = 'master'
64
+ end
65
+
66
+ project = DeepThought::Project.find_by_name(app)
67
+
68
+ if !project
69
+ return [500, "Hmm, that project doesn't appear to exist. Have you set it up?"]
70
+ end
71
+
72
+ options = {}
73
+ options[:branch] = branch
74
+
75
+ response = "executing deploy"
76
+
77
+ if actions
78
+ options[:actions] = actions
79
+
80
+ actions.each do |action|
81
+ response += "/#{action}"
82
+ end
83
+ end
84
+
85
+ response += " #{app}/#{branch}"
86
+
87
+ if environment
88
+ options[:environment] = environment
89
+ response += " to #{environment}"
90
+
91
+ if box
92
+ options[:box] = box
93
+ response += "/#{box}"
94
+ end
95
+ end
96
+
97
+ if variables
98
+ options[:variables] = variables
99
+ response += " with #{variables.to_s}"
100
+ end
101
+
102
+ if on_behalf_of
103
+ options[:on_behalf_of] = on_behalf_of
104
+ response += " on behalf of #{on_behalf_of}"
105
+ end
106
+
107
+ begin
108
+ DeepThought::Deployer.create(project, @user, 'api', options)
109
+
110
+ response
111
+ rescue => e
112
+ if ENV['RACK_ENV'] != 'test'
113
+ puts e.inspect
114
+ puts e.backtrace
115
+ end
116
+
117
+ [500, e.message]
118
+ end
119
+ end
120
+
121
+ post '/setup/:app' do
122
+ raw = request.body.read
123
+ data = JSON.parse(raw) if raw != ''
124
+
125
+ app = params[:app]
126
+
127
+ if data
128
+ repo_url = data['repo_url'] if data['repo_url']
129
+ end
130
+
131
+ if !repo_url
132
+ return [500, "Sorry, but I need a project name and repo url. No exceptions, despite how nicely you ask."]
133
+ end
134
+
135
+ project = Project.new(:name => app, :repo_url => repo_url)
136
+
137
+ if project.save
138
+ [200, "Set up new project called #{app} which pulls from #{repo_url}."]
139
+ else
140
+ [422, "Shit, something went wrong: #{project.errors.messages}."]
141
+ end
142
+ end
143
+
144
+ helpers do
145
+ def is_json?
146
+ is_json = false
147
+
148
+ request.accept.each do |a|
149
+ is_json = true if a.to_s == 'application/json'
150
+ end
151
+
152
+ is_json
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,342 @@
1
+ require 'sinatra'
2
+ require 'sinatra/json'
3
+ require 'rack/ssl'
4
+ require 'rack/csrf'
5
+ require 'haml'
6
+
7
+ module DeepThought
8
+ class App < Sinatra::Base
9
+ helpers Sinatra::JSON
10
+
11
+ set :root, File.dirname(__FILE__)
12
+ set :public_folder, File.join(root, 'public')
13
+ set :views, File.join(root, 'views')
14
+ set :haml, :layout => :"layouts/layout"
15
+
16
+ if ENV['RACK_ENV'] != 'development' && ENV['RACK_ENV'] != 'test'
17
+ use Rack::SSL
18
+ end
19
+
20
+ use Rack::Session::Cookie, :secret => ENV['SESSION_SECRET']
21
+ use Rack::Csrf, :raise => true
22
+ use Rack::MethodOverride
23
+
24
+ set :deep_thought_message, "Hello, human."
25
+
26
+ before /^(?!\/(login|logout))/ do
27
+ if !session[:user_id]
28
+ redirect '/login'
29
+ end
30
+ end
31
+
32
+ get '/' do
33
+ projects = DeepThought::Project.all
34
+
35
+ if projects.count > 0
36
+ settings.deep_thought_message = "Deep Thought has the answer."
37
+ else
38
+ settings.deep_thought_message = "Deep Thought knows of no projects."
39
+ end
40
+
41
+ haml :"projects/index", :locals => {:projects => projects}
42
+ end
43
+
44
+ get '/login' do
45
+ if session[:user_id]
46
+ redirect '/'
47
+ end
48
+
49
+ haml :"sessions/login"
50
+ end
51
+
52
+ post '/login' do
53
+ user = User.authenticate(params[:email], params[:password])
54
+
55
+ if user
56
+ session[:user_id] = user.id
57
+ redirect '/'
58
+ else
59
+ redirect '/login'
60
+ end
61
+ end
62
+
63
+ delete '/logout' do
64
+ session[:user_id] = nil
65
+ redirect '/login'
66
+ end
67
+
68
+ get '/projects/add/new' do
69
+ project = DeepThought::Project.new
70
+
71
+ haml :"projects/new", :locals => {:project => project}
72
+ end
73
+
74
+ get '/project/add/new' do
75
+ redirect "/projects/add/new"
76
+ end
77
+
78
+ post '/projects/add/new' do
79
+ project = DeepThought::Project.new(params[:project])
80
+
81
+ if project.save
82
+ redirect '/'
83
+ else
84
+ settings.deep_thought_message = "Deep Thought has a problem with your request."
85
+ haml :"projects/new", :locals => {:project => project}
86
+ end
87
+ end
88
+
89
+ get '/projects/edit/:name' do
90
+ project = DeepThought::Project.find_by_name(params[:name])
91
+
92
+ haml :"projects/edit", :locals => {:project => project}
93
+ end
94
+
95
+ get '/project/edit/:name' do
96
+ redirect "/projects/edit/#{params[:name]}"
97
+ end
98
+
99
+ put '/projects/edit/:name' do
100
+ project = DeepThought::Project.find_by_name(params[:name])
101
+
102
+ if project.update_attributes(params[:project])
103
+ redirect "/projects/#{project.name}"
104
+ else
105
+ settings.deep_thought_message = "Deep Thought has a problem with your request."
106
+ haml :"projects/edit", :locals => {:project => project}
107
+ end
108
+ end
109
+
110
+ delete '/projects/delete/:name' do
111
+ project = DeepThought::Project.find_by_name(params[:name])
112
+
113
+ project.destroy
114
+
115
+ redirect '/'
116
+ end
117
+
118
+ get '/projects/:name' do
119
+ project = DeepThought::Project.find_by_name(params[:name])
120
+ branches = DeepThought::Git.get_list_of_branches(project) || []
121
+
122
+ if branches.include?('master')
123
+ branches.unshift(branches.slice!(branches.index('master')))
124
+ end
125
+
126
+ deploy = DeepThought::Deploy.new
127
+
128
+ settings.deep_thought_message = "Now pondering: #{project.name}."
129
+
130
+ haml :"projects/show", :locals => {:project => project, :branches => branches, :deploy => deploy}
131
+ end
132
+
133
+ get '/project/:name' do
134
+ redirect "/projects/#{params[:name]}"
135
+ end
136
+
137
+ get '/projects/:name/deploys' do
138
+ project = DeepThought::Project.find_by_name(params[:name])
139
+ deploys = project.deploys.order('created_at DESC')
140
+
141
+ settings.deep_thought_message = "Now remembering: #{project.name}."
142
+
143
+ haml :"history/index", :locals => {:project => project, :deploys => deploys}
144
+ end
145
+
146
+ get '/project/:name/deploys' do
147
+ redirect "/projects/#{params[:name]}/deploys"
148
+ end
149
+
150
+ get '/projects/:name/deploys/:id' do
151
+ project = DeepThought::Project.find_by_name(params[:name])
152
+ deploy = project.deploys.find(params[:id])
153
+
154
+ settings.deep_thought_message = "Now remembering #{project.name} deploy: #{deploy.id}."
155
+
156
+ haml :"history/show", :locals => {:project => project, :deploy => deploy}
157
+ end
158
+
159
+ get '/projects/:name/deploy/:id' do
160
+ redirect "/projects/#{params[:name]}/deploys/#{params[:id]}"
161
+ end
162
+
163
+ get '/project/:name/deploys/:id' do
164
+ redirect "/projects/#{params[:name]}/deploys/#{params[:id]}"
165
+ end
166
+
167
+ get '/project/:name/deploy/:id' do
168
+ redirect "/projects/#{params[:name]}/deploys/#{params[:id]}"
169
+ end
170
+
171
+ post '/projects/:name/deploy' do
172
+ project = DeepThought::Project.find_by_name(params[:name])
173
+ branches = DeepThought::Git.get_list_of_branches(project) || []
174
+
175
+ if branches.include?('master')
176
+ branches.unshift(branches.slice!(branches.index('master')))
177
+ end
178
+
179
+ options = {}
180
+ options[:branch] = params[:deploy][:branch]
181
+ options[:environment] = params[:deploy][:environment] if !params[:deploy][:environment].blank?
182
+ options[:box] = params[:deploy][:box] if !params[:deploy][:box].blank?
183
+
184
+ if params[:deploy][:actions]
185
+ actions = params[:deploy][:actions]
186
+ actions.reject!(&:empty?).blank?
187
+
188
+ if actions.count > 0
189
+ options[:actions] = actions
190
+ end
191
+ end
192
+
193
+ if params[:deploy][:variables]
194
+ vars = params[:deploy][:variables]
195
+ keys = vars[:keys]
196
+ values = vars[:values]
197
+
198
+ variables = Hash[keys.zip(values)]
199
+ variables.reject! { |k, v| k == '' }
200
+ variables.reject! { |k, v| v == '' }
201
+
202
+ if variables.count > 0
203
+ options[:variables] = variables
204
+ end
205
+ end
206
+
207
+ begin
208
+ DeepThought::Deployer.create(project, current_user, 'web', options)
209
+
210
+ redirect "/projects/#{params[:name]}"
211
+ rescue => e
212
+ if ENV['RACK_ENV'] != 'test'
213
+ puts e.inspect
214
+ puts e.backtrace
215
+ end
216
+
217
+ settings.deep_thought_message = e.message
218
+ haml :"projects/show", :locals => {:project => project, :branches => branches}
219
+ end
220
+ end
221
+
222
+ get '/users' do
223
+ users = DeepThought::User.all
224
+
225
+ settings.deep_thought_message = "Deep Thought loves you."
226
+
227
+ haml :"users/index", :locals => {:users => users}
228
+ end
229
+
230
+ get '/users/new' do
231
+ user = DeepThought::User.new
232
+
233
+ haml :"users/new", :locals => {:user => user}
234
+ end
235
+
236
+ post '/users/new' do
237
+ user = DeepThought::User.new(params[:user])
238
+
239
+ if user.save
240
+ redirect '/users'
241
+ else
242
+ settings.deep_thought_message = "Deep Thought has a problem with your request."
243
+ haml :"users/new", :locals => {:user => user}
244
+ end
245
+ end
246
+
247
+ get '/users/:id' do
248
+ user = User.find(params[:id])
249
+
250
+ settings.deep_thought_message = "Deep Thought loves you."
251
+
252
+ haml :"users/show", :locals => {:user => user}
253
+ end
254
+
255
+ put '/users/:id' do
256
+ user = DeepThought::User.find(params[:id])
257
+
258
+ if user.update_attributes(params[:user])
259
+ redirect '/users'
260
+ else
261
+ settings.deep_thought_message = "Deep Thought has a problem with your request."
262
+ haml :"users/show", :locals => {:user => user}
263
+ end
264
+ end
265
+
266
+ delete '/users/:id' do
267
+ user = DeepThought::User.find(params[:id])
268
+
269
+ user.destroy
270
+
271
+ redirect '/users'
272
+ end
273
+
274
+ post '/users/:id/key' do
275
+ user = DeepThought::User.find(params[:id])
276
+
277
+ user.generate_api_key
278
+
279
+ redirect "/users/#{params[:id]}"
280
+ end
281
+
282
+ get '/deploying' do
283
+ pass unless is_json?
284
+
285
+ json :deploying => DeepThought::Deployer.is_deploying?
286
+ end
287
+
288
+ get '*' do
289
+ redirect '/'
290
+ end
291
+
292
+ helpers do
293
+ def csrf_tag
294
+ Rack::Csrf.csrf_tag(env)
295
+ end
296
+
297
+ def current_user
298
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
299
+ end
300
+
301
+ def current_route(item)
302
+ path = request.path_info
303
+
304
+ case item
305
+ when 'projects'
306
+ if path == '/' or path =~ %r{/projects}
307
+ "current"
308
+ end
309
+ when 'users'
310
+ unless path == "/users/#{@current_user.id}"
311
+ if path =~ %r{/users}
312
+ "current"
313
+ end
314
+ end
315
+ when 'me'
316
+ if path == "/users/#{@current_user.id}"
317
+ "current"
318
+ end
319
+ end
320
+ end
321
+
322
+ def is_json?
323
+ is_json = false
324
+
325
+ request.accept.each do |a|
326
+ is_json = true if a.to_s == 'application/json'
327
+ end
328
+
329
+ is_json
330
+ end
331
+
332
+ def is_deploying?
333
+ if DeepThought::Deployer.is_deploying?
334
+ settings.deep_thought_message = "Deep Thought is deploying..."
335
+ true
336
+ else
337
+ false
338
+ end
339
+ end
340
+ end
341
+ end
342
+ end