Skip to content

Commit

Permalink
Merge pull request #21 from inventionlabsSydney/release/1.2.0
Browse files Browse the repository at this point in the history
Release/1.2.0
  • Loading branch information
Karl Kloppenborg authored Aug 10, 2018
2 parents 742c7ed + 0c31bb9 commit 448cd62
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 127 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
*.o
*.a
mkmf.log
builds/**
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ Configurable options, shown here with defaults:
```

## Contributors

- [Karl Kloppenborg](https://github.com/inventionlabsSydney)
- [Andrew Babichev](https://github.com/Tensho)
- [NaixSpirit](https://github.com/NaixSpirit)
- [hpetru](https://github.com/hpetru)
- [jhollinger](https://github.com/jhollinger)
- [redrick](https://github.com/redrick)

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion capistrano-sneakers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.0.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 "rake", "~> 10.0"
Expand Down
97 changes: 97 additions & 0 deletions lib/capistrano/sneakers/helper_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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

2 changes: 1 addition & 1 deletion lib/capistrano/sneakers/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Capistrano
module Sneakers
VERSION = "0.1.1"
VERSION = "0.2.0"
end
end
29 changes: 19 additions & 10 deletions lib/capistrano/tasks/monit.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace :load do
task :defaults do
set :monit_bin, '/usr/bin/monit'
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_templates_path, 'config/deploy/templates'
end
Expand All @@ -20,7 +20,7 @@ namespace :sneakers do
namespace :monit do
task :add_default_hooks do
before 'deploy:updating', 'sneakers:monit:unmonitor'
after 'deploy:published', 'sneakers:monit:monitor'
after 'deploy:published', 'sneakers:monit:monitor'
end

desc 'Config Sneakers monit-service'
Expand All @@ -29,7 +29,7 @@ namespace :sneakers do
@role = 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 "#{fetch(:monit_bin)} reload"
Expand All @@ -39,40 +39,49 @@ namespace :sneakers do
desc 'Monitor Sneakers monit-service'
task :monitor 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

desc 'Unmonitor Sneakers monit-service'
task :unmonitor 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

desc 'Start Sneakers monit-service'
task :start 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

desc 'Stop Sneakers monit-service'
task :stop 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

desc 'Restart Sneakers monit-service'
task :restart 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

def sneakers_service_name
fetch(:sneakers_service_name, "sneakers_#{fetch(:application)}_#{fetch(:sneakers_env)}")
def sneakers_monit_service_name
fetch(:sneakers_monit_service_name, "sneakers_#{fetch(:application)}_#{fetch(:sneakers_env)}")
end

def sudo_if_needed(command)
Expand Down
Loading

0 comments on commit 448cd62

Please sign in to comment.