drnic / rails-templates forked from jeremymcanally/rails-templates

A library of neat Rails application templates.

This URL has Read+Write access

rails-templates / mocra-slicehost.rb
100644 382 lines (319 sloc) 12.974 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# mocra.rb
# from Dr Nic Williams @ http://mocra.com + http://drnicwilliams.com
#
# Optional:
# DOMAIN - parent domain (default: mocra.com)
# SKIP_GEMS=1 - don't install gems (useful if you know they are already installed)
#
# The following are just for twitter oauth registration:
# ORGANIZATION - name of your company (default: Mocra)
# DESCRIPTION - description of your app (default: This is a cool app)
#
# Required gems for this template:
# gem install highline
# gem install deprec
# gem install defunkt-github --source http://gems.github.com
# gem install uhlenbrock-slicehost-tools --source=http://gems.github.com
# gem install drnic-twitter --source=http://gems.github.com
#
# Some setup steps (if wanting twitter_auth support)
# twitter install
# twitter add
# -> enter username + password; repeat if you have multiple twitter accounts
 
# wrap template commands in block so their execution can be controlled
# in unit testing
def template(&block)
  @store_template = block
end
 
template do
  app_name = File.basename(root)
  application = app_name.gsub(/[_-]/, ' ').titleize
  app_subdomain = app_name.gsub(/[_\s]/, '-').downcase
  app_db = app_name.gsub(/[-\s]/, '_').downcase
  domain = ENV['DOMAIN'] || 'mocra.com'
  app_url = "#{app_subdomain}.#{domain}"
  organization = ENV['ORGANIZATION'] || "Mocra"
  description = ENV['DESCRIPTION'] || 'This is a cool app'
  skip_gems = ENV['SKIP_GEMS']
 
  github_user = run("git config --get github.user").strip
  if github_user.blank?
    puts <<-EOS.gsub(/^ /, '')
You need to install your github username and API token.
* Go to http://github.com/accounts
* Click "Global Git Config"
* Execute the two lines displayed
* Run "git config --list" to check that github.user and github.token are installed
EOS
    exit
  end
  
# Authentication selection
  auth = highline.choose(*%w[none restful_authentication twitter_auth]) do |menu|
    menu.prompt = "Which user authentication system? "
  end
  authentication = auth != "none"
  twitter_auth = auth == "twitter_auth"
  restful_authentication = auth == "restful_authentication"
 
# Setup slicehost slice
 
  slice_name = highline.choose(*slice_names) do |menu|
    menu.prompt = "Install http://#{app_url} application on which slice? "
  end
  run "slicehost-dns add_cname #{domain} #{app_subdomain} #{slice_name}"
 
# Setup twitter oauth on twitter.com
  if twitter_auth
    twitter_users = `twitter list | grep "^[* ] " | sed -e "s/[* ] //"`.split
    if twitter_users.size > 1
      twitter_user = highline.choose(*twitter_users) { |menu| menu.prompt = "Which twitter user? " }
    else
      twitter_user = twitter_users.first
    end
    twitter_permission = highline.choose("read-only", "read-write") do |menu|
      menu.prompt = "What access level does the application need to user's twitter accounts? "
    end
    twitter_readwrite_flag = (twitter_permission == 'read-write') ? ' --readwrite' : ''
 
    message = run "twitter register_oauth #{twitter_user} '#{application}' http://#{app_url} '#{description}' organization='#{organization}' organization_url=http://#{domain}#{twitter_readwrite_flag}"
    twitter_auth_keys = parse_keys(message)
  end
 
# Public/private github repo
  repo_privacy = highline.choose('public', 'private') { |menu| menu.prompt = "Public/private github repo? " }
  is_private_github = repo_privacy == 'private'
  
# Delete unnecessary files
  run "rm README"
  run "rm public/index.html"
  run "rm public/favicon.ico"
  run "rm public/robots.txt"
  run "rm -f public/javascripts/*"
  run "rm -rf test"
 
  file "README.md", ""
  
  file 'config/database.yml', <<-EOS.gsub(/^ /, '')
development:
adapter: mysql
encoding: utf8
reconnect: false
database: #{app_db}_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
 
test:
adapter: mysql
encoding: utf8
reconnect: false
database: #{app_db}_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
 
production:
adapter: mysql
encoding: utf8
reconnect: false
database: #{app_db}_production
pool: 5
username: root
password:
EOS
# Copy database.yml for distribution use
  run "cp config/database.yml config/database.yml.example"
  
