Skip to content

Commit

Permalink
Supervisor specify the mode of directory
Browse files Browse the repository at this point in the history
Signed-off-by: Yuta Iwama <ganmacs@gmail.com>
  • Loading branch information
ganmacs committed Feb 19, 2020
1 parent deb2207 commit 16c7edc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,13 @@ def reopen!
self
end

def apply_options(format: nil, time_format: nil)
def apply_options(format: nil, time_format: nil, log_dir_perm: nil)
$log.format = format if format
$log.time_format = time_format if time_format

if @path && log_dir_perm
File.chmod(log_dir_perm || 0755, File.dirname(@path))
end
end

def level=(level)
Expand Down Expand Up @@ -529,7 +533,7 @@ def run_supervisor(dry_run: false)
end
else
begin
FileUtils.mkdir_p(root_dir)
FileUtils.mkdir_p(root_dir, mode: @system_config.dir_permission || 0755)
rescue => e
raise Fluent::InvalidRootDirectory, "failed to create root directory:#{root_dir}, #{e.inspect}"
end
Expand Down Expand Up @@ -620,7 +624,11 @@ def configure(supervisor: false)
@system_config = build_system_config(@conf)

@log.level = @system_config.log_level
@log.apply_options(format: @system_config.log.format, time_format: @system_config.log.time_format)
@log.apply_options(
format: @system_config.log.format,
time_format: @system_config.log.time_format,
log_dir_perm: @system_config.dir_permission,
)

$log.info :supervisor, 'parsing config file is succeeded', path: @config_path

Expand Down
17 changes: 17 additions & 0 deletions test/command/test_fluentd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,23 @@ def write(chunk)
)
end

test 'success to start a worker2 with worker specific configuration' do
conf = <<CONF
<system>
root_dir #{@root_path}
dir_permission 0744
</system>
CONF
conf_path = create_conf_file('worker_section0.conf', conf)

FileUtils.rm_rf(@root_path) rescue nil

assert_path_not_exist(@root_path)
assert_log_matches(create_cmdline(conf_path), 'spawn command to main') # any message is ok
assert_path_exist(@root_path)
assert_equal '744', File.stat(@root_path).mode.to_s(8)[-3, 3]
end

test 'success to start a worker with worker specific configuration' do
conf = <<CONF
<system>
Expand Down
18 changes: 18 additions & 0 deletions test/test_logger_initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,28 @@ class LoggerInitializerTest < ::Test::Unit::TestCase

assert_false File.exist?(TMP_DIR)
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {})
mock.proxy(File).chmod(0o777, TMP_DIR).never

assert_nothing_raised do
logger.init(:supervisor, 0)
end

assert_true File.exist?(TMP_DIR)
end

test 'apply_options with log_dir_perm' do
path = File.join(TMP_DIR, 'fluent_with_path.log')

assert_false File.exist?(TMP_DIR)
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {})
mock.proxy(File).chmod(0o777, TMP_DIR).once

assert_nothing_raised do
logger.init(:supervisor, 0)
end

logger.apply_options(log_dir_perm: 0o777)
assert_true File.exist?(TMP_DIR)
assert_equal 0o777, (File.stat(TMP_DIR).mode & 0xFFF)
end
end

0 comments on commit 16c7edc

Please sign in to comment.