nginx_utils 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- N2ZmZDZjZjZkNWExMzkzNDViZjk5NTY3OTljYzYzOWIxZDJjZTgzYw==
4
+ ZjU5NmIzZjlmY2YyOWU3ZTliMjE1NzVlMjY5MTY5OTAyYzNlMDNmOA==
5
5
  data.tar.gz: !binary |-
6
- NTgxZjJlNjUzODRjYTY2MjYyYTk4MjZiYzA0ZWQwNTNkMDk2YzI3OA==
6
+ Y2YyYTYwNmI1NTAzZjU0OTNhYTE0NWIyMjdkOWY5ODE5YWFhMTBkOQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZTAxOWRiZmYzYjRmMThiMzRjYzA1ZWVlYzJiY2I0N2Y2NWU2YTRhOGQxYTcx
10
- Mzg5YjZiN2VlM2Q0N2E5ZmNkZmJkYTA1ZDMzM2QxMjBmZDkyMDU4MTJlMjNi
11
- YzY0OTFlYWE1NDNiN2I0YWVkZWFkYjE2M2IxMjc0ZjFhODE1NGI=
9
+ ZGJiMzBmMzY2OTY3M2IzMmRiMjliMTMxNDhmMjNlODNkMmEwZmU4MjU4ZTY3
10
+ Mjc5ZDNhMTg4NWM5ODYzMmI0Y2VjMThjMTM4NTcwY2M1MjcwOGE4ZmQ2MTcw
11
+ ODRmOGJlNWFiZmU2YzFmMmQyZjY4ODU3YzFjNzA3MDJkYjNiYWU=
12
12
  data.tar.gz: !binary |-
13
- ZmFjMGU4YTM3YTlhOWE0OWI5MTg2YzU3MDg1OGQyYjY0OTQ4NDAwNTZkNmQ2
14
- ZTFhZjZjYTkzMjNmNTQ0OTdlNGRlNTA3ZjUxNjIwMmMwM2FjODA1OWFiNDlm
15
- YmE4YjIyMDEyY2E4MWQ5MGIyMDkzMGI2NTkzZTUwYTMyN2E3MWM=
13
+ ZTY3Mzg5N2Q4NGZlOTNiZDc0NjA1OTFkOWM4ZTRkMDI5NGNhYTEzYjA5OTFl
14
+ Y2Q1ZTY5OTVhZTA4MjUxYmJiZmY2NTgwYjhkMTU2Mjg3MzkzYTE3OGRlNjBl
15
+ N2RkNGJiNTZjYjQ2N2I1NGU4MDQwYWJhYzRjZGVmMmU3NzY3ZTU=
data/README.md CHANGED
@@ -28,11 +28,12 @@ Or install it yourself as:
28
28
 
29
29
  From console:
30
30
 
31
- $ nginx_utils [status|logrotate] [options]
31
+ $ nginx_utils [status|logrotate|create_vhost] [options]
32
32
  Commands:
33
- nginx_utils help [COMMAND] # Describe available commands or one specific command
34
- nginx_utils logrotate -d # Nginx logrotate
35
- nginx_utils status example.com # Print status of Nginx
33
+ nginx_utils create_vhost [OPTIONS] # Create vhost config
34
+ nginx_utils help [COMMAND] # Describe available commands or one specific command
35
+ nginx_utils logrotate [OPTIONS] # Nginx logrotate
36
+ nginx_utils status example.com # Print status of Nginx
36
37
 
37
38
  $ nginx_utils status
38
39
  Active Connections: 1
@@ -41,6 +42,8 @@ From console:
41
42
 
42
43
  $ nginx_utils logrotate
43
44
 
45
+ $ nginx_utils create_vhost -T unicorn -D /usr/local/rails/app/tmp/unicorn.sock -n rails.example.com --only_https
46
+
44
47
  From ruby:
45
48
 
46
49
  ```ruby
@@ -147,6 +150,37 @@ Options that can be specified:
147
150
  * `:format` => `:ltsv` or `:combined`. If parser is specified, format is automatically `:custom`.
148
151
  * `:parser` => Parse with `String#scan`. Specified in Regexp.
149
152
 
