PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/config/deploy.rb

https://bitbucket.org/sittitep/tabletbkk
Ruby | 175 lines | 123 code | 31 blank | 21 comment | 6 complexity | 29e16aa4d459cf8cd627cfa409d8c401 MD5 | raw file
  1. require "bundler/capistrano"
  2. set :application, 'tabletbkk'
  3. set :user, 'root'
  4. set :domain, '27.254.66.30'
  5. set :port, 22
  6. set :applicationdir, "/var/www/tabletbkk"
  7. set :environment, 'production'
  8. set :scm, 'git'
  9. set :repository, "git@bitbucket.org:sittitep/tabletbkk.git"
  10. set :git_enable_submodules, 1 # if you have vendored rails
  11. set :branch, fetch(:branch, "master")
  12. set :git_shallow_clone, 1
  13. set :scm_verbose, true
  14. # roles (servers)
  15. role :web, domain
  16. role :app, domain
  17. role :db, domain, :primary => true
  18. # deploy config
  19. set :deploy_to, applicationdir
  20. set :deploy_via, :export
  21. set :keep_releases, 5
  22. # additional settings
  23. default_run_options[:pty] = true # Forgo errors when deploying from windows
  24. ssh_options[:forward_agent] = true
  25. #ssh_options[:keys] = %w(/home/user/.ssh/id_rsa) # If you are using ssh_keysset :chmod755, "app config db lib public vendor script script/ public/disp"set :use_sudo, false
  26. # if ruby not installed in different path, check with which ruby
  27. # set :default_environment, {
  28. # 'PATH' => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin/rubydir/bin:/usr/local/bin/rubydir"
  29. # }
  30. # Setup Shared Folders
  31. # that should be created inside the shared_path
  32. directory_configuration = %w(db config system)
  33. # Setup Symlinks
  34. # that should be created after each deployment
  35. symlink_configuration = [
  36. %w(config/database.yml config/database.yml),
  37. %w(system public/system)
  38. ]
  39. # Callbacks
  40. before "deploy:assets:precompile", "setup_symlinks"
  41. # Every deploy runs these commands
  42. after 'deploy:update_code' do
  43. puts "\n\n=== Running Custom Processes! ===\n\n"
  44. run "rm -r #{release_path}/public/system"
  45. create_production_log
  46. setup_symlinks
  47. set_permissions
  48. end
  49. after "deploy:restart", "deploy:cleanup"
  50. after 'deploy:setup', 'deploy:setup_shared_path'
  51. #
  52. # Manual Tasks
  53. #
  54. desc "Sets permissions for Rails Application"
  55. task :set_permissions do
  56. puts "\n\n=== Setting Permissions! ===\n\n"
  57. run "chown -R www-data:www-data #{deploy_to}"
  58. # run "chown -R nobody #{release_path}"
  59. end
  60. desc "Creates the production log if it does not exist"
  61. task :create_production_log do
  62. unless File.exist?(File.join(shared_path, 'log', 'production.log'))
  63. puts "\n\n=== Creating Production Log! ===\n\n"
  64. run "touch #{File.join(shared_path, 'log', 'production.log')}"
  65. end
  66. end
  67. desc "Creates symbolic links from shared folder"
  68. task :setup_symlinks do
  69. puts "\n\n=== Setting up Symbolic Links! ===\n\n"
  70. symlink_configuration.each do |config|
  71. run "ln -nfs #{File.join(shared_path, config[0])} #{File.join(release_path, config[1])}"
  72. end
  73. end
  74. # Passenger
  75. namespace :deploy do
  76. task :start do ; end
  77. task :stop do ; end
  78. task :restart, :roles => :app, :except => { :no_release => true } do
  79. run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  80. end
  81. desc "Sets up the shared path"
  82. task :setup_shared_path do
  83. puts "\n\n=== Setting up the shared path! ===\n\n"
  84. directory_configuration.each do |directory|
  85. run "mkdir -p #{shared_path}/#{directory}"
  86. end
  87. system "cap deploy:db:sync_yaml"
  88. end
  89. # database
  90. namespace :db do
  91. desc "|DarkRecipes| Create MySQL database and user for this environment using prompted values"
  92. task :setup, :roles => :db, :only => { :primary => true } do
  93. prepare_for_db_command
  94. sql = <<-SQL
  95. CREATE DATABASE #{db_name};
  96. GRANT ALL PRIVILEGES ON #{db_name}.* TO #{db_user}@localhost IDENTIFIED BY '#{db_pass}';
  97. SQL
  98. run "mysql --user=#{db_admin_user} -p --execute=\"#{sql}\"" do |channel, stream, data|
  99. if data =~ /^Enter password:/
  100. pass = Capistrano::CLI.password_prompt "Enter database password for '#{db_admin_user}':"
  101. channel.send_data "#{pass}\n"
  102. end
  103. end
  104. end
  105. desc "|DarkRecipes| Create database.yml in shared path with settings for current stage and test env"
  106. task :create_yaml do
  107. set(:db_user) { Capistrano::CLI.ui.ask "Enter #{environment} database username:" }
  108. set(:db_pass) { Capistrano::CLI.password_prompt "Enter #{environment} database password:" }
  109. db_config = ERB.new <<-EOF
  110. base: &base
  111. adapter: mysql2
  112. encoding: utf8
  113. username: #{db_user}
  114. password: #{db_pass}
  115. #{environment}:
  116. database: #{application}_#{environment}
  117. <<: *base
  118. test:
  119. database: #{application}_test
  120. <<: *base
  121. EOF
  122. put db_config.result, "#{shared_path}/config/database.yml"
  123. end
  124. desc "Syncs the database.yml file from the local machine to the remote machine"
  125. task :sync_yaml do
  126. puts "\n\n=== Syncing database yaml to the production server! ===\n\n"
  127. unless File.exist?("config/database.yml")
  128. puts "There is no config/database.yml.\n "
  129. exit
  130. end
  131. system "rsync -vr --exclude='.DS_Store' --rsh='ssh -p#{port}' config/database.yml #{user}@#{domain}:#{shared_path}/config/"
  132. end
  133. desc "reload the database with seed data"
  134. task :seed do
  135. run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
  136. end
  137. end
  138. end
  139. def prepare_for_db_command
  140. set :db_name, "#{application}_#{environment}"
  141. set(:db_admin_user) { Capistrano::CLI.ui.ask "Username with priviledged database access (to create db):" }
  142. set(:db_user) { Capistrano::CLI.ui.ask "Enter #{environment} database username:" }
  143. set(:db_pass) { Capistrano::CLI.password_prompt "Enter #{environment} database password:" }
  144. end