Using the Capistrano Deployment summary

Source: Internet
Author: User
Tags drupal ssh access

Using the Capistrano Deployment summary June 27, 2014 admin Comment Read review introduction Capistrano is a Ruby program that provides an advanced toolset to deploy your Web application to the server. Capistrano allows you to copy code from the source control repository (SVN or Git) to the server via SSH and perform pre/post-deployment functions such as restarting the Web server, operating the cache, renaming the file, migrating the database, and so on. Multiple machines can also be deployed at once with Capistrano. Install Capistrano in order to install Capistrano, your PC needs to have Ruby and RubyGems installed. # Gem List Capistrano-a-R http://ruby.taobao.org/*** REMOTE GEMS ***capistrano (3.2.1, 3.2.0, 3.1.0, 3.0.1, 3.0.0, 2.15. 5, 2.15.4, 2.15.3, 2.15.2, 2.15.1, 2.15.0, 2.14.2, 2.14.1, 2.13.5, 2.12.0, 2.11.2, 2.9.0, 2.8.0, 2.7.0, 2.6.0, 2.5.21, 2.5 .20, 2.5.19, 2.5.18, 2.5.17, 2.5.16, 2.5.15, 2.5.14, 2.5.13, 2.5.12, 2.5.11, 2.5.10, 2.5.9, 2.5.8, 2.5.7, 2.5.6, 2.5.5, 2. 5.4, 2.5.3, 2.5.2, 2.5.1, 2.5.0, 2.4.3, 2.4.2, 2.4.1, 2.4.0, 2.3.0, 2.2.0, 2.1.0, 2.0.0, 1.4.2, 1.4.1, 1.4.0, 1.3.1, 1.3.0  , 1.2.0, 1.1.0) capistrano-af83 (0.4.7, 0.4.6, 0.4.5, 0.4.4, 0.4.3, 0.4.2, 0.4.1, 0.4.0, 0.3.8, 0.3.2, 0.3.1, 0.3.0, 0.2.4, 0.2.3, 0.2.2, 0.2.1, 0.2.0, 0.1.9, 0.1.8, 0.1.7, 0.1.6, 0.1.5, 0.1.4, 0.1.3, 0.1.2, 0.1.1, 0.1.0) Capistrano-akamai (0.0.1 ) capistRano-alice (0.0.7, 0.0.6, 0.0.5, 0.0.4, 0.0.3, 0.0.2, 0.0.1) Capistrano-asgroup (0.0.1) The above command lists all of the current Capistrano versions and their associated plugins (there are many plugins, I'm not glued here), you can see that the latest version of Capistrano is 3.2.1. However, due to the Webistrano environment, 2.6.0 is used. This article is also installed and summarized in the 2.6.0 version. Note: There has been a big change between the Capistrano 3.x.x version and the Capistrano 2.x.x. This is somewhat similar to the difference between Python3 and Python2. Installation method: $ gem Install capistrano-v 2.6.0//If you need to install the Capistrano extension, add the following action $ gem install Capistrano-ext If you encounter a problem or want to learn more details, You can refer to the official Capistrano Getting Started guide or find the various components that make Capistrano work. Note: When installing, the dependent package is automatically installed, please confirm that the version number of NET-SSH is 2.7.0, otherwise you will encounter an SSH authentication error similar to the following when performing any deploy operation [Deploy:update_code] exception while rolling Back:capistrano::connectionerror, Connection failed for: ... (Net::ssh::authenticationfailed:authentication failed for user can refer to the issues section of the NET-SSH Project on GitHub.) Create the project in the terminal to navigate to your app's root directory and execute the following command: $ sudo capify. [Add] writing './capfile ' [Add] making directory './config ' [Add] writing './config/deploy.rb ' [done] capified!$ tree.├── Capfile└──config└──deploy.rb in the Capistrano 3.x.x version, this method cannot be used to create the project, you should use the following action: $ capify.--------------------------------------------------------------------------------Capistrano 3.x is incompatible with Capistrano 2.x. This command have become ' cap install ' in Capistrano 3.xFor more information see http://www.capistranorb.com/-------------- ------------------------------------------------------------------$ sudo cap installmkdir-p config/deploycreate Config/deploy.rbcreate config/deploy/staging.rbcreate config/deploy/production.rbmkdir-p lib/capistrano/  taskscapified$ tree.├──capfile├──config│├──deploy││├──production.rb││└──staging.rb│└──deploy.rb└── Lib└──capistrano└──tasks is not hard to see, the directory structure of the two have also undergone some changes. The individual tried the next 3.x.x version and found that its features and details were much more optimized than 2.x.x. It is recommended to use the new version if you do not use the old Webistrano as the Web front end. Since Webistrano has not been updated for more than three years (the author has also announced that it is no longer updated), it is not sure of its compatibility with Capistrano 3.x.x. Create a special Capfile file in the project, Capfile will help Capistrano properly load your deploy.rb template, while the Deploy.rb file defines the module, namespace, task, etc., during the deployment process, You can do this by simply pressing the host group to execute the corresponding task. Write Capistrano template 1, configure the Deploy.rb file in the following deploy.rb file for some detailed analysis require ' Bundler/capistrano ' #添加之后部署时会调用bUndle Install, if you do not need to comment out require "capistrano/ext/multistage" #多stage部署所需set: Stages,%w (development production) set:D Efault_stage, "Development" set:application, "Crm_app_end" #应用名称set: Repository, "Https://test.361way.com/svn/trunk"              Set:keep_releases, 5 #只保留5个备份set:d eploy_to, "/var/www/#{application}" #部署到远程机器的路径set: User, "User1" #登录部署机器的用户名set:p Assword, "user1" #登录部署机器的密码, if you do not have to enter a password when deploying default_run_options[:p ty] = True #pty: Pseudo-Logon Device #d Efault_run_options[:shell] = False #Disable sh Wrappingset:use_sudo, True #执行的命令中含有sudo, as Set to False, the user all operations have permissions Set:runner, "User2" #以user2用户启动服务set: Svn_username, "xxxx" SET:SCM,: Subvers Ion #注意subversion前有冒号, not less # Or: ' Accurev ', ' bzr ', ' CVS ', ' Darcs ', ' git ', ' mercurial ', ' perforce ', ' s    Ubversion ' or ' None ' #set:d Eploy_via: Copy #如果SCM设为空 can also be deployed #set:d omain by direct Copy local repo, "crm.abc.com" #custom Definerole:web,"192.168.0.13", "192.168.0.117" # Your HTTP server, Apache/etcrole:app, "192.168.0.13", "192.168  .0.117 "# This could be the same as your ' Web ' serverrole:d B," 192.168.0.13 ",:p rimary = True  # This is where Rails migrations would run#role:d B, "Your slave db-server here" #namespace:d eploy do desc "Remove and Destory this App "task:d estory,: roles =: app do run" CD #{deploy_to}/. /&& #{try_sudo} MV #{application}/tmp/#{application}_#{time.now.strftime ('%y%d%m%h%m%s ')} "#try_sudo with sudo right Limit execution command end after "Deploy:update", "Deploy:shared:setup" #after, before means to perform other tasks after or before a specific operation namespace: Shared do desc "Setup shared folder Symblink" Task:setup do Run "CD #{deploy_to}/current; RM-RF GKFX;        Ln-s #{shared_path}. "         End end after the "Deploy:setup", "Deploy:setup_chown" desc "Change the owner from the root to the User1" Task:setup_chown do Run "CD #{deploy_to}/. /&& #{try_sudo} chown-r #{user}:#{user} #{application} "End Task:start do run" CD #{deploy_to}/curr       Ent &&./crmd.sh start "#try_sudo" CD #{deploy_to}/current &&./restart.sh "End Task:stop do Run CD #{deploy_to}/current &&/crmd.sh Stop "end Task:restart do run" CD #{deploy_to}/curren T &&/crmd.sh Restart "EndEnd above about the environment section here specifically, if there are multiple stages to deploy, under Config Create the Deploy folder, under which there are stages files, filenames and set:s Tages,%w (development production) corresponds, such as Development.rb production.rb, set the corresponding variable in each file, Set:stages, ["Staging", "production "]set:d efault_stage," staging "production.rb settings are similar to the following: set:d eploy_to,"/var/www/#{application} "# The hostnames to deploy T O.role:web, "devel.example.com" # Specify one of the Web servers to use for database backups or updates.# this server Shou LD also be running drupal.role:d B, "devel.example.com",:p rimary = true# The username on the target system, if differ Ent from your LoCal username# Ssh_options[:user] = ' Alice ' # The path to Drushset:d Rush, "CD #{current_path}; /usr/bin/php/data/lib/php/drush/drush.php "Same Development.rb:server" my_fancy_server.com ": App,: web,:d B,:p rimary = > TrueSet:d eploy_to, "/var/www/fancy_shoes_development" in practical applications, we can also configure the use of different directories in different environments using the same host, or can be configured to contain different hosts in different environments. In a multi-environment, the command that executes the deployment needs to specify the environment: CAP Production Deploy: ... Using Cap Publishing in a 2.x.x multi-environment configuration can also be a non-context-specific environment, which requires that one of the environments be specified in the configuration as the default. In 3.x.x, in the demo project created by default, the Deploy directory has been created, and both the production and staging environment files have been generated, and when execution is enforced by default you need to specify the environment for the publication, so the default structure of 3.x.x is clearer. 2, verify that the configuration of the execution of deploy all ready, first try our recipe, in order to let Capistrano on the server to create the initial directory structure. Execute the following command from your app root: $ cap Deploy:setup When you execute the command, Capistrano will SSH to your server, enter the directory you specified in the DEPLOY_TO variable, and create a special Capistrano directory structure. If you encounter permissions or SSH access errors, you will get an error message. Take a closer look at the output of the Capistrano when the command executes. The final step before we use Capistrano to do the actual deployment is to make sure that the Setup commands are all set correctly on the server. Use the following command for simple verification: $ cap Deploy:check The command checks your native environment and server, and locates the problem. If you see an error message, repair and then run the command. Once you execute the CAP Deploy:check without errors, you can continue processing. We will also use the following statement during deployment (we need to define the task in DEPLOY.RB): $cap deploy:setup #建立部署路径 $cap deploy:upDate #部署 $cap deploy:start #启动服务 $cap deploy:stop #停止服务 $cap deploy:restart #重启服务 $cap deploy:rollback #回滚提示与技巧使用远端缓存改进性能 The way Capistrano works will create a new warehouse clone and export at each deployment. That will be slow, and you can speed up by adding some extension options to the deploy.rb recipe. Add the following to the DEPLOY.RB file that describes the location of the SCM settings: Set:d Eploy_via,: Remote_cache This command causes Capistrano to clone/export the warehouse only once on the server, and then use Svnup or git for each deployment Pul L instead. If you deploy regularly, you will find that speed up is obvious. Adding a custom Deployment hook (Hook) Capistrano is obviously more advanced than copying files over SSH. You can configure events or commands to execute after the file copy is complete, such as restarting the Web server, executing a custom script, and so on. Capistrano called these "tasks." For example, add the following code to the Deploy.rb file: namespace:d eploy do task:restart,: roles =: Web do run "Touch #{Current_path}/tmp/resta Rt.txt "End task:restart_daemons,: roles =: app do sudo" monit restart All-g daemons "The task in Endendcapistrano is very strong Large, we only touch the epidermis in this guide. You can create tasks before, after deployment, or by operating the server separately. This can be any type of maintenance: Restart the process, clean up files, send mail notifications, perform database migrations, run scripts, and so on. Our example includes two custom tasks. The "Restart" task is built into Capistrano and will be executed automatically when the deployment is complete. We use modern Rails application technology driven by passenger touch Tmp/restart.txt, your WEB server may require different commands. Our second example task is "Restart_daemons", and Capistrano does not perform this custom task by default. In order for it to run, we need to add a hook:after "deploy", "deploy:reStart_daemons "This command tells Capistrano to perform the task after our deployment operation is complete. The other available hooks are before, which perform the task before the file is copied. For before and after hooks, you can read the official Capistrano documentation: before Tasksafter Tasks associates a git branch with the environment because we have two server environments (staging and production), you may want to bind git branches to these environments. In this way, you can automatically deploy the staging branch to the staging environment, the master branch to the production environment. Simply add the following to Production.rb:set:branch, ' production ' and add the following to Staging.rbset:branch, ' staging ' Now every time you execute the CAP deploy, Capistra No will deploy code from your staging branch (because staging is our default environment). If you run cap production Deploy,capistrano The code will be deployed from your Master branch. Reference Document: Largetalk's CSDN blog Capistrano deploy Drupal app dl528888 51cto Blog use Capistrano deploy Web App GitHub wiki page

  

Using the Capistrano Deployment summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.