forked from railsmachine/moonshine
/
capistrano_integration.rb
484 lines (416 loc) · 16.6 KB
/
capistrano_integration.rb
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
require 'pathname'
require 'fileutils'
module Moonshine
class CapistranoIntegration
def self.load_defaults_info(capistrano_config)
capistrano_config.load do
# these are required at load time by capistrano, we'll set them later
set :application, ''
set :repository, ''
# sane defaults
set :branch, 'master'
set :scm, :git
set :git_enable_submodules, 1
set :keep_releases, 5
ssh_options[:paranoid] = false
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
set :noop, false
# fix common svn error
set :scm, :subversion if !! repository =~ /^svn/
# set some default values, so we don't have to fetch(:var, :some_default) in multiple places
set :local_config, []
set :shared_config, []
set :stage, nil
set :rails_env do
self[:stage] || 'production'
end
set :moonshine_manifest, 'application_manifest'
set :app_symlinks, []
set :ruby, :ree
# know the path to rails logs
set :rails_log do
"#{shared_path}/log/#{fetch(:rails_env)}.log"
end
set :rails_root, Pathname.new(ENV['RAILS_ROOT'] || Dir.pwd)
set :moonshine_yml_path, rails_root.join('config', 'moonshine.yml')
set :moonshine_yml do
if moonshine_yml_path.exist?
require 'yaml'
YAML::load(ERB.new(moonshine_yml_path.read).result)
else
puts "Missing #{moonshine_yml_path}"
puts "You can generate one using the moonshine generator. See `ruby script/generate moonshine --help` for details"
exit(1)
end
end
end
end
def self.load_callbacks_into(capistrano_config)
capistrano_config.load do
on :start, 'moonshine:configure'
unless fetch(:noop)
after 'deploy:restart', 'deploy:cleanup'
end
after 'multistage:ensure', 'moonshine:configure_stage'
end
end
def self.load_into(capistrano_config)
load_defaults_info(capistrano_config)
load_callbacks_into(capistrano_config)
capistrano_config.load do
namespace :moonshine do
desc "[internal]: populate capistrano with settings from moonshine.yml"
task :configure do
moonshine_yml.each do |key, value|
set key.to_sym, value
end
end
desc "[internal]: populate capistrano with settings from moonshine/<rails_env>.yml"
task :configure_stage do
set :moonshine_rails_env_yml_path, rails_root.join('config', 'moonshine', "#{rails_env}.yml")
set :moonshine_rails_env_yml do
if moonshine_rails_env_yml_path.exist?
require 'yaml'
YAML::load(ERB.new(moonshine_rails_env_yml_path.read).result)
else
{}
end
end
moonshine_rails_env_yml.each do |k,v|
set k.to_sym, v
end
end
desc <<-DESC
Bootstrap a barebones Ubuntu system with Git/Subversion, Ruby, RubyGems, and \
Moonshine dependencies. Called by deploy:setup.
DESC
task :bootstrap do
ruby.install
vcs.install
moonshine.setup_directories
shared_config.upload
end
desc <<-DESC
Applies the lib/moonshine_setup_manifest.rb manifest, which replicates the old \
capistrano deploy:setup behavior.
DESC
task :setup_directories do
set :moonshine_rails_env_yml_path, rails_root.join('config', 'moonshine', "#{rails_env}.yml")
if moonshine_rails_env_yml_path.exist?
run 'mkdir -p /tmp/moonshine'
upload moonshine_rails_env_yml_path.to_s, "/tmp/moonshine/#{rails_env}.yml"
end
upload moonshine_yml_path.to_s, '/tmp/moonshine.yml'
upload File.join(File.dirname(__FILE__), '..', 'moonshine_setup_manifest.rb'), '/tmp/moonshine_setup_manifest.rb'
sudo 'shadow_puppet /tmp/moonshine_setup_manifest.rb'
sudo 'rm /tmp/moonshine_setup_manifest.rb'
sudo 'rm /tmp/moonshine.yml'
end
desc 'Apply the Moonshine manifest for this application'
task :apply, :except => { :no_release => true } do
sudo "RAILS_ROOT=#{latest_release} DEPLOY_STAGE=#{ENV['DEPLOY_STAGE'] || fetch(:stage)} RAILS_ENV=#{fetch(:rails_env)} shadow_puppet #{latest_release}/app/manifests/#{fetch(:moonshine_manifest)}.rb"
end
desc 'No-op apply the Moonshine manifest for this application'
task :noop_apply, :except => { :no_release => true } do
sudo "RAILS_ROOT=#{latest_release} DEPLOY_STAGE=#{ENV['DEPLOY_STAGE'] || fetch(:stage)} RAILS_ENV=#{fetch(:rails_env)} shadow_puppet --noop #{latest_release}/app/manifests/#{fetch(:moonshine_manifest)}.rb"
end
desc 'Update code and then run a console. Useful for debugging deployment.'
task :update_and_console do
set :moonshine_apply, false
deploy.update_code
app.console
end
desc "Update code and then run 'rake environment'. Useful for debugging deployment."
task :update_and_rake do
set :moonshine_apply, false
deploy.update_code
run "cd #{latest_release} && RAILS_ENV=#{fetch(:rails_env)} rake --trace environment"
end
after 'deploy:finalize_update' do
local_config.upload
local_config.symlink
shared_config.symlink
app.symlinks.update
end
before 'deploy:symlink' do
if fetch(:noop)
noop_apply
else
apply if fetch(:moonshine_apply, true) == true
end
end
after 'deploy' do
deploy.rollback.default if fetch(:noop)
end
end
namespace :app do
namespace :symlinks do
desc <<-DESC
Link public directories to shared location.
DESC
task :update, :roles => [:app, :web] do
fetch(:app_symlinks).each do |link|
run "ln -nfs #{shared_path}/public/#{link} #{latest_release}/public/#{link}"
end
end
end
desc 'Run script/console on the first application server'
task :console, :roles => :app, :except => {:no_symlink => true} do
input = ''
if capture("test -f #{current_path}/script/console; echo $?").strip == "0"
command = "cd #{current_path} && ./script/console #{fetch(:rails_env)}"
prompt = /^(>|\?)>/
else
command = "cd #{current_path} && ./script/rails console #{fetch(:rails_env)}"
prompt = /:\d{3}:\d+(\*|>)/
end
run command do |channel, stream, data|
next if data.chomp == input.chomp || data.chomp == ''
print data
channel.send_data(input = $stdin.gets) if data =~ prompt
end
end
desc 'Show requests per second'
task :rps, :roles => :app, :except => {:no_symlink => true} do
count = 0
last = Time.now
run "tail -f #{rails_log}" do |ch, stream, out|
break if stream == :err
count += 1 if out =~ /^Completed in/
if Time.now - last >= 1
puts "#{ch[:host]}: %2d Requests / Second" % count
count = 0
last = Time.now
end
end
end
desc 'Tail the application log file of the first app server '
task :log, :roles => :app, :except => {:no_symlink => true} do
run "tail -f #{rails_log}" do |channel, stream, data|
puts "#{data}"
break if stream == :err
end
end
desc 'Tail vmstat'
task :vmstat, :roles => [:web, :db] do
run "vmstat 5" do |channel, stream, data|
puts "[#{channel[:host]}]"
puts data.gsub(/\s+/, "\t")
break if stream == :err
end
end
end
namespace :local_config do
desc <<-DESC
Uploads local configuration files to the application's shared directory for \
later symlinking (if necessary). Called if local_config is set.
DESC
task :upload do
fetch(:local_config).each do |file|
filename = File.basename(file)
path = File.dirname(file)
if File.exist?(file)
run "mkdir -p '#{shared_path}/#{path}'" unless path.empty?
parent.upload(file, "#{shared_path}/#{path}/#{filename}")
end
end
end
desc <<-DESC
Symlinks uploaded local configurations into the release directory.
DESC
task :symlink do
fetch(:local_config).each do |file|
filename = File.basename(file)
path = File.dirname(file)
run "mkdir -p '#{latest_release}/#{path}'" unless path.empty?
run "ls #{latest_release}/#{file} 2> /dev/null || ln -nfs #{shared_path}/#{path}/#{filename} #{latest_release}/#{file}"
end
end
end
namespace :shared_config do
desc <<-DESC
Uploads local configuration files to the application's shared directory for \
later symlinking (if necessary). Called if shared_config is set.
DESC
task :upload do
fetch(:shared_config).each do |file|
file = Pathname.new(file)
filename = file.basename
directory = file.dirname
run "mkdir -p '#{shared_path}/#{directory}'"
parent.upload(file.to_s, "#{shared_path}/#{directory}/#{filename}")
end
end
desc <<-DESC
Downloads remote configuration from the application's shared directory for \
local use.
DESC
task :download do
fetch(:shared_config).each do |file|
file = Pathname.new(file)
filename = file.basename
directory = file.dirname
FileUtils.mkdir_p(directory)
get "#{shared_path}/#{directory}/#{filename}", file.to_s
end
end
desc <<-DESC
Symlinks uploaded local configurations into the release directory.
DESC
task :symlink do
fetch(:shared_config).each do |file|
file = Pathname.new(file)
filename = file.basename
directory = file.dirname
run "mkdir -p '#{latest_release}/#{directory}'"
run "ls #{latest_release}/#{file} 2> /dev/null || ln -nfs #{shared_path}/#{directory}/#{filename} #{latest_release}/#{file}"
end
end
end
namespace :deploy do
desc 'Restart the Passenger processes on the app server by touching tmp/restart.txt.'
task :restart, :roles => :app, :except => { :no_release => true } do
unless fetch(:noop)
run "touch #{current_path}/tmp/restart.txt"
end
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with Passenger"
task t, :roles => :app do ; end
end
desc <<-DESC
Prepares one or more servers for deployment. Before you can use any \
of the Capistrano deployment tasks with your project, you will need to \
make sure all of your servers have been prepared with `cap deploy:setup'. When \
you add a new server to your cluster, you can easily run the setup task \
on just that server by specifying the HOSTS environment variable:
$ cap HOSTS=new.server.com deploy:setup
It is safe to run this task on servers that have already been set up; it \
will not destroy any deployed revisions or data.
DESC
task :setup, :except => { :no_release => true } do
moonshine.bootstrap
end
end
desc "does a no-op deploy. great for testing a potential deploy before running it!"
task :noop do
set :noop, true
end
namespace :ruby do
desc 'Forces a reinstall of Ruby and restarts Apache/Passenger'
task :upgrade do
install
sudo 'gem pristine --all'
apache.restart
end
desc 'Install Ruby + Rubygems'
task :install do
install_deps
send fetch(:ruby)
install_rubygems
install_moonshine_deps
end
task :mri do
apt
end
task :apt do
sudo 'apt-get install -q -y ruby-full'
end
task :remove_ruby_from_apt do
sudo 'apt-get remove -q -y ^.*ruby.* || true'
#TODO apt-pinning to ensure ruby is never installed via apt
end
task :ree do
remove_ruby_from_apt
run [
'cd /tmp',
'sudo rm -rf ruby-enterprise-1.8.6-20090610* || true',
'sudo mkdir -p /usr/lib/ruby/gems/1.8/gems || true',
'wget -q http://assets.railsmachine.com/other/ruby-enterprise-1.8.6-20090610.tar.gz',
'tar xzf ruby-enterprise-1.8.6-20090610.tar.gz',
'sudo /tmp/ruby-enterprise-1.8.6-20090610/installer --dont-install-useful-gems -a /usr'
].join(' && ')
end
task :ree187 do
remove_ruby_from_apt
run [
'cd /tmp',
'sudo rm -rf ruby-enterprise-1.8.7-2011.02* || true',
'sudo mkdir -p /usr/lib/ruby/gems/1.8/gems || true',
'wget -q http://rubyenterpriseedition.googlecode.com/files/ruby-enterprise-1.8.7-2011.02.tar.gz',
'tar xzf ruby-enterprise-1.8.7-2011.02.tar.gz',
'sudo /tmp/ruby-enterprise-1.8.7-2011.02/installer --dont-install-useful-gems --no-dev-docs -a /usr'
].join(' && ')
end
task :src187 do
remove_ruby_from_apt
run [
'cd /tmp',
'sudo rm -rf ruby-1.8.7-p249* || true',
'sudo mkdir -p /usr/lib/ruby/gems/1.8/gems || true',
'wget -q ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p249.tar.bz2',
'tar xjf ruby-1.8.7-p249.tar.bz2',
'cd /tmp/ruby-1.8.7-p249',
'./configure --prefix=/usr',
'make',
'sudo make install'
].join(' && ')
end
task :install_rubygems do
version = fetch(:rubygems_version, '1.4.2')
run [
'cd /tmp',
"sudo rm -rf rubygems-#{version}* || true",
"wget -q http://production.cf.rubygems.org/rubygems/rubygems-#{version}.tgz",
"tar xfz rubygems-#{version}.tgz",
"cd /tmp/rubygems-#{version}",
'sudo ruby setup.rb',
'sudo ln -s /usr/bin/gem1.8 /usr/bin/gem || true',
].join(' && ')
end
task :install_deps do
aptget.update
sudo 'apt-get install -q -y build-essential zlib1g-dev libssl-dev libreadline5-dev wget'
end
task :install_moonshine_deps do
rake_version = fetch(:rake_version, '>= 0')
sudo "gem install rake --no-rdoc --no-ri --version='#{rake_version}'"
sudo 'gem install i18n --no-rdoc --no-ri' # workaround for missing activesupport-3.0.2 dep on i18n
sudo 'gem install shadow_puppet --no-rdoc --no-ri'
if rails_root.join('Gemfile').exist?
bundler_version = fetch(:bundler_version, '1.0.9')
sudo "gem install bundler --no-rdoc --no-ri --version='#{bundler_version}'"
end
end
end
namespace :apache do
desc 'Restarts the Apache web server'
task :restart do
sudo 'service apache2 restart'
end
end
namespace :vcs do
desc "Installs the scm"
task :install do
package = case fetch(:scm).to_s
when 'svn' then 'subversion'
when 'git' then 'git-core'
else nil
end
sudo "apt-get -qq -y install #{package}" if package
end
end
namespace :aptget do
task :update do
sudo 'apt-get update'
end
end
end
end
end
end
require 'capistrano'
if Capistrano::Configuration.instance
Moonshine::CapistranoIntegration.load_into(Capistrano::Configuration.instance)
end