Class: Service::Install

Inherits:
Object
  • Object
show all
Includes:
Mongrel::Command::Base
Defined in:
lib/mongrel_service/init.rb

Instance Method Summary collapse

Instance Method Details

#configureObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongrel_service/init.rb', line 12

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"],
      ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
      ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"],
      ['-n', '--num-procs INT', "Number of processors active before clients denied", :@num_procs, 1024],
      ['-t', '--timeout TIME', "Timeout all requests after 100th seconds time", :@timeout, 0],
      ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
      ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
      ['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"],
      ['-B', '--debug', "Enable debugging mode", :@debug, false],
      ['-C', '--config PATH', "Use a config file", :@config_file, nil],
      ['-S', '--script PATH', "Load the given file as an extra config script.", :@config_script, nil],
      ['', '--prefix PATH', "URL prefix for Rails app", :@prefix, nil]
    ]
end

#runObject



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
# File 'lib/mongrel_service/init.rb', line 71

def run
  # check if mongrel_service.exe is in ruby bindir.
  gem_root = File.join(File.dirname(__FILE__), "..", "..")
  gem_executable = File.join(gem_root, "resources/mongrel_service.exe")
  bindir_executable = File.join(Config::CONFIG['bindir'], '/mongrel_service.exe')

  unless File.exist?(bindir_executable)
    STDERR.puts "** Copying native mongrel_service executable..."
    FileUtils.cp gem_executable, bindir_executable rescue nil
  end

  unless FileUtils.compare_file(bindir_executable, gem_executable)
    STDERR.puts "** Updating native mongrel_service executable..."
    FileUtils.rm_f bindir_executable rescue nil
    FileUtils.cp gem_executable, bindir_executable rescue nil
  end

  # build the command line
  argv = []

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

  # force indication of service mode (workaround Windows 2008 psapi issue)
  argv << "service"

  # use the 'single' service for now
  argv << "single"

  # command line setting override config file settings
  @options = { :host => @address,  :port => @port, :cwd => @cwd,
    :log_file => @log_file, :pid_file => @pid_file, :environment => @environment,
    :docroot => @docroot, :mime_map => @mime_map,
    :debug => @debug, :includes => ["mongrel"], :config_script => @config_script,
    :num_procs => @num_procs, :timeout => @timeout, :cpu => @cpu, :prefix => @prefix
  }

  # if we are using a config file, pass -c and -C to the service instead of each start parameter.
  if @config_file
    STDERR.puts "** Using #{@config_file} instead of command line parameters."
    conf = YAML.load_file(@config_file)

    # add the root folder (-c)
    argv << "-c \"#{conf[:cwd]}\""

    # use the config file
    argv << "-C \"#{@config_file}\""

  else
    # use the command line instead
    # now the options
    argv << "-e #{@options[:environment]}" if @options[:environment]
    argv << "-p #{@options[:port]}"
    argv << "-a #{@options[:host]}"  if @options[:host]
    argv << "-l \"#{@options[:log_file]}\"" if @options[:log_file]
    argv << "-P \"#{@options[:pid_file]}\""
    argv << "-c \"#{@options[:cwd]}\"" if @options[:cwd]
    argv << "-t #{@options[:timeout]}" if @options[:timeout]
    argv << "-m \"#{@options[:mime_map]}\"" if @options[:mime_map]
    argv << "-r \"#{@options[:docroot]}\"" if @options[:docroot]
    argv << "-n #{@options[:num_procs]}" if @options[:num_procs]
    argv << "-B" if @options[:debug]
    argv << "-S \"#{@options[:config_script]}\"" if @options[:config_script]
    argv << "-u #{@options[:cpu.to_i]}" if @options[:cpu]
    argv << "--prefix \"#{@options[:prefix]}\"" if @options[:prefix]
  end

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

  begin
    ServiceManager.create(
      @svc_name,
      @svc_display,
      argv.join(' ')
    )
    puts "#{@svc_display} service created."
  rescue ServiceManager::CreateError => e
    puts "There was a problem installing the service:"
    puts e
  end
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.



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
# File 'lib/mongrel_service/init.rb', line 36

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."

  valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file"
  valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
  valid_dir? @docroot, "Path to docroot not valid: #@docroot"
  valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
  valid_exists? @config_file, "Config file not there: #@config_file" if @config_file

  return @valid
end