-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththin_analytics_deploy.rb
112 lines (94 loc) · 3.46 KB
/
thin_analytics_deploy.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
require "bundler/capistrano"
require 'capistrano/ext/multistage'
set :stages, %w(production development staging)
set :default_stage, "development"
set :application, "thin_analytics"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :ssh_options, { :forward_agent => true }
set :git_enable_submodules,1
set :scm, "git"
set :repository, "[email protected]:thinchat/#{application}.git"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
after "deploy", "deploy:cleanup", "deploy:workers:start" # keep only the last 5 releases
def current_git_branch
`git symbolic-ref HEAD`.gsub("refs/heads/", "")
end
def prompt_with_default(message, default)
response = Capistrano::CLI.ui.ask "#{message} Default is: [#{default}] : "
response.empty? ? default : response
end
def set_branch
if current_git_branch != "master"
set :branch, ENV['BRANCH'] || prompt_with_default("Enter branch to deploy, or ENTER for default.", "#{current_git_branch.chomp}")
else
set :branch, ENV['BRANCH'] || "#{current_git_branch.chomp}"
end
end
set :branch, set_branch
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} #{application}"
task command, roles: :app, except: {no_release: true} do
sudo "god load #{current_path}/config/god/#{application}.#{rails_env}.god"
sudo "god #{command} #{application}"
end
end
namespace :workers do
%w[start stop restart].each do |command|
desc "#{command} #{application}"
task command, roles: :app, except: {no_release: true} do
sudo "god load #{current_path}/config/god/#{application}.#{rails_env}.god"
%w[analytics_bean].each do |worker|
sudo "god #{command} #{worker}"
end
end
end
end
desc "Deploy to a server for the first time (assumes you've run 'cap stage-name provision')"
task :fresh, roles: :app do
puts "Deploying to fresh server..."
end
after "deploy:fresh", "deploy:setup", "deploy"
desc "Create the database"
task :create_database, roles: :app do
run "cd #{release_path} && bundle exec rake RAILS_ENV=#{rails_env} db:create"
end
after "deploy:db_config", "deploy:create_database"
after "deploy:create_database", "deploy:migrate"
desc "Setup unicorn configuration"
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/unicorn/unicorn_#{rails_env}_init.sh /etc/init.d/unicorn_#{application}"
end
after "deploy:setup", "deploy:create_release_dir", "deploy:setup_config"
task :create_release_dir, :except => {:no_release => true} do
run "mkdir -p #{fetch :releases_path}"
end
desc "Copy secret/database.yml to config/database.yml"
task :db_config, roles: :app do
run "cp #{release_path}/config/secret/database.#{application}.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:db_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
namespace :god do
desc "Status of god tasks"
task :status, roles: :app do
sudo "god status"
end
desc "Load god file"
task :load_config, roles: :app do
sudo "god load #{current_path}/config/god/#{application}.#{rails_env}.god"
end
end