This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nginx.rb
186 lines (144 loc) · 4.79 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
meta :nginx do
accepts_list_for :source
def nginx_prefix
"/etc/nginx".p
end
def nginx_conf
nginx_prefix / "nginx.conf"
end
def status_conf
nginx_prefix / "conf.d/status.conf"
end
def vhost_conf
nginx_prefix / "sites-available/#{domain}.conf"
end
def vhost_common
nginx_prefix / "sites-available/#{domain}.common"
end
def vhost_link
nginx_prefix / "sites-enabled/#{domain}.conf"
end
def upstream_name
"#{domain}.upstream"
end
def cert_name
# Grab the TLD from the domain (e.g. dw.theconversation.com ->
# theconversation.com).
cert_domain = domain.to_s
.match(/([^.]+)\.([^.]+)$/).to_s
.gsub('.', '_')
"STAR_#{cert_domain}"
end
end
dep "vhost enabled.nginx", :app_name, :env, :domain, :path, :enable_https do
requires "vhost configured.nginx".with(app_name, env, domain, path, enable_https)
met? { vhost_link.exists? }
meet do
sudo "ln -sf '#{vhost_conf}' '#{vhost_link}'"
end
after { Util.reload_service('nginx') }
end
dep "proxy vhost enabled.nginx", :app_name, :domain, :enable_https, :proxy_port do
requires "proxy vhost configured.nginx".with(app_name, domain, enable_https, proxy_port)
met? { vhost_link.exists? }
meet do
sudo "ln -sf '#{vhost_conf}' '#{vhost_link}'"
end
after { Util.reload_service('nginx') }
end
dep "vhost configured.nginx", :app_name, :env, :domain, :enable_https, :path do
env.default!("production")
enable_https.default!("yes")
path.default("~#{domain}/current".p) if shell?("id", domain)
def application_socket
if has_unicorn_config?
path / "tmp/sockets/unicorn.socket"
elsif has_puma_config?
path / "tmp/sockets/puma.socket"
end
end
def has_unicorn_config?
"#{path}/config/unicorn.rb".p.exists?
end
def has_puma_config?
"#{path}/config/puma.rb".p.exists?
end
def up_to_date?(source_name, dest)
source = dependency.load_path.parent / source_name
if !source.p.exists?
true # If the source config doesn't exist, this is optional (i.e. a .common conf).
else
Babushka::Renderable.new(dest).from?(source) && Babushka::Renderable.new(dest).clean?
end
end
requires "configured.nginx"
met? do
up_to_date?("nginx/#{app_name}_vhost.conf.erb", vhost_conf) &&
up_to_date?("nginx/#{app_name}_vhost.common.erb", vhost_common)
end
meet do
render_erb "nginx/#{app_name}_vhost.conf.erb", to: vhost_conf, sudo: true
render_erb "nginx/#{app_name}_vhost.common.erb", to: vhost_common, sudo: true if (dependency.load_path.parent / "nginx/#{app_name}_vhost.common.erb").p.exists?
end
end
dep "proxy vhost configured.nginx", :app_name, :domain, :enable_https, :proxy_port do
enable_https.default!("yes")
def up_to_date?(source_name, dest)
source = dependency.load_path.parent / source_name
if !source.p.exists?
true # If the source config doesn't exist, this is optional (i.e. a .common conf).
else
Babushka::Renderable.new(dest).from?(source) && Babushka::Renderable.new(dest).clean?
end
end
requires "configured.nginx"
met? do
up_to_date?("nginx/#{app_name}_vhost.conf.erb", vhost_conf) &&
up_to_date?("nginx/#{app_name}_vhost.common.erb", vhost_common)
end
meet do
render_erb "nginx/#{app_name}_vhost.conf.erb", to: vhost_conf, sudo: true
render_erb "nginx/#{app_name}_vhost.common.erb", to: vhost_common, sudo: true if (dependency.load_path.parent / "nginx/#{app_name}_vhost.common.erb").p.exists?
end
end
dep "stats endpoint configured.nginx" do
def up_to_date?(source_name, dest)
source = dependency.load_path.parent / source_name
if !source.p.exists?
true # If the source config doesn't exist, this is optional (i.e. a .common conf).
else
Babushka::Renderable.new(dest).from?(source) && Babushka::Renderable.new(dest).clean?
end
end
requires "configured.nginx"
met? do
up_to_date?("nginx/status.conf.erb", status_conf)
end
meet do
render_erb "nginx/status.conf.erb", to: status_conf, sudo: true
end
end
dep "http basic logins.nginx", :domain, :username, :pass do
met? { shell("curl -I -u #{username}:#{pass} #{domain}").val_for("HTTP/1.1")[/^[25]0\d\b/] }
meet { (nginx_prefix / "conf/htpasswd").append("#{username}:#{pass.to_s.crypt(pass)}") }
after { Util.reload_service('nginx') }
end
dep "running.nginx" do
requires "configured.nginx"
met? do
Util.service_running?('nginx').tap do |result|
log "There is #{result ? 'something' : 'nothing'} listening on port 80."
end
end
meet { shell "systemctl start nginx" }
end
dep "configured.nginx" do
requires "nginx.bin"
met? do
Babushka::Renderable.new(nginx_conf).from?(dependency.load_path.parent / "nginx/nginx.conf.erb")
end
meet do
render_erb "nginx/nginx.conf.erb", to: nginx_conf, sudo: true
end
end
dep "nginx.bin"