Guest User

Untitled

a guest
May 24th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.14 KB | None | 0 0
  1. set :domain, CLI.ui.ask("Domain you are deploying to (IP Address or Hostname): ")
  2. set :local, "#{`ifconfig | grep "192"`.match(/192\.168\.\d+\.\d+/)}"
  3. set :application, "bart"
  4. set :keep_releases, 2
  5. set :scm, :git
  6. set :deploy_to, "/var/www/#{application}"
  7. set :user, "deploy"
  8. set :runner, "mongrel"
  9. set :use_sudo, :false
  10.  
  11. role :app, "#{domain}"
  12. role :web, "#{domain}"
  13. role :db, "#{domain}", :primary => true
  14.  
  15. # == CONFIG ====================================================================
  16. namespace :init do
  17. namespace :config do
  18. desc "Create database.yml"
  19. task :database do
  20. if Capistrano::CLI.ui.ask("Create database configuration? (y/n): ") == 'y'
  21. set :db_name, Capistrano::CLI.ui.ask("database: ")
  22. set :db_user, Capistrano::CLI.ui.ask("database user: ")
  23. set :db_pass, Capistrano::CLI.password_prompt("database password: ")
  24. database_configuration =<<-EOF
  25. ---
  26. login: &login
  27. adapter: mysql
  28. host: localhost
  29. database: #{db_name}
  30. username: #{db_user}
  31. password: #{db_pass}
  32.  
  33. production:
  34. <<: *login
  35.  
  36. EOF
  37. sudo "mkdir -p #{shared_path}"
  38. sudo "chown deploy:deploy #{shared_path}"
  39. run "mkdir -p #{shared_path}/config"
  40. put database_configuration, "#{shared_path}/config/database.yml"
  41. end
  42. end
  43.  
  44. desc "Create mongrel_cluster.yml"
  45. task :mongrel do
  46. if Capistrano::CLI.ui.ask("Create mongrel configuration? (y/n): ") == 'y'
  47. mongrel_cluster_configuration = <<-EOF
  48. ---
  49. user: #{runner}
  50. cwd: #{current_path}
  51. log_file: #{current_path}/log/mongrel.log
  52. port: "8000"
  53. environment: production
  54. group: mongrel
  55. address: 127.0.0.1
  56. pid_file: #{current_path}/tmp/pids/mongrel.pid
  57. servers: 3
  58. EOF
  59. run "mkdir -p #{shared_path}/config"
  60. put mongrel_cluster_configuration, "#{shared_path}/config/mongrel_cluster.yml"
  61.  
  62. run "mkdir -p #{shared_path}/log"
  63. sudo "touch #{shared_path}/log/production.log"
  64. sudo "chmod 0666 #{shared_path}/log/production.log"
  65.  
  66. run "mkdir -p #{shared_path}/tmp/pids"
  67.  
  68. # Survive reboot
  69. mongrel_cluster_reboot_script = <<-EOF
  70. #!/bin/bash
  71. CONF_DIR=/etc/mongrel_cluster/
  72. PID_DIR=#{current_path}/tmp/pids
  73. USER=#{runner}
  74. USER=mongrel
  75. RETVAL=0
  76.  
  77. # Gracefully exit if the controller is missing.
  78. which mongrel_cluster_ctl >/dev/null || exit 0
  79.  
  80. # Go no further if config directory is missing.
  81. [ -d "$CONF_DIR" ] || exit 0
  82.  
  83. case "$1" in
  84. start)
  85. # Create pid directory
  86. mkdir -p $PID_DIR
  87. chown $USER:$USER $PID_DIR
  88.  
  89. # remove stale pids
  90. rm -f $PID_DIR/mongrel.80*
  91.  
  92. mongrel_cluster_ctl start --clean -v -c $CONF_DIR
  93. RETVAL=$?
  94. ;;
  95. stop)
  96. mongrel_cluster_ctl stop --clean -v -c $CONF_DIR
  97. RETVAL=$?
  98. ;;
  99. restart)
  100. # remove stale pids
  101. rm -f $PID_DIR/mongrel.80*
  102.  
  103. mongrel_cluster_ctl restart --clean -v -c $CONF_DIR
  104. RETVAL=$?
  105. ;;
  106. status)
  107. mongrel_cluster_ctl status -v -c $CONF_DIR
  108. RETVAL=$?
  109. ;;
  110. *)
  111. echo "Usage: mongrel_cluster {start|stop|restart|status}"
  112. exit 1
  113. ;;
  114. esac
  115.  
  116. exit $RETVAL
  117. EOF
  118. run "mkdir -p #{shared_path}/scripts"
  119. put mongrel_cluster_reboot_script, "#{shared_path}/scripts/mongrel_cluster"
  120. sudo "cp #{shared_path}/scripts/mongrel_cluster /etc/init.d/mongrel_cluster"
  121. sudo "chmod +x /etc/init.d/mongrel_cluster"
  122. sudo "/usr/sbin/update-rc.d -f mongrel_cluster defaults"
  123. sudo "ln #{shared_path}/config/mongrel_cluster.yml /etc/mongrel_cluster/#{application}.yml"
  124. end
  125. end
  126.  
  127. desc "Create cron tasks for success testing, report caching and database backups"
  128. task :cron do
  129. if Capistrano::CLI.ui.ask("Create cron jobs? (y/n): ") == 'y'
  130. cron_configuration =<<-EOF
  131. # m h dom mon dow command
  132. 0 18 * * mon-fri mysqldump -u root openmrs > #{shared_path}/backup/openmrs.sql; /usr/local/bin/rsnapshot daily
  133. 0 18 * * sat /usr/local/bin/rsnapshot weekly
  134. * * * * mon-fri #{current_path}/script/runner -e production 'Success.verify'
  135.  
  136. 0 19 * * mon-fri #{current_path}/script/runner -e production 'Report.cache'
  137. 0 22 * * mon-fri #{current_path}/script/runner -e production 'Patient.update_defaulters'
  138. EOF
  139.  
  140. run "mkdir -p #{shared_path}/backup"
  141. run "echo 'Current cron configuration'"
  142. run "crontab -l; echo ---"
  143. put cron_configuration, "#{shared_path}/scripts/cron"
  144. # Note this overwrites the cron configuration for the deploy user every time, if you have other crontabs you have to do more work
  145. run "cat #{shared_path}/scripts/cron | crontab -"
  146. end
  147. end
  148.  
  149. desc "Change the ownership of the releases dir"
  150. task :own do
  151. sudo "chown -R deploy:deploy #{deploy_to}/releases"
  152. end
  153.  
  154. desc "Setup DNS/DHCP server"
  155. task :dns do
  156. if Capistrano::CLI.ui.ask("Setup DNS/DHCP server? (y/n): ") == 'y'
  157.  
  158. end
  159. end
  160.  
  161.  
  162. desc "Symlink shared configurations to current"
  163. task :localize, :roles => [:app] do
  164. %w[mongrel_cluster.yml database.yml].each do |f|
  165. run "ln -nsf #{shared_path}/config/#{f} #{current_path}/config/#{f}"
  166. end
  167. end
  168. end
  169. end
  170.  
  171. # == OpenMRS ===================================================================
  172. namespace :openmrs do
  173. desc "Load the OpenMRS application defaults"
  174. task :bootstrap_load_defaults, :roles => :app do
  175. run "cd #{current_path} && rake openmrs:bootstrap:load:defaults RAILS_ENV=production"
  176. end
  177.  
  178. desc "Load the OpenMRS site defaults"
  179. task :bootstrap_load_site, :roles => :app do
  180. set :site_arv_code, Capistrano::CLI.ui.ask("Enter the site ARV code: ")
  181. run "cd #{current_path} && rake openmrs:bootstrap:load:site SITE=#{site_arv_code} RAILS_ENV=production"
  182. end
  183. end
  184.  
  185.  
  186. # == NGINX =====================================================================
  187. namespace :nginx do
  188. desc "Start Nginx on the app server"
  189. task :start, :roles => :app do
  190. sudo "/etc/init.d/nginx start"
  191. end
  192.  
  193. desc "Restart the Nginx processes on the app server by starting and stopping the cluster"
  194. task :restart , :roles => :app do
  195. sudo "/etc/init.d/nginx restart"
  196. end
  197.  
  198. desc "Stop the Nginx processes on the app server"
  199. task :stop , :roles => :app do
  200. sudo "/etc/init.d/nginx stop"
  201. end
  202.  
  203. desc "Setup the Nginx conf file from the example config"
  204. task :setup , :roles => :app do
  205. sudo "cp #{current_path}/config/nginx.conf.example /etc/nginx/nginx.conf"
  206. end
  207. end
  208.  
  209. # == DATABASE ==================================================================
  210. namespace :db do
  211. desc "Backup your Database to #{shared_path}/backup"
  212. task :backup, :roles => :db, :only => {:primary => true} do
  213. set :db_name, Capistrano::CLI.ui.ask("Database: ")
  214. set :db_user, Capistrano::CLI.ui.ask("Database user: ")
  215. set :db_pass, Capistrano::CLI.password_prompt("Database password: ")
  216. now = Time.now
  217. run "mkdir -p #{shared_path}/backup"
  218. backup_time = [now.year,now.month,now.day,now.hour,now.min,now.sec].join('-')
  219. set :backup_file, "#{shared_path}/backup/#{application}-snapshot-#{backup_time}.sql"
  220. run "mysqldump --add-drop-table -u #{db_user} -p #{db_pass} #{db_name} --opt | bzip2 -c > #{backup_file}.bz2"
  221. end
  222. end
  223.  
  224. # == DEPLOY ======================================================================
  225. namespace :deploy do
  226. if Capistrano::CLI.ui.ask("Pull from current machine (#{local})? (y/n): ") == 'y'
  227. set :distribution, local
  228. set :repository, "git://#{distribution}/var/www/#{application}"
  229. elsif Capistrano::CLI.ui.ask("Pull from github.com (public)? (y/n): ") == 'y'
  230. set :repository, "git://github.com/baobab/bart.git"
  231. elsif Capistrano::CLI.ui.ask("Connect to distributed git repository? (y/n): ") == 'y'
  232. set :distribution, Capistrano::CLI.ui.ask("Repository address: ")
  233. set :repository, "git://#{distribution}/var/www/#{application}"
  234. else
  235. set :repository, "git://null"
  236. end
  237.  
  238. desc "Start mongrel cluster"
  239. task :start do
  240. run "cd #{current_path} && sudo mongrel_rails cluster::start"
  241. end
  242.  
  243. desc "Stop mongrel cluster"
  244. task :stop do
  245. run "cd #{current_path} && sudo mongrel_rails cluster::stop"
  246. end
  247.  
  248. desc "Restart mongrel cluster"
  249. task :restart do
  250. run "cd #{current_path} && sudo mongrel_rails cluster::restart"
  251. end
  252. end
  253.  
  254. # == TASKS =====================================================================
  255. before "deploy:migrate", "db:backup"
  256.  
  257. after "deploy", "deploy:cleanup"
  258. after "deploy:migrations", "deploy:cleanup"
  259. after "deploy:setup", "init:config:database"
  260. after "deploy:setup", "init:config:mongrel"
  261. after "deploy:setup", "init:config:cron"
  262. after "deploy:symlink", "init:config:localize"
  263. after "deploy:setup", "init:config:own"
  264.  
  265. task :after_update_code do
  266. sudo "chown mongrel:mongrel #{release_path}/public -R" # Caching, in a public app this is not a good idea on closed systems it is okay
  267. sudo "chown mongrel:mongrel #{release_path}/tmp -R"
  268. sudo "chown mongrel:mongrel #{release_path}/log -R"
  269. sudo "chown mongrel:mongrel #{shared_path}/pids -R"
  270. sudo "chown mongrel:mongrel #{shared_path}/config -R"
  271. end
Add Comment
Please, Sign In to add comment