Guest User

Untitled

a guest
Apr 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. ## mattly's vlad-fu
  2. # ~/.ruby is in ruby's load path
  3.  
  4. ## ~/.ruby/vlad/basic.rb
  5.  
  6. # meant to be used as part of Vlad the Deployer, as a 'core' set of tasks:
  7. # Vlad.load :core => :basic
  8. require 'vlad'
  9.  
  10. # used by update, out here so we can ensure all threads have the same value
  11. def now
  12. @now ||= Time.now.utc.strftime("%Y%m%d%H%M.%S")
  13. end
  14.  
  15. namespace :vlad do
  16. desc "Show the vlad setup. This is all the default variables for vlad
  17. tasks.".cleanup
  18.  
  19. task :debug do
  20. require 'yaml'
  21.  
  22. # force them into values
  23. Rake::RemoteTask.env.keys.each do |key|
  24. next if key =~ /_release|releases|sudo_password/
  25. Rake::RemoteTask.fetch key
  26. end
  27.  
  28. puts "# Environment:"
  29. puts
  30. y Rake::RemoteTask.env
  31. puts "# Roles:"
  32. y Rake::RemoteTask.roles
  33. end
  34.  
  35. desc "Setup your servers. Before you can use any of the deployment
  36. tasks with your project, you will need to make sure all of your
  37. servers have been prepared with 'rake vlad:setup'. It is safe to
  38. run this task on servers that have already been set up; it will
  39. not destroy any deployed revisions or data.".cleanup
  40.  
  41. remote_task :setup do
  42. run "mkdir -p #{[deploy_to, releases_path, scm_path, shared_path].join(' ')}"
  43. end
  44.  
  45. desc "Updates your application server to the latest revision. Syncs
  46. a copy of the repository, exports it as the latest release, fixes
  47. up your symlinks, symlinks the latest revision to current and logs
  48. the update.".cleanup
  49.  
  50. remote_task :update, :roles => :app do
  51. symlink = false
  52. rev = revision rescue 'head' # If revision doesn't exist, default to 'head'
  53. begin
  54. run [ "cd #{scm_path}",
  55. "#{source.checkout rev, '.'}",
  56. "#{source.export ".", release_path}",
  57. "chmod -R g+w #{latest_release}",
  58. ].join(" && ")
  59.  
  60. symlink = true
  61. run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
  62. run "echo #{now} $USER #{rev} #{File.basename release_path} >> #{deploy_to}/revisions.log"
  63. rescue => e
  64. run "rm -f #{current_path} && ln -s #{previous_release} #{current_path}" if
  65. symlink
  66. run "rm -rf #{release_path}"
  67. raise e
  68. end
  69. end
  70.  
  71. desc "Invoke a single command on every remote server. This is useful for
  72. performing one-off commands that may not require a full task to be written
  73. for them. Simply specify the command to execute via the COMMAND
  74. environment variable. To execute the command only on certain roles,
  75. specify the ROLES environment variable as a comma-delimited list of role
  76. names.
  77.  
  78. $ rake vlad:invoke COMMAND='uptime'".cleanup
  79.  
  80. remote_task :invoke do
  81. command = ENV["COMMAND"]
  82. abort "Please specify a command to execute on the remote servers (via the COMMAND environment variable)" unless command
  83. puts run(command)
  84. end
  85.  
  86. desc "Copy arbitrary files to the currently deployed version using
  87. FILES=a,b,c. This is useful for updating files piecemeal when you
  88. need to quickly deploy only a single file.
  89.  
  90. To use this task, specify the files and directories you want to copy as a
  91. comma-delimited list in the FILES environment variable. All directories
  92. will be processed recursively, with all files being pushed to the
  93. deployment servers. Any file or directory starting with a '.' character
  94. will be ignored.
  95.  
  96. $ rake vlad:upload FILES=templates,controller.rb".cleanup
  97.  
  98. remote_task :upload do
  99. file_list = (ENV["FILES"] || "").split(",")
  100.  
  101. files = file_list.map do |f|
  102. f = f.strip
  103. File.directory?(f) ? Dir["#{f}/**/*"] : f
  104. end.flatten
  105.  
  106. files = files.reject { |f| File.directory?(f) || File.basename(f)[0] == ?. }
  107.  
  108. abort "Please specify at least one file to update (via the FILES environment variable)" if files.empty?
  109.  
  110. files.each do |file|
  111. rsync file, File.join(current_path, file)
  112. end
  113. end
  114.  
  115. desc "Rolls back to a previous version and restarts. This is handy if you
  116. ever discover that you've deployed a lemon; 'rake vlad:rollback' and
  117. you're right back where you were, on the previously deployed
  118. version.".cleanup
  119.  
  120. remote_task :rollback do
  121. if releases.length < 2 then
  122. abort "could not rollback the code because there is no prior release"
  123. else
  124. run "rm #{current_path}; ln -s #{previous_release} #{current_path} && rm -rf #{current_release}"
  125. end
  126.  
  127. Rake::Task['vlad:start'].invoke
  128. end
  129.  
  130. desc "Clean up old releases. By default, the last 5 releases are kept on
  131. each server (though you can change this with the keep_releases variable).
  132. All other deployed revisions are removed from the servers.".cleanup
  133.  
  134. remote_task :cleanup do
  135. count = keep_releases
  136. old_releases = (releases - releases.last(count))
  137. if old_releases.empty? # count >= releases.size
  138. puts "no old releases to clean up"
  139. else
  140. puts "keeping #{count} of #{releases.length} deployed releases"
  141. run "sudo -p Password: rm -rf #{old_releases.map {|rel| File.join releases_path, rel}.join(' ')}"
  142. end
  143. end
  144.  
  145. end # namespace vlad
  146.  
  147.  
  148. ## ~/.ruby/vlad/nginx.rb
  149.  
  150. # meant to be used as part of Vlad the Deployer, as an 'app' set of tasks:
  151. # Vlad.load :app => :nginx
  152. require 'vlad'
  153.  
  154. desc "generates the config file from the template and yaml using generate_nginx_config gem"
  155. task :generate do
  156. `generate_nginx_config -y -t nginx.erb`
  157. end
  158.  
  159. namespace :vlad do
  160.  
  161. def sudo(command)
  162. run [sudo_cmd, '-p Password:', sudo_flags, command].join(' ')
  163. end
  164.  
  165. set :control_via, :initd
  166.  
  167. set :nginx_location, "/usr/bin/env nginx"
  168. set :config_location, "/etc/nginx"
  169. set :config_filename, "nginx.conf"
  170. set :pid_location, "/var/run/nginx.pid"
  171. set :raw, {
  172. :start => "#{nginx_location} -c #{config_location}/#{config_filename}",
  173. :stop => "kill -15 `cat #{pid_location}`",
  174. :reload => "kill -HUP `cat #{pid_location}`"
  175. }
  176.  
  177. set :initd_command, "/etc/init.d/nginx"
  178. set :initd, {
  179. :start => "#{initd_command} start",
  180. :stop => "#{initd_command} stop",
  181. :reload => "#{initd_command} restart"
  182. }
  183.  
  184. def command(cmd)
  185. set = self.send(control_via)
  186. set[cmd]
  187. end
  188.  
  189. desc "Reloads the Nginx config"
  190. remote_task :reload do
  191. sudo command(:reload)
  192. end
  193.  
  194. desc "Starts up Nginx from a cold start"
  195. remote_task :start do
  196. sudo command(:start)
  197. end
  198.  
  199. desc "Stops Nginx Dead"
  200. remote_task :stop do
  201. sudo command(:stop)
  202. end
  203. end
  204.  
  205. ## ~/.ruby/vlad/postfix.rb
  206.  
  207. namespace :vlad do
  208.  
  209. def sudo(command)
  210. run [sudo_cmd, "-p Password:", sudo_flags, command].join(' ')
  211. end
  212.  
  213. set :postfix_command, '/usr/sbin/postfix'
  214. set :postfix_init, '/etc/init.d/postfix'
  215. set :postmap_command, '/usr/sbin/postmap'
  216. set :etc_path, '/etc/postfix'
  217.  
  218. desc "copies the files to /etc/postfix"
  219. remote_task :copy_files_to_etc do
  220. sudo "cp -R #{current_path}/* #{etc_path}/"
  221. end
  222.  
  223. # WARNING: CAUSES WEIRD PERMISSION ERRORS ON UBUNTU 7!
  224. # desc "symlinks the deployment location to /etc"
  225. # remote_task :update_symlink do
  226. # sudo "ln -nfs #{current_path} #{etc_path}"
  227. # end
  228.  
  229. desc "compiles the hashmap files"
  230. remote_task :compile_maps do
  231. sudo "#{postmap_command} #{etc_path}/*.maps"
  232. end
  233.  
  234. # don't have to do this if we're copying instead of symlinking
  235. # desc "sets owner of the symlink and the deploy location to root so postfix doesn't get cranky"
  236. # remote_task :fix_permissions => :compile_maps do
  237. # sudo "#{postfix_command} set-permissions"
  238. # sudo "chown -h root:root #{etc_path}"
  239. # sudo "chown -R root:root #{current_release}"
  240. # sudo "chmod -R 644 #{current_release}"
  241. # sudo "chmod 755 #{current_release}/post-install"
  242. # sudo "chmod 755 #{current_release}/postfix-script"
  243. #
  244. # sudo "chown -R $USER:$USER #{previous_release}"
  245. # end
  246.  
  247. desc "Checks the main and master .cf files have ok syntax. Does NOT check that map files are compiled."
  248. remote_task :check_configuration => :compile_maps do
  249. sudo "#{postfix_command} check"
  250. end
  251.  
  252. desc "Reloads the Postfix configuration."
  253. remote_task :reload_configuration do
  254. sudo "#{postfix_command} reload"
  255. end
  256.  
  257. desc "Reloads or Starts Postfix as necessary"
  258. remote_task :restart_app do
  259. # how do we determine if postfix is running?
  260. end
  261.  
  262. desc "Stops the Postfix server."
  263. remote_task :stop_app do
  264. sudo "#{postfix_init} stop"
  265. end
  266.  
  267. desc "Starts the Postfix server."
  268. remote_task :start_app do
  269. sudo "#{postfix_init} start"
  270. end
  271.  
  272. end
Add Comment
Please, Sign In to add comment