用capistrano写一个简单的deploy脚本

 setp 1:
 
Ruby代码   
  1. gem install capistrano   
  2. gem install capistrano-ext  
           gem install capistrano
           gem install capistrano-ext




setp 2:  在项目根目录执行


 

Ruby代码 
 
  1. capify .  
capify .

   这将在根目录创建Capfile 和 config目录下创建deploy.rb 文件


setp 3: 编辑deploy.rb

Ruby代码   
  1. require 'bundler/capistrano'     #添加之后部署时会调用bundle install, 如果不需要就可以注释掉     
  2. require "capistrano/ext/multistage"     #多stage部署所需     
  3.      
  4.      
  5. set :stages, %w(development production)     
  6. set :default_stage"development"     
  7.      
  8.      
  9. set :application"crm_app_end"   #应用名称     
  10. set :repository,  "https://chang.abc.com/svn/engineering/vwaccount/mydeploy/trunk"        
  11. set :keep_releases, 5          #只保留5个备份     
  12.      
  13.      
  14. set :deploy_to"/var/www/#{application}"  #部署到远程机器的路径     
  15. set :user"user1"              #登录部署机器的用户名     
  16. set :password"user1"      #登录部署机器的密码, 如果不设部署时需要输入密码     
  17.      
  18.      
  19. default_run_options[:pty] = true          #pty: 伪登录设备     
  20. #default_run_options[:shell] = false     #Disable sh wrapping     
  21.      
  22.      
  23. set :use_sudotrue                            #执行的命令中含有sudo, 如果设为false, 用户所有操作都有权限     
  24. set :runner"user2"                          #以user2用户启动服务     
  25. set :svn_username"xxxx"          
  26.      
  27.      
  28. set :scm:subversion                        #     
  29. # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`                        
  30. #set :deploy_via, :copy                     #如果SCM设为空, 也可通过直接copy本地repo部署     
  31.      
  32.      
  33. #set :domain, "crm.abc.com"    #custom define     
  34.      
  35.      
  36. role :web"192.168.0.13""192.168.0.117"                          # Your HTTP server, Apache/etc     
  37. role :app"192.168.0.13""192.168.0.117"                          # This may be the same as your `Web` server     
  38. role :db,  "192.168.0.13":primary => true # This is where Rails migrations will run     
  39. #role :db,  "your slave db-server here"     
  40. #     
  41.            
  42.      
  43. namespace :deploy do     
  44.      
  45.      
  46.     desc "remove and destory this app"     
  47.     task :destory:roles => :app do     
  48.         run "cd #{deploy_to}/../ && #{try_sudo} mv #{application} /tmp/#{application}_#{Time.now.strftime('%Y%d%m%H%M%S')}"      #try_sudo 以sudo权限执行命令     
  49.     end     
  50.      
  51.      
  52.     after "deploy:update""deploy:shared:setup"              #after, before 表示在特定操作之后或之前执行其他任务     
  53.      
  54.      
  55.     namespace :shared do     
  56.         desc "setup shared folder symblink"     
  57.         task :setup do     
  58.             run "cd #{deploy_to}/current; rm -rf shared; ln -s #{shared_path} ."          
  59.         end     
  60.     end     
  61.      
  62.      
  63.     after "deploy:setup""deploy:setup_chown"     
  64.     desc "change owner from root to user1"     
  65.     task :setup_chown do     
  66.         run "cd #{deploy_to}/../ && #{try_sudo} chown -R #{user}:#{user} #{application}"     
  67.     end     
  68.      
  69.      
  70.      
  71.     task :start do     
  72.        run "cd #{deploy_to}/current && ./crmd.sh start"     
  73.        #try_sudo "cd #{deploy_to}/current && ./restart.sh"     
  74.     end     
  75.      
  76.      
  77.     task :stop do     
  78.        run "cd #{deploy_to}/current && ./crmd.sh stop"     
  79.     end     
  80.      
  81.      
  82.     task :restart do     
  83.        run "cd #{deploy_to}/current && ./crmd.sh restart"     
  84.     end     
  85.          
  86. end    
