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
|
# File 'lib/capistrano-kyan/capistrano_integration.rb', line 22
def self.load_into(capistrano_config)
capistrano_config.load do
before(CapistranoIntegration::TASKS) do
_cset(:app_env) { (fetch(:rails_env) rescue 'staging') }
_cset(:vhost_env) { fetch(:app_env) }
_cset(:vhost_tmpl_path) { 'config/vhosts' }
_cset(:vhost_tmpl_name) { 'vhost.conf.erb' }
_cset(:vhost_server_path) { '/etc/nginx/sites-enabled' }
_cset(:vhost_server_name) { File.basename(deploy_to) rescue fetch(:app_env) }
end
def appize(app, prefix = 'staging')
"#{app.gsub('.','_')}_#{prefix}"
end
def tmpl_server_location
run "mkdir -p #{shared_path}/config"
File.join(shared_path, 'config', "#{File.basename(deploy_to)}.conf")
end
def symlink(target, link)
run "ln -nfs #{target} #{link}"
end
def parse_template(template)
ERB.new(File.read(template), nil , '-').result(binding)
end
def build_vhost(path, name)
[
File.join(path, name),
File.join(File.dirname(__FILE__),'../../templates/vhost.conf.erb')
].each do |template|
if File.file? template
return parse_template(template)
end
end
end
namespace :deploy do
desc "Load the database with seed data"
task :seed do
run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{app_env}"
end
task :add_env do
put "RAILS_ENV=#{app_env}", "#{release_path}/.env"
end
end
after "deploy:finalize_update", "deploy:add_env"
namespace :nginx do
task :start, :roles => :app, :except => { :no_release => true } do
run "sudo /etc/init.d/nginx start"
end
task :stop, :roles => :app, :except => { :no_release => true } do
run "sudo /etc/init.d/nginx stop"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "sudo /etc/init.d/nginx restart"
end
task :reload, :roles => :app, :except => { :no_release => true } do
run "sudo /etc/init.d/nginx reload"
end
end
namespace :foreman do
desc "Export the Procfile to Ubuntu's upstart scripts"
task :export, :roles => :app do
run "cd #{release_path} && sudo foreman export upstart /etc/init -a #{vhost_server_name} -u #{user} -l #{shared_path}/log"
end
desc "Start the application services"
task :start, :roles => :app do
run "sudo start #{vhost_server_name}"
end
desc "Stop the application services"
task :stop, :roles => :app do
run "sudo stop #{vhost_server_name}"
end
desc "Restart the application services"
task :restart, :roles => :app do
run "sudo start #{vhost_server_name} || sudo restart #{vhost_server_name}"
end
end
namespace :kyan do
namespace :vhost do
desc <<-DESC
Creates and symlinks an Nginx virtualhost entry.
By default, this task uses a builtin template which you
see the output with rake kyan:vhost:show. If you need to
customise this, you can create your own erb template and
update the :vhost_tmpl_path and :vhost_tmpl_name variables.
DESC
task :setup, :except => { :no_release => true } do
if tmpl = build_vhost(vhost_tmpl_path, vhost_tmpl_name)
put tmpl, tmpl_server_location
symlink(tmpl_server_location, vhost_server_path)
else
puts "Could not find a suitable template."
end
end
desc "Displays the vhost that will be uploaded to server"
task :show, :except => { :no_release => true } do
puts build_vhost(vhost_tmpl_path, vhost_tmpl_name)
end
end
namespace :db do
desc <<-DESC
Creates a database.yml file in the apps shared path.
DESC
task :setup, :except => { :no_release => true } do
require 'digest/sha1'
app = appize(application, fetch(:stage))
database = fetch(:db_database, app)
username = fetch(:db_username, app)
password = Capistrano::CLI.ui.ask("DB password for #{database} (empty for default): ")
password = password.empty? ? Digest::SHA1.hexdigest(database) : password
default_template = <<-EOF
base: &base
encoding: utf8
adapter: postgresql
pool: <%= fetch(:db_pool, 5) %>
host: <%= fetch(:db_host, 'localhost') %>
<%= fetch(:stage) %>:
database: <%= database %>
username: <%= username %>
password: <%= password %>
<<: *base
EOF
location = fetch(:template_dir, "config/deploy") + '/database.yml.erb'
template = File.file?(location) ? File.read(location) : default_template
config = ERB.new(template, nil , '-')
run "mkdir -p #{shared_path}/config"
put config.result(binding), "#{shared_path}/config/database.yml"
end
task :symlink, :except => { :no_release => true } do
path_to_appl_database_yml = "#{release_path}/config/database.yml"
path_to_conf_database_yml = "#{shared_path}/config/database.yml"
run "ln -nfs #{path_to_conf_database_yml} #{path_to_appl_database_yml}"
end
end
after "deploy:finalize_update", "kyan:db:symlink"
end
end
end
|