1 # Ferret Win32 Service Daemon install script
\r
2 # created by Herryanto Siatono <herryanto@pluitsolutions.com>
\r
4 # see doc/README.win32 for usage instructions
\r
7 require 'win32/service'
\r
11 # Parse and validate service command and options
\r
12 class FerretServiceCommand
\r
13 COMMANDS = ['install', 'remove', 'start', 'stop', 'help']
\r
14 BANNER = "Usage: ruby script/ferret_service <command> [options]"
\r
16 attr_reader :options, :command
\r
23 COMMANDS.include?@command
\r
27 @options[:name] and !@options[:name].empty?
\r
30 def print_command_list
\r
32 puts "\nAvailable commands:\n"
\r
33 puts COMMANDS.map {|cmd| " - #{cmd}\n"}
\r
34 puts "\nUse option -h for each command to help."
\r
38 def validate_options
\r
40 errors << "Service name is required." unless @options[:name]
\r
42 if (errors.size > 0)
\r
43 errors << "Error found. Use: 'ruby script/ferret_service #{@command} -h' for to get help."
\r
44 puts errors.join("\n")
\r
50 @command = args.shift
\r
51 @command = @command.dup.downcase if @command
\r
53 # validate command and options
\r
54 print_command_list unless valid_command? or @command == 'help'
\r
56 opts_parser = create_options_parser
\r
58 opts_parser.parse!(args)
\r
59 rescue OptionParser::ParseError => e
\r
64 # validate required options
\r
68 def create_options_parser
\r
69 opts_parser = OptionParser.new
\r
70 opts_parser.banner = BANNER
\r
71 opts_parser.on("-n", "--name=NAME", "Service name") {|name| @options[:name] = name }
\r
72 opts_parser.on_tail("-t", "--trace", "Display stack trace when exception thrown") { @options[:trace] = true }
\r
73 opts_parser.on_tail("-h", "--help", "Show this help message") { puts opts_parser; exit }
\r
75 if ['install'].include?@command
\r
76 opts_parser.on("-d", "--display=NAME", "Service display name") {|name| @options[:display] = name }
\r
78 opts_parser.on("-l", "--log FILE", "Service log file") {|file| @options[:log] = file }
\r
79 opts_parser.on("-e", "--environment ENV ", "Rails environment") { |env|
\r
80 @options[:environment] = env
\r
81 ENV['RAILS_ENV'] = env
\r
88 # Install, Remove, Start and Stop Ferret DRb server Win32 service
\r
90 FERRET_DAEMON = 'ferret_daemon'
\r
99 if Service.exists?(@options[:name])
\r
100 puts "Service name '#{@options[:name]}' already exists."
\r
104 svc.create_service do |s|
\r
105 s.service_name = @options[:name]
\r
106 s.display_name = @options[:display]
\r
107 s.binary_path_name = binary_path_name
\r
108 s.dependencies = []
\r
112 puts "'#{@options[:name]}' service installed."
\r
120 Service.stop(@options[:name])
\r
125 Service.delete(@options[:name])
\r
126 puts "'#{@options[:name]}' service removed."
\r
134 Service.start(@options[:name])
\r
135 puts "'#{@options[:name]}' successfully started."
\r
143 Service.stop(@options[:name])
\r
144 puts "'#{@options[:name]}' successfully stopped.\n"
\r
151 svc_cmd = FerretServiceCommand.new
\r
153 @options = svc_cmd.options
\r
154 self.send(svc_cmd.command.to_sym)
\r
158 def handle_error(e)
\r
159 if @options[:trace]
\r
166 def binary_path_name
\r
168 path << "#{ENV['RUBY_HOME']}/bin/" if ENV['RUBY_HOME']
\r
169 path << "ruby.exe "
\r
170 path << File.expand_path("script/" + FERRET_DAEMON)
\r
171 path << " -e #{@options[:environment]} " if @options[:environment]
\r
172 path << " -l #{@options[:log]} " if @options[:log]
\r
178 Ferret::FerretService.new.run(ARGV)
\r