• Stars
    star
    154
  • Rank 233,691 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Maintenance Page Support For Capistrano

Capistrano::Maintenance

Installation

Add this line to your application's Gemfile:

gem 'capistrano', '~> 3.11'
gem 'capistrano-maintenance', '~> 1.2', require: false

And then execute:

$ bundle

Or install it yourself:

$ gem install capistrano-maintenance

Usage

Before using the maintenance tasks, you need to configure your webserver. How you do this depends on how your server is configured, but the following examples should help you on your way.

Application servers such as Passenger and unicorn requires you to set your public web directory to current/public. Both examples below are compatible with this setup.

Here is an example config for nginx. Note that this is just a part of the complete config, and will probably require modifications.

error_page 503 @503;

# Return a 503 error if the maintenance page exists.
if (-f /var/www/domain.com/shared/public/system/maintenance.html) {
  return 503;
}

location @503 {
  # Serve static assets if found.
  if (-f $request_filename) {
    break;
  }

  # Set root to the shared directory.
  root /var/www/domain.com/shared/public;
  rewrite ^(.*)$ /system/maintenance.html break;
}

And here is an example config for Apache. This will also need to be modified.

# Create an alias to the maintenance page used as error document.
Alias "/error" "/var/www/domain.com/shared/public/system/"
ErrorDocument 503 /error/maintenance.html

# Return 503 error if the maintenance page exists.
RewriteCond /var/www/domain.com/shared/public/system/maintenance.html -f
RewriteRule !^/error/maintenance.html - [L,R=503]

# Redirect all non-static requests to unicorn (or similar).
# Will not redirect any requests if the maintenance page exists.
RewriteCond /var/www/domain.com/shared/public/system/maintenance.html !-f
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://unicornserver%{REQUEST_URI} [P,QSA,L]

You can now require the gem in your Capfile:

require 'capistrano/maintenance'

Enable task

Present a maintenance page to visitors. Disables your application's web interface by writing a "#{maintenance_basename}.html" file to each web server. The servers must be configured to detect the presence of this file, and if it is present, always display it instead of performing the request.

By default, the maintenance page will just say the site is down for "maintenance", and will be back "shortly", but you can customize the page by specifying the REASON and UNTIL environment variables:

cap maintenance:enable REASON="hardware upgrade" UNTIL="12pm Central Time"

You can use a different template for the maintenance page by setting the :maintenance_template_path variable in your deploy.rb file with an absolute path.

set :maintenance_template_path, File.expand_path("../../app/path/to/maintenance.html.erb", __FILE__)

The template file should either be a plaintext or an erb file. For example:

<!DOCTYPE html>
<html>
<head>
    <title>Maintenance</title>
    <style type="text/css">
    body {
        width: 400px;
        margin: 100px auto;
        font: 300 120% "OpenSans", "Helvetica Neue", "Helvetica", Arial, Verdana, sans-serif;
    }

    h1 {
        font-weight: 300;
    }
    </style>
</head>
<body>
    <h1>Maintenance</h1>

    <p>The system is down for <%= reason ? reason : "maintenance" %><br>
           as of <%= Time.now.strftime("%F %H:%M %Z") %>.</p>

    <p>It'll be back <%= deadline ? deadline : "shortly" %>.</p>
</body>
</html>

You can customize which folder the maintenance page template is rendered to by setting the :maintenance_dirname variable. By default it renders to "#{shared_path}/public/system".

set :maintenance_dirname, -> { "#{current_path}/dist" }

By default, the maintenance page will be uploaded to all servers with the web role, but if your application has different needs, you can customize this using the maintenance_roles variable:

set :maintenance_roles, -> { roles([:web, :other_role]) }

Further customization will require that you write your own task.

Disable task

cap maintenance:disable

Makes the application web-accessible again. Removes the "#{maintenance_basename}.html" page generated by maintenance:enable, which (if your web servers are configured correctly) will make your application web-accessible again.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Authors

The original capistrano-maintenance gem was created by Thomas von Deyen and the implementation for Capistrano 3 in this repo was written by Kir Shatrov.

As a Capistrano team, we thank Thomas for collaboration and providing the push access to the capistrano-maintenance gem.

More Repositories

1

capistrano

A deployment automation tool built on Ruby, Rake, and SSH.
Ruby
12,631
star
2

sshkit

A toolkit for deploying code and assets to servers in a repeatable, testable, reliable way.
Ruby
1,110
star
3

rails

Official Ruby on Rails specific tasks for Capistrano
Ruby
858
star
4

symfony

Capistrano tasks for deploying the Symfony standard edition
Ruby
354
star
5

laravel

Gem for deploying Laravel projects with capistrano v3.*
Ruby
220
star
6

bundler

Bundler support for Capistrano 3.x
Ruby
219
star
7

rbenv

Idiomatic rbenv support for Capistrano 3.x
Ruby
201
star
8

composer

Capistrano extension for Composer tasks
Ruby
183
star
9

npm

Capistrano extension for npm
Ruby
157
star
10

rvm

Ruby
139
star
11

passenger

Passenger support for Capistrano 3.x
Ruby
138
star
12

notification-center

Capistrano integration with macOS Notification Center
Ruby
88
star
13

documentation

CSS
81
star
14

file-permissions

File permissions handling for Capistrano v3.*
Ruby
46
star
15

drupal-deploy

Gem for deploying Drupal projects with capistrano 3
Ruby
40
star
16

github

Ruby
26
star
17

capistrano-2.x-docs

An archive of the now-defunct Capistrano 2.x wiki
24
star
18

copy-files

Capistrano v3.* extension for copying files between releases
Ruby
23
star
19

chruby

Ruby
23
star
20

stats

Capistrano statistics, gem, server, manifest and dashboard
Go
12
star
21

packer

Packer manifest to build a debian image for Vagrant which we can use to test against. (and, also for inducting people to the Capistrano way)
Shell
5
star
22

danger

Common Dangerfile for Capistrano projects
Ruby
3
star
23

capistrano-features

A place for discussions about possible Capistrano developments.
2
star
24

capistrano-sprockets

Rails Sprocket Support For Capistrano
2
star