Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v3.0.0 candidate #201

Merged
merged 4 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## Changelog
- 3.0.0:
- Require capistrano 3.7+
- Implement the plugin system
- don't fail if puma was already running
- Added :puma_daemonize option (default is false)

- 2.0.0:
- Require puma 3.4+
- Require Capistrano 3.5+
Expand Down
19 changes: 14 additions & 5 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
Abdelkader Boudih
André Arko
Ariel Zerahia
ayaya
Barack Obama
Bart de Water
Benjamin Kim
Bin Huang
Bryan Liles
Claudio Poli
Cyril Rohr
dfang
Eric
Fritz Lee
Hnat Kubov
Ivan Schneider
Expand All @@ -25,21 +27,28 @@ Konstantin Papkovskiy
Kyle Decot
Lisa Hagemann
Lonre Wang
marshall-lee
Lucas Alves
Marcos Chicote
Matias De Santi
Michael C. Beck
Molfar
msbrigna
Neil Bartley
Peter
Philippe Nénert
Ponomarev Nikolay
Rafael Goulart
RavWar
ruohan.chen
Ruslan
SHIMADA Koji
Sergey Ponomarev
Shane O'Grady
Simon Males
Steve Madere
Suhail Patel
Suraj Shirvankar
ayaya
dfang
marshall-lee
mizukmb
msbrigna
ruohan.chen
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013-2016 Abdelkader Boudih
Copyright (c) 2013-2017 Abdelkader Boudih

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ And then execute:
# Capfile