# Set up .gitignore files
  run "touch tmp/.gitignore log/.gitignore vendor/.gitignore"
  run %{find . -type d -empty | grep -v "vendor" | grep -v ".git" | grep -v "tmp" | xargs -I xxx touch xxx/.gitignore}
  file '.gitignore', <<-EOS.gsub(/^ /, '')
.DS_Store
log/*.log
tmp/**/*
config/database.yml
config/initializers/site_keys.rb
EOS
 
# Set up git repository and commit all work so far to the repository
  git :init
  git :add => '.'
  git :commit => "-a -m 'Initial commit'"
 
# Authentication gems/plugins
 
if twitter_auth
  plugin 'twitter_auth', :git => 'git://github.com/mbleigh/twitter-auth.git', :submodule => true
elsif restful_authentication
  plugin 'restful_authentication', :git => 'git://github.com/technoweenie/restful-authentication.git', :submodule => true
end
 
 
# Set up session store initializer
  initializer 'session_store.rb', <<-EOS.gsub(/^ /, '')
ActionController::Base.session = { :session_key => '_#{(1..6).map { |x| (65 + rand(26)).chr }.join}_session', :secret => '#{(1..40).map { |x| (65 + rand(26)).chr }.join}' }
ActionController::Base.session_store = :active_record_store
EOS
 
# Set up sessions
  rake 'db:create:all'
  rake 'db:sessions:create'
 
# Install submoduled plugins
  plugin 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :submodule => true
  plugin 'state_machine', :git => 'git://github.com/pluginaweek/state_machine.git', :submodule => true
  plugin 'rails_footnotes', :git => 'git://github.com/josevalim/rails-footnotes.git', :submodule => true
  plugin 'machinist', :git => 'git://github.com/notahat/machinist.git', :submodule => true
  plugin 'paperclip', :git => 'git://github.com/thoughtbot/paperclip.git', :submodule => true
  plugin 'cucumber', :git => 'git://github.com/aslakhellesoy/cucumber.git', :submodule => true
  plugin 'webrat', :git => 'git://github.com/brynary/webrat.git', :submodule => true
  plugin 'email-spec', :git => 'git://github.com/bmabey/email-spec.git', :submodule => true
  plugin 'rspec', :git => 'git://github.com/dchelimsky/rspec.git', :submodule => true
  plugin 'rspec-rails', :git => 'git://github.com/dchelimsky/rspec-rails.git', :submodule => true
 
# Gems
  gem 'javan-whenever', :lib => false, :version => '>= 0.1.7', :source => 'http://gems.github.com'
  
# Gems - testing
  gem 'fakeweb', :version => '>= 1.2.0', :env => 'test'
  gem 'faker', :version => '>= 0.3.1', :env => 'test'
 
# Set up RSpec, user model, OpenID, etc, and run migrations
  generate "rspec"
  generate "cucumber"
  generate "email_spec"
  
  if twitter_auth
    generate "twitter_auth', '--oauth"
  elsif restful_authentication
    generate 'authenticated', 'user session --include-activation --rspec'
  end
  
# Hook for layouts, assets
  generate 'app_layout'
 
  append_file 'features/support/env.rb', <<-EOS.gsub(/^ /, '')
require "email_spec/cucumber"
require File.dirname(__FILE__) + "/../../spec/blueprints"
 
Before do
FakeWeb.allow_net_connect = false
end
EOS
 
  append_file 'spec/spec_helper.rb', <<-EOS.gsub(/^ /, '')
require File.dirname(__FILE__) + '/blueprints'
EOS
  
  file 'spec/blueprints.rb', <<-EOS.gsub(/^ /, '')
# Use 'Ruby Machinst.tmbundle' Cmd+B to generate blueprints from class names
require "faker"
 
Sham.name { Faker::Name.name }
Sham.login { Faker::Internet.user_name.gsub(/\W/, '')[0..14] } # max 15 chars
Sham.message { Faker::Lorem.sentence }
EOS
 
# Initial controllers/views
  generate 'rspec_controller', 'home index'
  
  if authentication
    generate 'rspec_controller', 'protected index'
 
    file 'app/controllers/protected_controller.rb', <<-EOS.gsub(/^ /, '')
class ProtectedController < ApplicationController
before_filter :login_required
 
def index
end
 
end
EOS
 
    file 'app/views/protected/index.html.erb', '<h3><%= current_user.login %></h3>'
  end
  
  if twitter_auth
    file 'app/views/home/index.html.erb', <<-EOS.gsub(/^ /, '')
<%= link_to "Protected", :controller => :protected %>
 
<h3>Recent users (<%= User.count %>)</h3>
<ul>
<% for user in User.all -%>
<li><%= image_tag(user.profile_image_url) %><%= user.name %> (<%= link_to h(user.login), "http://twitter.com/\#{h user.login}" %>)</li>
<% end -%>
</ul>
EOS
  else
    file 'app/views/home/index.html.erb', '<%= link_to "Protected Area", :controller => :protected %>'
  end
  
