/
deploy.rb
102 lines (87 loc) · 3.38 KB
/
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
require 'erb'
I18n.config.enforce_available_locales = false
set :backup_dir, '/home/app_runner/deploy_dump/'
set :shared_path, -> { shared_path }
set :rbenv_type, :user
set :rbenv_ruby, File.read('.ruby-version').strip
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :application, 'dashboard'
set :repo_url, "https://github.com/malmostad/intranet-dashboard.git"
set :user, 'app_runner'
set :deploy_to, "/home/#{fetch(:user)}/#{fetch(:application)}"
set :deploy_via, :remote_cache
set :pty, true
set :forward_agent, true
set :linked_files, %w{config/database.yml config/app_config.yml }
set :linked_dirs, %w{log tmp/pids tmp/sockets public/uploads reports}
set :default_env, { path: '$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH' }
set :keep_releases, 5
namespace :deploy do
%w[stop start restart upgrade].each do |command|
desc "#{command} unicorn server"
task command do
on roles(:all), except: {no_release: true} do
execute "/etc/init.d/unicorn_#{fetch(:application)} #{command}"
end
end
end
desc "Full restart of unicorn server"
task :full_restart do
on roles(:all), except: {no_release: true} do
execute "/etc/init.d/unicorn_#{fetch(:application)} restart"
end
end
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:all) do
unless `git rev-parse HEAD` == `git rev-parse origin/#{fetch(:branch)}`
puts "WARNING: HEAD is not the same as origin/#{fetch(:branch)}"
puts "Run `git push` to sync changes."
exit
end
end
end
desc "Are you sure?"
task :are_you_sure do
on roles(:all) do |server|
puts ""
puts "Environment: \033[0;32m#{fetch(:rails_env)}\033[0m"
puts "Remote branch: \033[0;32m#{fetch(:branch)}\033[0m"
puts "Server: \033[0;32m#{server.hostname}\033[0m"
puts ""
puts "Do you want to deploy?"
set :continue, ask("[y/n]:", "n")
if fetch(:continue).downcase != 'y' && fetch(:continue).downcase != 'yes'
puts "Deployment stopped"
exit
else
puts "Deployment starting"
end
end
end
desc 'Restart daemons'
task :restart_daemons do
on roles(:all) do
puts "Restarting daemons, this can take a while..."
ror_ruby = "cd #{release_path} && RAILS_ENV=#{fetch(:rails_env).to_s} #{fetch(:rbenv_prefix)} bundle exec ruby"
execute "#{ror_ruby} lib/daemons/feed_worker_ctl restart"
execute "#{ror_ruby} bin/delayed_job restart"
end
end
desc 'Perform a backup using mysqldump'
task :mysql_backup do
on roles(:all) do
filepath = "#{fetch(:backup_dir)}predeploy-#{fetch(:release_timestamp)}.sql.bz2"
text = capture "cat #{fetch(:shared_path)}/config/database.yml"
db_config = YAML::load(text)[fetch(:rails_env).to_s]
execute "mysqldump --user=#{db_config['username']} --password=#{db_config['password']} #{db_config['database']} | bzip2 -c > #{filepath}"
end
end
before :starting, "deploy:are_you_sure"
before :starting, "deploy:check_revision"
before :migrate, "deploy:mysql_backup"
after :published, "deploy:full_restart"
after :published, "deploy:restart_daemons" # Do it manually if needed with `sudo monit restart [feed_worker|delayed_job]`
after :finishing, "deploy:cleanup"
end