Guest User

Untitled

a guest
Apr 24th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. set :domain, "111.222.333.444"
  2. set :application, "YOURAPP"
  3.  
  4. set :keep_releases, 2
  5. set :repository, "git@#{domain}:#{application}.git"
  6. set :scm, :git
  7. set :deploy_via, :remote_cache
  8. set :deploy_to, "/var/www/#{application}"
  9.  
  10. set :user, "deploy"
  11. set :runner, "mongrel"
  12. set :use_sudo, :false
  13.  
  14. set :mongrel_conf, "#{current_path}/config/mongrel_cluster.yml"
  15.  
  16. role :app, "#{domain}"
  17. role :web, "#{domain}"
  18. role :db, "#{domain}", :primary => true
  19.  
  20. # == CONFIG ====================================================================
  21. namespace :init do
  22. namespace :config do
  23. desc "Create database.yml"
  24. task :database do
  25. set :db_user, Capistrano::CLI.ui.ask("database user: ")
  26. set :db_pass, Capistrano::CLI.password_prompt("database password: ")
  27. database_configuration =<<-EOF
  28. ---
  29. login: &login
  30. adapter: mysql
  31. database: #{application}
  32. host: localhost
  33. username: #{db_user}
  34. password: #{db_pass}
  35.  
  36. production:
  37. <<: *login
  38.  
  39. EOF
  40. run "mkdir -p #{shared_path}/config"
  41. put database_configuration, "#{shared_path}/config/database.yml"
  42. end
  43.  
  44. desc "Create mongrel_cluster.yml"
  45. task :mongrel do
  46. mongrel_cluster_configuration = <<-EOF
  47. ---
  48. user: mongrel
  49. cwd: #{current_path}
  50. log_file: #{current_path}/log/mongrel.log
  51. port: "8000"
  52. environment: production
  53. group: mongrel
  54. address: 127.0.0.1
  55. pid_file: #{current_path}/tmp/pids/mongrel.pid
  56. servers: 3
  57. EOF
  58. run "mkdir -p #{shared_path}/config"
  59. put mongrel_cluster_configuration, "#{shared_path}/config/mongrel_cluster.yml"
  60. end
  61.  
  62. desc "Symlink shared configurations to current"
  63. task :localize, :roles => [:app] do
  64. %w[mongrel_cluster.yml database.yml].each do |f|
  65. run "ln -nsf #{shared_path}/config/#{f} #{current_path}/config/#{f}"
  66. end
  67. end
  68. end
  69. end
  70.  
  71.  
  72. # == NGINX =====================================================================
  73. namespace :nginx do
  74. desc "Start Nginx on the app server."
  75. task :start, :roles => :app do
  76. sudo "/etc/init.d/nginx start"
  77. end
  78.  
  79. desc "Restart the Nginx processes on the app server by starting and stopping the cluster."
  80. task :restart , :roles => :app do
  81. sudo "/etc/init.d/nginx restart"
  82. end
  83.  
  84. desc "Stop the Nginx processes on the app server."
  85. task :stop , :roles => :app do
  86. sudo "/etc/init.d/nginx stop"
  87. end
  88. end
  89.  
  90. # == DATABASE ==================================================================
  91. namespace :db do
  92. desc "Backup your Database to #{shared_path}/db_backups"
  93. task :backup, :roles => :db, :only => {:primary => true} do
  94. set :db_user, Capistrano::CLI.ui.ask("Database user: ")
  95. set :db_pass, Capistrano::CLI.password_prompt("Database password: ")
  96. now = Time.now
  97. run "mkdir -p #{shared_path}/backup"
  98. backup_time = [now.year,now.month,now.day,now.hour,now.min,now.sec].join('-')
  99. set :backup_file, "#{shared_path}/backup/#{application}-snapshot-#{backup_time}.sql"
  100. run "mysqldump --add-drop-table -u #{db_user} -p #{db_pass} #{application} --opt | bzip2 -c > #{backup_file}.bz2"
  101. end
  102. end
  103.  
  104. # == MONGREL ===================================================================
  105. namespace :deploy do
  106. desc "Start mongrel cluster"
  107. task :start do
  108. run "cd #{current_path} && sudo mongrel_rails cluster::start"
  109. end
  110.  
  111. desc "Stop mongrel cluster"
  112. task :stop do
  113. run "cd #{current_path} && sudo mongrel_rails cluster::stop"
  114. end
  115.  
  116. desc "Restart mongrel cluster"
  117. task :restart do
  118. run "cd #{current_path} && sudo mongrel_rails cluster::restart"
  119. end
  120. end
  121.  
  122. # == TASKS =====================================================================
  123. before "deploy:migrate", "db:backup"
  124.  
  125. after "deploy", "deploy:cleanup"
  126. after "deploy:migrations", "deploy:cleanup"
  127. after "deploy:setup", "init:config:database"
  128. after "deploy:setup", "init:config:mongrel"
  129. after "deploy:symlink", "init:config:localize"
  130.  
  131. task :after_update_code do
  132. sudo "chown mongrel:mongrel #{release_path}/tmp -R"
  133. sudo "chown mongrel:mongrel #{release_path}/log -R"
  134. sudo "chown mongrel:mongrel #{shared_path}/pids -R"
  135. end
Add Comment
Please, Sign In to add comment