{"id":2908,"date":"2018-11-14T04:42:49","date_gmt":"2018-11-14T10:42:49","guid":{"rendered":"https:\/\/www.supportpro.com\/blog\/?p=2908"},"modified":"2019-10-30T02:40:25","modified_gmt":"2019-10-30T08:40:25","slug":"capistrano","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/capistrano\/","title":{"rendered":"Capistrano"},"content":{"rendered":"<p style=\"text-align: center;\"><a class=\"lightbox\" href=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2018\/11\/capis.png\" data-rel=\"penci-gallery-image-content\" ><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-3484 size-full\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2018\/11\/capis.png\" alt=\"Capistrano Logo\" width=\"400\" height=\"400\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2018\/11\/capis.png 400w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2018\/11\/capis-150x150.png 150w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2018\/11\/capis-300x300.png 300w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2018\/11\/capis-80x80.png 80w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2018\/11\/capis-144x144.png 144w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Capistrano is a remote server automation open-source tool written in Ruby that gives a set of advanced tools to deploy web applications to the servers.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To perform the deploy functions Capistrano does not require any big set of additional applications; it just requires an ssh connection with a new user who has the permission to copy the code from the source control repository to the server and Ruby Gems, the package manager for the Ruby programming language.<\/span><!--more--><\/p>\n<p><span style=\"font-weight: 400;\">Although Capistrano is written in Ruby, it can easily be used to deploy projects of any language.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Why Capistrano?<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">The main advantage of Capistrano is that it\u2019s also possible to deploy to multiple machines at once and can be deployed by writing just one console command cap and if something goes wrong the application can be rollback to the last release by using another one cap.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Capistrano decides on how to structure the scripts, where deployed files should be placed on the server, or how to perform common tasks.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Capistrano instance can be easily extended using the RubyGems package manager and can run each deployment task concurrently across those servers i.e the parallel execution of the task,\u00a0 that too without any downtime.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Main Features of Capistrano<\/strong><\/h2>\n<ul>\n<li><span style=\"font-weight: 400;\">SSH connection\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Capistrano does not require a big set of additional applications on the server. All it needs is an ssh connection, i.e. it depends on connecting to the server(s) with SSH using key-based (i.e. password-less) authentication.\u00a0<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Multiple stages<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Multiple stages are all about parallel execution of the task in a fleet of servers, as the Capistrano has the ability to run each task concurrently by giving specified roles to the participating servers.\u00a0<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">More roles for servers\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Capistrano assigns each server with one or more roles to control the tasks that are executed, as the application may need different types of servers for database, application, web etc.<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Extensible\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">As Capistrano is a Ruby program, the RubyGems package manager helps to extend the Capistrano instance. The plugins in the Capistrano for different frameworks provide a set of tasks. This plugin is intended to be used before any deployment task.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Also, some of the Ruby projects comes with Capistrano tasks built-in.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The official Capistrano Plugins are listed below.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">======<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; Bundler<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; Composer<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; Drupal<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; rbenv<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; RVM<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; Ruby on Rails<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; Symfony<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; Laravel<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; npm<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; Phusion Passenger<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&gt;&gt; chruby<\/span><\/p>\n<p><span style=\"font-weight: 400;\">======<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><strong>How Capistrano?<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Capistrano can be installed as a standalone Gem or bundled into the application and as Capistrano is a Ruby package, we will need to install Ruby on our system and can be set by running &#8216;cap install&#8217; in the project\u2019s root directory.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When the cap is run, Capistrano connects to the servers via SSH and executes the steps necessary to deploy your project.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Below is the structure of the files and directories for a Capistrano-enabled project with two\u00a0stages, namely staging\u00a0and\u00a0production.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The stages will have a set of servers that can have many roles.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">===<\/span><\/p>\n<pre><code>\u251c\u2500\u2500 Capfile\r\n\u251c\u2500\u2500 config\r\n\u2502   \u251c\u2500\u2500 deploy\r\n\u2502   \u2502   \u251c\u2500\u2500 production.rb\r\n\u2502   \u2502   \u2514\u2500\u2500 staging.rb\r\n\u2502   \u2514\u2500\u2500 deploy.rb\r\n\u2514\u2500\u2500 lib\r\n    \u2514\u2500\u2500 capistrano\r\n            \u2514\u2500\u2500 tasks<\/code><\/pre>\n<p><span style=\"font-weight: 400;\">===<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">The stage-specific configuration files are for introducing the Capistrano about the task on the servers.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The files that Capistrano creates are simply templates and we should make sure to edit the deploy.rb and stage files so that they contain values appropriate for the projects and the target servers.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">There are two modes for the execution of commands in Capistrano, <strong>interactive<\/strong> and <strong>non-interactive<\/strong> modes.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The difference is that the interactive mode is used to get immediate feedback when we don\u2019t know which command needed to be executed and the non-interactive mode is used when we know the command to be used and just want to see the result.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The latest released Capistrano v3 provides a default deploy flow and a rollback flow. The tasks involved are listed below.<\/span><\/p>\n<h3><span style=\"text-decoration: underline;\"><strong>Deploy flow<\/strong><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">deploy: starting &#8211;\u00a0 starts deployment<\/span><\/p>\n<p><span style=\"font-weight: 400;\">deploy: started\u00a0 &#8211;\u00a0 start hook for the custom tasks<\/span><\/p>\n<p><span style=\"font-weight: 400;\">deploy: updating\u00a0 &#8211;\u00a0 updates the servers to a new release<\/span><\/p>\n<p><span style=\"font-weight: 400;\">deploy: updated\u00a0\u00a0 &#8211;\u00a0 update hook for the custom tasks<\/span><\/p>\n<p><span style=\"font-weight: 400;\">deploy: publishing &#8211;\u00a0 publishes the new release<\/span><\/p>\n<p><span style=\"font-weight: 400;\">deploy: published\u00a0 &#8211;\u00a0 publish hook for the custom tasks<\/span><\/p>\n<p><span style=\"font-weight: 400;\">deploy: finishing\u00a0 &#8211;\u00a0 finish the deployment<\/span><\/p>\n<p><span style=\"font-weight: 400;\">deploy: finished &#8211;\u00a0 finish hook for the custom tasks<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The hooks are used for adding their own tasks.<\/span><\/p>\n<h3><span style=\"text-decoration: underline;\"><strong>Rollback flow<\/strong><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">deploy: starting <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Deploy: started <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Deploy: reverting- reverting the server to the previous version. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Deploy: reverted- reverted hook <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Deploy: publishing <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Deploy: published <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Deploy: finishing_rollback &#8211; finish the rollback\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Deploy: finished\u00a0 \u00a0\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Benefits of Capistrano\u00a0\u00a0<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">-&gt; Can deploy the application by one console command.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0cap &lt;stage_name&gt; deploy<\/span><\/p>\n<p><span style=\"font-weight: 400;\">-&gt; Can rollback the application by another command.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0cap &lt;stage_name&gt; rollback<\/span><\/p>\n<p><span style=\"font-weight: 400;\">-&gt; Can simply SSH to a lot of servers and perform a bunch of actions\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<ul>\n<li><span style=\"font-weight: 400;\">Capistrano is much easy such that it makes the development so simple and will decrease the workflow.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">The power of Capistrano becomes more specified when we have many servers with distinct roles.<\/span><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Capistrano is a remote server automation open-source tool written in Ruby that gives a set of advanced tools to deploy web applications to the servers. To perform the deploy functions&hellip;<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[67,5,3],"tags":[],"class_list":["post-2908","post","type-post","status-publish","format-standard","hentry","category-devops","category-general-topics","category-technical-articles"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/2908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=2908"}],"version-history":[{"count":23,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/2908\/revisions"}],"predecessor-version":[{"id":4092,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/2908\/revisions\/4092"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=2908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=2908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=2908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}