sign inHome | Recent Changes | All Pages | HelpSearch:

Capistrano 1

Don’t use this for setting up a new account. It is very old, and won’t work on our newer servers. It also uses what we consider depreciated technology.


# Introductory Capistrano config for PLANET ARGON.
#
# ORIGINAL BY: Jamis Buck
#
# Docs: http://manuals.rubyonrails.com/read/chapter/98
#
# HIGHLY MODIFIED BY: Geoffrey Grosenbach boss@topfunky.com
#
# Docs: http://nubyonrails.com/pages/shovel
#
# USE AT YOUR OWN RISK! THIS SCRIPT MODIFIES FILES, MAKES DIRECTORIES, AND STARTS
# PROCESSES. FOR ADVANCED OR DARING USERS ONLY!
#
# HACKED BY: George Anderson (ganderso@gmail.com) to work on PLANET ARGON shared hosting
#
# DESCRIPTION
#
# This is a customized recipe for easily deploying lighttpd to PLANET ARGON.
# It takes the instructions from five different manuals and puts them into a Capistrano
# Recipe.
#
# For full details/more information, see:
#   http://manuals.rubyonrails.com/read/book/17
#   http://nubyonrails.com/pages/shovel
#   http://www.lighttpd.net/documentation/configuration.html
#   http://www.brandalism.com/articles/2006/02/09/setting-up-lighttpd-on-planet-argon
#
# To setup lighty, first edit this file for your primary PLANET ARGON account.
# Then run:
#   rake remote:exec ACTION=setup_lighty
#
# This will create all the necessary config files for running lighttpd.
#
# DON'T USE DAEDALUS!
#
# From then, you can deploy your application to PLANET ARGON with Capistrano's standard
#   rake deploy
#
# Or rollback with
#   rake rollback
# 

#############
# Set these variables to work with your PLANET ARGON Account

set :application, 'blog' # The one you are deploying.
set :user, "vu2000" # Your PA username
set :pa_primary_domain, 'railsfanboy.com'
set :pa_server_ip, "64.34.217.149" # The IP address of the machine which hosts your shared space
set :lighty_port, 808080808080 # The port given to you by PLANET ARGON for running lighttpd
set :repository, "http://dev.railsfanboy.com/svn/#{application}/trunk" # Your SVN URL

set :deploy_to, "/var/www/virtual/#{pa_primary_domain}/#{application}/rails" 

#
#############

# By default, the source control module (scm) is set to "subversion". You can
# set it to any supported scm:
set :scm, :subversion

set :pa_home, "/home/#{user}" 

# This is a HIGHLY SIMPLIFIED deployment recipe.  If you are looking for something
# more sophisticated (e.g. using different machines for different roles), stop 
# reading this and go to: 
#    http://manuals.rubyonrails.com/read/book/17

role :app, pa_primary_domain
role :web, pa_primary_domain
role :db,  pa_primary_domain

# =============================================================================
# Additional tasks for setting up lighttpd on PLANET ARGON
# =============================================================================

desc "Make directories on PLANET ARGON and setup lighttpd config" 
task :setup_lighty do  
  write_lighty_config_file
  write_lighttpdctrl_script

  # Do initial setup and then get the new code for this site
  setup
  deploy

  # Start the lighty server
  set :lighttpdctrl_restart, "lighttpd/lighttpdctrl restart" 
  set :lighttpdctrl_start, "/usr/local/sbin/lighttpd -f lighttpd/lighttpd.conf" 
  run lighttpdctrl_start

  #run "/usr/local/sbin/lighttpd -f /home/#{user}/lighttpd/lighttpd.conf" 

  # Schedule cron job to start when server restarts
  #run "crontab -l > /home/#{user}/lighttpd/crontab.conf" # Backup existing settings
  run "echo '@reboot #{lighttpdctrl_start}' >> /home/#{user}/lighttpd/crontab.conf" 
  run "crontab /home/#{user}/lighttpd/crontab.conf" # Apply

end

# No sudo access on PA :)
desc "Restart the FCGI processes on the app server." 
task :restart, :roles => :app do
  run "ruby #{current_path}/script/process/reaper" 
end

desc "Write customized lighttpd.conf file to server." 
task :write_lighty_config_file do

   # Make the lighttpd directory
  run "if [ ! -d lighttpd ]; then mkdir lighttpd; fi" 

  # Make the config file
  buffer = render(:template => <<LIGHTY_CONFIG)
server.port     = <%= lighty_port %>
server.bind     = "<%= pa_server_ip %>" 
server.pid-file = "<%= pa_home %>/lighttpd/lighttpd.pid" 

server.dir-listing          = "disable" 
server.modules              = ( "mod_rewrite", "mod_redirect", "mod_access", "mod_fastcgi", "mod_accesslog", "mod_compress", "mod_scgi" )
server.indexfiles           = ( "dispatch.fcgi" )
server.document-root        = "/var/www/virtual/<%= pa_primary_domain %>/<%= application %>/rails/current/public" 
server.error-handler-404    = "/var/www/virtual/<%= pa_primary_domain %>/<%= application %>/rails/current/public/dispatch.fcgi" 
server.errorlog             = "/var/www/virtual/<%= pa_primary_domain %>/<%= application %>/rails/log/error.log" 

