-
Notifications
You must be signed in to change notification settings - Fork 299
/
puma.rb
127 lines (108 loc) · 3.57 KB
/
puma.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require 'capistrano/bundler'
require 'capistrano/plugin'
module Capistrano
module PumaCommon
def puma_switch_user(role, &block)
user = puma_user(role)
if user == role.user
block.call
else
backend.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 service_unit_type
## Jruby don't support notify
return "simple" if RUBY_ENGINE == "jruby"
fetch(:puma_service_unit_type,
## Check if sd_notify is available in the bundle
Gem::Specification.find_all_by_name("sd_notify").any? ? "notify" : "simple")
end
def compiled_template_puma(from, role)
@role = 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__),
File.expand_path("../templates/#{from}.rb.erb", __FILE__)
].detect { |path| File.file?(path) }
erb = File.read(file)
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.6')
StringIO.new(ERB.new(erb, nil, '-').result(binding))
else
StringIO.new(ERB.new(erb, trim_mode: '-').result(binding))
end
end
def template_puma(from, to, role)
backend.upload! compiled_template_puma(from, role), to
end
PumaBind = Struct.new(:full_address, :kind, :address) do
def unix?
kind == :unix
end
def ssl?
kind == :ssl
end
def tcp
kind == :tcp || ssl?
end
def local
if unix?
self
else
PumaBind.new(
localize_address(full_address),
kind,
localize_address(address)
)
end
end
private
def localize_address(address)
address.gsub(/0\.0\.0\.0(.+)/, "127.0.0.1\\1")
end
end
def puma_binds
Array(fetch(:puma_bind)).map do |m|
etype, address = /(tcp|unix|ssl):\/{1,2}(.+)/.match(m).captures
PumaBind.new(m, etype.to_sym, address)
end
end
end
class Puma < Capistrano::Plugin
include PumaCommon
def set_defaults
set_if_empty :puma_role, :web
set_if_empty :puma_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
set_if_empty :puma_access_log, -> { File.join(shared_path, 'log', "puma.log") }
set_if_empty :puma_error_log, -> { File.join(shared_path, 'log', "puma.log") }
# Chruby, Rbenv and RVM integration
append :chruby_map_bins, 'puma', 'pumactl' if fetch(:chruby_map_bins)
append :rbenv_map_bins, 'puma', 'pumactl' if fetch(:rbenv_map_bins)
append :rvm_map_bins, 'puma', 'pumactl' if fetch(:rvm_map_bins)
# Bundler integration
append :bundle_bins, 'puma', 'pumactl'
end
end
end
require 'capistrano/puma/systemd'