# -*- coding: utf-8 -*- Capistrano::Configuration.instance(true).load do |configuration| set :use_sudo, true _cset :deploy_to_parent, "/app" _cset :branch, "master" default_run_options[:pty] = true # this is capistrano's default location. # depending on the permissions of the server # you may need to create it and chown it over # to :user (e.g. chown -R robotuser:robotuser /u) set :deploy_to do "#{deploy_to_parent}/deploy" end set :deploy_to_gitrepo do "#{deploy_to}/#{application}" end set :local_base_dir do "#{`pwd`}".strip end set :service_name do "play-#{application}" end set :service_check_name do "#{application}" end _cset :_config, Array.new _cset :build_name, "ROOT" _cset :build_path, "not set.war" _cset :reeb_config_dir, "/etc/reeb" _cset :reeb_config_repository, "git@git.uhuila.cn:configuration/product.git" _cset :servers, "" set :local_git_dir do "/app/tmp/localgit_#{application}" end set :local_gitrepo do "#{local_git_dir}/#{application}" end role :app, :primary => true do if servers.empty? "1.1.1.1" else servers.split(/[,\s]+/) end end namespace :nrpe do desc "stop nagios nrpe service" task :stop do run "[ -e /etc/init.d/nrpe ] && sudo /etc/init.d/nrpe stop" end desc "start nagios nrpe service" task :start do run "[ -e /etc/init.d/nrpe ] && sudo /etc/init.d/nrpe start" run "[ -e /etc/init.d/nrpe ] && sudo /etc/init.d/nrpe reload" end end namespace :play do desc "start service" task :start do sudo "/etc/init.d/#{service_name} start" # sleep 1 # api_status = nil # until api_status # api_status = capture("echo `netstat -tln | grep 8080`") # sleep 60 # end # puts "API Started successfully." end desc "stop service" task :stop do sudo "/etc/init.d/#{service_name} stop" end desc "stop and start service" task :restart do play.stop play.start end desc "setup remote and locate play dir" task :setup do play.setup_remote play.setup_local end desc "setup remote play dir" task :setup_remote do run clone_repository_command() end desc "setup locate play dir" task :setup_local do system clone_repository_local_command() end desc "just pull current build version, doesn't add tag." task :pull do play.setup_local system "cd #{local_gitrepo}; git checkout #{branch}; git fetch; git rebase origin/#{branch};" unless play_name.empty? puts "name=#{play_name}, war=#{play_path}" system update_repository_local_command(play_name, play_path) else if war_config.nil? or war_config.size == 0 raise 'NO war_config' end war_config.each do |config| puts "name=#{config[:name]}, war=#{config[:war]}" system update_repository_local_command(config[:name], config[:war]) end end build_msg = play_name.empty? ? "all" : play_name system "cd #{local_gitrepo}; git add .; git commit -m 'build for #{build_msg}'" # push tags and latest code system "cd #{local_gitrepo}; git push origin #{branch}" if $? != 0 raise "git push failed" end end desc "tag build version. use -s tag=xxx to set tag's name" task :tag do play.setup_local system "cd #{local_gitrepo}; git checkout #{branch}; git fetch; git rebase origin/#{branch};" tag_name = configuration[:tag] || configuration[:build_version] raise "NO tag. pls use -s tag=xxx set tag_name" if tag_name.nil? system "cd #{local_gitrepo}; git tag #{tag_name};" system "cd #{local_gitrepo}; git push origin #{branch} --tags" if $? != 0 raise "git push --tags failed" end end desc "deploy. use -s tag=xxx to set tag's name, if NOT set tag, pull the repository's last version." task :deploy do play.setup_remote run play.setup_or_update_reeb_config tag_name = configuration[:tag] || configuration[:build_version] unless tag_name.nil? # raise "NO tag. pls use -s tag=xxx set tag_name" run update_repository_remote_command(tag_name) else run pull_repository_remote_command(branch) end end def self.clone_repository_local_command [ "rm -Rf #{local_git_dir}", "mkdir -p #{local_git_dir}", "cd #{local_git_dir}", "git clone #{repository} #{local_gitrepo}" ].join("; ") end def self.update_repository_local_command(name, path) unless path.start_with?('/') play_path = "#{local_base_dir}/#{path}" else play_path = path end [ "cd #{local_gitrepo}", "if [ -e #{local_gitrepo}/#{name} ]", "then git rm -rf #{local_gitrepo}/#{name}", "fi", "mkdir -p #{local_gitrepo}/#{name}", "cd #{local_gitrepo}/#{name}", "cp #{play_path}/* . -r" ].join("; ") end def self.setup_or_update_reeb_config [ "if [ ! -e #{reeb_config_dir} ]", "then sudo mkdir -p #{reeb_config_dir}", "sudo chown #{user} #{deploy_to}", "cd #{reeb_config_dir}", "git clone #{reeb_config_repository} .", "fi", "cd #{reeb_config_dir}", "git pull" ].join("; ") end def self.clone_repository_command [ "if [ ! -e #{deploy_to_gitrepo} ]", "then sudo mkdir -p #{deploy_to}", "sudo chown #{user} #{deploy_to}", "cd #{deploy_to}", "git clone #{repository} #{deploy_to_gitrepo}", "fi" ].join("; ") end def self.update_repository_remote_command(tag_name) [ # git reset --hard;git fetch;git reset --merge #{tag_name} "cd #{deploy_to_gitrepo}", "git reset --hard", "git fetch", "git reset --merge #{tag_name}", ].join("; ") end def self.pull_repository_remote_command(branch) [ # git reset --hard;git fetch;git reset --merge #{tag_name} "cd #{deploy_to_gitrepo}", "git reset --hard", "git fetch", "git rebase origin/#{branch}", ].join("; ") end end end