Commit 448cd627 by Karl Kloppenborg Committed by GitHub

Merge pull request #21 from inventionlabsSydney/release/1.2.0

Release/1.2.0
parents 742c7eda 0c31bb9f
...@@ -12,3 +12,4 @@ ...@@ -12,3 +12,4 @@
*.o *.o
*.a *.a
mkmf.log mkmf.log
builds/**
...@@ -37,9 +37,12 @@ Configurable options, shown here with defaults: ...@@ -37,9 +37,12 @@ Configurable options, shown here with defaults:
``` ```
## Contributors ## Contributors
- [Karl Kloppenborg](https://github.com/inventionlabsSydney)
- [Andrew Babichev](https://github.com/Tensho) - [Andrew Babichev](https://github.com/Tensho)
- [NaixSpirit](https://github.com/NaixSpirit) - [NaixSpirit](https://github.com/NaixSpirit)
- [hpetru](https://github.com/hpetru)
- [jhollinger](https://github.com/jhollinger)
- [redrick](https://github.com/redrick)
## Contributing ## Contributing
......
...@@ -20,7 +20,7 @@ Gem::Specification.new do |spec| ...@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.0.0' spec.required_ruby_version = '>= 2.0.0'
spec.add_dependency 'capistrano', '>= 3.9.0' spec.add_dependency 'capistrano', '>= 3.9.0'
spec.add_dependency 'sneakers' spec.add_dependency 'sneakers', '>= 2.6'
spec.add_development_dependency "bundler", "~> 1.7" spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rake", "~> 10.0"
......
module Capistrano
module Sneakers
module HelperMethods
def sneakers_each_process_with_index(reverse = false, &block)
_pid_files = sneakers_pid_files
_pid_files.reverse! if reverse
_pid_files.each_with_index do |pid_file, idx|
within release_path do
yield(pid_file, idx)
end
end
end
def sneakers_pid_files
sneakers_roles = Array(fetch(:sneakers_roles))
sneakers_roles.select! { |role| host.roles.include?(role) }
sneakers_roles.flat_map do |role|
processes = fetch(:sneakers_processes)
if processes == 1
fetch(:sneakers_pid)
else
Array.new(processes) { |idx| fetch(:sneakers_pid).gsub(/\.pid$/, "-#{idx}.pid") }
end
end
end
def sneakers_pid_file_exists?(pid_file)
test(*("[ -f #{pid_file} ]").split(' '))
end
def sneakers_process_exists?(pid_file)
test(*("kill -0 $( cat #{pid_file} )").split(' '))
end
def quiet_sneakers(pid_file)
if fetch(:sneakers_use_signals) || fetch(:sneakers_run_config)
execute :kill, "-USR1 `cat #{pid_file}`"
else
begin
execute :bundle, :exec, :sneakersctl, 'quiet', "#{pid_file}"
rescue SSHKit::Command::Failed
# If gems are not installed eq(first deploy) and sneakers_default_hooks as active
warn 'sneakersctl not found (ignore if this is the first deploy)'
end
end
end
def stop_sneakers(pid_file)
if fetch(:sneakers_run_config) == true
execute :kill, "-SIGTERM `cat #{pid_file}`"
else
if fetch(:stop_sneakers_in_background, fetch(:sneakers_run_in_background))
if fetch(:sneakers_use_signals)
background :kill, "-TERM `cat #{pid_file}`"
else
background :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout)
end
else
execute :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout)
end
end
end
def start_sneakers(pid_file, idx = 0)
if fetch(:sneakers_run_config) == true
# Use sneakers configuration prebuilt in
raise "[ set :workers, ['worker1', 'workerN'] ] not configured properly, please configure the workers you wish to use" if fetch(:sneakers_workers).nil? or fetch(:sneakers_workers) == false or !fetch(:sneakers_workers).kind_of? Array
workers = fetch(:sneakers_workers).compact.join(',')
info "Starting the sneakers processes"
with rails_env: fetch(:sneakers_env), workers: workers do
rake 'sneakers:run'
end
end
end
def sneakers_switch_user(role, &block)
user = sneakers_user(role)
if user == role.user
block.call
else
as user do
block.call
end
end
end
def sneakers_user(role)
properties = role.properties
properties.fetch(:sneakers_user) || fetch(:sneakers_user) || properties.fetch(:run_as) || role.user
end
end
end
end
module Capistrano module Capistrano
module Sneakers module Sneakers
VERSION = "0.1.1" VERSION = "0.2.0"
end end
end end
...@@ -2,7 +2,7 @@ namespace :load do ...@@ -2,7 +2,7 @@ namespace :load do
task :defaults do task :defaults do
set :monit_bin, '/usr/bin/monit' set :monit_bin, '/usr/bin/monit'
set :sneakers_monit_default_hooks, true set :sneakers_monit_default_hooks, true
set :sneakers_monit_conf_dir, -> { '/etc/monit/conf.d' } set :sneakers_monit_conf_dir, '/etc/monit/conf.d'
set :sneakers_monit_use_sudo, true set :sneakers_monit_use_sudo, true
set :sneakers_monit_templates_path, 'config/deploy/templates' set :sneakers_monit_templates_path, 'config/deploy/templates'
end end
...@@ -29,7 +29,7 @@ namespace :sneakers do ...@@ -29,7 +29,7 @@ namespace :sneakers do
@role = role @role = role
upload_sneakers_template 'sneakers_monit', "#{fetch(:tmp_dir)}/monit.conf", @role upload_sneakers_template 'sneakers_monit', "#{fetch(:tmp_dir)}/monit.conf", @role
mv_command = "mv #{fetch(:tmp_dir)}/monit.conf #{fetch(:sneakers_monit_conf_dir)}/#{sneakers_service_name}.conf" mv_command = "mv #{fetch(:tmp_dir)}/monit.conf #{fetch(:sneakers_monit_conf_dir)}/#{sneakers_monit_service_name}.conf"
sudo_if_needed mv_command sudo_if_needed mv_command
sudo_if_needed "#{fetch(:monit_bin)} reload" sudo_if_needed "#{fetch(:monit_bin)} reload"
...@@ -39,40 +39,49 @@ namespace :sneakers do ...@@ -39,40 +39,49 @@ namespace :sneakers do
desc 'Monitor Sneakers monit-service' desc 'Monitor Sneakers monit-service'
task :monitor do task :monitor do
on roles(fetch(:sneakers_roles)) do on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} monitor #{sneakers_service_name}" begin
sudo_if_needed "#{fetch(:monit_bin)} monitor #{sneakers_monit_service_name}"
rescue
invoke 'sneakers:monit:config'
sudo_if_needed "#{fetch(:monit_bin)} monitor #{sneakers_monit_service_name}"
end
end end
end end
desc 'Unmonitor Sneakers monit-service' desc 'Unmonitor Sneakers monit-service'
task :unmonitor do task :unmonitor do
on roles(fetch(:sneakers_roles)) do on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} unmonitor #{sneakers_service_name}" begin
sudo_if_needed "#{fetch(:monit_bin)} unmonitor #{sneakers_monit_service_name}"
rescue
# no worries here
end
end end
end end
desc 'Start Sneakers monit-service' desc 'Start Sneakers monit-service'
task :start do task :start do
on roles(fetch(:sneakers_roles)) do on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} start #{sneakers_service_name}" sudo_if_needed "#{fetch(:monit_bin)} start #{sneakers_monit_service_name}"
end end
end end
desc 'Stop Sneakers monit-service' desc 'Stop Sneakers monit-service'
task :stop do task :stop do
on roles(fetch(:sneakers_roles)) do on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} stop #{sneakers_service_name}" sudo_if_needed "#{fetch(:monit_bin)} stop #{sneakers_monit_service_name}"
end end
end end
desc 'Restart Sneakers monit-service' desc 'Restart Sneakers monit-service'
task :restart do task :restart do
on roles(fetch(:sneakers_roles)) do on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} restart #{sneakers_service_name}" sudo_if_needed "#{fetch(:monit_bin)} restart #{sneakers_monit_service_name}"
end end
end end
def sneakers_service_name def sneakers_monit_service_name
fetch(:sneakers_service_name, "sneakers_#{fetch(:application)}_#{fetch(:sneakers_env)}") fetch(:sneakers_monit_service_name, "sneakers_#{fetch(:application)}_#{fetch(:sneakers_env)}")
end end
def sudo_if_needed(command) def sudo_if_needed(command)
......
require 'capistrano/sneakers/helper_methods'
include Capistrano::Sneakers::HelperMethods
namespace :load do namespace :load do
task :defaults do task :defaults do
set :sneakers_default_hooks, true set :sneakers_default_hooks, true
...@@ -39,10 +42,10 @@ namespace :sneakers do ...@@ -39,10 +42,10 @@ namespace :sneakers do
desc 'Quiet sneakers (stop processing new tasks)' desc 'Quiet sneakers (stop processing new tasks)'
task :quiet do task :quiet do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do sneakers_switch_user(role) do
if test("[ -d #{current_path} ]") if test("[ -d #{current_path} ]")
each_process_with_index(true) do |pid_file, idx| sneakers_each_process_with_index(true) do |pid_file, idx|
if pid_file_exists?(pid_file) && process_exists?(pid_file) if pid_file_exists?(pid_file) && sneakers_process_exists?(pid_file)
quiet_sneakers(pid_file) quiet_sneakers(pid_file)
end end
end end
...@@ -54,10 +57,10 @@ namespace :sneakers do ...@@ -54,10 +57,10 @@ namespace :sneakers do
desc 'Stop sneakers' desc 'Stop sneakers'
task :stop do task :stop do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do sneakers_switch_user(role) do
if test("[ -d #{current_path} ]") if test("[ -d #{current_path} ]")
each_process_with_index(true) do |pid_file, idx| sneakers_each_process_with_index(true) do |pid_file, idx|
if pid_file_exists?(pid_file) && process_exists?(pid_file) if sneakers_pid_file_exists?(pid_file) && sneakers_process_exists?(pid_file)
stop_sneakers(pid_file) stop_sneakers(pid_file)
end end
end end
...@@ -69,9 +72,9 @@ namespace :sneakers do ...@@ -69,9 +72,9 @@ namespace :sneakers do
desc 'Start sneakers' desc 'Start sneakers'
task :start do task :start do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do sneakers_switch_user(role) do
each_process_with_index do |pid_file, idx| sneakers_each_process_with_index do |pid_file, idx|
unless pid_file_exists?(pid_file) && process_exists?(pid_file) unless sneakers_pid_file_exists?(pid_file) && sneakers_process_exists?(pid_file)
start_sneakers(pid_file, idx) start_sneakers(pid_file, idx)
end end
end end
...@@ -91,9 +94,9 @@ namespace :sneakers do ...@@ -91,9 +94,9 @@ namespace :sneakers do
desc 'Rolling-restart sneakers' desc 'Rolling-restart sneakers'
task :rolling_restart do task :rolling_restart do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do sneakers_switch_user(role) do
each_process_with_index(true) do |pid_file, idx| sneakers_each_process_with_index(true) do |pid_file, idx|
if pid_file_exists?(pid_file) && process_exists?(pid_file) if sneakers_pid_file_exists?(pid_file) && sneakers_process_exists?(pid_file)
stop_sneakers(pid_file) stop_sneakers(pid_file)
end end
start_sneakers(pid_file, idx) start_sneakers(pid_file, idx)
...@@ -105,10 +108,10 @@ namespace :sneakers do ...@@ -105,10 +108,10 @@ namespace :sneakers do
# Delete any pid file not in use # Delete any pid file not in use
task :cleanup do task :cleanup do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do sneakers_switch_user(role) do
each_process_with_index do |pid_file, idx| sneakers_each_process_with_index do |pid_file, idx|
unless process_exists?(pid_file) unless sneakers_process_exists?(pid_file)
if pid_file_exists?(pid_file) if sneakers_pid_file_exists?(pid_file)
execute "rm #{pid_file}" execute "rm #{pid_file}"
end end
end end
...@@ -122,104 +125,13 @@ namespace :sneakers do ...@@ -122,104 +125,13 @@ namespace :sneakers do
task :respawn do task :respawn do
invoke 'sneakers:cleanup' invoke 'sneakers:cleanup'
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do sneakers_switch_user(role) do
each_process_with_index do |pid_file, idx| sneakers_each_process_with_index do |pid_file, idx|
unless pid_file_exists?(pid_file) unless sneakers_pid_file_exists?(pid_file)
start_sneakers(pid_file, idx) start_sneakers(pid_file, idx)
end end
end end
end end
end end
end end
def each_process_with_index(reverse = false, &block)
_pid_files = pid_files
_pid_files.reverse! if reverse
_pid_files.each_with_index do |pid_file, idx|
within release_path do
yield(pid_file, idx)
end
end
end
def pid_files
sneakers_roles = Array(fetch(:sneakers_roles))
sneakers_roles.select! { |role| host.roles.include?(role) }
sneakers_roles.flat_map do |role|
processes = fetch(:sneakers_processes)
if processes == 1
fetch(:sneakers_pid)
else
Array.new(processes) { |idx| fetch(:sneakers_pid).gsub(/\.pid$/, "-#{idx}.pid") }
end
end
end
def pid_file_exists?(pid_file)
test(*("[ -f #{pid_file} ]").split(' '))
end
def process_exists?(pid_file)
test(*("kill -0 $( cat #{pid_file} )").split(' '))
end
def quiet_sneakers(pid_file)
if fetch(:sneakers_use_signals) || fetch(:sneakers_run_config)
execute :kill, "-USR1 `cat #{pid_file}`"
else
begin
execute :bundle, :exec, :sneakersctl, 'quiet', "#{pid_file}"
rescue SSHKit::Command::Failed
# If gems are not installed eq(first deploy) and sneakers_default_hooks as active
warn 'sneakersctl not found (ignore if this is the first deploy)'
end
end
end
def stop_sneakers(pid_file)
if fetch(:sneakers_run_config) == true
execute :kill, "-SIGTERM `cat #{pid_file}`"
else
if fetch(:stop_sneakers_in_background, fetch(:sneakers_run_in_background))
if fetch(:sneakers_use_signals)
background :kill, "-TERM `cat #{pid_file}`"
else
background :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout)
end
else
execute :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout)
end
end
end
def start_sneakers(pid_file, idx = 0)
if fetch(:sneakers_run_config) == true
# Use sneakers configuration prebuilt in
raise "[ set :workers, ['worker1', 'workerN'] ] not configured properly, please configure the workers you wish to use" if fetch(:sneakers_workers).nil? or fetch(:sneakers_workers) == false or !fetch(:sneakers_workers).kind_of? Array
workers = fetch(:sneakers_workers).compact.join(',')
info "Starting the sneakers processes"
with rails_env: fetch(:sneakers_env), workers: workers do
rake 'sneakers:run'
end
end
end
def switch_user(role, &block)
user = sneakers_user(role)
if user == role.user
block.call
else
as user do
block.call
end
end
end
def sneakers_user(role)
properties = role.properties
properties.fetch(:sneakers_user) || fetch(:sneakers_user) || properties.fetch(:run_as) || role.user
end
end end
# Monit configuration for Sneakers # Monit configuration for Sneakers : <%= fetch(:application) %>
# Service name: <%= sneakers_service_name %> check process <%= sneakers_monit_service_name %>
#
check process <%= sneakers_service_name %>
with pidfile "<%= fetch(:sneakers_pid) %>" with pidfile "<%= fetch(:sneakers_pid) %>"
start program = "/usr/bin/sudo -iu <%= sneakers_user(@role) %> /bin/bash -c 'cd <%= current_path %> && RAILS_ENV=<%= fetch(:sneakers_env) %> WORKERS=<%= fetch(:sneakers_workers).join(',') %> <%= SSHKit.config.command_map[:rake] %> sneakers:run'" start program = "/usr/bin/sudo -iu <%= sneakers_user(@role) %> /bin/bash -c 'cd <%= current_path %> && RAILS_ENV=<%= fetch(:sneakers_env) %> WORKERS=<%= fetch(:sneakers_workers).join(',') %> <%= SSHKit.config.command_map[:rake] %> sneakers:run'"
stop program = "/usr/bin/sudo -iu <%= sneakers_user(@role) %> /bin/bash -c 'kill -SIGTERM `cat <%= fetch(:sneakers_pid) %>`'" stop program = "/usr/bin/sudo -iu <%= sneakers_user(@role) %> /bin/bash -c 'kill -SIGTERM `cat <%= fetch(:sneakers_pid) %>`'"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment