vhost_generator 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/foreman-export-vhost +8 -8
- data/features/foreman-export-vhost.feature +9 -4
- data/features/output-apache.feature +213 -0
- data/features/output-nginx.feature +92 -2
- data/features/version.feature +1 -1
- data/lib/vhost_generator/apache_generator.rb +98 -0
- data/lib/vhost_generator/application.rb +1 -1
- data/lib/vhost_generator/nginx_generator.rb +12 -2
- data/lib/vhost_generator/version.rb +1 -1
- data/lib/vhost_generator/vhost_configuration.rb +3 -1
- data/spec/apache_generator_spec.rb +73 -0
- data/spec/nginx_generator_spec.rb +28 -9
- metadata +9 -4
data/bin/foreman-export-vhost
CHANGED
@@ -221,7 +221,13 @@ class ForemanExportApplication
|
|
221
221
|
end
|
222
222
|
|
223
223
|
def service
|
224
|
-
config.generator
|
224
|
+
if config.generator == 'nginx'
|
225
|
+
'nginx'
|
226
|
+
elsif config.generator == 'apache'
|
227
|
+
'apache2'
|
228
|
+
else
|
229
|
+
raise RuntimeError, "Can't guess service for generator=#{config.generator}"
|
230
|
+
end
|
225
231
|
end
|
226
232
|
|
227
233
|
def target
|
@@ -229,13 +235,7 @@ class ForemanExportApplication
|
|
229
235
|
end
|
230
236
|
|
231
237
|
def vhost_dir
|
232
|
-
|
233
|
-
'/etc/nginx/sites-enabled'
|
234
|
-
elsif config.generator == 'apache'
|
235
|
-
'/etc/apache2/sites-enabled'
|
236
|
-
else
|
237
|
-
raise RuntimeError, "Can't guess vhost_dir for generator=#{config.generator}"
|
238
|
-
end
|
238
|
+
"/etc/#{service}/sites-enabled"
|
239
239
|
end
|
240
240
|
|
241
241
|
def vhost_config
|
@@ -34,20 +34,20 @@ Feature: Wrap "foreman export"
|
|
34
34
|
-h, -H, --help Display this help message.
|
35
35
|
"""
|
36
36
|
|
37
|
-
Scenario: display source that will be executed
|
37
|
+
Scenario Outline: display source that will be executed with generator <generator>
|
38
38
|
Given a file named "Procfile" with:
|
39
39
|
"""
|
40
40
|
clock: ....
|
41
41
|
web: bundle exec webserver -p $PORT
|
42
42
|
"""
|
43
|
-
When I run `bundle exec foreman-export-vhost upstart /etc/init -f Procfile -a MYAPP -u MYUSER -p 6000 -c clock=1,web=2 -L 80,81 -S localhost,myapp.com -K web -G
|
43
|
+
When I run `bundle exec foreman-export-vhost upstart /etc/init -f Procfile -a MYAPP -u MYUSER -p 6000 -c clock=1,web=2 -L 80,81 -S localhost,myapp.com -K web -G <generator> -O assets_expire_in=15d -N -R`
|
44
44
|
Then the output should match:
|
45
45
|
"""
|
46
|
-
bundle exec vhost-generator -a MYAPP -f /.*/public -l 80,81 -s localhost,myapp.com -p 6100,6101 -g
|
46
|
+
bundle exec vhost-generator -a MYAPP -f /.*/public -l 80,81 -s localhost,myapp.com -p 6100,6101 -g <generator> -o assets_expire_in\\=15d \| sudo tee <config_dir>/vhost-MYAPP.conf
|
47
47
|
"""
|
48
48
|
And the output should contain:
|
49
49
|
"""
|
50
|
-
sudo service
|
50
|
+
sudo service <service> reload
|
51
51
|
sudo service MYAPP stop >/dev/null 2>&1 || true
|
52
52
|
sudo rm -rf /etc/init/MYAPP-*.conf
|
53
53
|
"""
|
@@ -61,3 +61,8 @@ Feature: Wrap "foreman export"
|
|
61
61
|
echo "Finished, now open your browser and see if everything works."
|
62
62
|
# Now, try to run this script again without the --dry-run switch!
|
63
63
|
"""
|
64
|
+
|
65
|
+
Scenarios:
|
66
|
+
| generator | service | config_dir |
|
67
|
+
| nginx | nginx | /etc/nginx/sites-enabled |
|
68
|
+
| apache | apache2 | /etc/apache2/sites-enabled |
|
@@ -0,0 +1,213 @@
|
|
1
|
+
Feature: Output apache configuration file
|
2
|
+
|
3
|
+
In order to run my application as an apache virtualhost
|
4
|
+
As a user of the library
|
5
|
+
I want to output a apache virtualhost configuration.
|
6
|
+
|
7
|
+
Scenario: using command-line options, multiple upstreams
|
8
|
+
When I run `bundle exec vhost-generator -g apache -o assets_expire_in=15d -a testapp -f html -l 80,81 -s localhost,my.server -p 5000,5001,5002 -r /myapp`
|
9
|
+
Then the output should match /FILE GENERATED BY.*EDIT AT YOUR OWN RISK/
|
10
|
+
And the output should contain:
|
11
|
+
"""
|
12
|
+
<VirtualHost *:80 *:81>
|
13
|
+
"""
|
14
|
+
And the output should contain:
|
15
|
+
"""
|
16
|
+
ServerName localhost
|
17
|
+
ServerAlias my.server
|
18
|
+
"""
|
19
|
+
And the output should match /DocumentRoot.*html/
|
20
|
+
And the output should contain:
|
21
|
+
"""
|
22
|
+
AllowEncodedSlashes On
|
23
|
+
"""
|
24
|
+
And the output should contain:
|
25
|
+
"""
|
26
|
+
<Proxy balancer://testapp>
|
27
|
+
BalancerMember http://localhost:5000
|
28
|
+
BalancerMember http://localhost:5001
|
29
|
+
BalancerMember http://localhost:5002
|
30
|
+
</Proxy>
|
31
|
+
"""
|
32
|
+
And the output should contain:
|
33
|
+
"""
|
34
|
+
# Redirect all non-static requests to instances
|
35
|
+
RewriteEngine On
|
36
|
+
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
|
37
|
+
RewriteRule ^/(.*)$ balancer://testapp%{REQUEST_URI} [P,QSA,L]
|
38
|
+
"""
|
39
|
+
And the output should contain:
|
40
|
+
"""
|
41
|
+
<Location /myapp/assets>
|
42
|
+
ExpiresActive On
|
43
|
+
Header unset ETag
|
44
|
+
FileETag None
|
45
|
+
ExpiresDefault "access plus 15 days"
|
46
|
+
</Location>
|
47
|
+
"""
|
48
|
+
And the output should contain:
|
49
|
+
"""
|
50
|
+
ErrorDocument 500 /500.html
|
51
|
+
ErrorDocument 502 /500.html
|
52
|
+
ErrorDocument 503 /500.html
|
53
|
+
ErrorDocument 504 /500.html
|
54
|
+
LimitRequestBody 2147483647
|
55
|
+
KeepAliveTimeout 10
|
56
|
+
</VirtualHost>
|
57
|
+
"""
|
58
|
+
|
59
|
+
Scenario: using environment variables, multiple upstreams
|
60
|
+
When I set env variable "GENERATOR" to "apache"
|
61
|
+
And I set env variable "GENERATOR_OPTIONS" to "assets_expire_in=15d"
|
62
|
+
And I set env variable "APPLICATION" to "testapp"
|
63
|
+
And I set env variable "STATIC_FOLDER" to "html"
|
64
|
+
And I set env variable "SERVER_PORTS" to "80,81"
|
65
|
+
And I set env variable "SERVER_NAMES" to "localhost,my.server"
|
66
|
+
And I set env variable "INSTANCE_PORTS" to "5000,5001,5002"
|
67
|
+
And I set env variable "RAILS_RELATIVE_URL_ROOT" to "/myapp"
|
68
|
+
And I run `bundle exec vhost-generator`
|
69
|
+
Then the output should match /FILE GENERATED BY.*EDIT AT YOUR OWN RISK/
|
70
|
+
And the output should contain:
|
71
|
+
"""
|
72
|
+
<VirtualHost *:80 *:81>
|
73
|
+
"""
|
74
|
+
And the output should contain:
|
75
|
+
"""
|
76
|
+
ServerName localhost
|
77
|
+
ServerAlias my.server
|
78
|
+
"""
|
79
|
+
And the output should match /DocumentRoot.*html/
|
80
|
+
And the output should contain:
|
81
|
+
"""
|
82
|
+
AllowEncodedSlashes On
|
83
|
+
"""
|
84
|
+
And the output should contain:
|
85
|
+
"""
|
86
|
+
<Proxy balancer://testapp>
|
87
|
+
BalancerMember http://localhost:5000
|
88
|
+
BalancerMember http://localhost:5001
|
89
|
+
BalancerMember http://localhost:5002
|
90
|
+
</Proxy>
|
91
|
+
"""
|
92
|
+
And the output should contain:
|
93
|
+
"""
|
94
|
+
# Redirect all non-static requests to instances
|
95
|
+
RewriteEngine On
|
96
|
+
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
|
97
|
+
RewriteRule ^/(.*)$ balancer://testapp%{REQUEST_URI} [P,QSA,L]
|
98
|
+
"""
|
99
|
+
And the output should contain:
|
100
|
+
"""
|
101
|
+
<Location /myapp/assets>
|
102
|
+
ExpiresActive On
|
103
|
+
Header unset ETag
|
104
|
+
FileETag None
|
105
|
+
ExpiresDefault "access plus 15 days"
|
106
|
+
</Location>
|
107
|
+
"""
|
108
|
+
And the output should contain:
|
109
|
+
"""
|
110
|
+
ErrorDocument 500 /500.html
|
111
|
+
ErrorDocument 502 /500.html
|
112
|
+
ErrorDocument 503 /500.html
|
113
|
+
ErrorDocument 504 /500.html
|
114
|
+
LimitRequestBody 2147483647
|
115
|
+
KeepAliveTimeout 10
|
116
|
+
</VirtualHost>
|
117
|
+
"""
|
118
|
+
|
119
|
+
Scenario: using command-line options, single upstream
|
120
|
+
When I run `bundle exec vhost-generator -g apache -o assets_expire_in=15d -a testapp -f html -l 80,81 -s localhost,my.server -p 5000 -r /myapp`
|
121
|
+
Then the output should match /FILE GENERATED BY.*EDIT AT YOUR OWN RISK/
|
122
|
+
And the output should contain:
|
123
|
+
"""
|
124
|
+
<VirtualHost *:80 *:81>
|
125
|
+
"""
|
126
|
+
And the output should contain:
|
127
|
+
"""
|
128
|
+
ServerName localhost
|
129
|
+
ServerAlias my.server
|
130
|
+
"""
|
131
|
+
And the output should match /DocumentRoot.*html/
|
132
|
+
And the output should contain:
|
133
|
+
"""
|
134
|
+
AllowEncodedSlashes On
|
135
|
+
"""
|
136
|
+
And the output should contain:
|
137
|
+
"""
|
138
|
+
# Redirect all non-static requests to instances
|
139
|
+
RewriteEngine On
|
140
|
+
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
|
141
|
+
RewriteRule ^/(.*)$ http://localhost:5000%{REQUEST_URI} [P,QSA,L]
|
142
|
+
"""
|
143
|
+
And the output should contain:
|
144
|
+
"""
|
145
|
+
<Location /myapp/assets>
|
146
|
+
ExpiresActive On
|
147
|
+
Header unset ETag
|
148
|
+
FileETag None
|
149
|
+
ExpiresDefault "access plus 15 days"
|
150
|
+
</Location>
|
151
|
+
"""
|
152
|
+
And the output should contain:
|
153
|
+
"""
|
154
|
+
ErrorDocument 500 /500.html
|
155
|
+
ErrorDocument 502 /500.html
|
156
|
+
ErrorDocument 503 /500.html
|
157
|
+
ErrorDocument 504 /500.html
|
158
|
+
LimitRequestBody 2147483647
|
159
|
+
KeepAliveTimeout 10
|
160
|
+
</VirtualHost>
|
161
|
+
"""
|
162
|
+
|
163
|
+
Scenario: using environment variables, single upstream
|
164
|
+
When I set env variable "GENERATOR" to "apache"
|
165
|
+
And I set env variable "GENERATOR_OPTIONS" to "assets_expire_in=15d"
|
166
|
+
And I set env variable "APPLICATION" to "testapp"
|
167
|
+
And I set env variable "STATIC_FOLDER" to "html"
|
168
|
+
And I set env variable "SERVER_PORTS" to "80,81"
|
169
|
+
And I set env variable "SERVER_NAMES" to "localhost,my.server"
|
170
|
+
And I set env variable "INSTANCE_PORTS" to "5000"
|
171
|
+
And I set env variable "RAILS_RELATIVE_URL_ROOT" to "/myapp"
|
172
|
+
And I run `bundle exec vhost-generator`
|
173
|
+
Then the output should match /FILE GENERATED BY.*EDIT AT YOUR OWN RISK/
|
174
|
+
And the output should contain:
|
175
|
+
"""
|
176
|
+
<VirtualHost *:80 *:81>
|
177
|
+
"""
|
178
|
+
And the output should contain:
|
179
|
+
"""
|
180
|
+
ServerName localhost
|
181
|
+
ServerAlias my.server
|
182
|
+
"""
|
183
|
+
And the output should match /DocumentRoot.*html/
|
184
|
+
And the output should contain:
|
185
|
+
"""
|
186
|
+
AllowEncodedSlashes On
|
187
|
+
"""
|
188
|
+
And the output should contain:
|
189
|
+
"""
|
190
|
+
# Redirect all non-static requests to instances
|
191
|
+
RewriteEngine On
|
192
|
+
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
|
193
|
+
RewriteRule ^/(.*)$ http://localhost:5000%{REQUEST_URI} [P,QSA,L]
|
194
|
+
"""
|
195
|
+
And the output should contain:
|
196
|
+
"""
|
197
|
+
<Location /myapp/assets>
|
198
|
+
ExpiresActive On
|
199
|
+
Header unset ETag
|
200
|
+
FileETag None
|
201
|
+
ExpiresDefault "access plus 15 days"
|
202
|
+
</Location>
|
203
|
+
"""
|
204
|
+
And the output should contain:
|
205
|
+
"""
|
206
|
+
ErrorDocument 500 /500.html
|
207
|
+
ErrorDocument 502 /500.html
|
208
|
+
ErrorDocument 503 /500.html
|
209
|
+
ErrorDocument 504 /500.html
|
210
|
+
LimitRequestBody 2147483647
|
211
|
+
KeepAliveTimeout 10
|
212
|
+
</VirtualHost>
|
213
|
+
"""
|
@@ -4,7 +4,7 @@ Feature: Output nginx configuration file
|
|
4
4
|
As a user of the library
|
5
5
|
I want to output a nginx virtualhost configuration.
|
6
6
|
|
7
|
-
Scenario: using command-line options
|
7
|
+
Scenario: using command-line options, multiple upstreams
|
8
8
|
When I run `bundle exec vhost-generator -g nginx -o assets_expire_in=15d -a testapp -f html -l 80,81 -s localhost,my.server -p 5000,5001,5002 -r /myapp`
|
9
9
|
Then the output should match /FILE GENERATED BY.*EDIT AT YOUR OWN RISK/
|
10
10
|
And the output should contain:
|
@@ -53,7 +53,7 @@ Feature: Output nginx configuration file
|
|
53
53
|
}
|
54
54
|
"""
|
55
55
|
|
56
|
-
Scenario: using environment variables
|
56
|
+
Scenario: using environment variables, multiple upstreams
|
57
57
|
When I set env variable "GENERATOR" to "nginx"
|
58
58
|
And I set env variable "GENERATOR_OPTIONS" to "assets_expire_in=15d"
|
59
59
|
And I set env variable "APPLICATION" to "testapp"
|
@@ -109,3 +109,93 @@ Feature: Output nginx configuration file
|
|
109
109
|
keepalive_timeout 10;
|
110
110
|
}
|
111
111
|
"""
|
112
|
+
|
113
|
+
Scenario: using command-line options, single upstream
|
114
|
+
When I run `bundle exec vhost-generator -g nginx -o assets_expire_in=15d -a testapp -f html -l 80,81 -s localhost,my.server -p 5000 -r /myapp`
|
115
|
+
Then the output should match /FILE GENERATED BY.*EDIT AT YOUR OWN RISK/
|
116
|
+
And the output should contain:
|
117
|
+
"""
|
118
|
+
server {
|
119
|
+
listen 80;
|
120
|
+
listen 81;
|
121
|
+
"""
|
122
|
+
And the output should contain:
|
123
|
+
"""
|
124
|
+
server_name localhost, my.server;
|
125
|
+
"""
|
126
|
+
And the output should match /root.*html;/
|
127
|
+
And the output should contain:
|
128
|
+
"""
|
129
|
+
try_files $uri/index.html $uri @upstream;
|
130
|
+
location @upstream {
|
131
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
132
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
133
|
+
proxy_set_header Host $http_host;
|
134
|
+
proxy_redirect off;
|
135
|
+
proxy_pass http://localhost:5000;
|
136
|
+
}
|
137
|
+
"""
|
138
|
+
And the output should contain:
|
139
|
+
"""
|
140
|
+
location /myapp/assets {
|
141
|
+
gzip_static on; # to serve pre-gzipped version
|
142
|
+
expires 15d;
|
143
|
+
add_header Cache-Control public;
|
144
|
+
}
|
145
|
+
"""
|
146
|
+
And the output should contain:
|
147
|
+
"""
|
148
|
+
error_page 500 502 503 504 /500.html;
|
149
|
+
client_max_body_size 4G;
|
150
|
+
keepalive_timeout 10;
|
151
|
+
}
|
152
|
+
"""
|
153
|
+
|
154
|
+
Scenario: using environment variables, single upstream
|
155
|
+
When I set env variable "GENERATOR" to "nginx"
|
156
|
+
And I set env variable "GENERATOR_OPTIONS" to "assets_expire_in=15d"
|
157
|
+
And I set env variable "APPLICATION" to "testapp"
|
158
|
+
And I set env variable "STATIC_FOLDER" to "html"
|
159
|
+
And I set env variable "SERVER_PORTS" to "80,81"
|
160
|
+
And I set env variable "SERVER_NAMES" to "localhost,my.server"
|
161
|
+
And I set env variable "INSTANCE_PORTS" to "5000"
|
162
|
+
And I set env variable "RAILS_RELATIVE_URL_ROOT" to "/myapp"
|
163
|
+
And I run `bundle exec vhost-generator`
|
164
|
+
Then the output should match /FILE GENERATED BY.*EDIT AT YOUR OWN RISK/
|
165
|
+
And the output should contain:
|
166
|
+
"""
|
167
|
+
server {
|
168
|
+
listen 80;
|
169
|
+
listen 81;
|
170
|
+
"""
|
171
|
+
And the output should contain:
|
172
|
+
"""
|
173
|
+
server_name localhost, my.server;
|
174
|
+
"""
|
175
|
+
And the output should match /root.*html;/
|
176
|
+
And the output should contain:
|
177
|
+
"""
|
178
|
+
try_files $uri/index.html $uri @upstream;
|
179
|
+
location @upstream {
|
180
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
181
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
182
|
+
proxy_set_header Host $http_host;
|
183
|
+
proxy_redirect off;
|
184
|
+
proxy_pass http://localhost:5000;
|
185
|
+
}
|
186
|
+
"""
|
187
|
+
And the output should contain:
|
188
|
+
"""
|
189
|
+
location /myapp/assets {
|
190
|
+
gzip_static on; # to serve pre-gzipped version
|
191
|
+
expires 15d;
|
192
|
+
add_header Cache-Control public;
|
193
|
+
}
|
194
|
+
"""
|
195
|
+
And the output should contain:
|
196
|
+
"""
|
197
|
+
error_page 500 502 503 504 /500.html;
|
198
|
+
client_max_body_size 4G;
|
199
|
+
keepalive_timeout 10;
|
200
|
+
}
|
201
|
+
"""
|
data/features/version.feature
CHANGED
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
module VhostGenerator
|
5
|
+
|
6
|
+
# Apache VhostGenerator
|
7
|
+
#
|
8
|
+
class ApacheGenerator
|
9
|
+
attr_reader :cfg, :options
|
10
|
+
def initialize(cfg, options={})
|
11
|
+
@cfg = cfg
|
12
|
+
@options = OpenStruct.new(default_options.merge(options))
|
13
|
+
@options.upstream ||= cfg.application
|
14
|
+
@options.has_upstream = cfg.instance_ports.length > 1
|
15
|
+
@options.proxy_pass =
|
16
|
+
if @options.has_upstream
|
17
|
+
"balancer://#{@options.upstream}"
|
18
|
+
elsif cfg.instance_ports.length > 0
|
19
|
+
"http://localhost:#{cfg.instance_ports.first}"
|
20
|
+
else
|
21
|
+
raise ArgumentError, "Please specify at least 1 instance-port."
|
22
|
+
end
|
23
|
+
# by commodity, support same syntax as nginx: 15d, 2m, 1y
|
24
|
+
expires = {'d' => 'days', 'm' => 'months', 'y' => 'years'}
|
25
|
+
@options.assets_expire_in.gsub!(/\A(\d+)([dmy])\Z/) do |_|
|
26
|
+
[$~[1], expires.fetch($~[2])].join(' ') # s/15d/15 days/g
|
27
|
+
end
|
28
|
+
# by commodity, support same syntax as nginx: 2k, 2M, 2G
|
29
|
+
sizes = {'k' => 1024, 'M' => 1024**2, 'G' => 1024**3}
|
30
|
+
if @options.client_max_body_size =~ /\A(\d+)([kMG])\Z/
|
31
|
+
@options.client_max_body_size = Integer($~[1]) * sizes[$~[2]] - 1
|
32
|
+
else
|
33
|
+
@options.client_max_body_size = Integer(@options.client_max_body_size)
|
34
|
+
end
|
35
|
+
# apache does not support body sizes > 2G
|
36
|
+
if @options.client_max_body_size > (size_max = 2 * sizes['G'] - 1)
|
37
|
+
@options.client_max_body_size = size_max
|
38
|
+
end
|
39
|
+
@options.freeze
|
40
|
+
end
|
41
|
+
|
42
|
+
def render
|
43
|
+
template.result(binding)
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
def default_options
|
49
|
+
Hash[ 'client_max_body_size' => '2G', # max for apache
|
50
|
+
'keepalive_timeout' => '10',
|
51
|
+
'assets_expire_in' => '60d' ].freeze
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def template
|
57
|
+
@template ||= ERB.new <<EOF
|
58
|
+
#### FILE GENERATED BY `<%= String(cfg.cmdline) %>`, EDIT AT YOUR OWN RISK ####
|
59
|
+
#### Note: you may need to a2enmod the following modules: proxy_http, proxy_balancer, rewrite, headers, expires
|
60
|
+
|
61
|
+
<VirtualHost <%= cfg.server_ports.map{ |p| "*:\#{p}" }.join(' ') %>>
|
62
|
+
<% unless cfg.server_names.empty? %>ServerName <%= cfg.server_names.first %><% end %>
|
63
|
+
<% if cfg.server_names.length >= 2 %>ServerAlias <%=
|
64
|
+
cfg.server_names[1..-1].join(' ') %><% end %>
|
65
|
+
|
66
|
+
DocumentRoot <%= cfg.static_folder %>;
|
67
|
+
|
68
|
+
AllowEncodedSlashes On
|
69
|
+
|
70
|
+
<% if options.has_upstream %>
|
71
|
+
<Proxy balancer://<%= options.upstream %>>
|
72
|
+
<% cfg.instance_ports.each do |p| %> BalancerMember http://localhost:<%= p %>
|
73
|
+
<% end %></Proxy>
|
74
|
+
<% end %>
|
75
|
+
|
76
|
+
# Redirect all non-static requests to instances
|
77
|
+
RewriteEngine On
|
78
|
+
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
|
79
|
+
RewriteRule ^/(.*)$ <%= options.proxy_pass %>%{REQUEST_URI} [P,QSA,L]
|
80
|
+
|
81
|
+
<Location <%= cfg.relative_root %>assets>
|
82
|
+
ExpiresActive On
|
83
|
+
Header unset ETag
|
84
|
+
FileETag None
|
85
|
+
ExpiresDefault "access plus <%= options.assets_expire_in %>"
|
86
|
+
</Location>
|
87
|
+
|
88
|
+
ErrorDocument 500 /500.html
|
89
|
+
ErrorDocument 502 /500.html
|
90
|
+
ErrorDocument 503 /500.html
|
91
|
+
ErrorDocument 504 /500.html
|
92
|
+
LimitRequestBody <%= options.client_max_body_size %>
|
93
|
+
KeepAliveTimeout <%= options.keepalive_timeout %>
|
94
|
+
</VirtualHost>
|
95
|
+
EOF
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -131,7 +131,7 @@ module VhostGenerator
|
|
131
131
|
rescue SystemExit => ex
|
132
132
|
# Exit silently with current status
|
133
133
|
raise
|
134
|
-
rescue OptionParser::InvalidOption => ex
|
134
|
+
rescue OptionParser::InvalidOption, ArgumentError => ex
|
135
135
|
$stderr.puts ex.message
|
136
136
|
exit(false)
|
137
137
|
rescue Exception => ex
|
@@ -11,6 +11,15 @@ module VhostGenerator
|
|
11
11
|
@cfg = cfg
|
12
12
|
@options = OpenStruct.new(default_options.merge(options))
|
13
13
|
@options.upstream ||= cfg.application
|
14
|
+
@options.has_upstream = cfg.instance_ports.length > 1
|
15
|
+
@options.proxy_pass =
|
16
|
+
if @options.has_upstream
|
17
|
+
@options.upstream
|
18
|
+
elsif cfg.instance_ports.length > 0
|
19
|
+
"localhost:#{cfg.instance_ports.first}"
|
20
|
+
else
|
21
|
+
raise ArgumentError, "Please specify at least 1 instance-port."
|
22
|
+
end
|
14
23
|
@options.freeze
|
15
24
|
end
|
16
25
|
|
@@ -31,10 +40,11 @@ module VhostGenerator
|
|
31
40
|
@template ||= ERB.new <<EOF
|
32
41
|
#### FILE GENERATED BY `<%= String(cfg.cmdline) %>`, EDIT AT YOUR OWN RISK ####
|
33
42
|
|
43
|
+
<% if options.has_upstream %>
|
34
44
|
upstream <%= options.upstream %> {
|
35
45
|
<% cfg.instance_ports.each do |p| %> server localhost:<%= p %> fail_timeout=0;
|
36
46
|
<% end %>}
|
37
|
-
|
47
|
+
<% end %>
|
38
48
|
server {
|
39
49
|
<% cfg.server_ports.each do |p| %>listen <%= p %>;
|
40
50
|
<% end %>
|
@@ -49,7 +59,7 @@ server {
|
|
49
59
|
proxy_set_header X-Forwarded-Proto $scheme;
|
50
60
|
proxy_set_header Host $http_host;
|
51
61
|
proxy_redirect off;
|
52
|
-
proxy_pass http://<%= options.
|
62
|
+
proxy_pass http://<%= options.proxy_pass %>;
|
53
63
|
}
|
54
64
|
|
55
65
|
location <%= cfg.relative_root %>assets {
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'vhost_generator/nginx_generator'
|
2
|
+
require 'vhost_generator/apache_generator'
|
2
3
|
require 'shellwords'
|
3
4
|
|
4
5
|
module VhostGenerator
|
@@ -85,7 +86,8 @@ module VhostGenerator
|
|
85
86
|
attr_writer :registry
|
86
87
|
def registry
|
87
88
|
# XXX use a real registry to reduce coupling
|
88
|
-
@registry ||= {'nginx' => VhostGenerator::NginxGenerator
|
89
|
+
@registry ||= {'nginx' => VhostGenerator::NginxGenerator,
|
90
|
+
'apache' => VhostGenerator::ApacheGenerator}
|
89
91
|
end
|
90
92
|
|
91
93
|
def parse_word_list(s)
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'vhost_generator/apache_generator'
|
2
|
+
|
3
|
+
describe VhostGenerator::ApacheGenerator do
|
4
|
+
let(:config) { double('vhost config').as_null_object }
|
5
|
+
subject do
|
6
|
+
described_class.new(config, 'upstream' => 'myupstream')
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#render" do
|
10
|
+
let(:output) { subject.render }
|
11
|
+
|
12
|
+
it "includes the cmdline in a comment" do
|
13
|
+
config.stub(:cmdline).and_return('CMDLINE')
|
14
|
+
expect(output).to match(/^#### FILE GENERATED BY .*CMDLINE/)
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when multiple upstreams" do
|
18
|
+
before { config.stub(:instance_ports).and_return([1337, 1338]) }
|
19
|
+
|
20
|
+
it "declares the named upstream" do
|
21
|
+
expect(output).to match(%r{<Proxy balancer://myupstream>})
|
22
|
+
end
|
23
|
+
|
24
|
+
it "declares all the requested upstream servers" do
|
25
|
+
expect(output).to include('BalancerMember http://localhost:1337')
|
26
|
+
expect(output).to include('BalancerMember http://localhost:1338')
|
27
|
+
end
|
28
|
+
|
29
|
+
it "proxies to the named upstream" do
|
30
|
+
expect(output).to include(
|
31
|
+
'RewriteRule ^/(.*)$ balancer://myupstream%{REQUEST_URI} [P,QSA,L]')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when single upstream" do
|
36
|
+
before { config.stub(:instance_ports).and_return([1337]) }
|
37
|
+
|
38
|
+
it "declares no upstream section" do
|
39
|
+
expect(output).not_to match(%r{<Proxy balancer://\w+>})
|
40
|
+
end
|
41
|
+
|
42
|
+
it "does not declare any upstream servers" do
|
43
|
+
expect(output).not_to include('BalancerMember http://localhost:1337')
|
44
|
+
end
|
45
|
+
|
46
|
+
it "proxies the single upstream directly" do
|
47
|
+
expect(output).to include(
|
48
|
+
'RewriteRule ^/(.*)$ http://localhost:1337%{REQUEST_URI} [P,QSA,L]')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "listens to the requested server ports" do
|
53
|
+
config.stub(:server_ports).and_return([12345, 12346])
|
54
|
+
expect(output).to include('<VirtualHost *:12345 *:12346>')
|
55
|
+
end
|
56
|
+
|
57
|
+
it "declares the server names it responds to" do
|
58
|
+
config.stub(:server_names).and_return(%w(host1 host2 host3))
|
59
|
+
expect(output).to include('ServerName host1')
|
60
|
+
expect(output).to include('ServerAlias host2 host3')
|
61
|
+
end
|
62
|
+
|
63
|
+
it "declares the requested document root" do
|
64
|
+
config.stub(:static_folder).and_return('STATIC-FOLDER')
|
65
|
+
expect(output).to include('DocumentRoot STATIC-FOLDER')
|
66
|
+
end
|
67
|
+
|
68
|
+
it "respects custom relative_roots" do
|
69
|
+
config.stub(:relative_root).and_return('RELATIVE_ROOT')
|
70
|
+
expect(output).to include('<Location RELATIVE_ROOTassets>')
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -14,18 +14,37 @@ describe VhostGenerator::NginxGenerator do
|
|
14
14
|
expect(output).to match(/^#### FILE GENERATED BY .*CMDLINE/)
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
context "when multiple upstreams" do
|
18
|
+
before { config.stub(:instance_ports).and_return([1337, 1338]) }
|
19
|
+
|
20
|
+
it "declares the named upstream" do
|
21
|
+
expect(output).to match(/upstream myupstream {/)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "declares all the requested upstream servers" do
|
25
|
+
expect(output).to include('server localhost:1337 fail_timeout=0;')
|
26
|
+
expect(output).to include('server localhost:1338 fail_timeout=0;')
|
27
|
+
end
|
20
28
|
|
21
|
-
|
22
|
-
|
29
|
+
it "proxies to the named upstream" do
|
30
|
+
expect(output).to include('proxy_pass http://myupstream;')
|
31
|
+
end
|
23
32
|
end
|
24
33
|
|
25
|
-
|
26
|
-
config.stub(:instance_ports).and_return([1337
|
27
|
-
|
28
|
-
|
34
|
+
context "when single upstream" do
|
35
|
+
before { config.stub(:instance_ports).and_return([1337]) }
|
36
|
+
|
37
|
+
it "declares no upstream section" do
|
38
|
+
expect(output).not_to match(/upstream \w+ {/)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "does not declare any upstream servers" do
|
42
|
+
expect(output).not_to include('server localhost:1337 fail_timeout=0;')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "proxies the single upstream directly" do
|
46
|
+
expect(output).to include('proxy_pass http://localhost:1337;')
|
47
|
+
end
|
29
48
|
end
|
30
49
|
|
31
50
|
it "listens to the requested server ports" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vhost_generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -94,11 +94,13 @@ files:
|
|
94
94
|
- bin/vhost-generator
|
95
95
|
- features/foreman-export-vhost.feature
|
96
96
|
- features/help.feature
|
97
|
+
- features/output-apache.feature
|
97
98
|
- features/output-nginx.feature
|
98
99
|
- features/step_definitions/dev_steps.rb
|
99
100
|
- features/support/env.rb
|
100
101
|
- features/version.feature
|
101
102
|
- lib/vhost_generator.rb
|
103
|
+
- lib/vhost_generator/apache_generator.rb
|
102
104
|
- lib/vhost_generator/application.rb
|
103
105
|
- lib/vhost_generator/capistrano.rb
|
104
106
|
- lib/vhost_generator/cmdline_builder.rb
|
@@ -107,6 +109,7 @@ files:
|
|
107
109
|
- lib/vhost_generator/vhost_configuration.rb
|
108
110
|
- lib/vhost_generator/vhost_generator_module.rb
|
109
111
|
- script/cibuild
|
112
|
+
- spec/apache_generator_spec.rb
|
110
113
|
- spec/application_env_spec.rb
|
111
114
|
- spec/application_options_spec.rb
|
112
115
|
- spec/cmdline_builder_spec.rb
|
@@ -127,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
127
130
|
version: '0'
|
128
131
|
segments:
|
129
132
|
- 0
|
130
|
-
hash:
|
133
|
+
hash: 336320189
|
131
134
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
135
|
none: false
|
133
136
|
requirements:
|
@@ -136,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
139
|
version: '0'
|
137
140
|
segments:
|
138
141
|
- 0
|
139
|
-
hash:
|
142
|
+
hash: 336320189
|
140
143
|
requirements: []
|
141
144
|
rubyforge_project:
|
142
145
|
rubygems_version: 1.8.23
|
@@ -147,11 +150,13 @@ summary: vhost_generator outputs nginx or apache VirtualHost configurations to r
|
|
147
150
|
test_files:
|
148
151
|
- features/foreman-export-vhost.feature
|
149
152
|
- features/help.feature
|
153
|
+
- features/output-apache.feature
|
150
154
|
- features/output-nginx.feature
|
151
155
|
- features/step_definitions/dev_steps.rb
|
152
156
|
- features/support/env.rb
|
153
157
|
- features/version.feature
|
154
158
|
- script/cibuild
|
159
|
+
- spec/apache_generator_spec.rb
|
155
160
|
- spec/application_env_spec.rb
|
156
161
|
- spec/application_options_spec.rb
|
157
162
|
- spec/cmdline_builder_spec.rb
|