153
+ ### VirtualHost
154
+
155
+ ```ruby
156
+ vhost = NginxUtils::VirtualHost.new(
157
+ vhost_type: :passenger,
158
+ server_name: "sinatra.example.com",
159
+ root: "/usr/local/sinatra/app/public",
160
+ only_https: true
161
+ )
162
+
163
+ puts vhost.config
164
+ ```
165
+
166
+ Options that can be specified:
167
+
168
+ * `:vhost_type` => `:unicorn` or `:passenger` or `:proxy` or `:normal`. `:normal` is default.
169
+ * `:destination` => IP address and port or UNIX domain socket path.
170
+ * `:prefix` => Root directory of Nginx.
171
+ * `:server_name` => Server name of virtual host.
172
+ * `:root` => Document root directory path.
173
+ * `:index` => Index files. `["index.html", "index.htm"].join(" ")` is default.
174
+ * `:auth_basic` => Basic realm. `nil` is default.
175
+ * `:auth_basic_user_file` => htpasswd file path.
176
+ * `:http` => Enable http block. default is true.
177
+ * `:https` => Enable https block. default is true.
178
+ * `:ssl_certificate` => Certigicate file path.
179
+ * `:ssl_certificate_key` => Certificate key file path.
180
+ * `:log_dir` => Log directory path.
181
+ * `:access_log_format` => Log format of access log. `:ltsv` is default.
182
+ * `:error_log_level` => Log level of error log. `:info` is default.
183
+
150
184
  ## Contributing
151
185
 
152
186
  1. Fork it
@@ -18,7 +18,7 @@ module NginxUtils
18
18
  end
19
19
  end
20
20
 
21
- desc "logrotate -d", "Nginx logrotate"
21
+ desc "logrotate [OPTIONS]", "Nginx logrotate"
22
22
  long_desc <<-LONGDESC
23
23
  `logrotate` will log rotation of Nginx.
24
24
  LONGDESC
@@ -32,5 +32,29 @@ module NginxUtils
32
32
  def logrotate
33
33
  NginxUtils::Logrotate.new(options).execute
34
34
  end
35
+
36
+ desc "create_vhost [OPTIONS]", "Create vhost config"
37
+ long_desc <<-LONGDESC
38
+ `create_vhost` will create vhost configuration.
39
+ LONGDESC
40
+ option :vhost_type, aliases: "-T", desc: "virtualhost type. default is normal. [normal|unicorn|passenger|proxy]"
41
+ option :destination, aliases: "-D", desc: "proxy destination. path of the socket file or ip address and port"
42
+ option :prefix, aliases: "-p", desc: "nginx root directory."
43
+ option :server_name, aliases: "-n", desc: "server name of vhosts."
44
+ option :root, aliases: "-d", desc: "document root path."
45
+ option :index, aliases: "-i", desc: "index files."
46
+ option :auth_basic, aliases: "-r", desc: "authentication realm."
47
+ option :auth_basic_user_file, aliases: "-u", desc: "user file of basic auth."
48
+ option :only_http, type: :boolean, desc: "disable https block."
49
+ option :only_https, type: :boolean, desc: "disable http block."
50
+ option :ssl_certificate, desc: "certificate file."
51
+ option :ssl_certificate_key, desc: "certificate key file."
52
+ option :log_dir, desc: "log directory path of virtual host."
53
+ option :access_log_format, desc: "access log format."
54
+ option :error_log_level, desc: "error log level."
55
+ def create_vhost
56
+ vhost = NginxUtils::VirtualHost.new(options)
57
+ puts vhost.config
58
+ end
35
59
  end
36
60
  end
