09.22 ruby saved
ian
Tags add more
capistrano and deployment  
Note
This is version 0.1 of a set of cake deploy tasks. Lots to do including:

1. handling rollback if the transaction fails;
2. deleting the app_for_deletion dir if the transaction succeeds;
3. abstracting out some of the tasks, e.g. detecting if an svn_username or svn_password is set and using the appropriate commands.
  1. # This defines a deployment "recipe" that you can feed to capistrano
  2. # (http://manuals.rubyonrails.com/read/book/17). It allows you to automate
  3. # (among other things) the deployment of your application.
  4.  
  5. # =============================================================================
  6. # REQUIRED VARIABLES
  7. # =============================================================================
  8. # You must always specify the application and repository for every recipe. The
  9. # repository must be the URL of the repository you want this recipe to
  10. # correspond to. The deploy_to path must be the path on each machine that will
  11. # form the root of the application path.
  12.  
  13. set :application, "YOUR APP NAME"
  14. set :repository, "YORU REPOSITORY PATH"
  15.  
  16. # =============================================================================
  17. # ROLES
  18. # =============================================================================
  19. # You can define any number of roles, each of which contains any number of
  20. # machines. Roles might include such things as :web, or :app, or :db, defining
  21. # what the purpose of each machine is. You can also specify options that can
  22. # be used to single out a specific subset of boxes in a particular role, like
  23. # :primary => true.
  24.  
  25. role :web, "" # WHERE TO DEPLOY TO e.g. www.mywebsite.com
  26. #role :app, "" # not used - if the application code lives separately to apache, the app box is named here
  27. #role :db,  "" # not used - if the database lives separately to apache & app, the database box is named here
  28.  
  29. # =============================================================================
  30. # OPTIONAL VARIABLES
  31. # =============================================================================
  32. # set :deploy_to, "/path/to/app" # defaults to "/u/apps/#{application}"
  33. # set :user, "flippy"            # defaults to the currently logged in user
  34. # set :scm, :darcs               # defaults to :subversion
  35. # set :svn, "/path/to/svn"       # defaults to searching the PATH
  36. # set :darcs, "/path/to/darcs"   # defaults to searching the PATH
  37. # set :cvs, "/path/to/cvs"       # defaults to searching the PATH
  38. # set :gateway, "gate.host.com"  # default to no gateway
  39.  
  40. set :app_root, "" # the full path to the app directory of your cake installation, e.g. /home/blah/www/app
  41. set :archive_to, "" # the full path to the archive directory to store tarred backups e.g. /home/blah/archive
  42. set :tmp_app_dir, "app_for_deletion" # the task temp stores the current app as this dir so rollback is easier
  43. set :svn_username, "" # if your svn is password protected, the username here
  44. set :svn_password, "" # if your svn is password protected, the password here
  45. set :checkout, "export" # I also prefer export, change to checkout if you prefer
  46.  
  47. # =============================================================================
  48. # SSH OPTIONS
  49. # =============================================================================
  50. # ssh_options[:keys] = %w(/path/to/my/key /path/to/another/key)
  51. # ssh_options[:port] = 25
  52. # ssh_options[:username] = "your ssh username here"
  53.  
  54. # =============================================================================
  55. # TASKS
  56. # =============================================================================
  57. # Define tasks that run on all (or only some) of the machines. You can specify
  58. # a role (or set of roles) that each task should be executed on. You can also
  59. # narrow the set of servers to a subset of a role by specifying options, which
  60. # must match the options given for the servers to select (like :primary => true)
  61. # Tasks may take advantage of several different helper methods to interact
  62. # with the remote server(s). These are:
  63. #
  64. # * run(command, options={}, &block): execute the given command on all servers
  65. #   associated with the current task, in parallel. The block, if given, should
  66. #   accept three parameters: the communication channel, a symbol identifying the
  67. #   type of stream (:err or :out), and the data. The block is invoked for all
  68. #   output from the command, allowing you to inspect output and act
  69. #   accordingly.
  70. # * sudo(command, options={}, &block): same as run, but it executes the command
  71. #   via sudo.
  72. # * delete(path, options={}): deletes the given file or directory from all
  73. #   associated servers. If :recursive => true is given in the options, the
  74. #   delete uses "rm -rf" instead of "rm -f".
  75. # * put(buffer, path, options={}): creates or overwrites a file at "path" on
  76. #   all associated servers, populating it with the contents of "buffer". You
  77. #   can specify :mode as an integer value, which will be used to set the mode
  78. #   on the file.
  79. # * render(template, options={}) or render(options={}): renders the given
  80. #   template and returns a string. Alternatively, if the :template key is given,
  81. #   it will be treated as the contents of the template to render. Any other keys
  82. #   are treated as local variables, which are made available to the (ERb)
  83. #   template.
  84.  
  85. desc "Archive current version to the archives dir as a tarred file"
  86. task :archive do
  87.     date = Time.now.strftime("%b_%d_%Y_%H_%M_%S")
  88.     run "tar cvzf #{archive_to}/app_#{date}.tar.gz #{app_root}"
  89. end
  90.  
  91.  
  92. desc "Move current version to a temp dir ready for deletion"
  93. task :move_for_deletion do
  94.     run "mv #{app_root} #{tmp_app_dir}"
  95. end
  96.  
  97.  
  98. desc "Export app only from password protected SVN repos to app folder"
  99. task :update_app do
  100.     run "svn -q #{checkout} #{repository} #{app_root} --username #{svn_username} --password #{svn_password} --no-auth-cache"
  101. end
  102.  
  103.  
  104. desc "Set permissions correctly on tmp file"
  105. task :set_perms do
  106.     run "chmod 777 #{app_root}/tmp"
  107.     run "chmod 777 #{app_root}/tmp/cache"
  108.     run "chmod 777 #{app_root}/tmp/cache/models"
  109.     run "chmod 777 #{app_root}/tmp/cache/persistent"
  110.     run "chmod 777 #{app_root}/tmp/cache/views"
  111.     run "chmod -R 777 #{app_root}/tmp/logs"
  112.     #run "chmod 777 #{app_root}/tmp/sessions" not used
  113.     #run "chmod 777 #{app_root}/tmp/tests" not used
  114. end
  115.  
  116. # You can use "transaction" to indicate that if any of the tasks within it fail,
  117. # all should be rolled back (for each task that specifies an on_rollback
  118. # handler).
  119.  
  120. desc "All deployment tasks wrapped up into a transaction"
  121. task :deploy do
  122.     transaction do
  123.         archive
  124.         move_for_deletion
  125.         update_app
  126.         set_perms
  127.     end
  128. end
Parsed in 0.023 seconds, using GeSHi 1.0.7.14

Modify this Paste