部署 capstrano 中自定义方法为什么只能在第一台机器运行

suupic · 2012年07月31日 · 2298 次阅读

用 capstrano 管理 tomcat 为了处理 tomcat 正常关闭写了一个方法 tomcat_shutdown_for_deploy,之前运行正常 现在需要给 tomcat 增加部署节点,却发现 capstrano 只能获取到第一个节点 问题就出在这个方法上 task 里包含 tomcat_shutdown_for_deploy,则只能获取第一个节点:

* 14:58:35 == Currently executing `libra:deploy'
  servers: ["172.16.9.11"]

注释掉 tomcat_shutdown_for_deploy,就能得到正确的多个节点:

* 14:59:06 == Currently executing `libra:deploy'
  servers: ["172.16.9.11", "172.16.9.12"]

why?

task:

desc "发布#{app_name}"
task :deploy, :roles => [current_role] do
  tomcat_shutdown_for_deploy
  update
  startup
end

相关方法:

def file_exist?(file)
  result = capture "test -e #{file} && echo true || echo false"
  result.chomp! == "true" ? true : false
end

def tomcat_shutdown_for_deploy
  #若进程数大于2则认为tomcat在运行(2个为ps进程和管道分解的grep进程)
  puts capture("/bin/ps -ef |grep Dcatalina.home=#{tomcat_root}").to_s
  process_exist = (capture("/bin/ps -ef |grep Dcatalina.home=#{tomcat_root}|wc -l").to_i > 2)
  pidfile_exist = file_exist?("#{tomcat_pid}")
  if process_exist
    if pidfile_exist
      puts "#{app_name}正在运行,开始关闭..."
     run "bash -l -c '#{tomcat_root}/bin/shutdown.sh -force'"
    else
      puts "#{app_name}正在运行,但pid文件不存在,异常!需人工处理"
      run "bash -l -c '#{tomcat_root}/bin/shutdown.sh -force'"
    end
  else
    if pidfile_exist
      puts "#{app_name}没有运行,但pid文件存在,清理文件:#{tomcat_pid}"
      run "bash -l -c 'rm -f #{tomcat_pid}'"
    else
      puts "#{app_name}正常关闭状态"
    end
  end
end
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号