@@ -1,3 +1,3 @@
1
1
  module NginxUtils
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,75 @@
1
+ # coding: utf-8
2
+
3
+ module NginxUtils
4
+ class VirtualHost
5
+ attr_accessor :vhost_type, :destination, :http, :https, :prefix, :server_name, :root, :index, :log_dir, :access_log_format, :error_log_level, :auth_basic, :auth_basic_user_file, :ssl_certificate, :ssl_certificate_key
6
+
7
+ def initialize(options={})
8
+ options = options.inject({}){|r,(k,v)| r.store(k.to_sym, v); r}
9
+ set_vhost_type(options)
10
+ set_common_params(options)
11
+ set_protocols(options)
12
+ set_log_params(options)
13
+ end
14
+
15
+ def set_vhost_type(options)
16
+ # Arguments: vhost_type, destination in options
17
+ if options[:vhost_type].nil?
18
+ @vhost_type = :normal
19
+ else
20
+ case options[:vhost_type].to_sym
21
+ when :unicorn then
22
+ @vhost_type = :unicorn
23
+ @destination = options[:destination] || "127.0.0.1:8080"
24
+ when :proxy then
25
+ @vhost_type = :proxy
26
+ @destination = options[:destination] || "127.0.0.1:8080"
27
+ when :passenger then
28
+ @vhost_type = :passenger
29
+ else
30
+ @vhost_type = :normal
31
+ end
32
+ end
33
+
34
+ if @destination =~ /\.sock$/ && @destination !~ /^unix:/
35
+ @destination = "unix:#{@destination}"
36
+ end
37
+ end
38
+
39
+ def set_common_params(options)
40
+ # Arguments: prefix, server_name, root, index, auth_basic, auth_basic_user_file in options
41
+ @prefix = options[:prefix] || "/usr/local/nginx"
42
+ @server_name = options[:server_name] || "example.com"
43
+ @root = options[:root] || File.join(@prefix, "vhosts", @server_name, "html")
44
+ @index = options[:index] || ["index.html", "index.htm"].join(" ")
45
+ @auth_basic = options[:auth_basic]
46
+ if @auth_basic
47
+ @auth_basic_user_file = options[:auth_basic_user_file] || File.join(@prefix, "vhosts", @server_name, "etc", "users")
48
+ end
49
+ end
50
+
51
+ def set_protocols(options)
52
+ # Arguments: http, https, ssl_certificate, ssl_certificate_key in options
53
+ @http = options[:http].nil? ? true : options[:http]
54
+ @https = options[:https].nil? ? true : options[:https]
55
+ @http = false if options[:only_https]
56
+ @https = false if options[:only_http]
57
+ if @https
58
+ @ssl_certificate = options[:ssl_certificate] || File.join(@prefix, "vhosts", @server_name, "ssl.crt", "server.crt")
59
+ @ssl_certificate_key = options[:ssl_certificate_key] || File.join(@prefix, "vhosts", @server_name, "ssl.key", "server.key")
60
+ end
61
+ end
62
+
63
+ def set_log_params(options)
64
+ # Arguments: log_dir, access_log_format, error_log_level in options
65
+ @log_dir = options[:log_dir] || File.join(@prefix, "vhosts", @server_name, "logs")
66
+ @access_log_format = options[:access_log_format] || :ltsv
67
+ @error_log_level = options[:error_log_level] || :info
68
+ end
69
+
70
+ def config
71
+ content = open(File.expand_path("../../../template/virtual_host.erb", __FILE__)).read
72
+ ERB.new(content).result(binding).gsub(/^\s+$/, "").gsub(/\n+/, "\n")
73
+ end
74
+ end
75
+ end
data/lib/nginx_utils.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require "logger"
4
+ require "erb"
4
5
  require "net/http"
5
6
  require "thor"
6
7
 
@@ -9,3 +10,4 @@ require "nginx_utils/logrotate"
9
10
  require "nginx_utils/logreader"
10
11
  require "nginx_utils/status"
11
12
  require "nginx_utils/cli"
13
+ require "nginx_utils/virtual_host"
@@ -3,7 +3,7 @@
3
3
  require "spec_helper"
4
4
  require "stringio"
5
5
 
6
- describe "NginxUtils::CLI" do
6
+ describe NginxUtils::CLI do
7
7
  def capture(stream)
8
8
  begin
9
9
  stream = stream.to_s
@@ -112,4 +112,187 @@ describe "NginxUtils::CLI" do
112
112
  NginxUtils::CLI.start(args)
113
113
  end
114
114
  end
