From 0be67f4f121a9a1e850a01cd1432676e4c3d3ff3 Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Fri, 25 Oct 2019 12:33:21 +0630 Subject: [PATCH] SX cap production setup files --- lib/capistrano/substitute_strings.rb | 12 +++++++ lib/capistrano/tasks/check_revision.cap | 14 ++++++++ .../tasks/compile_assets_locally.cap | 17 ++++++++++ lib/capistrano/tasks/logs.cap | 14 ++++++++ lib/capistrano/tasks/monit.cap | 10 ++++++ lib/capistrano/tasks/nginx.cap | 22 +++++++++++++ lib/capistrano/tasks/restart.cap | 10 ++++++ lib/capistrano/tasks/run_tests.cap | 18 ++++++++++ lib/capistrano/tasks/setup_config.cap | 33 +++++++++++++++++++ lib/capistrano/template.rb | 32 ++++++++++++++++++ 10 files changed, 182 insertions(+) create mode 100644 lib/capistrano/substitute_strings.rb create mode 100644 lib/capistrano/tasks/check_revision.cap create mode 100644 lib/capistrano/tasks/compile_assets_locally.cap create mode 100644 lib/capistrano/tasks/logs.cap create mode 100644 lib/capistrano/tasks/monit.cap create mode 100644 lib/capistrano/tasks/nginx.cap create mode 100644 lib/capistrano/tasks/restart.cap create mode 100644 lib/capistrano/tasks/run_tests.cap create mode 100644 lib/capistrano/tasks/setup_config.cap create mode 100644 lib/capistrano/template.rb diff --git a/lib/capistrano/substitute_strings.rb b/lib/capistrano/substitute_strings.rb new file mode 100644 index 00000000..d4b24432 --- /dev/null +++ b/lib/capistrano/substitute_strings.rb @@ -0,0 +1,12 @@ +# we often want to refer to variables which +# are defined in subsequent stage files. This +# let's us use the {{var}} to represent fetch(:var) +# in strings which are only evaluated at runtime. + +def sub_strings(input_string) + output_string = input_string + input_string.scan(/{{(\w*)}}/).each do |var| + output_string.gsub!("{{#{var[0]}}}", fetch(var[0].to_sym)) + end + output_string +end diff --git a/lib/capistrano/tasks/check_revision.cap b/lib/capistrano/tasks/check_revision.cap new file mode 100644 index 00000000..72b4aa12 --- /dev/null +++ b/lib/capistrano/tasks/check_revision.cap @@ -0,0 +1,14 @@ +namespace :deploy do + desc "checks whether the currently checkout out revision matches the + remote one we're trying to deploy from" + task :check_revision do + branch = fetch(:branch) + unless `git rev-parse HEAD` == `git rev-parse #{branch}` + puts "WARNING: HEAD is not the same as #{branch}" + puts "Run `git push` to sync changes or make sure you've" + puts "checked out the branch: #{branch} as you can only deploy" + puts "if you've got the target branch checked out" + exit + end + end +end diff --git a/lib/capistrano/tasks/compile_assets_locally.cap b/lib/capistrano/tasks/compile_assets_locally.cap new file mode 100644 index 00000000..e6264e81 --- /dev/null +++ b/lib/capistrano/tasks/compile_assets_locally.cap @@ -0,0 +1,17 @@ +namespace :deploy do + desc "compiles assets locally then rsyncs" + task :compile_assets_locally do + run_locally do + execute "RAILS_ENV=#{fetch(:rails_env)} bundle exec rake assets:precompile" + end + on roles(:app) do |role| + run_locally do + execute"rsync -av ./public/assets/ #{role.user}@#{role.hostname}:#{release_path}/public/assets/;" + end + sudo "chmod -R 755 #{release_path}/public/assets/" + end + run_locally do + execute "rm -rf ./public/assets" + end + end +end diff --git a/lib/capistrano/tasks/logs.cap b/lib/capistrano/tasks/logs.cap new file mode 100644 index 00000000..7bbfac2e --- /dev/null +++ b/lib/capistrano/tasks/logs.cap @@ -0,0 +1,14 @@ +namespace :logs do + task :tail, :file do |t, args| + if args[:file] + on roles(:app) do + execute "tail -f #{shared_path}/log/#{args[:file]}.log" + end + else + puts "please specify a logfile e.g: 'rake logs:tail[logfile]" + puts "will tail 'shared_path/log/logfile.log'" + puts "remember if you use zsh you'll need to format it as:" + puts "rake 'logs:tail[logfile]' (single quotes)" + end + end +end diff --git a/lib/capistrano/tasks/monit.cap b/lib/capistrano/tasks/monit.cap new file mode 100644 index 00000000..5dbac6a2 --- /dev/null +++ b/lib/capistrano/tasks/monit.cap @@ -0,0 +1,10 @@ +namespace :monit do + %w(start stop restart).each do |task_name| + desc "#{task_name} Monit" + task task_name do + on roles(:app), in: :sequence, wait: 5 do + sudo "service monit #{task_name}" + end + end + end +end diff --git a/lib/capistrano/tasks/nginx.cap b/lib/capistrano/tasks/nginx.cap new file mode 100644 index 00000000..dfadd5c6 --- /dev/null +++ b/lib/capistrano/tasks/nginx.cap @@ -0,0 +1,22 @@ +namespace :nginx do + %w(start stop restart reload).each do |task_name| + desc "#{task } Nginx" + task task_name do + on roles(:app), in: :sequence, wait: 5 do + sudo "/etc/init.d/nginx #{task_name}" + end + end + end + + desc "Remove default Nginx Virtual Host" + task "remove_default_vhost" do + on roles(:app) do + if test("[ -f /etc/nginx/sites-enabled/default ]") + sudo "rm /etc/nginx/sites-enabled/default" + puts "removed default Nginx Virtualhost" + else + puts "No default Nginx Virtualhost to remove" + end + end + end +end diff --git a/lib/capistrano/tasks/restart.cap b/lib/capistrano/tasks/restart.cap new file mode 100644 index 00000000..ff8c028f --- /dev/null +++ b/lib/capistrano/tasks/restart.cap @@ -0,0 +1,10 @@ +namespace :deploy do + desc 'Commands for unicorn application' + %w(start stop force-stop restart upgrade reopen-logs).each do |command| + task command.to_sym do + on roles(:app), in: :sequence, wait: 5 do + sudo "/etc/init.d/unicorn_#{fetch(:full_app_name)} #{command}" + end + end + end +end diff --git a/lib/capistrano/tasks/run_tests.cap b/lib/capistrano/tasks/run_tests.cap new file mode 100644 index 00000000..32f26f9c --- /dev/null +++ b/lib/capistrano/tasks/run_tests.cap @@ -0,0 +1,18 @@ +namespace :deploy do + desc "Runs test before deploying, can't deploy unless they pass" + task :run_tests do + test_log = "log/capistrano.test.log" + tests = fetch(:tests) + tests.each do |test| + puts "--> Running tests: '#{test}', please wait ..." + unless system "bundle exec rspec #{test} > #{test_log} 2>&1" + puts "--> Tests: '#{test}' failed. Results in: #{test_log} and below:" + system "cat #{test_log}" + exit; + end + puts "--> '#{test}' passed" + end + puts "--> All tests passed" + system "rm #{test_log}" + end +end diff --git a/lib/capistrano/tasks/setup_config.cap b/lib/capistrano/tasks/setup_config.cap new file mode 100644 index 00000000..81ce88d9 --- /dev/null +++ b/lib/capistrano/tasks/setup_config.cap @@ -0,0 +1,33 @@ +namespace :deploy do + task :setup_config do + on roles(:app) do + # make the config dir + execute :mkdir, "-p #{shared_path}/config" + full_app_name = fetch(:full_app_name) + + # config files to be uploaded to shared/config, see the + # definition of smart_template for details of operation. + # Essentially looks for #{filename}.erb in deploy/#{full_app_name}/ + # and if it isn't there, falls back to deploy/#{shared}. Generally + # everything should be in deploy/shared with params which differ + # set in the stage files + config_files = fetch(:config_files) + config_files.each do |file| + smart_template file + end + + # which of the above files should be marked as executable + executable_files = fetch(:executable_config_files) + executable_files.each do |file| + execute :chmod, "+x #{shared_path}/config/#{file}" + end + + # symlink stuff which should be... symlinked + symlinks = fetch(:symlinks) + + symlinks.each do |symlink| + sudo "ln -nfs #{shared_path}/config/#{symlink[:source]} #{sub_strings(symlink[:link])}" + end + end + end +end diff --git a/lib/capistrano/template.rb b/lib/capistrano/template.rb new file mode 100644 index 00000000..07bd0be3 --- /dev/null +++ b/lib/capistrano/template.rb @@ -0,0 +1,32 @@ +# will first try and copy the file: +# config/deploy/#{full_app_name}/#{from}.erb +# to: +# shared/config/to +# if the original source path doesn exist then it will +# search in: +# config/deploy/shared/#{from}.erb +# this allows files which are common to all enviros to +# come from a single source while allowing specific +# ones to be over-ridden +# if the target file name is the same as the source then +# the second parameter can be left out +def smart_template(from, to=nil) + to ||= from + full_to_path = "#{shared_path}/config/#{to}" + if from_erb_path = template_file(from) + from_erb = StringIO.new(ERB.new(File.read(from_erb_path)).result(binding)) + upload! from_erb, full_to_path + info "copying: #{from_erb} to: #{full_to_path}" + else + error "error #{from} not found" + end +end + +def template_file(name) + if File.exist?((file = "config/deploy/#{fetch(:full_app_name)}/#{name}.erb")) + return file + elsif File.exist?((file = "config/deploy/shared/#{name}.erb")) + return file + end + return nil +end