Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the Windows Service name Fluentd runs as to be configurable. #1548

Merged
merged 2 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions lib/fluent/command/fluentd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@
if Fluent.windows?
require 'windows/library'
include Windows::Library

opts.merge!(
:winsvc_name => 'fluentdwinsvc',
:winsvc_display_name => 'Fluentd Windows Service',
:winsvc_desc => 'Fluentd is an event collector system.',
)

op.on('-x', '--signame INTSIGNAME', "an object name which is used for Windows Service signal (Windows only)") {|s|
opts[:signame] = s
Expand All @@ -174,6 +180,18 @@
op.on('--reg-winsvc-fluentdopt OPTION', "specify fluentd option paramters for Windows Service. (Windows only)") {|s|
opts[:fluentdopt] = s
}

op.on('--winsvc-name NAME', "The Windows Service name to run as (Windows only)") {|s|
opts[:winsvc_name] = s
}

op.on('--winsvc-display-name DISPLAY_NAME', "The Windows Service display name (Windows only)") {|s|
opts[:winsvc_display_name] = s
}

op.on('--winsvc-desc DESC', "The Windows Service description (Windows only)") {|s|
opts[:winsvc_desc] = s
}
end


Expand Down Expand Up @@ -229,9 +247,6 @@
early_exit = false
start_service = false
if winsvcinstmode = opts[:regwinsvc]
FLUENTD_WINSVC_NAME="fluentdwinsvc"
FLUENTD_WINSVC_DISPLAYNAME="Fluentd Windows Service"
FLUENTD_WINSVC_DESC="Fluentd is an event collector system."
require 'fileutils'
require "win32/service"
require "win32/registry"
Expand All @@ -249,42 +264,43 @@
start_service = true
end


Service.create(
service_name: FLUENTD_WINSVC_NAME,
service_name: opts[:winsvc_name],
host: nil,
service_type: Service::WIN32_OWN_PROCESS,
description: FLUENTD_WINSVC_DESC,
description: opts[:winsvc_desc],
start_type: start_type,
error_control: Service::ERROR_NORMAL,
binary_path_name: "\"#{ruby_path}\" -C \"#{binary_path}\" winsvc.rb",
binary_path_name: "\"#{ruby_path}\" -C \"#{binary_path}\" winsvc.rb --service-name #{opts[:winsvc_name]}",
load_order_group: "",
dependencies: [""],
display_name: FLUENTD_WINSVC_DISPLAYNAME
display_name: opts[:winsvc_display_name]
)
when 'u'
if Service.status(FLUENTD_WINSVC_NAME).current_state != 'stopped'
if Service.status(opts[:winsvc_name]).current_state != 'stopped'
begin
Service.stop(FLUENTD_WINSVC_NAME)
Service.stop(opts[:winsvc_name])
rescue => ex
puts "Warning: Failed to stop service: ", ex
end
end
Service.delete(FLUENTD_WINSVC_NAME)
Service.delete(opts[:winsvc_name])
else
# none
end
early_exit = true
end

if fluentdopt = opts[:fluentdopt]
Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\fluentdwinsvc", Win32::Registry::KEY_ALL_ACCESS) do |reg|
Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\#{opts[:winsvc_name]}", Win32::Registry::KEY_ALL_ACCESS) do |reg|
reg['fluentdopt', Win32::Registry::REG_SZ] = fluentdopt
end
early_exit = true
end

if start_service
Service.start(FLUENTD_WINSVC_NAME)
Service.start(opts[:winsvc_name])
end

exit 0 if early_exit
Expand Down
1 change: 0 additions & 1 deletion lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ def self.default_options
supervise: true,
standalone_worker: false,
signame: nil,
winsvcreg: nil,
}
end

Expand Down
36 changes: 25 additions & 11 deletions lib/fluent/winsvc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
# limitations under the License.
#

INTEVENTOBJ_NAME = "fluentdwinsvc"

begin

require 'optparse'
require 'windows/debug'
require 'Windows/Library'
require 'win32/daemon'
Expand All @@ -27,34 +26,50 @@
include Windows::Library
include Windows::Debug

def read_fluentdopt
op = OptionParser.new
opts = {service_name: nil}
op.on('--service-name NAME', "The name of the Windows Service") {|name|
opts[:service_name] = name
}
op.parse(ARGV)
if opts[:service_name] == nil
raise "Error: No Windows Service name set. Use '--service-name'"
end

def read_fluentdopt(service_name)
require 'win32/Registry'
Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\fluentdwinsvc") do |reg|
Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\#{service_name}") do |reg|
reg.read("fluentdopt")[1] rescue ""
end
end

def service_main_start
def service_main_start(service_name)
ruby_path = 0.chr * 260
GetModuleFileName.call(0, ruby_path,260)
ruby_path = ruby_path.rstrip.gsub(/\\/, '/')
rubybin_dir = ruby_path[0, ruby_path.rindex("/")]
opt = read_fluentdopt
Process.spawn("\"#{rubybin_dir}/ruby.exe\" \"#{rubybin_dir}/fluentd\" #{opt} -x #{INTEVENTOBJ_NAME}")
opt = read_fluentdopt(service_name)
Process.spawn("\"#{rubybin_dir}/ruby.exe\" \"#{rubybin_dir}/fluentd\" #{opt} -x #{service_name}")
end

class FluentdService < Daemon
@pid = 0
@service_name = ''

def initialize(service_name)
@service_name = service_name
end

def service_main
@pid = service_main_start

@pid = service_main_start(@service_name)
while running?
sleep 10
end
end

def service_stop
ev = Win32::Event.open(INTEVENTOBJ_NAME)
ev = Win32::Event.open(@service_name)
ev.set
ev.close
if @pid > 0
Expand All @@ -63,9 +78,8 @@ def service_stop
end
end

FluentdService.mainloop
FluentdService.new(opts[:service_name]).mainloop

rescue Exception => err
raise
end