nginxtra 1.8.0.11 → 1.8.1.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ module Nginxtra
7
7
  end
8
8
 
9
9
  def convert(options)
10
- raise Nginxtra::Error::ConvertFailed.new("The convert method can only be called once!") if converted?
10
+ raise Nginxtra::Error::ConvertFailed, "The convert method can only be called once!" if converted?
11
11
  header
12
12
  compile_options options[:binary_status]
13
13
  config_file options[:config]
@@ -16,32 +16,33 @@ module Nginxtra
16
16
  end
17
17
 
18
18
  private
19
+
19
20
  def header
20
21
  @output.puts "nginxtra.config do"
21
- @indentation + 1
22
+ @indentation.increment
22
23
  end
23
24
 
24
25
  def compile_options(status)
25
26
  return unless status
26
27
  options = (status[/^configure arguments:\s*(.*)$/, 1] || "").strip
27
28
  return if options.empty?
28
- options = options.split /\s+/
29
+ options = options.split(/\s+/)
29
30
  process_passenger_compile_options! options
30
31
 
31
32
  options.each do |option|
32
33
  next if invalid_compile_option? option
33
34
  @output.print @indentation
34
- @output.puts %{compile_option "#{option}"}
35
+ @output.puts %(compile_option "#{option}")
35
36
  end
36
37
  end
37
38
 
38
39
  def process_passenger_compile_options!(options)
39
- return if options.select { |x| x =~ /^--add-module.*\/passenger.*/ }.empty?
40
+ return if options.select { |x| x =~ %r{^--add-module.*/passenger.*} }.empty?
40
41
  @output.print @indentation
41
42
  @output.puts "require_passenger!"
42
43
 
43
44
  options.delete_if do |x|
44
- next true if x =~ /^--add-module.*\/passenger.*/
45
+ next true if x =~ %r{^--add-module.*/passenger.*}
45
46
  ["--with-http_ssl_module", "--with-http_gzip_static_module", "--with-cc-opt=-Wno-error"].include? x
46
47
  end
47
48
  end
@@ -57,8 +58,8 @@ module Nginxtra
57
58
  def config_file(input)
58
59
  return unless input
59
60
  @output.print @indentation
60
- @output.puts %{file "nginx.conf" do}
61
- @indentation + 1
61
+ @output.puts %(file "nginx.conf" do)
62
+ @indentation.increment
62
63
  line = Nginxtra::ConfigConverter::Line.new @indentation, @output
63
64
 
64
65
  each_token(input) do |token|
@@ -70,8 +71,8 @@ module Nginxtra
70
71
  end
71
72
  end
72
73
 
73
- raise Nginxtra::Error::ConvertFailed.new("Unexpected end of file!") unless line.empty?
74
- @indentation - 1
74
+ raise Nginxtra::Error::ConvertFailed, "Unexpected end of file!" unless line.empty?
75
+ @indentation.decrement
75
76
  @output.print @indentation
76
77
  @output.puts "end"
77
78
  end
@@ -79,7 +80,10 @@ module Nginxtra
79
80
  def each_token(input)
80
81
  token = Nginxtra::ConfigConverter::Token.new
81
82
 
82
- while c = input.read(1)
83
+ loop do
84
+ c = input.read(1)
85
+ break unless c
86
+
83
87
  if c == "#"
84
88
  chomp_comment input
85
89
  else
@@ -90,19 +94,21 @@ module Nginxtra
90
94
  end
91
95
 
92
96
  yield token.instance while token.ready?
93
- raise Nginxtra::Error::ConvertFailed.new("Unexpected end of file in mid token!") unless token.value.empty?
97
+ raise Nginxtra::Error::ConvertFailed, "Unexpected end of file in mid token!" unless token.value.empty?
94
98
  end
95
99
 
96
100
  def chomp_comment(input)
97
- while c = input.read(1)
101
+ loop do
102
+ c = input.read(1)
103
+ break unless c
98
104
  break if c == "\n"
99
105
  end
100
106
  end
101
107
 
102
108
  def footer
103
- @indentation - 1
109
+ @indentation.decrement
104
110
  @output.puts "end"
105
- raise Nginxtra::Error::ConvertFailed.new("Missing end blocks!") unless @indentation.done?
111
+ raise Nginxtra::Error::ConvertFailed, "Missing end blocks!" unless @indentation.done?
106
112
  end
107
113
 
108
114
  def converted!
@@ -114,7 +120,7 @@ module Nginxtra
114
120
  end
115
121
 
116
122
  class Token
117
- KEYWORDS = ["break", "if", "return"].freeze
123
+ KEYWORDS = %w(break if return).freeze
118
124
  TERMINAL_CHARACTERS = ["{", "}", ";"].freeze
119
125
  attr_reader :value
120
126
 
@@ -124,16 +130,16 @@ module Nginxtra
124
130
  @ready = false
125
131
  end
126
132
 
127
- def is_if?
133
+ def if?
128
134
  @value == "if"
129
135
  end
130
136
 
131
137
  def if_start!
132
- @value.gsub! /^\(/, ""
138
+ @value.gsub!(/^\(/, "")
133
139
  end
134
140
 
135
141
  def if_end!
136
- @value.gsub! /\)$/, ""
142
+ @value.gsub!(/\)$/, "")
137
143
  end
138
144
 
139
145
  def terminal_character?
@@ -153,7 +159,7 @@ module Nginxtra
153
159
  end
154
160
 
155
161
  def instance
156
- raise Nginxtra::Error::ConvertFailed.new("Whoops!") unless ready?
162
+ raise Nginxtra::Error::ConvertFailed, "Whoops!" unless ready?
157
163
  token = Nginxtra::ConfigConverter::Token.new @value
158
164
  reset!
159
165
  token
@@ -181,11 +187,12 @@ module Nginxtra
181
187
  if @value =~ /^\d+$/
182
188
  @value
183
189
  else
184
- %{"#{@value.gsub("\\") { "\\\\" }}"}
190
+ %("#{@value.gsub("\\") { "\\\\" }}")
185
191
  end
186
192
  end
187
193
 
188
194
  private
195
+
189
196
  def space!
190
197
  return if @value.empty?
191
198
  @ready = true
@@ -202,11 +209,11 @@ module Nginxtra
202
209
  end
203
210
 
204
211
  def reset!
205
- if @next
206
- @value = @next
207
- else
208
- @value = ""
209
- end
212
+ @value = if @next
213
+ @next
214
+ else
215
+ ""
216
+ end
210
217
 
211
218
  @next = nil
212
219
  @ready = false
@@ -240,21 +247,22 @@ module Nginxtra
240
247
  elsif @tokens.last.block_end?
241
248
  puts_block_end
242
249
  else
243
- raise Nginxtra::Error::ConvertFailed.new "Can't puts invalid line!"
250
+ raise Nginxtra::Error::ConvertFailed, "Can't puts invalid line!"
244
251
  end
245
252
  end
246
253
 
247
254
  private
248
- def is_if?
249
- @tokens.first.is_if?
255
+
256
+ def if?
257
+ @tokens.first.if?
250
258
  end
251
259
 
252
260
  def passenger?
253
- ["passenger_root", "passenger_ruby", "passenger_enabled"].include? @tokens.first.value
261
+ %w(passenger_root passenger_ruby passenger_enabled).include? @tokens.first.value
254
262
  end
255
263
 
256
264
  def puts_line
257
- raise Nginxtra::Error::ConvertFailed.new("Line must have a first label!") unless @tokens.length > 1
265
+ raise Nginxtra::Error::ConvertFailed, "Line must have a first label!" unless @tokens.length > 1
258
266
  return puts_passenger if passenger?
259
267
  print_indentation
260
268
  print_first
@@ -272,12 +280,12 @@ module Nginxtra
272
280
  elsif @tokens.first.value == "passenger_enabled"
273
281
  print_newline "passenger_on!"
274
282
  else
275
- raise Nginxtra::Error::ConvertFailed.new("Whoops!")
283
+ raise Nginxtra::Error::ConvertFailed, "Whoops!"
276
284
  end
277
285
  end
278
286
 
279
287
  def puts_block_start
280
- raise Nginxtra::Error::ConvertFailed.new("Block start must have a first label!") unless @tokens.length > 1
288
+ raise Nginxtra::Error::ConvertFailed, "Block start must have a first label!" unless @tokens.length > 1
281
289
  print_indentation
282
290
  print_first
283
291
  print_args
@@ -286,7 +294,7 @@ module Nginxtra
286
294
  end
287
295
 
288
296
  def puts_block_end
289
- raise Nginxtra::Error::ConvertFailed.new("Block end can't have labels!") unless @tokens.length == 1
297
+ raise Nginxtra::Error::ConvertFailed, "Block end can't have labels!" unless @tokens.length == 1
290
298
  unindent
291
299
  print_indentation
292
300
  print_newline("end")
@@ -305,7 +313,7 @@ module Nginxtra
305
313
  return if args.empty?
306
314
  @output.print " "
307
315
 
308
- if is_if?
316
+ if if?
309
317
  args.first.if_start!
310
318
  args.last.if_end!
311
319
  end
@@ -318,11 +326,11 @@ module Nginxtra
318
326
  end
319
327
 
320
328
  def indent
321
- @indentation + 1
329
+ @indentation.increment
322
330
  end
323
331
 
324
332
  def unindent
325
- @indentation - 1
333
+ @indentation.decrement
326
334
  end
327
335
  end
328
336
  end
@@ -8,7 +8,7 @@ module Nginxtra
8
8
  end
9
9
 
10
10
  def output(thor)
11
- options = @options || { :header => message }
11
+ options = @options || { header: message }
12
12
  Nginxtra::Error.print_error thor, options
13
13
  end
14
14
  end
@@ -16,6 +16,27 @@ module Nginxtra
16
16
  # Raised if config conversion fails.
17
17
  class ConvertFailed < Nginxtra::Error::Base; end
18
18
 
19
+ # Raised if /etc/init.d/nginx is missing.
20
+ class NginxInitScriptMissing < Nginxtra::Error::ConvertFailed
21
+ HEADER = "Cannot find nginx binary!".freeze
22
+ MESSAGE = "Either point to it via --nginx-bin or ignore the binary with --ignore-nginx-bin".freeze
23
+
24
+ def initialize
25
+ super("Cannot find nginx binary", header: HEADER, message: MESSAGE)
26
+ end
27
+ end
28
+
29
+ # Raised if the nginx binary cannot be determined from the
30
+ # /etc/init.d/nginx init script.
31
+ class UndeterminedNginxBinary < Nginxtra::Error::ConvertFailed
32
+ HEADER = "Cannot find nginx binary!".freeze
33
+ MESSAGE = "The binary location of nginx cannot be determined from /etc/init.d/nginx. Either point to it via --nginx-bin or ignore the binary with --ignore-nginx-bin".freeze
34
+
35
+ def initialize
36
+ super("Cannot determine nginx binary", header: HEADER, message: MESSAGE)
37
+ end
38
+ end
39
+
19
40
  # Raised when something is in an illegal state, such as when
20
41
  # running nginxtra_rails from a directory other than a rails root
21
42
  # directory.
@@ -25,11 +46,72 @@ module Nginxtra
25
46
  # --prefix compile option.
26
47
  class InvalidConfig < Nginxtra::Error::Base; end
27
48
 
49
+ # Subclass of InvalidConfig that indicates an option was provided
50
+ # that is not allowed.
51
+ class InvalidCompilationOption < Nginxtra::Error::InvalidConfig
52
+ MESSAGES = {
53
+ "prefix" => "The --prefix compile option is not allowed with nginxtra. It is reserved so nginxtra can control where nginx is compiled and run from.",
54
+ "sbin-path" => "The --sbin-path compile option is not allowed with nginxtra. It is reserved so nginxtra can control what binary is used to run nginx.",
55
+ "conf-path" => "The --conf-path compile option is not allowed with nginxtra. It is reserved so nginxtra can control the configuration entirely via #{Nginxtra::Config::FILENAME}.",
56
+ "pid-path" => "The --pid-path compile option is not allowed with nginxtra. It is reserved so nginxtra can control where the pid file is created."
57
+ }.freeze
58
+
59
+ def initialize(parameter)
60
+ super("Invalid compilation option --#{parameter}", header: "Invalid compilation option --#{parameter}", message: MESSAGES[parameter])
61
+ end
62
+ end
63
+
64
+ # Raised when the nginxtra config is missing
65
+ class MissingNginxConfig < Nginxtra::Error::InvalidConfig
66
+ HEADER = "Missing definition for nginx.conf!".freeze
67
+ MESSAGE = "You must define your nginx.conf configuration in your nginxtra.conf.rb file.".freeze
68
+
69
+ def initialize
70
+ super("Missing definition for nginx.conf", header: HEADER, message: MESSAGE)
71
+ end
72
+ end
73
+
74
+ # Raised if a partial is called with anything but no arguments or
75
+ # a single Hash.
76
+ class InvalidPartialArguments < Nginxtra::Error::InvalidConfig
77
+ HEADER = "Invalid partial arguments!".freeze
78
+ MESSAGE = "You can only pass 0 arguments or 1 hash to a partial!".freeze
79
+
80
+ def initialize
81
+ super("Invalid partial arguments", header: HEADER, message: MESSAGE)
82
+ end
83
+ end
84
+
85
+ # Raised if you reference passenger without the passenger gem.
86
+ class MissingPassengerGem < Nginxtra::Error::InvalidConfig
87
+ HEADER = "Missing passenger gem!".freeze
88
+ MESSAGE = "You cannot reference passenger unless the passenger gem is installed!".freeze
89
+
90
+ def initialize
91
+ super("Missing passenger gem", header: HEADER, message: MESSAGE)
92
+ end
93
+ end
94
+
95
+ # Raise when a config file doesn't actually specify the config
96
+ class NoConfigSpecified < Nginxtra::Error::InvalidConfig
97
+ def initialize(config_path)
98
+ super("No configuration is specified in #{config_path}!")
99
+ end
100
+ end
101
+
28
102
  # Raised when the config file cannot be found.
29
103
  class MissingConfig < Nginxtra::Error::Base; end
30
104
 
31
105
  # Raised when installing and nginx is detected to be installed.
32
- class NginxDetected < Nginxtra::Error::Base; end
106
+ class NginxDetected < Nginxtra::Error::Base
107
+ HEADER = "It appears nginx is already installed!".freeze
108
+ MESSAGE = "Since /etc/init.d/nginx exists, you might have an existing nginx installation that will conflict with nginxtra. " \
109
+ "If you want to install nginxtra alongside nginx (at your own risk), please include the --ignore-nginx-check option to bypass this check.".freeze
110
+
111
+ def initialize
112
+ super("Uninstall nginx before installing nginxtra", header: HEADER, message: MESSAGE)
113
+ end
114
+ end
33
115
 
34
116
  # Raised when a run command fails.
35
117
  class RunFailed < Nginxtra::Error::Base; end
@@ -42,15 +124,13 @@ module Nginxtra
42
124
  end
43
125
 
44
126
  def protect(thor)
45
- begin
46
- yield
47
- rescue Nginxtra::Error::Base => e
48
- e.output thor
49
- raise if thor.options["trace"]
50
- rescue => e
51
- print_error thor, :header => "An unexpected error occurred!"
52
- raise if thor.options["trace"]
53
- end
127
+ yield
128
+ rescue Nginxtra::Error::Base => e
129
+ e.output thor
130
+ raise if thor.options["trace"]
131
+ rescue
132
+ print_error thor, header: "An unexpected error occurred!"
133
+ raise if thor.options["trace"]
54
134
  end
55
135
  end
56
136
  end
@@ -5,14 +5,14 @@ module Nginxtra
5
5
  class CLI < Thor
6
6
  include Thor::Actions
7
7
 
8
- class_option "trace", :type => :boolean, :banner => "Output stack traces on error"
8
+ class_option "trace", type: :boolean, banner: "Output stack traces on error"
9
9
 
10
10
  map "-v" => "version"
11
11
 
12
12
  desc "server", "Start rails using nginxtra"
13
- method_option "port", :type => :numeric, :banner => "Specify the port to use to run the server on", :aliases => "-p", :default => 3000
14
- method_option "environment", :banner => "Specify the rails environment to run the server with", :aliases => "-e", :default => "development"
15
- method_option "verbose", :type => :boolean, :banner => "Attempts to output the log while the server is running", :aliases => "-V"
13
+ method_option "port", type: :numeric, banner: "Specify the port to use to run the server on", aliases: "-p", default: 3000
14
+ method_option "environment", banner: "Specify the rails environment to run the server with", aliases: "-e", default: "development"
15
+ method_option "verbose", type: :boolean, banner: "Attempts to output the log while the server is running", aliases: "-V"
16
16
  def server
17
17
  Nginxtra::Error.protect self do
18
18
  Nginxtra::Actions::Rails::Server.new(self, nil).server
@@ -4,18 +4,20 @@ module Nginxtra
4
4
  # The status class encapsulates current state of nginxtra, such as
5
5
  # when the last time nginx was compiled and with what options.
6
6
  class Status
7
- @@status = nil
8
7
  # The name of the file that stores the state.
9
8
  FILENAME = ".nginxtra_status".freeze
10
9
 
11
10
  class << self
11
+ attr_accessor :status
12
+ private :status, :status=
13
+
12
14
  # Retrieve an option from the state stored in the filesystem.
13
15
  # This will first load the state from the .nginxtra_status file
14
16
  # in the root of the nginxtra gem directory, if it has not yet
15
17
  # been loaded.
16
18
  def[](option)
17
19
  load!
18
- @@status[option]
20
+ status[option]
19
21
  end
20
22
 
21
23
  # Store an option to the status state. This will save out to
@@ -23,24 +25,25 @@ module Nginxtra
23
25
  # return value will be the value stored to the given option key.
24
26
  def[]=(option, value)
25
27
  load!
26
- @@status[option] = value
28
+ status[option] = value
27
29
  save!
28
30
  value
29
31
  end
30
32
 
31
33
  private
34
+
32
35
  # Load the status state, if it hasn't yet been loaded. If there
33
36
  # is no file yet existing, then the state is simply initialized
34
37
  # to an empty hash (and it is NOT stored to the filesystem til
35
38
  # the first write).
36
39
  def load!
37
- return if @@status
40
+ return if status
38
41
 
39
- if File.exists? path
40
- @@status = YAML.load File.read(path)
41
- else
42
- @@status = {}
43
- end
42
+ self.status = if File.exist? path
43
+ YAML.load File.read(path)
44
+ else
45
+ {}
46
+ end
44
47
  end
45
48
 
46
49
  # The full path to the file with the state.
@@ -51,7 +54,7 @@ module Nginxtra
51
54
  # Save the current state to disk.
52
55
  def save!
53
56
  File.open path, "w" do |file|
54
- file << YAML.dump(@@status)
57
+ file << YAML.dump(status)
55
58
  end
56
59
  end
57
60
  end