Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3cbb90f97e
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 461 lines (396 sloc) 19.285 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
Capistrano::Configuration.instance(:must_exist).load do

  require 'capistrano/recipes/deploy/scm'
  require 'capistrano/recipes/deploy/strategy'

  def _cset(name, *args, &block)
    unless exists?(name)
      set(name, *args, &block)
    end
  end

  # =========================================================================
  # These variables MUST be set in the client capfiles. If they are not set,
  # the deploy will fail with an error.
  # =========================================================================

  _cset(:application) { abort "Please specify the name of your application, set :application, 'foo'" }
  _cset(:repository) { abort "Please specify the repository that houses your application's code, set :repository, 'foo'" }

  # =========================================================================
  # These variables may be set in the client capfile if their default values
  # are not sufficient.
  # =========================================================================

  _cset :scm, :subversion
  _cset :deploy_via, :checkout

  _cset(:deploy_to) { "/u/apps/#{application}" }
  _cset(:revision) { source.head }

  # =========================================================================
  # These variables should NOT be changed unless you are very confident in
  # what you are doing. Make sure you understand all the implications of your
  # changes if you do decide to muck with these!
  # =========================================================================

  _cset(:source) { Capistrano::Deploy::SCM.new(scm, self) }
  _cset(:real_revision) { source.local.query_revision(revision) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } } }

  _cset(:strategy) { Capistrano::Deploy::Strategy.new(deploy_via, self) }

  _cset(:release_name) { set :deploy_timestamped, true; Time.now.utc.strftime("%Y%m%d%H%M%S") }

  _cset :version_dir, "releases"
  _cset :shared_dir, "shared"
  _cset :shared_children, [] # Empty!
  _cset :current_dir, "current"

  _cset(:releases_path) { File.join(deploy_to, version_dir) }
  _cset(:shared_path) { File.join(deploy_to, shared_dir) }
  _cset(:current_path) { File.join(deploy_to, current_dir) }
  _cset(:release_path) { File.join(releases_path, release_name) }

  _cset(:releases) { capture("ls -xt #{releases_path}").split.reverse }
  _cset(:current_release) { File.join(releases_path, releases.last) }
  _cset(:previous_release) { releases.length > 1 ? File.join(releases_path, releases[-2]) : nil }

  _cset(:current_revision) { capture("cat #{current_path}/REVISION").chomp }
  _cset(:latest_revision) { capture("cat #{current_release}/REVISION").chomp }
  _cset(:previous_revision) { capture("cat #{previous_release}/REVISION").chomp }

  _cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }

  # some tasks, like symlink, need to always point at the latest release, but
  # they can also (occassionally) be called standalone. In the standalone case,
  # the timestamped release_path will be inaccurate, since the directory won't
  # actually exist. This variable lets tasks like symlink work either in the
  # standalone case, or during deployment.
  _cset(:latest_release) { exists?(:deploy_timestamped) ? release_path : current_release }

  # =========================================================================
  # These are helper methods that will be available to your recipes.
  # =========================================================================

  # Auxiliary helper method for the `deploy:check' task. Lets you set up your
  # own dependencies.
  def depend(location, type, *args)
    deps = fetch(:dependencies, {})
    deps[location] ||= {}
    deps[location][type] ||= []
    deps[location][type] << args
    set :dependencies, deps
  end

  # Temporarily sets an environment variable, yields to a block, and restores
  # the value when it is done.
  def with_env(name, value)
    saved, ENV[name] = ENV[name], value
    yield
  ensure
    ENV[name] = saved
  end

  # logs the command then executes it locally.
  # returns the command output as a string
  def run_locally(cmd)
    logger.trace "executing locally: #{cmd.inspect}" if logger
    `#{cmd}`
  end

  # If a command is given, this will try to execute the given command, as
  # described below. Otherwise, it will return a string for use in embedding in
  # another command, for executing that command as described below.
  #
  # If :run_method is :sudo (or :use_sudo is true), this executes the given command
  # via +sudo+. Otherwise is uses +run+. If :as is given as a key, it will be
  # passed as the user to sudo as, if using sudo. If the :as key is not given,
  # it will default to whatever the value of the :admin_runner variable is,
  # which (by default) is unset.
  #
  # THUS, if you want to try to run something via sudo, and what to use the
  # root user, you'd just to try_sudo('something'). If you wanted to try_sudo as
  # someone else, you'd just do try_sudo('something', :as => "bob"). If you
  # always wanted sudo to run as a particular user, you could do
  # set(:admin_runner, "bob").
  def try_sudo(*args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    command = args.shift
    raise ArgumentError, "too many arguments" if args.any?

    as = options.fetch(:as, fetch(:admin_runner, nil))
    via = fetch(:run_method, :sudo)
    if command
      invoke_command(command, :via => via, :as => as)
    elsif via == :sudo
      sudo(:as => as)
    else
      ""
    end
  end

  # Same as sudo, but tries sudo with :as set to the value of the :runner
  # variable (which defaults to "app").
  def try_runner(*args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    args << options.merge(:as => fetch(:runner, "app"))
    try_sudo(*args)
  end

  # =========================================================================
  # These are the tasks that are available to help with deploying web apps,
  # and specifically, Rails applications. You can have cap give you a summary
  # of them with `cap -T'.
  # =========================================================================

  namespace :deploy do
    desc <<-DESC
Deploys your project. Handy wrapper to hook into the beginning of the deployment. Note that \
this will generally only work for applications that have already been deployed \
once. For a "cold" deploy, you'll want to take a look at the `deploy:cold' \
task, which handles the cold start specifically.
DESC
    task :default do
      logger.debug "Beginning railsless-deploy task - deploy:default"
      update
    end

    desc <<-DESC
Prepares one or more servers for deployment. Before you can use any \
of the Capistrano deployment tasks with your project, you will need to \
make sure all of your servers have been prepared with `cap deploy:setup'. When \
you add a new server to your cluster, you can easily run the setup task \
on just that server by specifying the HOSTS environment variable:

$ cap HOSTS=new.server.com deploy:setup

It is safe to run this task on servers that have already been set up; it \
will not destroy any deployed revisions or data.
DESC
    task :setup, :except => { :no_release => true } do
      logger.debug "Beginning railsless-deploy task - deploy:setup"
      dirs = [deploy_to, releases_path, shared_path]
      dirs += shared_children.map { |d| File.join(shared_path, d) }
      run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
    end

    desc <<-DESC
Copies your project and updates the symlink. It does this in a \
transaction, so that if either `update_code' or `symlink' fail, all \
changes made to the remote servers will be rolled back, leaving your \
system in the same state it was in before `update' was invoked. Usually, \
you will want to call `deploy' instead of `update', but `update' can be \
handy if you want to deploy, but not immediately restart your application.
DESC
    task :update do
      logger.debug "Beginning railsless-deploy task - deploy:update"
      transaction do
        update_code
        symlink
      end
    end

    desc <<-DESC
Copies your project to the remote servers. This is the first stage \
of any deployment; moving your updated code and assets to the deployment \
servers. You will rarely call this task directly, however; instead, you \
should call the `deploy' task (to do a complete deploy) or the `update' \
task (if you want to perform the `restart' task separately).

You will need to make sure you set the :scm variable to the source \
control software you are using (it defaults to :subversion), and the \
:deploy_via variable to the strategy you want to use to deploy (it \
defaults to :checkout).
DESC
    task :update_code, :except => { :no_release => true } do
      logger.debug "Beginning railsless-deploy task - deploy:update_code"
      on_rollback { run "rm -rf #{release_path}; true" }
      transaction do
        update_cache
        compress_bundle
        upload_bundle
        decompress_bundle
      end
      finalize_update
    end

    desc <<-DESC
[internal] Updates the local cached copy of the code
DESC
    task :update_cache, :except => { :no_release => true } do
      strategy.update_cache!
    end

    desc <<-DESC
[internal] Creates a compressed bundle from the local code cache
DESC
    task :compress_bundle, :except => { :no_release => true } do
      strategy.compress_bundle!
    end

    desc <<-DESC
[internal] Uploads the compressed bundle to the target node
DESC
    task :upload_bundle, :except => { :no_release => true } do
      strategy.upload_bundle!
    end

    desc <<-DESC
[internal] Decompress the uploaded bundle on the target node
DESC
    task :decompress_bundle, :except => { :no_release => true } do
      strategy.decompress_bundle!
    end

    desc <<-DESC
[internal] Touches up the released code. This is called by update_code \
after the basic deploy finishes. It assumes a Rails project was deployed, \
so if you are deploying something else, you may want to override this \
task with your own environment's requirements.

This task will make the release group-writable (if the :group_writable \
variable is set to true, which is the default). It will then set up \
symlinks to the shared directory for the log, system, and tmp/pids \
directories, and will lastly touch all assets in public/images, \
public/stylesheets, and public/javascripts so that the times are \
consistent (so that asset timestamping works). This touch process \
is only carried out if the :normalize_asset_timestamps variable is \
set to true, which is the default.
DESC
    task :finalize_update, :except => { :no_release => true } do
      logger.debug "Beginning railsless-deploy task - deploy:finalize_update"
      run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
    end

    desc <<-DESC
Updates the symlink to the most recently deployed version. Capistrano works \
by putting each new release of your application in its own directory. When \
you deploy a new version, this task's job is to update the `current' symlink \
to point at the new version. You will rarely need to call this task \
directly; instead, use the `deploy' task (which performs a complete \
deploy, including `restart') or the 'update' task (which does everything \
except `restart').
DESC
    task :symlink, :except => { :no_release => true } do
      logger.debug "Beginning railsless-deploy task - deploy:symlink"
      on_rollback do
        if previous_release
          run "rm -f #{current_path}; ln -s #{previous_release} #{current_path}; true"
        else
          logger.important "no previous release to rollback to, rollback of symlink skipped"
        end
      end

      run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
    end

    desc <<-DESC
Copy files to the currently deployed version. This is useful for updating \
files piecemeal, such as when you need to quickly deploy only a single \
file. Some files, such as updated templates, images, or stylesheets, \
might not require a full deploy, and especially in emergency situations \
it can be handy to just push the updates to production, quickly.

To use this task, specify the files and directories you want to copy as a \
comma-delimited list in the FILES environment variable. All directories \
will be processed recursively, with all files being pushed to the \
deployment servers.

$ cap deploy:upload FILES=templates,controller.rb

Dir globs are also supported:

$ cap deploy:upload FILES='config/apache/*.conf'
DESC
    task :upload, :except => { :no_release => true } do
      logger.debug "Beginning railsless-deploy task - deploy:upload"
      files = (ENV["FILES"] || "").split(",").map { |f| Dir[f.strip] }.flatten
      abort "Please specify at least one file or directory to update (via the FILES environment variable)" if files.empty?

      files.each { |file| top.upload(file, File.join(current_path, file)) }
    end

    namespace :rollback do
      desc <<-DESC
[internal] Points the current symlink at the previous revision.
This is called by the rollback sequence, and should rarely (if
ever) need to be called directly.
DESC
      task :revision, :except => { :no_release => true } do
        logger.debug "Beginning railsless-deploy task - rollback:upload"
        if previous_release
          run "rm #{current_path}; ln -s #{previous_release} #{current_path}"
        else
          abort "could not rollback the code because there is no prior release"
        end
      end

      desc <<-DESC
[internal] Removes the most recently deployed release.
This is called by the rollback sequence, and should rarely
(if ever) need to be called directly.
DESC
      task :cleanup, :except => { :no_release => true } do
        logger.debug "Beginning railsless-deploy task - rollback:cleanup"
        run "if [ `readlink #{current_path}` != #{current_release} ]; then rm -rf #{current_release}; fi"
      end

      desc <<-DESC
Rolls back to the previously deployed version. The `current' symlink will \
be updated to point at the previously deployed version, and then the \
current release will be removed from the servers.
DESC
      task :code, :except => { :no_release => true } do
        logger.debug "Beginning railsless-deploy task - rollback:code"
        revision
        cleanup
      end

      desc <<-DESC
Rolls back to a previous version and restarts. This is handy if you ever \
discover that you've deployed a lemon; `cap rollback' and you're right \
back where you were, on the previously deployed version.
DESC
      task :default do
        logger.debug "Beginning railsless-deploy task - rollback:default"
        revision
        cleanup
      end
    end

    desc <<-DESC
Clean up old releases. By default, the last 5 releases are kept on each \
server (though you can change this with the keep_releases variable). All \
other deployed revisions are removed from the servers. By default, this \
will use sudo to clean up the old releases, but if sudo is not available \
for your environment, set the :use_sudo variable to false instead.
DESC
    task :cleanup, :except => { :no_release => true } do
      logger.debug "Beginning railsless-deploy task - deploy:cleanup"
      count = fetch(:keep_releases, 5).to_i
      if count >= releases.length
        logger.important "no old releases to clean up"
      else
        logger.info "keeping #{count} of #{releases.length} deployed releases"

        directories = (releases - releases.last(count)).map { |release|
          File.join(releases_path, release) }.join(" ")

        try_sudo "rm -rf #{directories}"
      end
    end

    desc <<-DESC
Test deployment dependencies. Checks things like directory permissions, \
necessary utilities, and so forth, reporting on the things that appear to \
be incorrect or missing. This is good for making sure a deploy has a \
chance of working before you actually run `cap deploy'.

You can define your own dependencies, as well, using the `depend' method:

depend :remote, :gem, "tzinfo", ">=0.3.3"
depend :local, :command, "svn"
depend :remote, :directory, "/u/depot/files"
DESC
    task :check, :except => { :no_release => true } do
      logger.debug "Beginning railsless-deploy task - deploy:check"
      dependencies = strategy.check!

      other = fetch(:dependencies, {})
      other.each do |location, types|
        types.each do |type, calls|
          if type == :gem
            dependencies.send(location).command(fetch(:gem_command, "gem")).or("`gem' command could not be found. Try setting :gem_command")
          end

          calls.each do |args|
            dependencies.send(location).send(type, *args)
          end
        end
      end

      if dependencies.pass?
        puts "You appear to have all necessary dependencies installed"
      else
        puts "The following dependencies failed. Please check them and try again:"
        dependencies.reject { |d| d.pass? }.each do |d|
          puts "--> #{d.message}"
        end
        abort
      end
    end

    desc <<-DESC
Deploys and starts a `cold' application. This is useful if you have not \
deployed your application before, or if your application is (for some \
other reason) not currently running. It will deploy the code, run any \
pending migrations, and then instead of invoking `deploy:restart', it will \
invoke `deploy:start' to fire up the application servers.
DESC
    task :cold do
      logger.debug "Beginning railsless-deploy task - deploy:cold"
      update
    end

    namespace :pending do
      desc <<-DESC
Displays the `diff' since your last deploy. This is useful if you want \
to examine what changes are about to be deployed. Note that this might \
not be supported on all SCM's.
DESC
      task :diff, :except => { :no_release => true } do
        logger.debug "Beginning railsless-deploy task - pending:diff"
        system(source.local.diff(current_revision))
      end

      desc <<-DESC
Displays the commits since your last deploy. This is good for a summary \
of the changes that have occurred since the last deploy. Note that this \
might not be supported on all SCM's.
DESC
      task :default, :except => { :no_release => true } do
        logger.debug "Beginning railsless-deploy task - pending:default"
        from = source.next_revision(current_revision)
        system(source.local.log(from))
      end
    end

  end

end # Capistrano::Configuration.instance(:must_exist).load do
Something went wrong with that request. Please try again.