Skip to content

Instantly share code, notes, and snippets.

@arubis
Created August 31, 2017 20:37
Show Gist options
  • Save arubis/c5d1f2e1be9fc8b0b56880d968289e43 to your computer and use it in GitHub Desktop.
Save arubis/c5d1f2e1be9fc8b0b56880d968289e43 to your computer and use it in GitHub Desktop.
namespace :maintenance do
desc "Turn on maintenance mode"
task :enable do
require 'erb'
require 'i18n'
# Set up i18n (if app config changes, this will need separate updating)
I18n.load_path = Dir.glob(File.join(Dir.pwd, 'config', 'locales', '**', '*.yml'))
reason = ENV['REASON']
deadline = ENV['UNTIL']
template = "public/maintenance.html.erb"
result = ERB.new(File.read("#{template}")).result(binding)
rendered_path = "#{shared_path}/public/system/"
rendered_name = "maintenance.html"
puts "Trying internationalization: #{I18n.t('common.login')}"
on roles(:webserver) do
if test "[ ! -d #{rendered_path} ]"
info 'Creating missing directories.'
execute :mkdir, '-pv', rendered_path
end
upload!(StringIO.new(result), rendered_path + rendered_name)
execute "chmod 644 #{rendered_path + rendered_name}"
end
end
desc "Turn off maintenance mode"
task :disable do
on roles(:webserver) do
execute "rm -f #{shared_path}/public/system/maintenance.html"
end
end
namespace :notify do
task :enable do
Slackistrano::Capistrano.new(self).run(:mainton)
puts %{>>> If you are enabling maintenance, you should consider announcing \n>>> at https://manage.statuspage.io/pages/hns7sdjrhgyt/incidents as well.}.colorize(:yellow) if fetch(:stage) == :production
end
task :disable do
Slackistrano::Capistrano.new(self).run(:maintoff)
end
end
after "maintenance:enable", "maintenance:notify:enable"
after "maintenance:disable", "maintenance:notify:disable"
end
set :slack_run_mainton, -> { true }
set :slack_msg_mainton, -> { ":wrench: Maintenance mode has been enabled by #{fetch(:deploy_user)}" }
set :slack_run_maintoff, -> { true }
set :slack_msg_maintoff, -> { ":lz: Maintenance mode has been disabled by #{fetch(:deploy_user)}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment