Class: ThinService::Command::Commands::Install

Inherits:
Base
  • Object
show all
Includes:
ServiceInstall
Defined in:
lib/thin_service/command.rb

Instance Attribute Summary

Attributes inherited from Base

#done_validating, #valid

Instance Method Summary collapse

Methods included from ServiceInstall

#add_service, #validate_thin_service_exe

Methods inherited from Base

#failure, #help, #initialize, #options, #valid?, #valid_dir?, #valid_exists?, #valid_file?

Constructor Details

This class inherits a constructor from ThinService::Command::Base

Instance Method Details

#configureObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/thin_service/command.rb', line 155

def configure
    options [
      ['-N', '--name SVC_NAME', "Required name for the service to be registered/installed.", :@svc_name, nil],
      ['-D', '--display SVC_DISPLAY', "Adjust the display name of the service.", :@svc_display, nil],
      ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
      ['-p', '--port PORT', "Which port to bind to", :@port, 3000],
      ['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"],             
      ['-t', '--timeout TIME', "Timeout for requests in seconds", :@timeout, 30],             
      ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],   
      ['-D', '--debug', "Enable debugging mode", :@debug, false],
      [''  , '--max-persistent-conns INT', "Maximum number of persistent connections", :@max_persistent_conns, 512],
      [''  , '--ssl', "Enables SSL", :@ssl, nil],
      [''  , '--ssl-key-file PATH', "Path to private key", :@ssl_key_file, nil],
      [''  , '--ssl-cert-file PATH', "Path to certificate", :@ssl_cert_file, nil],
      [''  , '--ssl-verify', "Enables SSL certificate verification", :@ssl_verify, nil],
      [''  , '--prefix PATH', "URL prefix for Rails app", :@prefix, nil],
    ]
end

#runObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/thin_service/command.rb', line 206

def run
 # check if thin_service.exe is in ruby bindir.
 bindir_executable = validate_thin_service_exe

 # build the command line
 argv = []

 # start using the native executable
 argv << '"' + bindir_executable + '"'

 # force indication of service mode
 argv << "start"

 # now the options
 argv << "-e #{@environment}" if @environment
 argv << "-p #{@port}"
 argv << "-a #{@address}"  if @address
 argv << "-c \"#{@cwd}\"" if @cwd
 argv << "-t #{@timeout}" if @timeout
 argv << "-D" if @debug
 argv << "--max-persistent-conns #{@max_persistent_conns}" if @max_persistent_conns
 argv << "--ssl" if @ssl
 argv << "--ssl-key-file \"#{@ssl_key_file}\"" if @ssl_key_file
 argv << "--ssl-cert-file \"#{@ssl_cert_file}\"" if @ssl_cert_file
 argv << "--ssl-verify" if @ssl_verify
 argv << "--prefix \"#{@prefix}\"" if @prefix

 # concat remaining non-parsed ARGV
 argv.concat(ARGV)

 add_service( @svc_name, @svc_display, argv)

end

#validateObject

When we validate the options, we need to make sure the –root is actually RAILS_ROOT of the rails application we wanted to serve, because later “as service” no error show to trace this.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/thin_service/command.rb', line 177

def validate
  @cwd = File.expand_path(@cwd)
  valid_dir? @cwd, "Invalid path to change to: #@cwd"

  # change there to start, then we'll have to come back after daemonize
  Dir.chdir(@cwd)

  valid? @svc_name != nil, "A service name is mandatory."
  valid? !ServiceManager.exist?(@svc_name), "The service already exist, please remove it first."

  # default service display to service name
  @svc_display = @svc_name if !@svc_display

  # start with the premise of app really exist.
  app_exist = true
  %w{app config log}.each do |path|
    if !File.directory?(File.join(@cwd, path))
      app_exist = false
      break
    end
  end

  valid?(@prefix[0].chr == "/" && @prefix[-1].chr != "/", "Prefix must begin with / and not end in /") if @prefix

  valid? app_exist == true, "The path you specified isn't a valid Rails application."

  return @valid
end