configuration = Capistrano::Configuration.respond_to?(:instance) ? Capistrano::Configuration.instance(:must_exist) : Capistrano.configuration(:must_exist) configuration.load do def with_role(role, &block) if role_defined?(role) original, ENV['HOSTS'] = ENV['HOSTS'], find_servers(:roles =>role).map{|d| d.host}.join(",") begin yield ensure ENV['HOSTS'] = original end end end # # Returns true if at least 1 server has been assigned to the role # def role_defined?(role) find_servers(:roles => role).count > 0 end task :symlink_init_d_script do puts "ensuring init_d script is symlinked" run "test -L /etc/init.d/#{application_stub} || #{sudo} ln -s #{script_path}/init_d /etc/init.d/#{application_stub}" end task :symlink_log_dirs do puts "ensuring #{std_log_dir} exists" mkdir_unless_exists( std_log_dir ) puts "setting ownership on #{std_log_dir} & files to #{user}" run "#{sudo} chown -R #{user} #{std_log_dir}" shared_log_dir = "#{shared_dir}/log" puts "ensuring #{shared_log_dir} exists" mkdir_unless_exists( shared_log_dir ) puts "ensuring log dir #{shared_log_dir} is symlinked to #{std_log_dir}" symlink_dir( std_log_dir, shared_log_dir ) puts "setting ownership on #{shared_log_dir} & files to #{user}" run "#{sudo} chown -R #{user} #{shared_log_dir}" puts "ensuring log dir #{web_dir}/log is symlinked to #{std_log_dir}" symlink_dir( std_log_dir, "#{web_dir}/log" ) puts "setting ownership on log dir & files to #{user}" run "#{sudo} chown -R #{user} #{web_dir}/log" puts "removing default #{current_path}/log symlink" run "#{sudo} rm #{current_path}/log" end task :symlink_pid_dirs do std_pid_dir = "#{web_dir}/tmp/pids" shared_pid_dir = "#{shared_dir}/pids" puts "ensuring #{shared_pid_dir} exists" mkdir_unless_exists( shared_pid_dir ) puts "ensuring #{std_pid_dir} exists" mkdir_unless_exists( std_pid_dir ) puts "ensuring pid dir #{std_pid_dir} is symlinked to #{shared_pid_dir}" symlink_dir( shared_pid_dir, std_pid_dir ) puts "setting ownership on #{web_dir}/tmp & files to #{user}" run "#{sudo} chown -R #{user} #{web_dir}/tmp" puts "setting ownership on #{shared_pid_dir} & files to #{user}" run "#{sudo} chown -R #{user} #{shared_pid_dir}" end task :symlink_site_cache_dir do puts "Removing existing symlink from /tmp/cache/sites" remove_dir_if_exists( "#{web_dir}/tmp/cache" ) puts "Creating /shared/cache/sites dir if needed" run "mkdir -p #{shared_dir}/cache/sites && #{sudo} chown -R #{user} #{shared_dir}/cache" puts "Symlinking /shared/cache into /tmp/" symlink_dir("#{shared_dir}/cache","#{web_dir}/tmp/cache") end task :regenerate_monit do puts "regenerating monit config file(s)" run "test -d #{shared_dir}/templates || mkdir -p #{shared_dir}/templates" run "cd #{web_dir} && #{sudo} rake itrigga:generator PATTERN=monit TEMPLATE_DIR=#{web_dir}/script/templates OUTPUT_DIR=#{shared_dir}/templates CONFIG_FILE=`readlink /etc/#{config_file_name}` BASE_DIR=#{shared_dir}" puts "setting permissions & ownership on monit config files" set_permissions_on_files_in_dir( :dir=>"#{shared_dir}/templates", :ownership=>"root", :permissions=>"0700") end task :resymlink_monit do remove_dir_if_exists("/etc/monit/conf.d/#{application_stub}") sudo "mkdir /etc/monit/conf.d/#{application_stub}" puts "re-symlinking monit config files" symlink_files_in_dir_by_regex("#{shared_dir}/templates","/etc/monit/conf.d/#{application_stub}",".*monit$") # overwrite the default monit monitrc file to just have the includes for the conf.d dir run "(test -f /etc/monit/monitrc && #{sudo} rm -f /etc/monit/monitrc) || echo no existing monitrc file" sudo "echo \"echo 'include /etc/monit/conf.d/**/*' > /etc/monit/monitrc\" | #{sudo} bash" run "#{sudo} chown root:root /etc/monit/monitrc && #{sudo} chmod 0700 /etc/monit/monitrc" end task :reload_monit do run "#{sudo} monit reload" end task :regenerate_logrotate do puts "regenerating logrotate config file" run "test -d #{shared_dir}/templates || mkdir -p #{shared_dir}/templates" run "cd #{web_dir} && #{sudo} rake itrigga:generator PATTERN=logrotate TEMPLATE_DIR=#{web_dir}/script/templates OUTPUT_DIR=#{shared_dir}/templates CONFIG_FILE=false BASE_DIR=#{shared_dir}" puts "setting permissions & ownership on logrotate config files" set_permissions_on_files_in_dir( :dir=>"#{shared_dir}/templates", :ownership=>"root", :permissions=>"0700" ) end task :resymlink_logrotate do logrotate_config_file = "#{shared_dir}/templates/logrotate" std_logrotate_file = "/etc/logrotate.d/#{application_stub}" puts "re-symlinking logrotate config" run "(test -L #{std_logrotate_file} && #{sudo} rm #{std_logrotate_file}) || echo nothing to remove" run "#{sudo} ln -s #{logrotate_config_file} #{std_logrotate_file} " end task :chown_entire_project_dir do puts "chowning entire #{application_stub} dir to #{user_and_group}" run "#{sudo} chown -R #{user_and_group} #{deploy_to}" end task :regenerate_db_scripts do puts "regenerating db scripts" end task :regenerate_cron_scripts do puts "regenerating cron scripts" run "test -d #{shared_dir}/templates || mkdir -p #{shared_dir}/templates" with_role :solr do solr_lockfile_script_path = File.join(shared_dir,"templates","solr_lockfile_check.script") run "cd #{web_dir} && #{sudo} rake itrigga:generator PATTERN=solr_lockfile_check.cron TEMPLATE_DIR=#{web_dir}/script/templates OUTPUT_DIR=#{shared_dir}/templates CONFIG_FILE=false SOLR_LOCKFILE_SCRIPT_PATH=#{solr_lockfile_script_path}" end puts "setting permissions & ownership on cron scripts" set_permissions_on_files_in_dir( :dir=>"#{shared_dir}/templates", :ownership=>"root", :permissions=>"0700" ) end task :symlink_cron_scripts do puts "symlinking any cron scripts in scripts/cron into /etc/cron.d/" sudo "rm -f /etc/cron.d/solr_lockfile_check" symlink_files_in_dir_by_regex("#{shared_dir}/templates","/etc/cron.d",".*cron$") # because cron is stupid any symlinks in /etc/cron.d/ that end in .cron will *not* be run. Not at all. So lets fix those up # removes the .cron from the end of any symlinks on /etc/cron.d/ run "echo 'for f in `find /etc/cron.d/ -type l -name '*.cron'`;do ln -s `readlink $f` ${f/.cron/} && rm $f; done' | #{sudo} bash" # give cron a kick to pick up the new changes sudo "service cron reload" end task :regenerate_monitoring_scripts do puts "regenerating monitoring scripts" run "test -d #{shared_dir}/templates || mkdir -p #{shared_dir}/templates" with_role :solr do solr_lockfile_path = File.join(shared_dir,"solr","data","index","*.lock") run "cd #{web_dir} && #{sudo} rake itrigga:generator PATTERN=solr_lockfile_check.script TEMPLATE_DIR=#{web_dir}/script/templates OUTPUT_DIR=#{shared_dir}/templates CONFIG_FILE=false SOLR_LOCKFILE_PATH=#{solr_lockfile_path} SOLR_TIMEOUT=900" end puts "setting permissions and ownership for solr_lockfile check script" set_permissions_on_files_in_dir( :dir=>"#{shared_dir}/templates", :ownership=>"root", :permissions=>"0700" ) end end