url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )

# mimetype mapping
mimetype.assign             = (
  ".pdf"          =>      "application/pdf",
  ".sig"          =>      "application/pgp-signature",
  ".spl"          =>      "application/futuresplash",
  ".class"        =>      "application/octet-stream",
  ".ps"           =>      "application/postscript",
  ".torrent"      =>      "application/x-bittorrent",
  ".dvi"          =>      "application/x-dvi",
  ".gz"           =>      "application/x-gzip",
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
  ".swf"          =>      "application/x-shockwave-flash",
  ".tar.gz"       =>      "application/x-tgz",
  ".tgz"          =>      "application/x-tgz",
  ".tar"          =>      "application/x-tar",
  ".zip"          =>      "application/zip",
  ".mp3"          =>      "audio/mpeg",
  ".m3u"          =>      "audio/x-mpegurl",
  ".wma"          =>      "audio/x-ms-wma",
  ".wax"          =>      "audio/x-ms-wax",
  ".ogg"          =>      "audio/x-wav",
  ".wav"          =>      "audio/x-wav",
  ".gif"          =>      "image/gif",
  ".jpg"          =>      "image/jpeg",
  ".jpeg"         =>      "image/jpeg",
  ".png"          =>      "image/png",
  ".xbm"          =>      "image/x-xbitmap",
  ".xpm"          =>      "image/x-xpixmap",
  ".xwd"          =>      "image/x-xwindowdump",
  ".css"          =>      "text/css",
  ".html"         =>      "text/html",
  ".htm"          =>      "text/html",
  ".js"           =>      "text/javascript",
  ".asc"          =>      "text/plain",
  ".c"            =>      "text/plain",
  ".conf"         =>      "text/plain",
  ".text"         =>      "text/plain",
  ".txt"          =>      "text/plain",
  ".dtd"          =>      "text/xml",
  ".xml"          =>      "text/xml",
  ".mpeg"         =>      "video/mpeg",
  ".mpg"          =>      "video/mpeg",
  ".mov"          =>      "video/quicktime",
  ".qt"           =>      "video/quicktime",
  ".avi"          =>      "video/x-msvideo",
  ".asf"          =>      "video/x-ms-asf",
  ".asx"          =>      "video/x-ms-asf",
  ".wmv"          =>      "video/x-ms-wmv",
  ".bz2"          =>      "application/x-bzip",
  ".tbz"          =>      "application/x-bzip-compressed-tar",
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar" 
 )

## deny access the file-extensions
#
# ~  is for backupfiles from vi, emacs, joe, ...
# .inc is often used for code includes which should in general not be part
#      of the document-root
url.access-deny             = ( "~", ".inc" )

###############
# To add other domains or subdomains, I strongly suggest you see:
#   http://www.brandalism.com/articles/2006/02/09/setting-up-lighttpd-on-planet-argon
# for excellent, well-illustrated examples  
#

### config for: blog.railsfanboy.com
$HTTP["host"] =~ "blog\.railsfanboy.(com)" {
  server.document-root = "/var/www/virtual/<%= pa_primary_domain %>/<%= application %>/rails/current/public/" 
  server.errorlog          = "/var/www/virtual/<%= pa_primary_domain %>/<%= application %>/rails/log/lighttpd_error.log" 
  accesslog.filename      = "/var/www/virtual/<%= pa_primary_domain %>/<%= application %>/rails/log/lighttpd_access.log" 
  server.error-handler-404 = "/dispatch.fcgi" 

  fastcgi.server  = (
    ".fcgi" => (
      "rails" => (
        "socket" => "/var/www/virtual/<%= pa_primary_domain %>/<%= application %>/var/lighttpd-fcgi.socket",
        "bin-path" => "/var/www/virtual/<%= pa_primary_domain %>/<%= application %>/rails/current/public/dispatch.fcgi",
        "bin-environment" => ("RAILS_ENV" => "production"),
        "min-procs" => 2,
        "max-procs" => 2,
        "idle-timeout" => 120
      )
    )
  )
}

LIGHTY_CONFIG

  # Write the config file to disk
  put buffer, "lighttpd/lighttpd.conf" #, :mode => xxxx  
end

desc "Sets up lighttpd control script for starting, stopping, and restarting lighty" 
task :write_lighttpdctrl_script do  
  buffer = render(:template => <<LIGHTTPDCTRL)
#!/bin/sh                                                                                            

#
# lighty update script to kill stray processes.
#
# AUTHOR: Jarkko Laine http://jlaine.net/blog
#

LIGHTTPD_CONF=<%= pa_home %>/lighttpd/lighttpd.conf
PIDFILE=<%= pa_home %>/lighttpd/lighttpd.pid