# Miscellaneous configuration
  if twitter_auth
    append_file 'config/environments/development.rb', "\n\nOpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE\n"
    append_file 'config/environments/test.rb', "\n\nOpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE\n"
    
    file 'config/twitter_auth.yml', <<-EOS.gsub(/^ /, '')
development:
strategy: oauth
oauth_consumer_key: #{twitter_auth_keys[:key]}
oauth_consumer_secret: #{twitter_auth_keys[:secret]}
base_url: "https://twitter.com"
api_timeout: 10
remember_for: 14 # days
oauth_callback: "http://#{app_subdomain}.local/oauth_callback"
test:
strategy: oauth
oauth_consumer_key: #{twitter_auth_keys[:key]}
oauth_consumer_secret: #{twitter_auth_keys[:secret]}
base_url: "https://twitter.com"
api_timeout: 10
remember_for: 14 # days
oauth_callback: "http://#{app_subdomain}.local/oauth_callback"
production:
strategy: oauth
oauth_consumer_key: #{twitter_auth_keys[:key]}
oauth_consumer_secret: #{twitter_auth_keys[:secret]}
base_url: "https://twitter.com"
api_timeout: 10
remember_for: 14 # days
oauth_callback: "http://#{app_url}/oauth_callback"
EOS
  elsif restful_authentication
    environment("config.active_record.observers = :user_observer")
    initializer("mailer.rb", <<-EOS.gsub(/^ /, ''))
mailer_options = YAML.load_file("\#{RAILS_ROOT}/config/mailer.yml")
ActionMailer::Base.smtp_settings = mailer_options
EOS
    file("config/mailer.yml", <<-EOS.gsub(/^ /, ''))
:address: mail.authsmtp.com
:domain: #{domain}
:authentication: :login
:user_name: USERNAME
:password: PASSWORD
EOS
    append_file("app/views/users/new.html.erb", <<-EOS.gsub(/^ /, ''))
<h2>FIRST - setup activation config/mailer.yml for your mail server</h2>
EOS
    gsub_file("app/controllers/application_controller.rb", /class ApplicationController < ActionController::Base/mi) do
      "class ApplicationController < ActionController::Base\n include AuthenticatedSystem"
    end
  end
 
# Setup whenever (the cron DSL)
  run "wheneverize ."
 
# Run migrations
  rake 'db:migrate'
  rake 'db:test:clone'
 
# Routes
  if twitter_auth
    route "map.login '/login', :controller => 'session', :action => 'new'"
    route "map.session_create '/sessions/create', :controller => 'session', :action => 'create'"
    route "map.session_destroy '/sessions/destroy', :controller => 'session', :action => 'destroy'"
    route "map.oauth_callback '/oauth_callback', :controller => 'session', :action => 'oauth_callback'"
    
  elsif restful_authentication
    # restful-authentication seems to create other routes but not this one
    route "map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil"
  end
  
  route "map.root :controller => 'home', :action => 'index'"
  
# Deployment (generates Capfiy)
  generate "deploy', '--github-user=#{github_user} #{'--public' unless is_private_github} --domain=#{app_url}"
 
# Initialize submodules
  git :submodule => "init"
 
# Commit all work so far to the repository
  git :add => '.'
  git :commit => "-a -m 'Plugins and config'"
 
# GitHub project creation
  run "github create-from-local#{ ' --private' if is_private_github }"
 
# Deploy!
 
  run "cap deploy:setup"
  run "cap deploy:cold"
  run "cap deploy:install_gems"
 
  git :add => '.'
  git :commit => "-a -m 'Deprec config'"
  git :push => 'origin master'
 
# Success!
  log "SUCCESS! Your app is running at http://#{app_url}"
 
end
 
def highline
  @highline ||= begin
    require "highline"
    HighLine.new
  end
end
 
def parse_keys(message)
  {
    :key => (message.match(/Consumer key:\s+(.*)/)[1] rescue "TWITTER_CONSUMERKEY"),
    :secret => (message.match(/Consumer secret:\s+(.*)/)[1] rescue "TWITTER_CONSUMERSECRET")
  }
end
 
def run_template
  @store_template.call
end
 
def slice_names
  slicehost_list = run "slicehost-slice list"
  slicehost_list.split("\n").map { |name_ip| name_ip.match(/\+\s+([^\s]+)\s/)[1] }
end
 
run_template unless ENV['TEST_MODE'] # hold off running the template whilst in unit testing mode