require 'bundler/capistrano'     #添加之后部署时会调用bundle install, 如果不需要就可以注释掉  
require "capistrano/ext/multistage"     #多stage部署所需  
  
  
set :stages, %w(development production)  
set :default_stage, "development"  
  
  
set :application, "crm_app_end"   #应用名称  
set :repository,  "https://chang.abc.com/svn/engineering/vwaccount/mydeploy/trunk"     
set :keep_releases, 5          #只保留5个备份  
  
  
set :deploy_to, "/var/www/#{application}"  #部署到远程机器的路径  
set :user, "user1"              #登录部署机器的用户名  
set :password, "user1"      #登录部署机器的密码, 如果不设部署时需要输入密码  
  
  
default_run_options[:pty] = true          #pty: 伪登录设备  
#default_run_options[:shell] = false     #Disable sh wrapping  
  
  
set :use_sudo, true                            #执行的命令中含有sudo, 如果设为false, 用户所有操作都有权限  
set :runner, "user2"                          #以user2用户启动服务  
set :svn_username, "xxxx"       
  
  
set :scm, :subversion                        #  
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`                     
#set :deploy_via, :copy                     #如果SCM设为空, 也可通过直接copy本地repo部署  
  
  
#set :domain, "crm.abc.com"    #custom define  
  
  
role :web, "192.168.0.13", "192.168.0.117"                          # Your HTTP server, Apache/etc  
role :app, "192.168.0.13", "192.168.0.117"                          # This may be the same as your `Web` server  
role :db,  "192.168.0.13", :primary => true # This is where Rails migrations will run  
#role :db,  "your slave db-server here"  
#  
        
  
namespace :deploy do  
  
  
    desc "remove and destory this app"  
    task :destory, :roles => :app do  
        run "cd #{deploy_to}/../ && #{try_sudo} mv #{application} /tmp/#{application}_#{Time.now.strftime('%Y%d%m%H%M%S')}"      #try_sudo 以sudo权限执行命令  
    end  
  
  
    after "deploy:update", "deploy:shared:setup"              #after, before 表示在特定操作之后或之前执行其他任务  
  
  
    namespace :shared do  
        desc "setup shared folder symblink"  
        task :setup do  
            run "cd #{deploy_to}/current; rm -rf shared; ln -s #{shared_path} ."       
        end  
    end  
  
  
    after "deploy:setup", "deploy:setup_chown"  
    desc "change owner from root to user1"  
    task :setup_chown do  
        run "cd #{deploy_to}/../ && #{try_sudo} chown -R #{user}:#{user} #{application}"  
    end  
  
  
  
    task :start do  
       run "cd #{deploy_to}/current && ./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}/current && ./crmd.sh restart"  
    end  
      
end  



setup 4: 在项目根目录执行 
          
Ruby代码   
  1. cap deploy:setup #建立部署路径   
  2. cap deploy:update #部署   
  3. cap deploy:start    #启动服务   
  4. cap deploy:stop   #停止服务   
  5. cap deploy:restart #重启服务  
  
             cap deploy:setup #建立部署路径
             cap deploy:update #部署
             cap deploy:start    #启动服务
             cap deploy:stop   #停止服务
             cap deploy:restart #重启服务




setup 5: 如果有多个stage要部署,则在config下创建deploy文件夹, 在该文件夹下有各stages文件, 文件名和 set :stages, %w(development production) 对应, 如development.rb production.rb,在各文件中设置相应变量即可, 然后可用 cap production deploy:... 来执行对应production的操作


其实capistrano最终都是转变成shell命令来完成任务的, 所以纯粹用shell脚本也可以完成相应功能, 但相对于晦涩的shell命令, capistrano明显更好懂, 并且它有很多默认操作非常好用, 比如setup, update, 如果自己用shell来实现非常麻烦, capistrano允许在脚本中嵌入shell, 比如上面的start, restart等, 这非常方便, 很多capistrano做不到的就可以用shell去搞定了
这事最近又有了新进展,我在一个django项目中用capistrano来发布,虽然python下也有一个叫fabric的东西,但那个还不熟,先用这个,有时间学习一下fabric再说

require 'bundler/capistrano'     #添加之后部署时会调用bundle install, 如果不需要就可以注释掉   


 

Ruby代码   
  1. #require "capistrano/ext/multistage"     #多stage部署所需       
  2.        
  3. set :application"app"   #应用名称       
  4. set :scm:subversion                             
  5. set :repository,  "svn://0.0.0.0/trunk/src"              
  6. set :keep_releases, 5          #只保留5个备份       
  7.        
  8. set :deploy_to"/var/www/#{application}"  #部署到远程机器的路径       
  9. set :user"xxx"              #登录部署机器的用户名       
  10. set :password"xxxxx"      #登录部署机器的密码, 如果不设部署时需要输入密码       
  11.        
  12. default_run_options[:pty] = true          #pty: 伪登录设备       
  13. #default_run_options[:shell] = false     #Disable sh wrapping       
  14.        
  15. #set :use_sudo, true                            #执行的命令中含有sudo, 如果设为false, 用户所有操作都有权限       
  16. set :runner"xxxx"                          #以用户启动服务       
  17.                           
  18. #set :deploy_via, :copy                     #如果SCM设为空, 也可通过直接copy本地repo部署       
  19.       
  20. role :web"172.16.120.222"#, "192.168.0.117"                          # Your HTTP server, Apache/etc       
  21. role :app"172.16.120.222"#, "192.168.0.117"                          # This may be the same as your `Web` server       
  22. role :db,  "172.16.120.222":primary => true # This is where Rails migrations will run       
  23. #role :db,  "your slave db-server here"       
  24. #       
  25.              
  26. namespace :deploy do       
  27.        
  28.        
  29.     desc "remove and destory this app"       
  30.     task :destory:roles => :app do       
  31.         run "cd #{deploy_to}/../ && #{try_sudo} mv #{application} /tmp/#{application}_#{Time.now.strftime('%Y%d%m%H%M%S')}"      #try_sudo 以sudo权限执行命令       
  32.     end       
  33.          
  34. #    after "deploy:setup", "deploy:setting_link"     
  35. #    desc "symlink for settings.py"     
  36. #    task :setting_link do     
  37. #        run "cd #{deploy_to}; #{try_sudo} touch current; #{try_sudo} ln -s current releases/#{application}"     
  38. #    end     
  39.        
  40.     after "deploy:update""deploy:shared:setup"              #after, before 表示在特定操作之后或之前执行其他任务       
  41.        
  42.     namespace :shared do       
  43.         desc "setup shared folder symblink"       
  44.         task :setup do     
  45.             run "cd #{deploy_to}/current; ln -s #{shared_path} x"            
  46.             run "cd #{deploy_to}/current; rm -rf log; ln -s #{shared_path}/log log"            
  47.             run "cd #{deploy_to}/releases; rm -f #{application} &&  __realversion__=`realpath ../current` && ln -s $__realversion__ #{application}"     
  48.         end       
  49.     end       
  50.        
  51.     after "deploy:setup""deploy:setup_chown"       
  52.     desc "change owner from root to user1"       
  53.     task :setup_chown do       
  54.         run "cd #{deploy_to}/../ && #{try_sudo} chown -R #{user}:#{user} #{application}"       
  55.     end       
  56.       
  57.     task :default do      
  58.         transaction do      
  59.             update_code     
  60.             symlink     
  61.         end     
  62.     end     
  63.      
  64.    task :update_code:except => { :no_release => true } do      
  65.         on_rollback { run "rm -rf #{release_path}; true" }      
  66.         strategy.deploy!      
  67.     end     
  68.      
  69.    before "deploy:rollback""deploy:clean_adaptive"     
  70.    task :clean_adaptive do     
  71.         run "cd #{deploy_to}/releases; rm -f #{application}"     
  72.    end     
  73.             
  74.         
  75.    after "deploy:rollback""deploy:rollback_set"     
  76.    task :rollback_set do     
  77.         run "cd #{deploy_to}/releases; rm -f #{application} &&  __realversion__=`realpath ../current` && ln -s $__realversion__ #{application}"     
  78.    end     
  79.        
  80.     task :start do       
  81.        run "cd #{deploy_to}/current && ./adaptive_pool.sh start"       
  82.        run "cd #{deploy_to}/current && ./adaptive_exam.sh start"        
  83.     end       
  84.        
  85.        
  86.     task :stop do       
  87.        run "cd #{deploy_to}/current && ./adaptive_pool.sh stop"       
  88.        run "cd #{deploy_to}/current && ./adaptive_exam.sh stop"       
  89.     end       
  90.        
  91.        
  92.     task :restart do       
  93.        run "cd #{deploy_to}/current && ./adaptive_pool.sh restart"       
  94.        run "cd #{deploy_to}/current && ./adaptive_exam.sh restart"       
  95.     end       
  96.            
  97. end      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值