115
+
116
+ describe "#create_vhost" do
117
+ let!(:vhost) {
118
+ vhost_mock = double("vhost mock")
119
+ vhost_mock.should_receive(:config).and_return("virtual host configuration")
120
+ vhost_mock
121
+ }
122
+
123
+ it "output for stdout" do
124
+ NginxUtils::VirtualHost.should_receive(:new).and_return(vhost)
125
+ args = ["create_vhost"]
126
+ expect(
127
+ capture(:stdout) {
128
+ NginxUtils::CLI.start(args)
129
+ }
130
+ ).to eq("virtual host configuration\n")
131
+ end
132
+
133
+ it "vhost_type option" do
134
+ options = {"vhost_type" => "unicorn"}
135
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
136
+ args = ["create_vhost", "-T", "unicorn"]
137
+ expect(
138
+ capture(:stdout) {
139
+ NginxUtils::CLI.start(args)
140
+ }
141
+ ).to eq("virtual host configuration\n")
142
+ end
143
+
144
+ it "destination option" do
145
+ options = {"destination" => "/usr/local/rails/app/tmp/unicorn.sock"}
146
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
147
+ args = ["create_vhost", "-D", "/usr/local/rails/app/tmp/unicorn.sock"]
148
+ expect(
149
+ capture(:stdout) {
150
+ NginxUtils::CLI.start(args)
151
+ }
152
+ ).to eq("virtual host configuration\n")
153
+ end
154
+
155
+ it "prefix option" do
156
+ options = {"prefix" => "/opt/nginx"}
157
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
158
+ args = ["create_vhost", "-p", "/opt/nginx"]
159
+ expect(
160
+ capture(:stdout) {
161
+ NginxUtils::CLI.start(args)
162
+ }
163
+ ).to eq("virtual host configuration\n")
164
+ end
165
+
166
+ it "server_name option" do
167
+ options = {"server_name" => "nginx_utils.example.com"}
168
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
169
+ args = ["create_vhost", "-n", "nginx_utils.example.com"]
170
+ expect(
171
+ capture(:stdout) {
172
+ NginxUtils::CLI.start(args)
173
+ }
174
+ ).to eq("virtual host configuration\n")
175
+ end
176
+
177
+ it "root option" do
178
+ options = {"root" => "/var/lib/nginx/www"}
179
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
180
+ args = ["create_vhost", "-d", "/var/lib/nginx/www"]
181
+ expect(
182
+ capture(:stdout) {
183
+ NginxUtils::CLI.start(args)
184
+ }
185
+ ).to eq("virtual host configuration\n")
186
+ end
187
+
188
+ it "index option" do
189
+ options = {"index" => "index.rb"}
190
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
191
+ args = ["create_vhost", "-i", "index.rb"]
192
+ expect(
193
+ capture(:stdout) {
194
+ NginxUtils::CLI.start(args)
195
+ }
196
+ ).to eq("virtual host configuration\n")
197
+ end
198
+
199
+ it "auth_basic option" do
200
+ options = {"auth_basic" => "Auth"}
201
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
202
+ args = ["create_vhost", "-r", "Auth"]
203
+ expect(
204
+ capture(:stdout) {
205
+ NginxUtils::CLI.start(args)
206
+ }
207
+ ).to eq("virtual host configuration\n")
208
+ end
209
+
210
+ it "auth_basic_user_file option" do
211
+ options = {"auth_basic_user_file" => "/var/lib/nginx/user"}
212
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
213
+ args = ["create_vhost", "-u", "/var/lib/nginx/user"]
214
+ expect(
215
+ capture(:stdout) {
216
+ NginxUtils::CLI.start(args)
217
+ }
218
+ ).to eq("virtual host configuration\n")
219
+ end
220
+
221
+ it "only_http option" do
222
+ options = {"only_http" => true}
223
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
224
+ args = ["create_vhost", "--only_http"]
225
+ expect(
226
+ capture(:stdout) {
227
+ NginxUtils::CLI.start(args)
228
+ }
229
+ ).to eq("virtual host configuration\n")
230
+ end
231
+
232
+ it "only_https option" do
233
+ options = {"only_https" => true}
234
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
235
+ args = ["create_vhost", "--only_https"]
236
+ expect(
237
+ capture(:stdout) {
238
+ NginxUtils::CLI.start(args)
239
+ }
240
+ ).to eq("virtual host configuration\n")
241
+ end
242
+
243
+ it "ssl_certificate option" do
244
+ options = {"ssl_certificate" => "/var/lib/nginx/vhosts/example.com/ssl.crt/server.crt"}
245
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
246
+ args = ["create_vhost", "--ssl_certificate", "/var/lib/nginx/vhosts/example.com/ssl.crt/server.crt"]
247
+ expect(
248
+ capture(:stdout) {
249
+ NginxUtils::CLI.start(args)
250
+ }
251
+ ).to eq("virtual host configuration\n")
252
+ end
253
+
254
+ it "ssl_certificate_key option" do
255
+ options = {"ssl_certificate_key" => "/var/lib/nginx/vhosts/example.com/ssl.key/server.key"}
256
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
257
+ args = ["create_vhost", "--ssl_certificate_key", "/var/lib/nginx/vhosts/example.com/ssl.key/server.key"]
258
+ expect(
259
+ capture(:stdout) {
260
+ NginxUtils::CLI.start(args)
261
+ }
262
+ ).to eq("virtual host configuration\n")
263
+ end
264
+
265
+ it "log_dir option" do
266
+ options = {"log_dir" => "/var/log"}
267
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
268
+ args = ["create_vhost", "--log_dir", "/var/log"]
269
+ expect(
270
+ capture(:stdout) {
271
+ NginxUtils::CLI.start(args)
272
+ }
273
+ ).to eq("virtual host configuration\n")
274
+ end
275
+
276
+ it "access_log_format option" do
277
+ options = {"access_log_format" => "combined"}
278
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
279
+ args = ["create_vhost", "--access_log_format", "combined"]
280
+ expect(
281
+ capture(:stdout) {
282
+ NginxUtils::CLI.start(args)
283
+ }
284
+ ).to eq("virtual host configuration\n")
285
+ end
286
+
287
+ it "error_log_level option" do
288
+ options = {"error_log_level" => "error"}
289
+ NginxUtils::VirtualHost.should_receive(:new).with(options).and_return(vhost)
290
+ args = ["create_vhost", "--error_log_level", "error"]
291
+ expect(
292
+ capture(:stdout) {
293
+ NginxUtils::CLI.start(args)
294
+ }
295
+ ).to eq("virtual host configuration\n")
296
+ end
297
+ end
115
298
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe "NginxUtils::Logreader" do
5
+ describe NginxUtils::Logreader do
6
6
  let!(:io_mock) do
