Commit 5945d4db by Karl Kloppenborg Committed by GitHub

Merge pull request #14 from Tensho/sneakers-monit-templates-path

Mimicry capistrano-sidekiq
parents 2a0b7e85 d0580808
......@@ -2,7 +2,10 @@
## [Unreleased]
- Add CHANGELOG @Tensho
## 0.1.0
- Mimicry capistrano-sidekiq @Tensho
- Add sneakers_monit_templates_path @Tensho
## 0.0.2
......
......@@ -6,36 +6,44 @@
Add this line to your application's Gemfile:
```ruby
gem 'capistrano-sneakers'
```
gem 'capistrano-sneakers', github: 'inventionlabsSydney/capistrano-sneakers'
And then execute:
$ bundle
Or install it yourself as:
$ gem install capistrano-sneakers
## Usage
## Usage
```ruby
# Capfile
require 'capistrano/sneakers'
# Include monit tasks
# require 'capistrano/sneakers/monit'
require 'capistrano/sneakers/monit' # optional, to require monit tasks
```
Configurable options, shown here with defaults:
```ruby
:sneakers_default_hooks => true
:sneakers_pid => File.join(shared_path, 'tmp', 'pids', 'sneakers.pid') # ensure this path exists in production before deploying
:sneakers_env => fetch(:rack_env, fetch(:rails_env, fetch(:stage)))
:sneakers_log => File.join(shared_path, 'log', 'sneakers.log')
:sneakers_roles => :app
:sneakers_processes => 1
# sneakers monit
:sneakers_monit_conf_dir => '/etc/monit/conf.d'
:sneakers_monit_use_sudo => true
:monit_bin => '/usr/bin/monit'
:sneakers_monit_templates_path => 'config/deploy/templates'
```
## Contributors
- [Andrew Babichev](https://github.com/Tensho)
- [NaixSpirit](https://github.com/NaixSpirit)
## Contributing
1. Fork it ( https://github.com/NaixSpirit/capistrano-sneakers/fork )
1. Fork it (https://github.com/inventionlabsSydney/capistrano-sneakers)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
......
......@@ -5,11 +5,11 @@ require 'capistrano/sneakers/version'
Gem::Specification.new do |spec|
spec.name = "capistrano-sneakers"
spec.version = Capistrano::Sneakers::VERSION
spec.authors = ["Spirit"]
spec.email = ["neverlandxy.naix@gmail.com"]
spec.summary = %q{Sneakers integration only for Capistrano3}
spec.description = %q{Sneakers integration only for Capistrano3}
spec.homepage = "https://github.com/NaixSpirit/capistrano-sneakers"
spec.authors = ["Karl Kloppenborg, Andrew Babichev, NaixSpirit"]
spec.email = ["karl@hyperconnect.com.au", "andrew.babichev@gmail.com", "neverlandxy.naix@gmail.com"]
spec.summary = %q{Sneakers integration for Capistrano}
spec.description = %q{Sneakers integration for Capistrano}
spec.homepage = "https://github.com/inventionlabsSydney/capistrano-sneakers"
spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0")
......
module Capistrano
module Sneakers
VERSION = "0.0.2"
VERSION = "0.1.0"
end
end
namespace :load do
task :defaults do
set :sneakers_monit_conf_dir, -> { "/etc/monit/conf.d/#{sneakers_monit_service_name}.conf" }
set :monit_bin, '/usr/bin/monit'
set :sneakers_monit_default_hooks, true
set :sneakers_monit_conf_dir, -> { '/etc/monit/conf.d' }
set :sneakers_monit_use_sudo, true
set :sneakers_monit_bin, '/usr/bin/monit'
set :sneakers_monit_templates_path, 'config/deploy/templates'
end
end
namespace :deploy do
before :starting, :check_sidekiq_monit_hooks do
if fetch(:sneakers_default_hooks) && fetch(:sneakers_monit_default_hooks)
invoke 'sneakers:monit:add_default_hooks'
end
end
end
namespace :sneakers do
namespace :monit do
desc 'Add Sneakers to monit'
task :add_default_hooks do
before 'deploy:updating', 'sneakers:monit:unmonitor'
after 'deploy:published', 'sneakers:monit:monitor'
end
desc 'Config Sneakers monit-service'
task :config do
on roles(fetch(:sneakers_role)) do |role|
on roles(fetch(:sneakers_roles)) do |role|
@role = role
template_sneakers 'sneakers_monit.conf', "#{fetch(:tmp_dir)}/monit.conf", @role
sudo_if_needed "mv #{fetch(:tmp_dir)}/monit.conf #{fetch(:sneakers_monit_conf_dir)}"
sudo_if_needed "#{fetch(:sneakers_monit_bin)} reload"
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"
sudo_if_needed mv_command
sudo_if_needed "#{fetch(:monit_bin)} reload"
end
end
desc 'Enable Sneakers monit'
desc 'Monitor Sneakers monit-service'
task :monitor do
on roles(fetch(:sneakers_role)) do
sudo_if_needed "#{fetch(:sneakers_monit_bin)} monitor #{sneakers_monit_service_name}"
on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} monitor #{sneakers_service_name}"
end
end
desc 'Disable Sneakers monit'
desc 'Unmonitor Sneakers monit-service'
task :unmonitor do
on roles(fetch(:sneakers_role)) do
sudo_if_needed "#{fetch(:sneakers_monit_bin)} unmonitor #{sneakers_monit_service_name}"
on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} unmonitor #{sneakers_service_name}"
end
end
desc 'Start Sneakers through monit'
desc 'Start Sneakers monit-service'
task :start do
on roles(fetch(:sneakers_role)) do
sudo_if_needed "#{fetch(:sneakers_monit_bin)} start #{sneakers_monit_service_name}"
on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} start #{sneakers_service_name}"
end
end
desc 'Stop Sneakers through monit'
desc 'Stop Sneakers monit-service'
task :stop do
on roles(fetch(:sneakers_role)) do
sudo_if_needed "#{fetch(:sneakers_monit_bin)} stop #{sneakers_monit_service_name}"
on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} stop #{sneakers_service_name}"
end
end
desc 'Restart Sneakers through monit'
desc 'Restart Sneakers monit-service'
task :restart do
on roles(fetch(:sneakers_role)) do
sudo_if_needed "#{fetch(:sneakers_monit_bin)} restart #{sneakers_monit_service_name}"
on roles(fetch(:sneakers_roles)) do
sudo_if_needed "#{fetch(:monit_bin)} restart #{sneakers_service_name}"
end
end
before 'deploy:updating', 'sneakers:monit:unmonitor'
after 'deploy:published', 'sneakers:monit:monitor'
def sneakers_monit_service_name
fetch(:sneakers_monit_service_name, "sneakers_#{fetch(:application)}_#{fetch(:stage)}")
def sneakers_service_name
fetch(:sneakers_service_name, "sneakers_#{fetch(:application)}_#{fetch(:sneakers_env)}")
end
def sudo_if_needed(command)
fetch(:sneakers_monit_use_sudo) ? sudo(command) : execute(command)
end
def template_sneakers(from, to, role)
[
File.join('lib', 'capistrano', 'templates', "#{from}-#{role.hostname}-#{fetch(:stage)}.rb"),
File.join('lib', 'capistrano', 'templates', "#{from}-#{role.hostname}.rb"),
File.join('lib', 'capistrano', 'templates', "#{from}-#{fetch(:stage)}.rb"),
File.join('lib', 'capistrano', 'templates', "#{from}.rb.erb"),
File.join('lib', 'capistrano', 'templates', "#{from}.rb"),
File.join('lib', 'capistrano', 'templates', "#{from}.erb"),
File.expand_path("../../templates/#{from}.rb.erb", __FILE__),
File.expand_path("../../templates/#{from}.erb", __FILE__)
].each do |path|
if File.file?(path)
erb = File.read(path)
upload! StringIO.new(ERB.new(erb).result(binding)), to
break
end
end
def upload_sneakers_template(from, to, role)
template = sneakers_template(from, role)
upload!(StringIO.new(ERB.new(template).result(binding)), to)
end
def sneakers_template(name, role)
local_template_directory = fetch(:sneakers_monit_templates_path)
search_paths = [
"#{name}-#{role.hostname}-#{fetch(:stage)}.erb",
"#{name}-#{role.hostname}.erb",
"#{name}-#{fetch(:stage)}.erb",
"#{name}.erb"
].map { |filename| File.join(local_template_directory, filename) }
global_search_path = File.expand_path(
File.join(*%w[.. .. .. generators capistrano sneakers monit templates], "#{name}.conf.erb"),
__FILE__
)
search_paths << global_search_path
template_path = search_paths.detect { |path| File.file?(path) }
File.read(template_path)
end
end
end
require 'rails/generators/base'
module Capistrano
module Sneakers
module Monit
module Generators
class TemplateGenerator < Rails::Generators::Base
namespace "capistrano:sneakers:monit:template"
desc "Create local monitrc.erb, and erb files for monitored processes for customization"
source_root File.expand_path('../templates', __FILE__)
argument :templates_path, type: :string,
default: "config/deploy/templates",
banner: "path to templates"
def copy_template
copy_file "sneakers_monit.conf.erb", "#{templates_path}/sidekiq_monit.erb"
end
end
end
end
end
end
# Monit configuration for Sneakers
# Service name: <%= sneakers_monit_service_name %>
# Service name: <%= sneakers_service_name %>
#
check process <%= sneakers_monit_service_name %>
check process <%= sneakers_service_name %>
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'"
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