forked from benhoskings/babushka-deps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx.rb
191 lines (181 loc) · 6.95 KB
/
nginx.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
meta :nginx do
accepts_list_for :source
accepts_list_for :extra_source
def nginx_bin
var(:nginx_prefix) / 'sbin/nginx'
end
def nginx_conf
var(:nginx_prefix) / 'conf/nginx.conf'
end
def nginx_cert_path
var(:nginx_prefix) / 'conf/certs'
end
def nginx_conf_for domain, ext
var(:nginx_prefix) / "conf/vhosts/#{domain}.#{ext}"
end
def nginx_conf_link_for domain
var(:nginx_prefix) / "conf/vhosts/on/#{domain}.conf"
end
def passenger_root
Babushka::GemHelper.gem_path_for('passenger')
end
def nginx_running?
shell "netstat -an | grep -E '^tcp.*[.:]80 +.*LISTEN'"
end
def restart_nginx
if nginx_running?
log_shell "Restarting nginx", "#{nginx_bin} -s reload", :sudo => true
end
end
end
dep 'vhost enabled.nginx' do
requires 'vhost configured.nginx'
met? { nginx_conf_link_for(var(:domain)).exists? }
meet { sudo "ln -sf '#{nginx_conf_for(var(:domain), 'conf')}' '#{nginx_conf_link_for(var(:domain))}'" }
after { restart_nginx }
end
dep 'vhost configured.nginx' do
define_var :www_aliases, :default => L{
"#{var :domain} #{var :extra_domains}".split(' ').compact.map(&:strip).reject {|d|
d.starts_with? '*.'
}.reject {|d|
d.starts_with? 'www.'
}.map {|d|
"www.#{d}"
}.join(' ')
}
requires 'webserver configured.nginx'
define_var :vhost_type, :default => 'passenger', :choices => %w[passenger proxy static]
define_var :document_root, :default => L{ '/srv/http' / var(:domain) }
met? { nginx_conf_for(var(:domain), 'conf').exists? }
meet {
render_erb "nginx/#{var :vhost_type}_vhost.conf.erb", :to => nginx_conf_for(var(:domain), 'conf'), :sudo => true
render_erb "nginx/#{var :vhost_type}_vhost.common.erb", :to => nginx_conf_for(var(:domain), 'common'), :sudo => true, :optional => true
}
after { restart_nginx if nginx_conf_link_for(var(:domain)).exists? }
end
dep 'self signed cert.nginx' do
requires 'webserver installed.src'
met? { %w[key csr crt].all? {|ext| (nginx_cert_path / "#{var :domain}.#{ext}").exists? } }
meet {
cd nginx_cert_path, :create => "700", :sudo => true do
log_shell("generating private key", "openssl genrsa -out #{var :domain}.key 2048", :sudo => true) and
log_shell("generating certificate", "openssl req -new -key #{var :domain}.key -out #{var :domain}.csr",
:sudo => true, :input => [
var(:country, :default => 'AU'),
var(:state),
var(:city, :default => ''),
var(:organisation),
var(:organisational_unit, :default => ''),
var(:domain),
var(:email),
'', # password
'', # optional company name
'' # done
].join("\n")
) and
log_shell("signing certificate with key", "openssl x509 -req -days 365 -in #{var :domain}.csr -signkey #{var :domain}.key -out #{var :domain}.crt", :sudo => true)
end
}
end
dep 'webserver running.nginx' do
requires 'webserver configured.nginx', 'webserver startup script.nginx'
met? {
nginx_running?.tap {|result|
log "There is #{result ? 'something' : 'nothing'} listening on #{result ? result.scan(/[0-9.*]+[.:]80/).first : 'port 80'}"
}
}
meet :on => :linux do
sudo '/etc/init.d/nginx start'
end
meet :on => :osx do
log_error "launchctl should have already started nginx. Check /var/log/system.log for errors."
end
end
dep 'webserver startup script.nginx' do
requires 'webserver installed.src'
on :linux do
requires 'rcconf.managed'
met? { shell("rcconf --list").val_for('nginx') == 'on' }
meet {
render_erb 'nginx/nginx.init.d.erb', :to => '/etc/init.d/nginx', :perms => '755', :sudo => true
sudo 'update-rc.d nginx defaults'
}
end
on :osx do
met? { !sudo('launchctl list').split("\n").grep(/org\.nginx/).empty? }
meet {
render_erb 'nginx/nginx.launchd.erb', :to => '/Library/LaunchDaemons/org.nginx.plist', :sudo => true
sudo 'launchctl load -w /Library/LaunchDaemons/org.nginx.plist'
}
end
end
dep 'webserver configured.nginx' do
requires 'webserver installed.src', 'www user and group'
define_var :nginx_prefix, :default => '/opt/nginx'
met? {
if babushka_config? nginx_conf
configured_root = nginx_conf.read.val_for('passenger_root')
(configured_root == passenger_root).tap {|result|
log_result "nginx is configured to use #{File.basename configured_root}", :result => result
}
end
}
meet {
render_erb 'nginx/nginx.conf.erb', :to => nginx_conf, :sudo => true
}
after {
sudo "mkdir -p #{var(:nginx_prefix) / 'conf/vhosts/on'}"
restart_nginx
}
end
dep 'passenger built' do
requires 'passenger.gem', 'build tools', 'curl.managed'
met? {
%W[
./agents/nginx/PassengerHelperAgent
./agents/PassengerLoggingAgent
./agents/PassengerWatchdog
./ext/common/libpassenger_common.a
./ext/ruby/#{Babushka::GemHelper.ruby_binary_slug}/passenger_native_support.#{Babushka::Base.host.library_ext}
].all? {|obj|
(Babushka::GemHelper.gem_path_for('passenger') / obj).exists?
}
}
meet {
cd Babushka::GemHelper.gem_path_for('passenger') do
shell "rake clean nginx", :sudo => Babushka::GemHelper.should_sudo?
end
}
end
dep 'webserver installed.src' do
requires 'passenger built', 'pcre.managed', 'libssl headers.managed', 'zlib headers.managed'
merge :versions, {:nginx => '0.7.67', :nginx_upload_module => '2.2.0'}
source "http://nginx.org/download/nginx-#{var(:versions)[:nginx]}.tar.gz"
extra_source "http://www.grid.net.ru/nginx/download/nginx_upload_module-#{var(:versions)[:nginx_upload_module]}.tar.gz"
configure_args "--with-pcre", "--with-http_ssl_module",
L{ "--add-module='#{Babushka::GemHelper.gem_path_for('passenger') / 'ext/nginx'}'" },
"--add-module='../../nginx_upload_module-#{var(:versions)[:nginx_upload_module]}/nginx_upload_module-#{var(:versions)[:nginx_upload_module]}'"
setup {
prefix var(:nginx_prefix, :default => '/opt/nginx')
provides var(:nginx_prefix) / 'sbin/nginx'
}
# The build process needs to write to passenger_root/ext/nginx.
configure { log_shell "configure", default_configure_command, :sudo => Babushka::GemHelper.should_sudo? }
build { log_shell "build", "make", :sudo => Babushka::GemHelper.should_sudo? }
install { log_shell "install", "make install", :sudo => true }
met? {
if !File.executable?(var(:nginx_prefix) / 'sbin/nginx')
unmet "nginx isn't installed"
else
installed_version = shell(var(:nginx_prefix) / 'sbin/nginx -V') {|shell| shell.stderr }.val_for('nginx version').sub('nginx/', '')
if installed_version != var(:versions)[:nginx]
unmet "an outdated version of nginx is installed (#{installed_version})"
elsif !shell(var(:nginx_prefix) / 'sbin/nginx -V') {|shell| shell.stderr }[Babushka::GemHelper.gem_path_for('passenger').to_s + '/']
unmet "nginx is installed, but built against the wrong passenger version"
else
met "nginx-#{installed_version} is installed"
end
end
}
end