Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. # Ulimate Capistrano Script by sutto@sutto.net, slightly modified to
  2. # include details to handle Apache also.
  3. #
  4. # You can set variables before if you'd like
  5. # Suitable for a vps-style system where each part is on the same domain.
  6.  
  7. # Application Specific Details
  8. set :application, ""
  9. set :domain, ""
  10. set :deploy_via, :export
  11. set :repository, ""
  12. set :scm_username, ""
  13. set :scm_password, ""
  14.  
  15. # Location
  16. set :deploy_to, "/var/apps/#{application}"
  17.  
  18. # Deployment details
  19. set :user, "deploy" unless exists?(:user)
  20. set :runner, user unless exists?(:runner)
  21. set :server_type, :thin unless exists?(:server_type)
  22. set :deploy_port, 9000 unless exists?(:deploy_port)
  23. set :cluster_instances, 3 unless exists?(:cluster_instances)
  24. set :use_sqlite3, true unless exists?(:use_sqlite)
  25. set :keep_releases, 3 unless exists?(:keep_releases)
  26.  
  27. # Paths
  28. set :shared_database_path, "#{shared_path}/databases"
  29. set :shared_config_path, "#{shared_path}/configs"
  30. set :shared_uploaded_images_path, "#{shared_path}/uploaded_images"
  31. set :public_uploaded_images_path, "#{current_path}/public/images/uploaded"
  32.  
  33. # Our helper methods
  34. def public_configuration_location_for(server = :thin)
  35. "#{current_path}/config/#{server}.yml"
  36. end
  37.  
  38. def shared_configuration_location_for(server = :thin)
  39. "#{shared_config_path}/#{server}.yml"
  40. end
  41.  
  42.  
  43. # Our Server Roles
  44. role :app, domain.to_s
  45. role :web, domain.to_s
  46. role :db, domain.to_s, :primary => true
  47.  
  48.  
  49. namespace :configuration do
  50.  
  51. desc "Links the local copies of the shared images folder"
  52. task :localize, :roles => :app do
  53. run "rm -rf #{public_uploaded_images_path}" # God. Damned. Reversing it removes ALL images. Not fun.
  54. run "ln -nsf #{shared_uploaded_images_path} #{public_uploaded_images_path}"
  55. end
  56.  
  57. desc "Makes link for database"
  58. task :make_default_folders, :roles => :app do
  59. run "mkdir -p #{shared_config_path}"
  60. run "mkdir -p #{shared_uploaded_images_path}"
  61. end
  62.  
  63. end
  64.  
  65.  
  66.  
  67. # Application Server Choices
  68.  
  69. namespace :mongrel do
  70.  
  71. desc "Generate a mongrel configuration file"
  72. task :build_configuration, :roles => :app do
  73. config_options = {
  74. "user" => (runner || user),
  75. "group" => (runner || user),
  76. "log_file" => "#{current_path}/log/mongrel.log",
  77. "cwd" => current_path,
  78. "port" => deploy_port,
  79. "servers" => cluster_instances,
  80. "environment" => "production",
  81. "address" => "localhost",
  82. "pid_file" => "#{current_path}/tmp/pids/mongrel.pid"
  83. }.to_yaml
  84. put config_options, shared_configuration_location_for(:mongrel)
  85. end
  86.  
  87. desc "Links the configuration file"
  88. task :link_configuration_file, :roles => :app do
  89. run "ln -nsf #{shared_configuration_location_for(:mongrel)} #{public_configuration_location_for(:mongrel)}"
  90. end
  91.  
  92. desc "Setup Mongrel Cluster After Code Update"
  93. task :link_global_configuration, :roles => :app do
  94. run "ln -nsf /etc/mongrel_cluster/#{application}.yml"
  95. end
  96.  
  97. %w(start stop restart).each do |action|
  98. desc "#{action} this app's Mongrel Cluster"
  99. task action.to_sym, :roles => :app do
  100. run "mongrel_rails cluster::#{action} -C #{shared_configuration_location_for(:mongrel)}"
  101. end
  102. end
  103.  
  104. end
  105.  
  106.  
  107. namespace :thin do
  108.  
  109. desc "Generate a thin configuration file"
  110. task :build_configuration, :roles => :app do
  111. config_options = {
  112. "user" => (runner || user),
  113. "group" => (runner || user),
  114. "log" => "#{current_path}/log/thin.log",
  115. "chdir" => current_path,
  116. "port" => deploy_port,
  117. "servers" => cluster_instances.to_i,
  118. "environment" => "production",
  119. "address" => "localhost",
  120. "pid" => "#{current_path}/tmp/pids/log.pid"
  121. }.to_yaml
  122. put config_options, shared_configuration_location_for(:thin)
  123. end
  124.  
  125. desc "Links the configuration file"
  126. task :link_configuration_file, :roles => :app do
  127. run "ln -nsf #{shared_configuration_location_for(:thin)} #{public_configuration_location_for(:thin)}"
  128. end
  129.  
  130. desc "Setup Thin Cluster After Code Update"
  131. task :link_global_configuration, :roles => :app do
  132. run "ln -nsf #{shared_configuration_location_for(:thin)} /etc/thin/#{application}.yml"
  133. end
  134.  
  135. %w(start stop restart).each do |action|
  136. desc "#{action} this app's Thin Cluster"
  137. task action.to_sym, :roles => :app do
  138. run "thin #{action} -C #{shared_configuration_location_for(:thin)}"
  139. end
  140. end
  141.  
  142. end
  143.  
  144.  
  145.  
  146. # Our Database Stuff - currently only sqlite3
  147.  
  148. namespace :sqlite3 do
  149.  
  150. desc "Generate a database configuration file"
  151. task :build_configuration, :roles => :db do
  152. db_options = {
  153. "adapter" => "sqlite3",
  154. "database" => "db/production.sqlite3"
  155. }
  156. config_options = {"production" => db_options}.to_yaml
  157. put config_options, "#{shared_config_path}/sqlite_config.yml"
  158. end
  159.  
  160. desc "Links the configuration file"
  161. task :link_configuration_file, :roles => :db do
  162. run "ln -nsf #{shared_config_path}/sqlite_config.yml #{current_path}/config/database.yml"
  163. run "touch #{shared_database_path}/production.sqlite3"
  164. run "ln -nsf #{shared_database_path}/production.sqlite3 #{current_path}/db/production.sqlite3"
  165. end
  166.  
  167. desc "Make a shared database folder"
  168. task :make_shared_folder, :roles => :db do
  169. run "mkdir -p #{shared_database_path}"
  170. end
  171.  
  172. end
  173.  
  174.  
  175.  
  176. # Our Web Servers
  177.  
  178. namespace :nginx do
  179.  
  180. desc "Start Nginx on the app server."
  181. task :start, :roles => :web do
  182. run "/etc/init.d/nginx start"
  183. end
  184.  
  185. desc "Restart the Nginx processes on the app server by starting and stopping the cluster."
  186. task :restart , :roles => :web do
  187. run "/etc/init.d/nginx restart"
  188. end
  189.  
  190. desc "Stop the Nginx processes on the app server."
  191. task :stop , :roles => :web do
  192. run "/etc/init.d/nginx stop"
  193. end
  194.  
  195. %w(start stop restart reload).each do |action|
  196. desc "#{action} the Nginx processes on the web server."
  197. task action.to_sym , :roles => :web do
  198. run "/etc/init.d/nginx #{action}"
  199. end
  200. end
  201.  
  202. end
  203.  
  204.  
  205. namespace :apache do
  206.  
  207. desc "Start Apache on the app server."
  208. task :start, :roles => :web do
  209. run "/etc/init.d/httpd start"
  210. end
  211.  
  212. desc "Restart the Apache processes on the app server by starting and stopping the cluster."
  213. task :restart , :roles => :web do
  214. run "/etc/init.d/httpd restart"
  215. end
  216.  
  217. desc "Stop the Apache processes on the app server."
  218. task :stop , :roles => :web do
  219. run "/etc/init.d/httpd stop"
  220. end
  221.  
  222. %w(start stop restart reload).each do |action|
  223. desc "#{action} the Apache processes on the web server."
  224. task action.to_sym , :roles => :web do
  225. run "/etc/init.d/httpd #{action}"
  226. end
  227. end
  228.  
  229. end
  230.  
  231.  
  232.  
  233. # Our magic
  234.  
  235. namespace :deploy do
  236. %w(start stop restart).each do |action|
  237. desc "#{action} our server"
  238. task action.to_sym do
  239. find_and_execute_task("#{server_type}:#{action}")
  240. end
  241. end
  242. end
  243.  
  244.  
  245. # After Tasks
  246. after "deploy:setup", "configuration:make_default_folders"
  247. after "deploy:setup", "#{server_type}:build_configuration"
  248.  
  249. #after "#{server_type}:build_configuration", "#{server_type}:link_global_configuration"
  250.  
  251. after "deploy:symlink", "configuration:localize"
  252. after "deploy:symlink", "#{server_type}:link_configuration_file"
  253.  
  254. if use_sqlite3 after "deploy:setup", "sqlite3:make_shared_folder"
  255. after "deploy:setup", "sqlite3:build_configuration"
  256. after "deploy:symlink", "sqlite3:link_configuration_file"
  257. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement