-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCapfile
207 lines (172 loc) · 6.16 KB
/
Capfile
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
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
# Uncomment if you are using Rails' asset pipeline
# load 'deploy/assets'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy' # remove this line to skip loading any of the default tasks
#load 'deploy/assets'
#$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm/capistrano"
rvmrc = "rvm use #{rvm_ruby_string}"
set :rvm_type, :user
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :rails_env, "production"
config = YAML::load(File.open("config/database.yml"))
adapter = config[rails_env]["adapter"]
database = config[rails_env]["database"]
db_username = config[rails_env]["username"]
db_password = config[rails_env]["password"]
set :local_folder_path, "tmp/backup"
set :timestamp, Time.new.to_i.to_s
namespace :backup do
desc "Backup a database"
task :db do
file_name = "#{database}-#{timestamp}.sql"
archive_ext = "7z"
dump_file_path = "#{shared_path}/backup/#{file_name}"
output_file = "#{dump_file_path}.#{archive_ext}"
require 'yaml'
run "mkdir -p #{shared_path}/backup"
if adapter == "postgresql"
run "pg_dump -U #{db_username} #{database} > #{dump_file_path}"
run "cd #{shared_path} && 7z a #{output_file} #{dump_file_path} && rm #{dump_file_path}"
else
puts "Cannot backup, adapter #{adapter} is not implemented for backup yet"
end
system "mkdir -p #{local_folder_path}"
download(output_file, "#{local_folder_path}/#{file_name}.#{archive_ext}")
end
desc "Backup public/system folder"
task :sys do
file_name = "#{application}-system-#{timestamp}.7z"
file_path = "#{shared_path}/backup/#{file_name}"
run "7z a #{file_path} #{shared_path}/system"
download(file_path, "#{local_folder_path}/#{file_name}")
end
desc "Clean backup folder"
task :clean do
run "rm -rfv #{shared_path}/backup/*"
end
end
if exists?(:backup_db) && fetch(:backup_db) == true
before "deploy:update", "backup:db"
end
if exists?(:backup_sys) && fetch(:backup_sys) == true
before "deploy:update", "backup:sys"
end
namespace :db do
task :create do
run "echo \"create database #{database};\" | #{sudo} -u postgres psql"
run "echo \"create user #{db_username} with password '#{db_password}';\" | #{sudo} -u postgres psql"
run "echo \"grant all privileges on database booqpro_production to booqpro;\" | #{sudo} -u postgres psql"
end
task :seed do
run "cd #{current_path} && bundle exec rake RAILS_ENV=#{rails_env} db:seed"
end
end
#after "deploy:setup", "db:create"
namespace :assets do
desc "Assets precompile"
task :precompile do
#run "cd #{current_path} && bundle exec rake assets:precompile"
if exists?(:assets) && fetch(:assets) == true
system("bundle exec rake assets:precompile && cd public && tar czf assets.tar.gz assets/")
upload("public/assets.tar.gz","#{current_path}/public/assets.tar.gz")
system("rm public/assets.tar.gz")
run("cd #{current_path}/public && rm -rf assets/ && tar xzf assets.tar.gz && rm assets.tar.gz")
else
puts "assets is disabled in config/deploy.rb to enable add line set :assets, true"
end
end
end
namespace :nginx do
desc "Restart nginx"
task :restart do
run "#{sudo} /etc/init.d/nginx restart"
end
desc "Reload nginx"
task :reload do
run "#{sudo} /etc/init.d/nginx reload"
end
desc "Start nginx"
task :start do
run "#{sudo} /etc/init.d/nginx start"
end
desc "Stop nginx"
task :stop do
run "#{sudo} /etc/init.d/nginx stop"
end
desc "Add app nginx conf to server"
task :conf do
default_nginx_template = <<-EOF
server {
listen 80;
server_name #{server_name};
root #{current_path}/public;
location / {
try_files $uri @unicorn;
}
location @unicorn {
proxy_set_header Client-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://unix:#{shared_path}/pids/unicorn.sock;
}
}
EOF
#location = fetch(:template_dir, "config") + '/nginx.conf.erb'
#template = File.file?(location) ? File.read(location) : default_nginx_template
config = ERB.new(default_nginx_template)
# puts config.result
put config.result(binding), "#{shared_path}/nginx.conf"
run "#{sudo} ln -sfv #{shared_path}/nginx.conf /etc/nginx/sites-enabled/#{application}"
end
desc "Del nginx config"
task :delconf do
run "#{sudo} rm -v /etc/nginx/sites-enabled/#{application}"
end
end
after "deploy:setup", "nginx:conf"
after "nginx:conf", "nginx:reload"
after "nginx:delconf", "nginx:reload"
namespace :bundle do
desc "Run bundle install"
task :install, :roles => :app, :except => {:no_release => true} do
run "cd #{current_path} && bundle install"
end
end
namespace :log do
desc "tail -f production.log"
task :tail do
stream("tail -f -n 0 #{current_path}/log/production.log")
end
end
desc "Create .rvmrc & files"
task :create_rvmrc do
put rvmrc, "#{current_path}/.rvmrc"
end
namespace :install do
desc "Install apt-nyaa"
task :aptnyaa do
run "#{sudo} apt-get --assume-yes install wget > /dev/null 2>&1 && cd /usr/bin/ && #{sudo} wget -Nq https://raw.github.com/nyaa/UbuntuScript/master/apt-nyaa && #{sudo} chmod +x apt-nyaa"
end
end
set :unicorn_conf, "#{current_path}/config/unicorn.rb"
set :unicorn_pid, "#{shared_path}/pids/unicorn.pid"
namespace :unicorn do
desc "start unicorn"
task :start, :roles => :app, :except => {:no_release => true} do
run "cd #{current_path} && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D"
end
desc "stop unicorn"
task :stop, :roles => :app, :except => {:no_release => true} do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
end
#desc "restart unicorn"
#task :stop, :roles => :app, :except => {:no_release => true} do
# run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{current_path} && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D; fi"
#end
end
after "deploy:symlink", "assets:precompile", "create_rvmrc", "bundle:install"
after "deploy:restart","unicorn:stop", "unicorn:start"
################################