7
7
  io = double("io mock").as_null_object
8
8
  File.stub(:open).and_return(io)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe "NginxUtils::Logrotate" do
5
+ describe NginxUtils::Logrotate do
6
6
  let(:rotate) {NginxUtils::Logrotate.new}
7
7
 
8
8
  let!(:logger_mock) do
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe "NginxUtils::Status" do
5
+ describe NginxUtils::Status do
6
6
  let(:body) {"Active connections: 1 \nserver accepts handled requests\n 4 5 51 \nReading: 1 Writing: 3 Waiting: 2 \n"}
7
7
  let(:status) {{active_connections: 1, accepts: 4, handled: 5, requests: 51, reading: 1, writing: 3, waiting: 2}}
8
8
 
@@ -0,0 +1,245 @@
1
+ # coding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe NginxUtils::VirtualHost do
6
+ describe "#initialize" do
7
+ after {NginxUtils::VirtualHost.new}
8
+
9
+ it "set_vhost_type method should be called" do
10
+ NginxUtils::VirtualHost.any_instance.should_receive(:set_vhost_type)
11
+ end
12
+
13
+ it "set_common_params method should be called" do
14
+ NginxUtils::VirtualHost.any_instance.should_receive(:set_common_params)
15
+ NginxUtils::VirtualHost.any_instance.stub(:set_protocols).and_return(true)
16
+ NginxUtils::VirtualHost.any_instance.stub(:set_log_params).and_return(true)
17
+ end
18
+
19
+ it "set_protocols method should be called" do
20
+ NginxUtils::VirtualHost.any_instance.should_receive(:set_protocols)
21
+ end
22
+
23
+ it "set_log_params method should be called" do
24
+ NginxUtils::VirtualHost.any_instance.should_receive(:set_log_params)
25
+ end
26
+ end
27
+
28
+ describe "#set_vhost_type" do
29
+ context "with default params" do
30
+ its(:vhost_type){should eq(:normal)}
31
+ its(:destination){should be_nil}
32
+ end
33
+
34
+ context "with custom params" do
35
+ it "vhost_type should be a unicorn" do
36
+ vhost = NginxUtils::VirtualHost.new(vhost_type: :unicorn)
37
+ expect(vhost.vhost_type).to eq(:unicorn)
38
+ expect(vhost.destination).to eq("127.0.0.1:8080")
39
+ end
40
+
41
+ it "vhost_type should be a proxy" do
42
+ vhost = NginxUtils::VirtualHost.new(vhost_type: :proxy)
43
+ expect(vhost.vhost_type).to eq(:proxy)
44
+ expect(vhost.destination).to eq("127.0.0.1:8080")
45
+ end
46
+
47
+ it "vhost_type should be a passenger" do
48
+ vhost = NginxUtils::VirtualHost.new(vhost_type: :passenger)
49
+ expect(vhost.vhost_type).to eq(:passenger)
50
+ expect(vhost.destination).to be_nil
51
+ end
52
+
53
+ it "vhost_type should be a normal if unknown param" do
54
+ vhost = NginxUtils::VirtualHost.new(vhost_type: :unknown)
55
+ expect(vhost.vhost_type).to eq(:normal)
56
+ expect(vhost.destination).to be_nil
57
+ end
58
+
59
+ it "destination should be specified unix domain socket" do
60
+ vhost = NginxUtils::VirtualHost.new(vhost_type: :unicorn, destination: "/usr/local/rails/app/tmp/unicorn.sock")
61
+ expect(vhost.destination).to eq("unix:/usr/local/rails/app/tmp/unicorn.sock")
62
+ end
63
+
64
+ it "destination should be specified ip address and port" do
65
+ vhost = NginxUtils::VirtualHost.new(vhost_type: :unicorn, destination: "127.0.0.1:3000")
66
+ expect(vhost.destination).to eq("127.0.0.1:3000")
67
+ end
68
+ end
69
+ end
70
+
71
+ describe "#set_common_params" do
72
+ context "with default params" do
73
+ its(:prefix){should eq("/usr/local/nginx")}
74
+ its(:server_name){should eq("example.com")}
75
+ its(:root){should eq("/usr/local/nginx/vhosts/example.com/html")}
76
+ its(:index){should eq("index.html index.htm")}
77
+ its(:auth_basic){should be_nil}
78
+ its(:auth_basic_user_file){should be_nil}
79
+ end
80
+
81
+ context "with custom params" do
82
+ it "prefix should be a specified parameter" do
83
+ vhost = NginxUtils::VirtualHost.new(prefix: "/opt/nginx")
84
+ expect(vhost.prefix).to eq("/opt/nginx")
85
+ expect(vhost.root).to match(/^\/opt\/nginx/)
86
+ end
87
+
88
+ it "server_name should be a specified parameter" do
89
+ vhost = NginxUtils::VirtualHost.new(server_name: "nginx_utils.example.com")
90
+ expect(vhost.server_name).to eq("nginx_utils.example.com")
91
+ end
92
+
93
+ it "root should be a specified parameter" do
94
+ vhost = NginxUtils::VirtualHost.new(root: "/var/lib/nginx/www")
95
+ expect(vhost.root).to eq("/var/lib/nginx/www")
96
+ end
97
+
98
+ it "index should be a specified parameter" do
99
+ vhost = NginxUtils::VirtualHost.new(index: "index.rb")
100
+ expect(vhost.index).to eq("index.rb")
101
+ end
102
+
103
+ it "auth_basic should be a specified parameter" do
104
+ vhost = NginxUtils::VirtualHost.new(auth_basic: "Auth")
105
+ expect(vhost.auth_basic).to eq("Auth")
106
+ end
107
+
108
+ it "auth_basic_user_file should be a specified parameter" do
109
+ vhost = NginxUtils::VirtualHost.new(auth_basic: "Auth", auth_basic_user_file: "/var/lib/nginx/users")
110
+ expect(vhost.auth_basic_user_file).to eq("/var/lib/nginx/users")
111
+ end
112
+ end
113
+ end
114
+
115
+ describe "#set_protocols" do
116
+ context "with default params" do
117
+ its(:http){should be_true}
118
+ its(:https){should be_true}
119
+ its(:ssl_certificate){should eq("/usr/local/nginx/vhosts/example.com/ssl.crt/server.crt")}
120
+ its(:ssl_certificate_key){should eq("/usr/local/nginx/vhosts/example.com/ssl.key/server.key")}
121
+ end
122
+
123
+ context "with custom params" do
124
+ it "http should be a false" do
125
+ vhost = NginxUtils::VirtualHost.new(http: false)
126
+ expect(vhost.http).to be_false
127
+ end
128
+
129
+ it "https should be a false" do
130
+ vhost = NginxUtils::VirtualHost.new(https: false)
131
+ expect(vhost.https).to be_false
132
+ end
133
+
134
+ it "ssl_certificate should be a nil if https is false" do
135
+ vhost = NginxUtils::VirtualHost.new(https: false)
136
+ expect(vhost.ssl_certificate).to be_nil
137
+ end
138
+
139
+ it "ssl_certificate_key should be a nil if https is false" do
140
+ vhost = NginxUtils::VirtualHost.new(https: false)
141
+ expect(vhost.ssl_certificate_key).to be_nil
142
+ end
143
+
144
+ it "https should be a false if specified only_http" do
145
+ vhost = NginxUtils::VirtualHost.new(only_http: true)
146
+ expect(vhost.https).to be_false
147
+ end
148
+
149
+ it "http should be a false if specified only_https" do
150
+ vhost = NginxUtils::VirtualHost.new(only_https: true)
151
+ expect(vhost.http).to be_false
152
+ end
153
+ end
154
+ end
155
+
156
+ describe "#set_log_params" do
157
+ context "with default params" do
158
+ its(:log_dir){should eq("/usr/local/nginx/vhosts/example.com/logs")}
159
+ its(:access_log_format){should eq(:ltsv)}
160
+ its(:error_log_level){should eq(:info)}
161
+ end
162
+
163
+ context "with custom params" do
164
+ it "log_dir should be a specified parameter" do
165
+ vhost = NginxUtils::VirtualHost.new(log_dir: "/var/log")
166
+ expect(vhost.log_dir).to eq("/var/log")
167
+ end
168
+
169
+ it "access_log_format should be a specified parameter" do
170
+ vhost = NginxUtils::VirtualHost.new(access_log_format: "combined")
171
+ expect(vhost.access_log_format).to eq("combined")
172
+ end
173
+
174
+ it "error_log_level should be a specified parameter" do
175
+ vhost = NginxUtils::VirtualHost.new(error_log_level: "error")
176
+ expect(vhost.error_log_level).to eq("error")
177
+ end
178
+ end
179
+ end
180
+
181
+ describe "#config" do
182
+ subject {
183
+ virtual_host = NginxUtils::VirtualHost.new(
184
+ vhost_type: :unicorn,
185
+ destination: "/usr/local/rails/app/tmp/unicorn.sock",
186
+ prefix: "/opt/nginx",
187
+ server_name: "nginx_utils.example.com",
188
+ index: "index.rb",
189
+ log_dir: "/var/log",
190
+ access_log_format: "combined",
191
+ error_log_level: "error",
192
+ auth_basic: "Auth"
193
+ )
194
+ virtual_host.config
195
+ }
196
+
197
+ it "upstream block should be defined" do
198
+ should match(/upstream backend-unicorn/)
199
+ end
200
+
201
+ it "http block should be defined" do
202
+ should match(/listen 80;/)
203
+ end
204
+
205
+ it "https block should be defined" do
206
+ should match(/listen 443 ssl;/)
207
+ end
208
+
209
+ it "unix domain socket should be defined" do
210
+ should match(/server unix:\/usr\/local\/rails\/app\/tmp\/unicorn\.sock;/)
211
+ end
212
+
213
+ it "server_name should be defined" do
214
+ should match(/server_name nginx_utils.example.com;/)
215
+ end
216
+
217
+ it "index should be defined" do
218
+ should match(/index index.rb;/)
219
+ end
220
+
221
+ it "access_log should be defined" do
222
+ should match(/access_log \/var\/log\/access.log combined;/)
223
+ end
224
+
225
+ it "error_log should be defined" do
226
+ should match(/error_log \/var\/log\/error.log error;/)
227
+ end
228
+
229
+ it "auth_basic should be defined" do
230
+ should match(/auth_basic "Auth";/)
231
+ end
232
+
233
+ it "auth_basic_user_file should be defined" do
234
+ should match(/auth_basic_user_file \/opt\/nginx\/vhosts\/nginx_utils\.example\.com\/etc\/users;/)
235
+ end
236
+
237
+ it "try_files should be defined" do
238
+ should match(/try_files/)
239
+ end
240
+
241
+ it "proxy_pass should be defined" do
242
+ should match(/proxy_pass http:\/\/backend-unicorn;/)
243
+ end
244
+ end
245
+ end
@@ -0,0 +1,111 @@
1
+ # <%= @server_name %>.conf
2
+ # Created by nginx_utils version <%= NginxUtils::VERSION %>
3
+ <% if @vhost_type == :unicorn %>
4
+ upstream backend-unicorn {
5
+ server <%= @destination %>;
6
+ }
7
+ <% end %>
8
+
9
+ <% if @http %>
10
+ server {
11
+ listen 80;
12
+ server_name <%= @server_name %>;
13
+ <% if @vhost_type != :proxy %>
14
+ root <%= @root %>;
15
+ index <%= @index %>;
16
+ <% end %>
17
+
18
+ access_log <%= [File.join(@log_dir, "access.log"), @access_log_format.to_s].join(" ") %>;
19
+ error_log <%= [File.join(@log_dir, "error.log"), @error_log_level.to_s].join(" ") %>;
20
+
21
+ <% if @auth_basic %>
22
+ auth_basic "<%= @auth_basic %>";
23
+ auth_basic_user_file <%= @auth_basic_user_file %>;
24
+ <% end %>
25
+
26
+ <% if @vhost_type != :normal %>
27
+ location / {
28
+ <% if @vhost_type != :proxy %>
29
+ try_files $uri @proxy;
30
+ <% else %>
31
+ proxy_pass http://<%= @destination %>;
32
+ proxy_redirect default;
33
+ <% end %>
34
+ }
35
+ <% end %>
36
+
37
+ <% if @vhost_type != :normal && @vhost_type != :proxy %>
38
+ location @proxy {
39
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
40
+ proxy_set_header Host $http_host;
41
+ proxy_set_header X-Forwarded-Proto https;
42
+ proxy_redirect off;
43
+
44
+ <% if @vhost_type == :passenger %>
45
+ passenger_enabled on;
46
+ <% end %>
47
+
48
+ <% if @vhost_type == :unicorn %>
49
+ proxy_pass http://backend-unicorn;
50
+ <% end %>
51
+ }
52
+ <% end %>
53
+ }
54
+ <% end %>
55
+
56
+ <% if @https %>
57
+ server {
58
+ listen 443 ssl;
59
+ server_name <%= @server_name %>;
60
+ <% if @vhost_type != :proxy %>
61
+ root <%= @root %>;
62
+ index <%= @index %>;
63
+ <% end %>
64
+
65
+ access_log <%= [File.join(@log_dir, "ssl_access.log"), @access_log_format.to_s].join(" ") %>;
66
+ error_log <%= [File.join(@log_dir, "ssl_error.log"), @error_log_level.to_s].join(" ") %>;
67
+
68
+ ssl on;
69
+ ssl_certificate <%= @ssl_certificate %>;
70
+ ssl_certificate_key <%= @ssl_certificate_key %>;
71
+
72
+ ssl_session_timeout 5m;
73
+
74
+ ssl_protocols SSLv2 SSLv3 TLSv1;
75
+ ssl_ciphers HIGH:!aNULL:!MD5;
76
+ ssl_prefer_server_ciphers on;
77
+
78
+ <% if @auth_basic %>
79
+ auth_basic "<%= @auth_basic %>";
80
+ auth_basic_user_file <%= @auth_basic_user_file %>;
81
+ <% end %>
82
+
83
+ <% if @vhost_type != :normal %>
84
+ location / {
85
+ <% if @vhost_type != :proxy %>
86
+ try_files $uri @proxy;
87
+ <% else %>
88
+ proxy_pass http://<%= @destination %>;
89
+ proxy_redirect default;
90
+ <% end %>
91
+ }
92
+ <% end %>
93
+
94
+ <% if @vhost_type != :normal && @vhost_type != :proxy %>
95
+ location @proxy {
96
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
97
+ proxy_set_header Host $http_host;
98
+ proxy_set_header X-Forwarded-Proto https;
99
+ proxy_redirect off;
100
+
101
+ <% if @vhost_type == :passenger %>
102
+ passenger_enabled on;
103
+ <% end %>
104
+
105
+ <% if @vhost_type == :unicorn %>
106
+ proxy_pass http://backend-unicorn;
107
+ <% end %>
108
+ }
109
+ <% end %>
110
+ }
111
+ <% end %>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nginx_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - i2bskn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-13 00:00:00.000000000 Z
11
+ date: 2013-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -88,12 +88,15 @@ files:
88
88
  - lib/nginx_utils/logrotate.rb
89
89
  - lib/nginx_utils/status.rb
90
90
  - lib/nginx_utils/version.rb
91
+ - lib/nginx_utils/virtual_host.rb
91
92
  - nginx_utils.gemspec
92
93
  - spec/nginx_utils/cli_spec.rb
93
94
  - spec/nginx_utils/logreader_spec.rb
94
95
  - spec/nginx_utils/logrotate_spec.rb
95
96
  - spec/nginx_utils/status_spec.rb
97
+ - spec/nginx_utils/virtual_host_spec.rb
96
98
  - spec/spec_helper.rb
99
+ - template/virtual_host.erb
97
100
  homepage: https://github.com/i2bskn/nginx_utils
98
101
  licenses:
99
102
  - MIT
@@ -123,4 +126,5 @@ test_files:
123
126
  - spec/nginx_utils/logreader_spec.rb
124
127
  - spec/nginx_utils/logrotate_spec.rb
125
128
  - spec/nginx_utils/status_spec.rb
129
+ - spec/nginx_utils/virtual_host_spec.rb
126
130
  - spec/spec_helper.rb