case "$1" in
    start)
      # Starts the lighttpd deamon                                                                         
      echo "Starting Lighttpd" 
      lighttpd -f $LIGHTTPD_CONF

  ;;
    stop)
      # stops the daemon by cat'ing the pidfile                                                            
        echo "Stopping Lighttpd" 
      if test -f $PIDFILE ; then kill `cat $PIDFILE` ; fi
      # kill the fastcgi processes, too                                                                    
          ps aux | grep <%= user %> | grep lighttpd | egrep -v 'grep' | egrep -v 'lighttpdctrl' | awk '{print $2}' | xargs --no-run-if-empty kill -9
  ;;
    restart)
          ## Stop the service regardless of whether it was                                                     
          ## running or not, start it again.                                                                   
          echo "Restarting Lighttpd" 
      $0 stop
      $0 start
  ;;
    reload)
      # reloads the config file by sending HUP                                                             
          echo "Reloading config" 
      kill -HUP `cat $PIDFILE`
  ;;
    *)
      echo "Usage: lighttpdctrl (start|stop|restart|reload)" 
      exit 1
  ;;
esac
LIGHTTPDCTRL

  put buffer, "lighttpd/lighttpdctrl", :mode => 0755
end

desc "Deletes all directories and contents made by setup_lighty" 
task :teardown_lighty do

  #run "rm -rf lighttpd" 

  ## TODO Clear cron jobs that were setup

  ## Kill processes
  #run "killall -9 lighttpd" 
  #run "killall -9 ruby" 

end

#desc "Start the spinner daemon, using local lighttpdctrl" 
#task :spinner, :roles => :app do
#  run "lighttpd/lighttpdctrl restart" 
#end

desc "Restart lighty" 
task :restart, :roles => :app do
  run "lighttpd/lighttpdctrl restart" 
end

# =====================================
# Sample tasks from original deploy.rb
# Some may not work with PLANET ARGON (spinner, etc.)
# =====================================

# Define tasks that run on all (or only some) of the machines. You can specify
# a role (or set of roles) that each task should be executed on. You can also
# narrow the set of servers to a subset of a role by specifying options, which
# must match the options given for the servers to select (like :primary => true)

desc <<DESC
An imaginary backup task. (Execute the 'show_tasks' task to display all
available tasks.)
DESC

task :backup, :roles => :db, :only => { :primary => true } do
  # the on_rollback handler is only executed if this task is executed within
  # a transaction (see below), AND it or a subsequent task fails.
  on_rollback { delete "/tmp/dump.sql" }

  run "mysqldump -u theuser -p thedatabase > /tmp/dump.sql" do |ch, stream, out|
    ch.send_data "thepassword\n" if out =~ /^Enter password:/
  end
end

# Tasks may take advantage of several different helper methods to interact
# with the remote server(s). These are:
#
# * run(command, options={}, &block): execute the given command on all servers
#   associated with the current task, in parallel. The block, if given, should
#   accept three parameters: the communication channel, a symbol identifying the
#   type of stream (:err or :out), and the data. The block is invoked for all
#   output from the command, allowing you to inspect output and act
#   accordingly.
# * sudo(command, options={}, &block): same as run, but it executes the command
#   via sudo.
# * delete(path, options={}): deletes the given file or directory from all
#   associated servers. If :recursive => true is given in the options, the
#   delete uses "rm -rf" instead of "rm -f".
# * put(buffer, path, options={}): creates or overwrites a file at "path" on
#   all associated servers, populating it with the contents of "buffer". You
#   can specify :mode as an integer value, which will be used to set the mode
#   on the file.
# * render(template, options={}) or render(options={}): renders the given
#   template and returns a string. Alternatively, if the :template key is given,
#   it will be treated as the contents of the template to render. Any other keys
#   are treated as local variables, which are made available to the (ERb)
#   template.

desc "Demonstrates the various helper methods available to recipes." 
task :helper_demo do
  # "setup" is a standard task which sets up the directory structure on the
  # remote servers. It is a good idea to run the "setup" task at least once
  # at the beginning of your app's lifetime (it is non-destructive).
  setup

  buffer = render("maintenance.rhtml", :deadline => ENV['UNTIL'])
  put buffer, "#{shared_path}/system/maintenance.html", :mode => 0644
  sudo "killall -USR1 dispatch.fcgi" 
  run "#{release_path}/script/spin" 
  delete "#{shared_path}/system/maintenance.html" 
end

# You can use "transaction" to indicate that if any of the tasks within it fail,
# all should be rolled back (for each task that specifies an on_rollback
# handler).

desc "A task demonstrating the use of transactions." 
task :long_deploy do
  transaction do
    update_code
    disable_web
    symlink
    migrate
  end

  restart
  enable_web
end

desc "Used only for deploying when the spinner isn't running" 
task :cold_deploy do
  transction do
    update_code
    symlink
  end

  spinner
end


Powered by JunebugWiki v0.0.28 Last edited by djohnson on July 17, 2007 06:08 PM (diff)
Version 3 (current) «olderversions