From ba04068bfc96e12b98a33a8dad4dfc5f9583ff3d Mon Sep 17 00:00:00 2001 From: r888888888 Date: Wed, 19 Aug 2015 16:51:15 -0700 Subject: [PATCH] move cap tasks into separate files --- Capfile | 121 +------------------------- lib/capistrano/tasks/delayed_job.rake | 38 ++++++++ lib/capistrano/tasks/nginx.rake | 28 ++++++ lib/capistrano/tasks/symlink.rake | 25 ++++++ lib/capistrano/tasks/web.rake | 21 +++++ 5 files changed, 114 insertions(+), 119 deletions(-) create mode 100644 lib/capistrano/tasks/delayed_job.rake create mode 100644 lib/capistrano/tasks/nginx.rake create mode 100644 lib/capistrano/tasks/symlink.rake create mode 100644 lib/capistrano/tasks/web.rake diff --git a/Capfile b/Capfile index b810212e1..d46de2179 100644 --- a/Capfile +++ b/Capfile @@ -13,126 +13,9 @@ require 'capistrano3/unicorn' # Load custom tasks from `lib/capistrano/tasks` if you have any defined Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } -namespace :local_config do - desc "Link the local config files" - task :link_local_files do - on roles(:app) do - execute :ln, "-s", "#{deploy_to}/shared/config/danbooru_local_config.rb", "#{release_path}/config/danbooru_local_config.rb" - execute :ln, "-s", "#{deploy_to}/shared/config/database.yml", "#{release_path}/config/database.yml" - execute :ln, "-s", "#{deploy_to}/shared/config/newrelic.yml", "#{release_path}/config/newrelic.yml" - end - end -end - -namespace :data do - task :link_directories do - on roles(:app) do - execute :rm, "-f", "#{release_path}/public/data" - execute :ln, "-s", "#{deploy_to}/shared/data", "#{release_path}/public/data" - - execute :mkdir, "-p", "#{release_path}/public/cache" - execute :mkdir, "-p", "#{deploy_to}/shared/system/cache" - execute :touch, "#{deploy_to}/shared/system/cache/tags.json" - execute :ln, "-s", "#{deploy_to}/shared/system/cache/tags.json", "#{release_path}/public/cache/tags.json" - execute :touch, "#{deploy_to}/shared/system/cache/tags.json.gz" - execute :ln, "-s", "#{deploy_to}/shared/system/cache/tags.json.gz", "#{release_path}/public/cache/tags.json.gz" - end - end -end - -namespace :web do - desc "Present a maintenance page to visitors." - task :disable do - on roles(:app) do - maintenance_html_path = "#{current_path}/public/maintenance.html.bak" - if test("[ -e #{maintenance_html_path} ]") - execute :mv, maintenance_html_path, "#{current_path}/public/maintenance.html" - end - end - end - - desc "Makes the application web-accessible again." - task :enable do - on roles(:app) do - maintenance_html_path = "#{current_path}/public/maintenance.html" - if test("[ -e #{maintenance_html_path} ]") - execute :mv, maintenance_html_path, "#{current_path}/public/maintenance.html.bak" - end - end - end -end - -namespace :nginx do - desc "Shut down Nginx" - task :stop do - on roles(:web) do - as :user => "root" do - execute "/etc/init.d/nginx", "stop" - end - end - end - - desc "Start Nginx" - task :start do - on roles(:web) do - as :user => "root" do - execute "/etc/init.d/nginx", "start" - end - end - end - - desc "Reload Nginx" - task :reload do - on roles(:web) do - as :user => "root" do - execute "/etc/init.d/nginx", "reload" - end - end - end -end - -namespace :delayed_job do - desc "Start delayed_job process" - task :start do - on roles(:app) do - within current_path do - hostname = capture("hostname").strip - execute :bundle, "exec", "script/delayed_job", "--queues=default,#{hostname}", "-n 2", "start" - end - end - end - - desc "Stop delayed_job process" - task :stop do - on roles(:app) do - within current_path do - execute :bundle, "exec", "script/delayed_job", "stop" - end - end - end - - desc "Restart delayed_job process" - task :restart do - on roles(:app) do - find_and_execute_task("delayed_job:stop") - find_and_execute_task("delayed_job:start") - end - end - - task :kill do - on roles(:app) do - procs = capture("ps -A -o pid,command").split(/\r\n|\r|\n/).grep(/delayed_job/).map(&:to_i) - - if procs.any? - execute "for i in #{procs.join(' ')} ; do kill -s TERM $i ; done" - end - end - end -end - after "delayed_job:stop", "delayed_job:kill" -after "deploy:symlink:shared", "local_config:link_local_files" -after "deploy:symlink:shared", "data:link_directories" +after "deploy:symlink:shared", "symlink:local_files" +after "deploy:symlink:shared", "symlink:directories" before "deploy:started", "web:disable" before "deploy:started", "delayed_job:stop" after "deploy:published", "delayed_job:start" diff --git a/lib/capistrano/tasks/delayed_job.rake b/lib/capistrano/tasks/delayed_job.rake new file mode 100644 index 000000000..26f4e3ca8 --- /dev/null +++ b/lib/capistrano/tasks/delayed_job.rake @@ -0,0 +1,38 @@ +namespace :delayed_job do + desc "Start delayed_job process" + task :start do + on roles(:app) do + within current_path do + hostname = capture("hostname").strip + execute :bundle, "exec", "script/delayed_job", "--queues=default,#{hostname}", "-n 2", "start" + end + end + end + + desc "Stop delayed_job process" + task :stop do + on roles(:app) do + within current_path do + execute :bundle, "exec", "script/delayed_job", "stop" + end + end + end + + desc "Restart delayed_job process" + task :restart do + on roles(:app) do + find_and_execute_task("delayed_job:stop") + find_and_execute_task("delayed_job:start") + end + end + + task :kill do + on roles(:app) do + procs = capture("ps -A -o pid,command").split(/\r\n|\r|\n/).grep(/delayed_job/).map(&:to_i) + + if procs.any? + execute "for i in #{procs.join(' ')} ; do kill -s TERM $i ; done" + end + end + end +end \ No newline at end of file diff --git a/lib/capistrano/tasks/nginx.rake b/lib/capistrano/tasks/nginx.rake new file mode 100644 index 000000000..f31225f2d --- /dev/null +++ b/lib/capistrano/tasks/nginx.rake @@ -0,0 +1,28 @@ +namespace :nginx do + desc "Shut down Nginx" + task :stop do + on roles(:web) do + as :user => "root" do + execute "/etc/init.d/nginx", "stop" + end + end + end + + desc "Start Nginx" + task :start do + on roles(:web) do + as :user => "root" do + execute "/etc/init.d/nginx", "start" + end + end + end + + desc "Reload Nginx" + task :reload do + on roles(:web) do + as :user => "root" do + execute "/etc/init.d/nginx", "reload" + end + end + end +end \ No newline at end of file diff --git a/lib/capistrano/tasks/symlink.rake b/lib/capistrano/tasks/symlink.rake new file mode 100644 index 000000000..365ac92d0 --- /dev/null +++ b/lib/capistrano/tasks/symlink.rake @@ -0,0 +1,25 @@ +namespace :symlink do + desc "Link the local config files" + task :local_files do + on roles(:app) do + execute :ln, "-s", "#{deploy_to}/shared/config/danbooru_local_config.rb", "#{release_path}/config/danbooru_local_config.rb" + execute :ln, "-s", "#{deploy_to}/shared/config/database.yml", "#{release_path}/config/database.yml" + execute :ln, "-s", "#{deploy_to}/shared/config/newrelic.yml", "#{release_path}/config/newrelic.yml" + end + end + + desc "Link the local directories" + task :directories do + on roles(:app) do + execute :rm, "-f", "#{release_path}/public/data" + execute :ln, "-s", "#{deploy_to}/shared/data", "#{release_path}/public/data" + + execute :mkdir, "-p", "#{release_path}/public/cache" + execute :mkdir, "-p", "#{deploy_to}/shared/system/cache" + execute :touch, "#{deploy_to}/shared/system/cache/tags.json" + execute :ln, "-s", "#{deploy_to}/shared/system/cache/tags.json", "#{release_path}/public/cache/tags.json" + execute :touch, "#{deploy_to}/shared/system/cache/tags.json.gz" + execute :ln, "-s", "#{deploy_to}/shared/system/cache/tags.json.gz", "#{release_path}/public/cache/tags.json.gz" + end + end +end \ No newline at end of file diff --git a/lib/capistrano/tasks/web.rake b/lib/capistrano/tasks/web.rake new file mode 100644 index 000000000..207ae2d8f --- /dev/null +++ b/lib/capistrano/tasks/web.rake @@ -0,0 +1,21 @@ +namespace :web do + desc "Present a maintenance page to visitors." + task :disable do + on roles(:app) do + maintenance_html_path = "#{current_path}/public/maintenance.html.bak" + if test("[ -e #{maintenance_html_path} ]") + execute :mv, maintenance_html_path, "#{current_path}/public/maintenance.html" + end + end + end + + desc "Makes the application web-accessible again." + task :enable do + on roles(:app) do + maintenance_html_path = "#{current_path}/public/maintenance.html" + if test("[ -e #{maintenance_html_path} ]") + execute :mv, maintenance_html_path, "#{current_path}/public/maintenance.html.bak" + end + end + end +end \ No newline at end of file