Add Fluentd::Agent::Common#config_merge

Signed-off-by: Kenji Okimoto <okimoto@clear-code.com>
This commit is contained in:
Kenji Okimoto 2018-09-13 15:34:39 +09:00
parent 653a86cfd6
commit 82c62df65b
No known key found for this signature in database
GPG Key ID: F9E3E329A5C5E4A1
2 changed files with 86 additions and 4 deletions

View File

@ -76,6 +76,25 @@ class Fluentd
end end
end end
def config_merge(content)
if content.start_with?("<label ")
label = content.slice(/<label\s+(.+?)>/, 1)
key = "label:#{label}"
parsed_config = parse_config(config)
if parsed_config.key?(key)
offset = parsed_config[key][0][:pos] + parsed_config[key][0][:size]
label, sections = parse_label_section(content, offset)
parsed_config[key][0][:sections]["filter"].concat(sections["filter"])
parsed_config[key][0][:sections]["match"].concat(sections["match"])
config_write(dump_parsed_config(parsed_config))
else
config_append(content)
end
else
config_append(content)
end
end
def configuration def configuration
if File.exists? config_file if File.exists? config_file
::Fluentd::Agent::Configuration.new(config_file) ::Fluentd::Agent::Configuration.new(config_file)
@ -160,7 +179,7 @@ class Fluentd
when "label" when "label"
label_content = header + scanner.scan_until(%r{^</label>}) label_content = header + scanner.scan_until(%r{^</label>})
label, sections = parse_label_section(label_content, started) label, sections = parse_label_section(label_content, started)
contents["label:#{label}"] << { label: label, pos: started, sections: sections } contents["label:#{label}"] << { label: label, pos: started, sections: sections, size: label_content.size }
else else
raise TypeError, "Unknown section: #{started}: #{section_type}" raise TypeError, "Unknown section: #{started}: #{section_type}"
end end

View File

@ -80,7 +80,7 @@ class Fluentd
{ pos: 0, content: source_section_content } { pos: 0, content: source_section_content }
], ],
"label:@INPUT" => [ "label:@INPUT" => [
{ label: "@INPUT", pos: 60, sections: sections } { label: "@INPUT", pos: 60, sections: sections, size: 116 }
] ]
} }
assert_equal(expected, actual) assert_equal(expected, actual)
@ -157,10 +157,10 @@ class Fluentd
{ pos: 61, content: source_section_content2 }, { pos: 61, content: source_section_content2 },
], ],
"label:@INPUT" => [ "label:@INPUT" => [
{ label: "@INPUT", pos: 124, sections: input_sections } { label: "@INPUT", pos: 124, sections: input_sections, size: 136 }
], ],
"label:@MAIN" => [ "label:@MAIN" => [
{ label: "@MAIN", pos: 260, sections: main_sections } { label: "@MAIN", pos: 260, sections: main_sections, size: 211 }
] ]
} }
assert_equal(expected, actual) assert_equal(expected, actual)
@ -186,5 +186,68 @@ class Fluentd
assert_equal(fixture_content("config/multi-label.conf"), config) assert_equal(fixture_content("config/multi-label.conf"), config)
end end
end end
sub_test_case "#config_merge" do
test "simple" do
stub(@agent).config { fixture_content("config/simple.conf") }
stub(@agent).backup_config
content = <<-CONFIG
<match dummy3>
@type stdout
</match>
CONFIG
mock(@agent).config_append(content)
@agent.config_merge(content)
end
test "simple with label" do
stub(@agent).config { fixture_content("config/simple.conf") }
stub(@agent).backup_config
content = <<-CONFIG
<label @INPUT>
<match dummy3>
@type stdout
</match>
</label>
CONFIG
mock(@agent).config_append(content)
@agent.config_merge(content)
end
test "append to label" do
stub(@agent).config { fixture_content("config/label.conf") }
stub(@agent).config_file { "tmp/fluent.conf" }
stub(@agent).backup_config
content = <<-CONFIG
<label @INPUT>
<match dummy3>
@type stdout
</match>
</label>
CONFIG
mock(@agent).config_write(<<-CONFIG)
<source>
@type dummy
tag dummy
@label @INPUT
</source>
<label @INPUT>
<filter dummy>
@type stdout
</filter>
<match dummy>
@type stdout
</match>
<match dummy3>
@type stdout
</match>
</label>
CONFIG
@agent.config_merge(content)
end
end
end end
end end