Hi,本周第一天没什么事,所以就先分享一下我研究自动化代码部署与回滚软件的经验。这个软件有什么用途?主要是解决自动进行代码的部署,避免手动部署时出现错误,节省时间,同时在出现问题的时候,能回滚到之前的版本或者你指定的版本),我在gitlab里找到了这样的软件,名为capistrano。下面就先给大家介绍一下。
文章结构
一、介绍
二、要求的环境
三、安装
四、命令行测试
五、代码部署结合git)
六、代码部署结合svn)
七、代码回滚
八、总结
九、namespace
一、介绍
Capistrano是一种在多台服务器上运行脚本的开源工具,它主要用于部署web应用。它自动完成多台服务器上新版本的同步更新,包括数据库的改变。Capistrano最初由Jamis Buck用Ruby开发,并用RubyGems部署渠道部署。现在Capistrano不仅限于应用Ruby on Rails的 web应用框架,而且可以用于部署用其他框架的web应用程序,比如用PHP开发的。Capistran最初是用来应用于bash指令行。现在Ruby on Rails框架的用于也可以使用它的新特性,例如,对当前web应用部署改变使其更新版本,或者使其回滚到之前的旧版本。
二、要求的环境
1、Ruby一定要1.9.x;
2、server端与client端一定要进行ssh信任或者client端统一一个相同的密码;
三、安装
gem install capistrano
四、命令行测试
root@ubuntu:/tmp# cat capfile task :du, :hosts => "ubuntu.hadoop.com" do run "df -h" end
在ubuntu.hadoop.com本机)机器上运行df –h命令查看磁盘空间
root@ubuntu:/tmp# cap du * 2013-06-25 13:34:48 executing `du' * executing "df -h" servers: ["ubuntu.hadoop.com"] [ubuntu.hadoop.com] executing command ** [out :: ubuntu.hadoop.com] Filesystem Size Used Avail Use% Mounted on ** [out :: ubuntu.hadoop.com] /dev/mapper/ubuntu-root 39G 3.3G 33G 10% / ** [out :: ubuntu.hadoop.com] udev 489M 4.0K 489M 1% /dev ** [out :: ubuntu.hadoop.com] tmpfs 199M 336K 199M 1% /run ** [out :: ubuntu.hadoop.com] none 5.0M 0 5.0M 0% /run/lock ** [out :: ubuntu.hadoop.com] none 498M 0 498M 0% /run/shm ** [out :: ubuntu.hadoop.com] /dev/sda1 228M 51M 166M 24% /boot command finished in 981ms
可以看到类似使用python的fabric,但capistrano比fabric多了一个代码回滚功能。
需要注意的是如果你的client机器的ssh端口不是默认22的话,你还想操作的话,需要添加
ssh_options[:port] = 50020#ssh port
如果不添加的话,就会出现connection failed for: ip (Errno::ECONNREFUSED: Connection refused - connect(2))
如果你在使用iptables的机器上使用capistrano,只需要开启ssh端口就可以正常的使用capistrano。
五、代码部署结合git)
测试环境
主机名 ip 状态 ruby ubuntu.hadoop.com 192.168.56.102 server 1.9.3 server.hadoo.com 192.168.56.101 client 1.9.3
1、进入tmp目录,然后创建capi目录
cd /tmp mkdir capi cd capi
2、capistrano部署我的应用
capify .
这将产生两个文件,一个是capfile,这个是Capistrano需要的主要文件,就像make自动产生makefile,rake自动产生Rakefile一样,Capistrano默认寻找并加载capfile文件,默认产生的rapfile非常小,它所做的事就是加载“config/deploy.rb";第二个文件是"config/deploy.rb",这个文件包含你的应用程序部署所需的设置。Ralis的所有设置文档都放在config目录下,通常,你不需要管capfile文件,只需要把精力放在config/deploy.rb的设置和优化上。如果你的Capistrano用于非Rails环境,你可能只有一个capfile文件,而没有config/deploy.rb这个文件。
3、修改config/deploy.rb文件
set :application, "test" set :scm, :git set :repository, "ssh://git@192.168.1.250/data/gitrepo/jsonLib.git" set :deploy_to, "/tmp/result" set :normalize_asset_timestamps, false # set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none` role :web, "192.168.56.101" # Your HTTP server, Apache/etc role :app, "192.168.56.101" # This may be the same as your `Web` server role :db, "192.168.56.101", :primary => true # This is where Rails migrations will run #role :db, "your slave db-server here"
我的修改内容如上
主要是从192.168.1.250里获取git库,然后应用部署到192.168.56.102机器的/tmp/result目录。
set :application, "test"
是告诉Capistrano我们的应用程序被"调用"了
set :scm, :git
默认启动svn,而不是git,如果使用的话,需要set :scm, :git开启。
set :repository, "ssh://git@192.168.1.250/data/gitrepo/jsonLib.git"
然后我们需要告诉Capistrano我们的源代码在哪里,这个地址你的本地主机和服务器都可以到达。
set :deploy_to, "/tmp/result"
我们需要告诉Capistran我们的应用放在服务器的哪里,如果不加的话,默认为"/u/apps/#{application}"。
set :normalize_asset_timestamps, false
如果不添加这句话,在deploy:update的时候会出现
*** [err :: 192.168.56.101] find: `/tmp/result_svn/releases/20130625071724/public/images' *** [err :: 192.168.56.101] : No such file or directory *** [err :: 192.168.56.101] find: `/tmp/result_svn/releases/20130625071724/public/stylesheets': No such file or directory *** [err :: 192.168.56.101] find: `/tmp/result_svn/releases/20130625071724/public/javascripts': No such file or directory
原因是?xml:namespace>
Capistrano default behavior is to 'touch' all assets files. (To make sure that any cache get the deployment date). Assets are images, stylesheets, etc. If your PHP application is not using these directories, capistrano complains in such an ugly way. To disable asset timestamps updates, simply add:
app,web,db是Capistrano需要的三种角色:
web:你的服务器软件运行的地方;
app:你的应用层运行的地方;
db:迁移运行的地方;
4、部署目录结构
cap deploy:setup
会在192.168.56.101创建/tmp/result目录,然后在这个目录里创建releases与shared目录
root@ubuntu:/tmp/capi# cap deploy:setup * 2013-06-25 15:26:47 executing `deploy:setup' * executing "sudo -p 'sudo password: ' mkdir -p /tmp/result /tmp/result/releases /tmp/result/shared /tmp/result/shared/system /tmp/result/shared/log /tmp/result/shared/pids" servers: ["192.168.56.101"] [192.168.56.101] executing command command finished in 781ms * executing "sudo -p 'sudo password: ' chmod g+w /tmp/result /tmp/result/releases /tmp/result/shared /tmp/result/shared/system /tmp/result/shared/log /tmp/result/shared/pids" servers: ["192.168.56.101"] [192.168.56.101] executing command command finished in 40ms
5、检查
root@ubuntu:/tmp/capi# cap deploy:check * 2013-06-25 15:29:04 executing `deploy:check' * executing "test -d /tmp/result/releases" servers: ["192.168.56.101"] [192.168.56.101] executing command command finished in 819ms * executing "test -w /tmp/result" servers: ["192.168.56.101"] [192.168.56.101] executing command command finished in 25ms * executing "test -w /tmp/result/releases" servers: ["192.168.56.101"] [192.168.56.101] executing command command finished in 26ms * executing "which git" servers: ["192.168.56.101"] [192.168.56.101] executing command command finished in 26ms You appear to have all necessary dependencies

