Skip to content

Commit c4c7b53

Browse files
authored
out_file: add symlink_path_use_relative option to use relative path instead of absolute path in symlink_path (#4904)
**Which issue(s) this PR fixes**: None. **What this PR does / why we need it**: This PR moves from an absolute link to a relative link for `symlink_path` parameter. Otherwise, this does not work in a dockerized environment where the path is wired to the the outside of the container. I can also add an additional parameter to switch between absolute and relative linking if necessary and/or desired. **Docs Changes**: TODO https://docs.fluentd.org/input/tail **Release Note**: Same as the title. --------- Signed-off-by: Florian Dobener <florian.dobener@dwd.de> Signed-off-by: domna <florian.dobener@physik.hu-berlin.de>
1 parent fa2eb58 commit c4c7b53

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/fluent/plugin/out_file.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require 'fileutils'
1818
require 'zlib'
1919
require 'time'
20+
require 'pathname'
2021

2122
require 'fluent/plugin/output'
2223
require 'fluent/config/error'
@@ -54,6 +55,8 @@ class FileOutput < Output
5455
config_param :recompress, :bool, default: false
5556
desc "Create symlink to temporary buffered file when buffer_type is file (disabled on Windows)."
5657
config_param :symlink_path, :string, default: nil
58+
desc "Use relative path for symlink target (default: false)"
59+
config_param :symlink_path_use_relative, :bool, default: false
5760

5861
config_section :format do
5962
config_set_default :@type, 'out_file'
@@ -97,7 +100,12 @@ def generate_chunk(metadata)
97100
if chunk.metadata == @latest_metadata
98101
sym_path = @_output_plugin_for_symlink.extract_placeholders(@_symlink_path, chunk)
99102
FileUtils.mkdir_p(File.dirname(sym_path), mode: @_output_plugin_for_symlink.dir_perm)
100-
FileUtils.ln_sf(chunk.path, sym_path)
103+
if @_output_plugin_for_symlink.symlink_path_use_relative
104+
relative_path = Pathname.new(chunk.path).relative_path_from(Pathname.new(File.dirname(sym_path)))
105+
FileUtils.ln_sf(relative_path, sym_path)
106+
else
107+
FileUtils.ln_sf(chunk.path, sym_path)
108+
end
101109
end
102110
chunk
103111
end

test/plugin/test_out_file.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,24 @@ def parse_system(text)
779779
end
780780
end
781781

782-
def run_and_check(d, symlink_path)
782+
test 'relative symlink' do
783+
omit "Windows doesn't support symlinks" if Fluent.windows?
784+
785+
conf = CONFIG + %[
786+
symlink_path #{SYMLINK_PATH}
787+
symlink_path_use_relative true
788+
]
789+
symlink_path = "#{SYMLINK_PATH}"
790+
791+
d = create_driver(conf)
792+
begin
793+
run_and_check(d, symlink_path, relative_symlink=true)
794+
ensure
795+
FileUtils.rm_rf(symlink_path)
796+
end
797+
end
798+
799+
def run_and_check(d, symlink_path, relative_symlink=false)
783800
d.run(default_tag: 'tag') do
784801
es = Fluent::OneEventStream.new(event_time("2011-01-02 13:14:15 UTC"), {"a"=>1})
785802
d.feed(es)
@@ -794,7 +811,14 @@ def run_and_check(d, symlink_path)
794811
assert File.exist?(symlink_path)
795812

796813
meta = d.instance.metadata('tag', event_time("2011-01-03 14:15:16 UTC"), {})
797-
assert_equal d.instance.buffer.instance_eval{ @stage[meta].path }, File.readlink(symlink_path)
814+
if relative_symlink
815+
target_path = d.instance.buffer.instance_eval{ @stage[meta].path }
816+
link_target = File.readlink(symlink_path)
817+
expected_path = Pathname.new(target_path).relative_path_from(Pathname.new(File.dirname(symlink_path))).to_s
818+
assert_equal expected_path, link_target
819+
else
820+
assert_equal d.instance.buffer.instance_eval{ @stage[meta].path }, File.readlink(symlink_path)
821+
end
798822
end
799823
end
800824
end

0 commit comments

Comments
 (0)