Capistrano-Bereitstellung
Capistrano is a tool used for automation of the application deployment process. Capistrano is mainly used for deploying rails apps. Changes made to the rails app can be easily transferred to the server using cap deploying. Capistrano can be configured with any version control system like SVN / GIT for deploying an app. Also, we can define application server type (mongrel, mod_rails / Fcgi) on which the app has be deployed in Capistrano. Here I’m going to discuss cap deploying an app using SVN with Mongrel cluster as the application server.
Installation:Capistrano kann wie unten gezeigt als Rubinstein installiert werden: $ Gem installiert Capistrano
Konfigurieren einer Rails-App für die Verwendung von Capistrano:
Dies kann durch Ausführen des folgenden Befehls in der Rails-App erfolgen:
$ capify .
Der obige Befehl erstellt eine Capfile Und config/deploy.rb Datei.
user@hostname [~/railsapp]$ capify . [add] writing ‘./Capfile’ [add] writing ‘./config/deploy.rb’
user@hostname [~/railsapp]$ cat Capfile
load ‘deploy’ if respond_to?(:namespace) # cap2 differentiator Dir[‘vendor/plugins/*/recipes/*.rb’].each { |plugin| load(plugin) }
load ‘config/deploy’ # remove this line to skip loading any of the default tasks
user@hostname [~/railsapp]$ cat config/deploy.rb
set :application, “set your application name here” set :repository, “set your repository location here”
# If you aren’t deploying to /u/apps/#{application} on the target # servers (which is the default), you can specify the actual location # via the :deploy_to variable: # set :deploy_to, “/var/www/#{application}”
# If you aren’t using Subversion to manage your source code, specify # your SCM below: # set :scm, :subversion
role :app, “your app-server here” role :web, “your web-server here” role :db, “your db-server here”, :primary => true
PS: Alternativ können wir auch direkt erstellen Capfile Und Deploy.rb file under the rails app and load necessary data to it using ‘vi’ editor.
Capfile und Deploy.rb:
The Capfile contains all the tasks which has to be performed while cap deploying. All the functions (tasks) inside the Capfile can be defined using the keyword ‘task’. Group of functions is termed as namespace.
Beispiel-Capfile:
load ‘deploy’ if respond_to?(:namespace) # cap2 differentiator
load ‘config/deploy’
Namespace :deploy do
Aufgabe :start, :roles => :app do
run “cd #{current_path} && mongrel_rails cluster::configure -e production -p #{mongrel_port} -N #{mongrel_nodes} -c #{current_path} –user #{user} –group #{user}”
run “cd #{current_path} && mongrel_rails cluster::start”
run “rm -rf /home/#{user}/public_html; ln -s #{current_path}/public /home/#{user}/public_html”
run “mkdir -p #{deploy_to}/shared/config”
run “mv #{current_path}/config/mongrel_cluster.yml #{deploy_to}/shared/config/mongrel_cluster.yml”
run “ln -s #{deploy_to}/shared/config/mongrel_cluster.yml #{current_path}/config/mongrel_cluster.yml”
Ende
Aufgabe :restart, :roles => :app do
run “ln -s #{deploy_to}/shared/config/mongrel_cluster.yml #{current_path}/config/mongrel_cluster.yml”
run “cd #{current_path} && mongrel_rails cluster::restart”
run “cd #{current_path} && chmod 755 #{chmod755}”
Ende
Ende
The config/deploy.rb file contains all the variables which are used while cap deploying. Say, in the above Capfile we’re accessing different varibles like – mongrel_port, user, chmod755 etc the same should be first defined in the config/deploy.rb file then only Capistrano will execute the task properly.
Beispiel config/deploy.rb:
set :application, “railsapp”
set :domain, “hostname”
set :user, “user”
set :repository, “svn+ssh://#{user}@#{domain}/home/#{user}/svn/#{application}/trunk”
set :use_sudo, false
set:group_writable, false
set :deploy_to, “/home/#{user}/apps/#{application}”
set :deploy_via, :checkout
set :chmod755, “app config db lib public vendor script script/* public/disp*”
set :mongrel_port, “4000”
set :mongrel_nodes, “2”
default_run_options[:pty] = true
Rolle: App, Domäne
Rolle:Web, Domäne
Rolle :db, Domäne, :primary => true
Hier müssen wir den entsprechenden Anwendungsnamen, Domänennamen und SSH-Benutzer platzieren. Die Rails-App sollte in das SVN-Repository importiert werden, bevor mit der Bereitstellung fortgefahren wird. Das SVN-Repository kann wie unten gezeigt eingerichtet werden.
Benutzer@Hostname [~]$ mkdir svn
user@hostname [~]$ svnadmin create ~/svn/railsapp
PS: Stellen Sie sicher, dass Sie auch im SVN-Repository denselben Namen der Rails-App angeben.
Sobald dies erledigt ist, können wir die Rails-App in das SVN-Repository importieren:
Benutzer@Hostname [~]$ mkdir -p ~/temp/railsapp/tags ~/temp/railsapp/branches
Benutzer@Hostname [~]$ cp -pR ~/railsapp ~/temp/railsapp/trunk
Stellen Sie hier sicher, dass Sie beim Erstellen des Trunk-Verzeichnisses den vollständigen Pfad zu „railsapp“ (Ihrem Rails-App-Verzeichnis) angeben. Außerdem müssen in der Datei „trunk/config/deploy.rb“ der richtige Benutzer, die richtige Anwendung und die richtige Domäne angegeben werden.
Die Rails-App befindet sich im Trunk-Verzeichnis und kann jetzt in das SVN-Repository importiert werden.
user@hostname [~]$ svn import temp/railsapp file:///home/user/svn/railsapp -m “message”
Die in das SVN-Repository importierte Rails-App (z. B. auf dem Server) kann wie unten gezeigt auf dem lokalen Computer ausgecheckt werden:
user@localmachine [~]$ svn co svn+ssh://user@hostname/home/user/svn/railsapp/trunk Ordnername
Passwort: SSH-Passwort des Benutzers
Jetzt können wir die bearbeitete Datei capfile und die Datei „deploy.rb“ im ausgecheckten Ordner finden. Führen Sie anschließend im RAILS_ROOT der lokalen Versionsanwendung den folgenden Befehl aus.
$-Cap-Deploy:Setup
$-Cap-Bereitstellung: kalt
The command ‘cap deploy:setup’ will setup the initial directory structure required for the cap deployed app.
Sagen,
/home/user/apps
/home/user/apps/appname
/home/user/apps/appname/releases
/home/user/apps/appname/shared
The command ‘cap deploy:cold’ will check out the rails app from the svn repository and place it under a directory inside – /home/user/apps/appname/releases. Later a sym link will be created from /home/user/apps/appname/releases/ /home/user/apps/appname/current. Also the document root will be pointed to the public directory of the current release as mentioned in the Capfile.
Das Release-Verzeichnis enthält die auf dem Server bereitgestellten Apps und der App-Ordner wird nach dem Datum und der Uhrzeit benannt, zu der die Rails-App bereitgestellt wurde.
Sagen,
Benutzer@Hostname [~/apps/railsapp/releases]$ ls -al
insgesamt 24
drwxrwxr-x 6 Benutzer Benutzer 4096 28. April 2009 .
drwxrwxr-x 4 Benutzer Benutzer 4096 9. Juli 2009 ..
drwxr-xr-x 16 Benutzer Benutzer 4096 15. Okt. 2008 20081015183834
drwxr-xr-x 21 Benutzer Benutzer 4096 3. Februar 2009 20081015183941
drwxr-xr-x 8 Benutzer Benutzer 4096 28. April 2009 20090428215604
drwxr-xr-x 8 Benutzer Benutzer 4096 28. April 2009 20090428221701
In der Rails-App übliche Verzeichnisse (z. B. Bilder, Konfigurationsdateien, Protokolldateien) werden im freigegebenen Verzeichnis abgelegt und mit der aktuellen Version verknüpft.
Verweise: http://www.capify.org/index.php/Capistrano http://www.hostingrails.com/Capistrano-2-x-Deploying-Your-Rails-Application-on-HostingRails-com
Mitwirkender: Vinayan
Nehmen Sie Kontakt mit uns auf.