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
| lock '3.1.0'
set :application, "exmaple" set :repo_url, "git@github.com:example/example-server.git"
set :deploy_to, "/var/www/#{fetch(:application)}"
set :bundle_roles, :bundle set :bundle_flags, '--deployment --quiet' set :bundle_path, -> { shared_path.join('vendor/bundle') }
set :app_maintenance_file, "/var/tmp/.app_maintenance" set :nginx_maintenance_file, "/var/tmp/.nginx_maintenance"
set :user, (ENV['CAP_USER'] || ENV['USER'] || "#{`whoami`.chomp}") set :ssh_options, { user: fetch(:user), port: 10022, forward_agent: true, }
set :new_relic_app_name, "example" set :new_relic_monitor_mode, false
set :unicorn_worker_processes, 2 set :unicorn_working_directory, current_path set :unicorn_sock, "/tmp/unicorn.sock" set :unicorn_backlog, 128 set :unicorn_port, 8080 set :unicorn_timeout, 30 set :unicorn_preload_app, true set :unicorn_pid_file, File.join(current_path, "tmp", 'pids', 'unicorn.pid') set :unicorn_stderr_path, File.join(current_path, "log", "unicorn.stderr.log") set :unicorn_stdout_path, File.join(current_path, "log", "unicorn.stdout.log")
set :pty, true
SSHKit.config.command_map[:rake] = 'bundle exec rake'
set :filter, roles: %w{app web kvs migrator job bundle}
set :linked_dirs, %w{log tmp vendor/bundle public/system public/assets public/uploads}
set :keep_releases, 5
set :config_path, File.dirname(__FILE__) set :templates_path, File.join(fetch(:config_path), "deploy", "templates")
namespace :deploy do
desc 'Restart application' task :restart do on roles(:app), in: :sequence, wait: 5 do end end
after :publishing, :restart
after :restart, :clear_cache do on roles(:web), in: :groups, limit: 3, wait: 10 do end end
namespace :before_hook do desc 'Start a deployment, make sure server(s) ready.' task :starting do puts "example:change_permission】" invoke('example:change_permission')
puts "example:git_setup】" invoke('example:git_setup') end end
before :starting, 'before_hook:starting'
namespace :after_hook do desc 'Update server(s) by setting up a new release.' task :updating do puts "example:setup】" invoke('example:setup:config') end
desc 'Finished' task :finished do on release_roles :all do execute :rm, "-rf", "#{fetch(:tmp_dir)}/#{fetch(:application)}" end end end
after :updating, 'after_hook:updating' after :finishing, 'cleanup' after :finished, 'after_hook:finished' end
namespace :example do namespace :setup do task :config do on roles(:app), in: :sequence do tmpdir_path = File.join(fetch(:tmp_dir), fetch(:user)) execute "mkdir -p #{tmpdir_path}"
Dir.chdir(fetch(:templates_path)) do |path| Dir.glob("*.erb") do |file| remote_filename = file.gsub(/.erb$/, "") remote_path = File.join(release_path, "config", remote_filename) info "Uploading: #{file} -> #{remote_path}"
file_content = ERB.new(File.read(file)) converted_content = StringIO.new(file_content.result(binding)) tmpfile = File.join(tmpdir_path, remote_filename) upload! converted_content, tmpfile
execute "mv #{tmpfile} #{remote_path}" execute "chmod a+r #{remote_path}" end end end end end
namespace :nginx do desc "start nginx process" task :start do on roles(:web), in: :sequence do sudo "/etc/init.d/nginx start" end end
desc "restart nginx process" task :restart do on roles(:web), in: :sequence do sudo "/etc/init.d/nginx restart" end end
desc "reload nginx configuration" task :reload do on roles(:web), in: :sequence do sudo "/etc/init.d/nginx reload" end end
desc "stop nginx process" task :stop do on roles(:web), in: :sequence do sudo "/etc/init.d/nginx stop" end end end
namespace :stage do desc "input stage validation" task :validation do run_locally do
ask(:input_stage, "stage == #{fetch(:stage)}")
if fetch(:input_stage) == fetch(:stage).to_s puts "valid!" else raise "Invalid stage" end end end end
task :git_setup do on roles(:app) do within "#{fetch(:deploy_to)}/repo" do sudo "chmod -R g+ws *" sudo "chgrp -R example-users *" execute :git, "config repo.core.sharedRepository true" end end end
task :change_permission do on roles(:all) do within fetch(:deploy_to) do sudo "chmod -R g+w *" sudo "chgrp -R example-users *" end end end
namespace :db do set :db_environmental_variables, ->() { { rails_env: fetch(:rails_env), } }
desc "rake db:create" task :create do on roles(:migrator), in: :sequence do with rails_env: fetch(:rails_env) do within current_path do execute :bundle, :exec, :rake, 'db:create' end end end end
desc "rake db:seed" task :seed do on roles(:migrator), in: :sequence do within current_path do env_variables = fetch(:db_environmental_variables).dup env_variables[:tables] = ENV['TABLES'] if ENV['TABLES'] env_variables[:catalogs] = ENV['CATALOGS'] if ENV['CATALOGS'] with env_variables do execute :bundle, :exec, :rake, 'db:seed' end end end end end end
namespace :maintenance do desc "Start nginx maintenance(End command: maintenance:nginx_end)" task :nginx_start do on roles(:web) do execute :touch, fetch(:nginx_maintenance_file) execute :chmod, '777', fetch(:nginx_maintenance_file) end end
desc "End nginx maintenance" task :nginx_end do on roles(:web) do execute :rm, '-f', fetch(:nginx_maintenance_file) end end
desc "Start maintenance" task :start do on roles(:web) do execute :touch, fetch(:app_maintenance_file) execute :chmod, '777', fetch(:app_maintenance_file) end end
desc "End maintenance" task :end do on roles(:web) do execute :rm, '-f', fetch(:app_maintenance_file) end end
desc "put maitenance json" task :json do on roles(:web) do remote_system_path = File.join(current_path, 'public', 'system')
file_path = File.join('public', 'system', 'maintenance.json') output_path = File.join(remote_system_path, "maintenance.json")
upload_file(file_path, output_path)
execute :chmod, '-R a+r ', output_path execute :chmod, '-R g+w ', output_path end end end
|