require 'capistrano/puma'
require 'capistrano/puma/workers' # if you want to control the workers (in cluster mode)
require 'capistrano/puma/jungle' # if you need the jungle tasks
require 'capistrano/puma/monit' # if you need the monit tasks
require 'capistrano/puma/nginx' # if you want to upload a nginx site template
install_plugin Capistrano::Puma
install_plugin Capistrano::Puma::Workers # if you want to control the workers (in cluster mode)
install_plugin Capistrano::Puma::Jungle # if you need the jungle tasks
install_plugin Capistrano::Puma::Monit # if you need the monit tasks
install_plugin Capistrano::Puma::Nginx # if you want to upload a nginx site template
```

### Config
Expand Down
4 changes: 2 additions & 2 deletions capistrano3-puma.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'capistrano/puma/version'

Gem::Specification.new do |spec|
spec.name = 'capistrano3-puma'
spec.version = Capistrano::Puma::VERSION
spec.version = Capistrano::PumaVERSION
spec.authors = ['Abdelkader Boudih']
spec.email = ['[email protected]']
spec.description = %q{Puma integration for Capistrano 3}
Expand All @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.files = `git ls-files`.split($/)
spec.require_paths = ['lib']

spec.add_dependency 'capistrano', '~> 3.5'
spec.add_dependency 'capistrano', '~> 3.7'
spec.add_dependency 'capistrano-bundler'
spec.add_dependency 'puma' , '~> 3.4'
end
113 changes: 112 additions & 1 deletion lib/capistrano/puma.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,113 @@
require 'capistrano/bundler'
load File.expand_path('../tasks/puma.rake', __FILE__)
require "capistrano/plugin"

module Capistrano
module PumaCommon
def puma_switch_user(role, &block)
user = puma_user(role)
if user == role.user
block.call
else
as user do
block.call
end
end
end

def puma_user(role)
properties = role.properties
properties.fetch(:puma_user) || # local property for puma only
fetch(:puma_user) ||
properties.fetch(:run_as) || # global property across multiple capistrano gems
role.user
end

def puma_bind
Array(fetch(:puma_bind)).collect do |bind|
"bind '#{bind}'"
end.join("\n")
end


def template_puma(from, to, role)
file = [
"lib/capistrano/templates/#{from}-#{role.hostname}-#{fetch(:stage)}.rb",
"lib/capistrano/templates/#{from}-#{role.hostname}.rb",
"lib/capistrano/templates/#{from}-#{fetch(:stage)}.rb",
"lib/capistrano/templates/#{from}.rb.erb",
"lib/capistrano/templates/#{from}.rb",
"lib/capistrano/templates/#{from}.erb",
"config/deploy/templates/#{from}.rb.erb",
"config/deploy/templates/#{from}.rb",
"config/deploy/templates/#{from}.erb",
File.expand_path("../templates/#{from}.erb", __FILE__),
].detect { |path| File.file?(path) }
erb = File.read(file)
backend.upload! StringIO.new(ERB.new(erb, nil, '-').result(binding)), to
end
end

class Puma < Capistrano::Plugin
include PumaCommon

def define_tasks
eval_rakefile File.expand_path('../tasks/puma.rake', __FILE__)
end

def set_defaults
set_if_empty :puma_role, :app
set_if_empty :puma_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
# Configure "min" to be the minimum number of threads to use to answer
# requests and "max" the maximum.
set_if_empty :puma_threads, [0, 16]
set_if_empty :puma_workers, 0
set_if_empty :puma_rackup, -> { File.join(current_path, 'config.ru') }
set_if_empty :puma_state, -> { File.join(shared_path, 'tmp', 'pids', 'puma.state') }
set_if_empty :puma_pid, -> { File.join(shared_path, 'tmp', 'pids', 'puma.pid') }
set_if_empty :puma_bind, -> { File.join("unix://#{shared_path}", 'tmp', 'sockets', 'puma.sock') }
set_if_empty :puma_default_control_app, -> { File.join("unix://#{shared_path}", 'tmp', 'sockets', 'pumactl.sock') }
set_if_empty :puma_conf, -> { File.join(shared_path, 'puma.rb') }
set_if_empty :puma_access_log, -> { File.join(shared_path, 'log', 'puma_access.log') }
set_if_empty :puma_error_log, -> { File.join(shared_path, 'log', 'puma_error.log') }
set_if_empty :puma_init_active_record, false
set_if_empty :puma_preload_app, false
set_if_empty :puma_daemonize, false

# Chruby, Rbenv and RVM integration
append :chruby_map_bins, 'puma', 'pumactl'
append :rbenv_map_bins, 'puma', 'pumactl'
append :rvm_map_bins, 'puma', 'pumactl'

# Bundler integration
append :bundle_bins, 'puma', 'pumactl'
end

def register_hooks
after 'deploy:check', 'puma:check'
after 'deploy:finished', 'puma:smart_restart'
end

def puma_workers
fetch(:puma_workers, 0)
end

def puma_preload_app?
fetch(:puma_preload_app)
end

def puma_daemonize?
fetch(:puma_daemonize)
end

def puma_plugins
Array(fetch(:puma_plugins)).collect do |bind|
"plugin '#{bind}'"
end.join("\n")
end
end
end

require 'capistrano/puma/workers'
require 'capistrano/puma/monit'
require 'capistrano/puma/jungle'
require 'capistrano/puma/nginx'
32 changes: 30 additions & 2 deletions lib/capistrano/puma/jungle.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# Load jungle tasks
load File.expand_path('../../tasks/jungle.rake', __FILE__)
module Capistrano
class Puma::Jungle < Capistrano::Plugin
include PumaCommon

def set_defaults
set_if_empty :puma_jungle_conf, '/etc/puma.conf'
set_if_empty :puma_run_path, '/usr/local/bin/run-puma'
end

def define_tasks
eval_rakefile File.expand_path('../../tasks/jungle.rake', __FILE__)
end

private

def debian_install
template_puma 'puma-deb', "#{fetch(:tmp_dir)}/puma", @role
execute "chmod +x #{fetch(:tmp_dir)}/puma"
sudo "mv #{fetch(:tmp_dir)}/puma /etc/init.d/puma"
sudo 'update-rc.d -f puma defaults'
end

def rhel_install
template_puma 'puma-rpm', "#{fetch(:tmp_dir)}/puma", @role
execute "chmod +x #{fetch(:tmp_dir)}/puma"
sudo "mv #{fetch(:tmp_dir)}/puma /etc/init.d/puma"
sudo 'chkconfig --add puma'
end
end
end
32 changes: 30 additions & 2 deletions lib/capistrano/puma/monit.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# Load monit tasks
load File.expand_path('../../tasks/monit.rake', __FILE__)
module Capistrano
class Puma::Monit < Capistrano::Plugin
def register_hooks
before 'deploy:updating', 'puma:monit:unmonitor'
after 'deploy:published', 'puma:monit:monitor'
end

def define_tasks
eval_rakefile File.expand_path('../../tasks/monit.rake', __FILE__)
end

def set_defaults
set_if_empty :puma_monit_conf_dir, -> { "/etc/monit/conf.d/#{puma_monit_service_name}.conf" }
set_if_empty :puma_monit_use_sudo, true
set_if_empty :puma_monit_bin, '/usr/bin/monit'
end

def puma_monit_service_name
fetch(:puma_monit_service_name, "puma_#{fetch(:application)}_#{fetch(:stage)}")
end

def sudo_if_needed(command)
if fetch(:puma_monit_use_sudo)
sudo command
else
execute command
end
end
end
end
21 changes: 20 additions & 1 deletion lib/capistrano/puma/nginx.rb
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
load File.expand_path('../../tasks/nginx.rake', __FILE__)
module Capistrano
class Puma::Nginx < Capistrano::Plugin
include PumaCommon
def set_defaults
# Nginx and puma configuration
set_if_empty :nginx_config_name, "#{fetch(:application)}_#{fetch(:stage)}"
set_if_empty :nginx_sites_available_path, '/etc/nginx/sites-available'
set_if_empty :nginx_sites_enabled_path, '/etc/nginx/sites-enabled'
set_if_empty :nginx_server_name, "localhost #{fetch(:application)}.local"
set_if_empty :nginx_flags, 'fail_timeout=0'
set_if_empty :nginx_http_flags, fetch(:nginx_flags)
set_if_empty :nginx_socket_flags, fetch(:nginx_flags)
set_if_empty :nginx_use_ssl, false
end

def define_tasks
eval_rakefile File.expand_path('../../tasks/nginx.rake', __FILE__)
end
end
end
4 changes: 1 addition & 3 deletions lib/capistrano/puma/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module Capistrano
module Puma
VERSION = '2.0.0'
end
PumaVERSION = '3.0.0'
end
9 changes: 7 additions & 2 deletions lib/capistrano/puma/workers.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# Load monit tasks
load File.expand_path('../../tasks/workers.rake', __FILE__)
module Capistrano
class Puma::Workers < Capistrano::Plugin
def define_tasks
eval_rakefile File.expand_path('../../tasks/workers.rake', __FILE__)
end
end
end
33 changes: 4 additions & 29 deletions lib/capistrano/tasks/jungle.rake
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
namespace :load do
task :defaults do
set :puma_jungle_conf, '/etc/puma.conf'
set :puma_run_path, '/usr/local/bin/run-puma'
end
end

git_plugin = self

namespace :puma do
namespace :jungle do

desc 'Install Puma jungle'
task :install do
on roles(fetch(:puma_role)) do |role|
@role = role
template_puma 'run-puma', "#{fetch(:tmp_dir)}/run-puma", role
git_plugin.template_puma 'run-puma', "#{fetch(:tmp_dir)}/run-puma", role
execute "chmod +x #{fetch(:tmp_dir)}/run-puma"
sudo "mv #{fetch(:tmp_dir)}/run-puma #{fetch(:puma_run_path)}"
if test '[ -f /etc/redhat-release ]'
#RHEL flavor OS
rhel_install
git_plugin.rhel_install
elsif test '[ -f /etc/lsb-release ]'
#Debian flavor OS
debian_install
git_plugin.debian_install
else
#Some other OS
error 'This task is not supported for your OS'
Expand All @@ -30,23 +23,6 @@ namespace :puma do
end
end


def debian_install
template_puma 'puma-deb', "#{fetch(:tmp_dir)}/puma", @role
execute "chmod +x #{fetch(:tmp_dir)}/puma"
sudo "mv #{fetch(:tmp_dir)}/puma /etc/init.d/puma"
sudo 'update-rc.d -f puma defaults'

end

def rhel_install
template_puma 'puma-rpm', "#{fetch(:tmp_dir)}/puma" , @role
execute "chmod +x #{fetch(:tmp_dir)}/puma"
sudo "mv #{fetch(:tmp_dir)}/puma /etc/init.d/puma"
sudo 'chkconfig --add puma'
end


desc 'Setup Puma config and install jungle script'
task :setup do
invoke 'puma:config'
Expand Down Expand Up @@ -76,6 +52,5 @@ namespace :puma do
end
end
end

end
end
Loading