Guest User

Untitled

a guest
Oct 8th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. require 'rubygems'
  2. require 'net/ssh'
  3.  
  4. STDOUT.sync = true; STDOUT.flush;
  5. STDERR.sync = true; STDERR.flush;
  6.  
  7. APP_NAME = "xxx"
  8. APP_ROOT = "/var/www/#{APP_NAME}"
  9. GIT_URL = "/home/www/repositories/xxx.git"
  10. GIT_BRANCH = "origin/master"
  11. RELEASE_PATH = "#{APP_ROOT}/current"
  12. SHARED_PATH = "#{APP_ROOT}/shared"
  13.  
  14.  
  15. def init
  16. puts "\ninit deploy config..."
  17. accounts = YAML::load( File.open(Rails.root.to_s + "/config/deploy.usr.yml") )
  18.  
  19. $config = {
  20. :www => {
  21. :host => accounts["host"],
  22. :user => accounts["usr"],
  23. :password => accounts["pwd"],
  24. :port => accounts["port"] || 22
  25. }
  26. }
  27. end
  28.  
  29. def get_host(role)
  30. $config[role]
  31. end
  32.  
  33. def run(cmd)
  34. output = ''
  35. host = get_host(:www)
  36.  
  37. Net::SSH.start(host[:host], host[:user], :port => host[:port], :password => host[:password]) do |ssh|
  38. ssh.exec!(cmd) do |ch, stream, data|
  39. if stream == :stderr
  40. p '------------------------------'
  41. p data
  42. p '------------------------------'
  43. else
  44. output += data
  45. end
  46. end
  47. end
  48. output
  49. end
  50.  
  51.  
  52.  
  53. namespace :deploy do
  54.  
  55. desc "!deploy setup - just run it in the first time!"
  56. task :setup do
  57. puts "\nSetup ..."
  58. init
  59.  
  60. target_dirs = %w(
  61. shared/log
  62. shared/tmp/sockets
  63. shared/tmp/pids
  64. shared/tmp/sessions
  65. shared/tmp/cache
  66. shared/config
  67. )
  68.  
  69. mkdir_commands = target_dirs.map{ |target_dir| "mkdir -p #{APP_ROOT}/#{target_dir}" }
  70. run "#{mkdir_commands.join(" && ")} && git clone #{GIT_URL} #{RELEASE_PATH}"
  71.  
  72. cmd_touch_log_files = %w(production.log unicorn.stderr.log unicorn.stdout.log).map do |log|
  73. "touch #{SHARED_PATH}/log/#{log}"
  74. end
  75. run "#{cmd_touch_log_files.join(" && ")}"
  76.  
  77. puts "done"
  78. end
  79.  
  80.  
  81. desc "Deploy app"
  82. task :go do
  83. puts "deploy app..."
  84. init
  85. update_code
  86. precompile_assets
  87. make_symlinks
  88. end
  89. end
  90.  
  91.  
  92. def cleanup
  93. puts "\nCleanning ..."
  94. puts "done"
  95. end
  96.  
  97.  
  98. def update_code
  99. puts "\nUpdate code ..."
  100. run "cd #{RELEASE_PATH}; git fetch origin; git reset --hard #{GIT_BRANCH}"
  101. puts "done"
  102. end
  103.  
  104.  
  105. def precompile_assets
  106. puts "\nPrecompile assets ..."
  107. run "cd #{RELEASE_PATH}; /usr/local/bin/rake assets:precompile"
  108. puts "done"
  109. end
  110.  
  111.  
  112. def make_symlinks
  113. puts "\nBuilding symlinks ..."
  114.  
  115. cmd_mkdir = %w(tmp/sockets tmp/pids tmp/cache tmp/sessions log public/cache).map do |dir|
  116. "mkdir -p #{RELEASE_PATH}/#{dir}"
  117. end
  118. run "#{cmd_mkdir.join(" && ")}"
  119.  
  120. normal_symlinks = %w(
  121. config/database.yml
  122. tmp/sockets
  123. tmp/pids
  124. tmp/cache
  125. tmp/sessions
  126. log/production.log
  127. log/unicorn.stderr.log
  128. log/unicorn.stdout.log
  129. )
  130.  
  131. commands = normal_symlinks.map do |path|
  132. sym_lnk_path = "#{RELEASE_PATH}/#{path}"
  133. "rm -rf #{sym_lnk_path} && ln -s #{SHARED_PATH}/#{path} #{sym_lnk_path}"
  134. end
  135. run "#{commands.join(" && ")}"
  136.  
  137. other_commands = []
  138. rails_cache_path = "#{RELEASE_PATH}/public/cache"
  139. other_commands << "rm -rf #{rails_cache_path} && ln -s #{SHARED_PATH}/tmp/cache #{rails_cache_path}"
  140. run "#{other_commands.join(" && ")}"
  141.  
  142. puts "done"
  143. end
  144.  
  145.  
  146.  
  147. namespace :unicorn do
  148.  
  149. desc "Start unicorn"
  150. task :start do
  151. init
  152. puts "\nStart unicorn ..."
  153.  
  154. run "cd #{RELEASE_PATH} && /usr/local/bin/unicorn -c #{RELEASE_PATH}/config/unicorn.conf.rb -E production -D"
  155.  
  156. puts "done."
  157. end
  158.  
  159. desc "Restart unicorn"
  160. task :restart do
  161. init
  162. puts "\nReStart unicorn ..."
  163.  
  164. run "kill -USR2 `cat #{RELEASE_PATH}/tmp/pids/unicorn.pid`"
  165.  
  166. puts "done."
  167. end
  168.  
  169. desc "Stop unicorn"
  170. task :stop do
  171. init
  172. puts "\nStop unicorn ..."
  173.  
  174. run "kill -QUIT `cat #{RELEASE_PATH}/tmp/pids/unicorn.pid`"
  175.  
  176. puts "done."
  177. end
  178. end
Add Comment
Please, Sign In to add comment