mirror of
https://github.com/fluent/fluentd-ui.git
synced 2025-08-19 21:41:11 +02:00
Merge pull request #152 from fluent/dryrun_button_to_config_edit
Dryrun button to config edit
This commit is contained in:
commit
a4c88f23aa
@ -17,14 +17,24 @@ class Fluentd::SettingsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
Fluent::Config::V1Parser.parse(params[:config], @fluentd.config_file)
|
if params[:dryrun]
|
||||||
@fluentd.agent.config_write params[:config]
|
if dryrun(params[:config])
|
||||||
@fluentd.agent.restart if @fluentd.agent.running?
|
flash.now[:success] = I18n.t('messages.dryrun_is_passed')
|
||||||
redirect_to daemon_setting_path(@fluentd)
|
else
|
||||||
rescue Fluent::ConfigParseError => e
|
flash.now[:danger] = @fluentd.agent.last_error_message
|
||||||
@config = params[:config]
|
end
|
||||||
@error = e.message
|
@config = params[:config]
|
||||||
render "edit"
|
render "edit"
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
update_config(params[:config])
|
||||||
|
redirect_to daemon_setting_path(@fluentd)
|
||||||
|
rescue Fluent::ConfigParseError => e
|
||||||
|
@config = params[:config]
|
||||||
|
flash.now[:danger] = e.message
|
||||||
|
render "edit"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def source_and_output
|
def source_and_output
|
||||||
@ -37,4 +47,17 @@ class Fluentd::SettingsController < ApplicationController
|
|||||||
def set_config
|
def set_config
|
||||||
@config = @fluentd.agent.config
|
@config = @fluentd.agent.config
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def dryrun(conf)
|
||||||
|
tmpfile = Tempfile.open("fluentd-test-config")
|
||||||
|
tmpfile.write params[:config]
|
||||||
|
tmpfile.close
|
||||||
|
@fluentd.agent.dryrun(tmpfile.path)
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_config(conf)
|
||||||
|
Fluent::Config::V1Parser.parse(conf, @fluentd.config_file)
|
||||||
|
@fluentd.agent.config_write conf
|
||||||
|
@fluentd.agent.restart if @fluentd.agent.running?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -23,5 +23,6 @@ class Fluentd
|
|||||||
# td-agent: /etc/td-agent/td-agent.conf
|
# td-agent: /etc/td-agent/td-agent.conf
|
||||||
# - https://github.com/treasure-data/td-agent/blob/master/debian/td-agent.postinst#L69
|
# - https://github.com/treasure-data/td-agent/blob/master/debian/td-agent.postinst#L69
|
||||||
# fluentd: /etc/fluent/fluent.conf (by fluentd -s)
|
# fluentd: /etc/fluent/fluent.conf (by fluentd -s)
|
||||||
|
class ConfigError < StandardError; end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -45,6 +45,10 @@ class Fluentd
|
|||||||
errors
|
errors
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def last_error_message
|
||||||
|
recent_errors(1).first.try(:[], :subject) || ""
|
||||||
|
end
|
||||||
|
|
||||||
def pid_file
|
def pid_file
|
||||||
extra_options[:pid_file] || self.class.default_options[:pid_file]
|
extra_options[:pid_file] || self.class.default_options[:pid_file]
|
||||||
end
|
end
|
||||||
|
@ -42,12 +42,20 @@ class Fluentd
|
|||||||
actual_reload
|
actual_reload
|
||||||
end
|
end
|
||||||
|
|
||||||
def dryrun
|
def dryrun!(file_path = nil)
|
||||||
Bundler.with_clean_env do
|
Bundler.with_clean_env do
|
||||||
system("fluentd -q --dry-run #{options_to_argv}")
|
system("fluentd -q --dry-run #{options_to_argv(config_file: file_path)}", out: File::NULL, err: File::NULL)
|
||||||
|
raise ::Fluentd::Agent::ConfigError, last_error_message unless $?.exitstatus.zero?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def config_syntax_check
|
||||||
|
Fluent::Config::V1Parser.parse(params[:config], config_file)
|
||||||
|
true
|
||||||
|
rescue Fluent::ConfigParseError
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
def version
|
def version
|
||||||
Bundler.with_clean_env do
|
Bundler.with_clean_env do
|
||||||
`fluentd --version`.strip
|
`fluentd --version`.strip
|
||||||
@ -56,15 +64,6 @@ class Fluentd
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def options_to_argv
|
|
||||||
argv = ""
|
|
||||||
argv << " --use-v1-config"
|
|
||||||
argv << " -c #{config_file}"
|
|
||||||
argv << " -d #{pid_file}"
|
|
||||||
argv << " -o #{log_file}"
|
|
||||||
argv
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_fluentd_options
|
def validate_fluentd_options
|
||||||
dryrun
|
dryrun
|
||||||
end
|
end
|
||||||
|
@ -73,6 +73,13 @@ class Fluentd
|
|||||||
backup_files_in_old_order.reverse
|
backup_files_in_old_order.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def dryrun(file_path = nil)
|
||||||
|
dryrun!(file_path)
|
||||||
|
true
|
||||||
|
rescue ::Fluentd::Agent::ConfigError
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def backup_config
|
def backup_config
|
||||||
@ -173,6 +180,15 @@ class Fluentd
|
|||||||
thread.join
|
thread.join
|
||||||
thread.value.exitstatus.zero?
|
thread.value.exitstatus.zero?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def options_to_argv(opts = {})
|
||||||
|
argv = ""
|
||||||
|
argv << " --use-v1-config"
|
||||||
|
argv << " -c #{opts[:config_file] || config_file}"
|
||||||
|
argv << " -d #{opts[:pid_file] || pid_file}"
|
||||||
|
argv << " -o #{opts[:log_file] || log_file}"
|
||||||
|
argv
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -16,6 +16,13 @@ class Fluentd
|
|||||||
`/usr/sbin/td-agent --version`.strip
|
`/usr/sbin/td-agent --version`.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def dryrun!(file_path = nil)
|
||||||
|
Bundler.with_clean_env do
|
||||||
|
system("/usr/sbin/td-agent --dry-run #{options_to_argv(config_file: file_path)}", out: File::NULL, err: File::NULL)
|
||||||
|
raise ::Fluentd::Agent::ConfigError, last_error_message unless $?.exitstatus.zero?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
case FluentdUI.platform
|
case FluentdUI.platform
|
||||||
when :macosx
|
when :macosx
|
||||||
include Macosx
|
include Macosx
|
||||||
|
@ -16,10 +16,6 @@ class Fluentd
|
|||||||
dryrun && stop && start
|
dryrun && stop && start
|
||||||
end
|
end
|
||||||
|
|
||||||
def dryrun
|
|
||||||
detached_command("/usr/sbin/td-agent --dry-run -q --use-v1-config -c #{config_file}")
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def plist
|
def plist
|
||||||
|
@ -17,10 +17,6 @@ class Fluentd
|
|||||||
# https://github.com/treasure-data/td-agent/blob/master/debian/td-agent.init#L156
|
# https://github.com/treasure-data/td-agent/blob/master/debian/td-agent.init#L156
|
||||||
detached_command('/etc/init.d/td-agent restart')
|
detached_command('/etc/init.d/td-agent restart')
|
||||||
end
|
end
|
||||||
|
|
||||||
def dryrun
|
|
||||||
detached_command('/etc/init.d/td-agent configtest')
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
- page_title t('.page_title', label: @fluentd.label)
|
- page_title t('.page_title', label: @fluentd.label)
|
||||||
|
|
||||||
- if @error
|
|
||||||
%pre.alert.alert-danger= @error
|
|
||||||
|
|
||||||
= form_tag(daemon_setting_path(@fluentd), method: :patch) do
|
= form_tag(daemon_setting_path(@fluentd), method: :patch) do
|
||||||
.form-group
|
.form-group
|
||||||
= text_area_tag "config", @config, class: "form-control", rows: 40, class: "js-fluentd-config-editor"
|
= text_area_tag "config", @config, class: "form-control", rows: 40, class: "js-fluentd-config-editor"
|
||||||
%p.text.text-danger= t('terms.notice_restart_for_config_edit', brand: fluentd_ui_brand)
|
%p.text.text-danger= t('terms.notice_restart_for_config_edit', brand: fluentd_ui_brand)
|
||||||
= submit_tag t("terms.update"), class: "btn btn-primary"
|
= submit_tag t("terms.update"), class: "btn btn-primary"
|
||||||
|
= submit_tag t("terms.configtest"), class: "btn btn-default", name: "dryrun"
|
||||||
|
@ -6,3 +6,8 @@ checkout:
|
|||||||
post:
|
post:
|
||||||
- mkdir -p tmp
|
- mkdir -p tmp
|
||||||
- sync
|
- sync
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
pre:
|
||||||
|
- wget http://packages.treasuredata.com.s3.amazonaws.com/2/ubuntu/precise/pool/contrib/t/td-agent/td-agent_2.1.3-0_amd64.deb
|
||||||
|
- sudo dpkg -i td-agent_2.1.3-0_amd64.deb
|
||||||
|
@ -96,32 +96,9 @@ describe Fluentd::Agent do
|
|||||||
describe "#restart" do
|
describe "#restart" do
|
||||||
it_should_behave_like "Restart strategy"
|
it_should_behave_like "Restart strategy"
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#dryrun" do
|
|
||||||
subject { instance.dryrun }
|
|
||||||
|
|
||||||
describe "valid/invalid" do
|
|
||||||
before { instance.stub(:system).and_return(ret) }
|
|
||||||
|
|
||||||
context "valid config" do
|
|
||||||
let(:ret) { true }
|
|
||||||
it { should be_truthy }
|
|
||||||
end
|
|
||||||
|
|
||||||
context "invalid config" do
|
|
||||||
let(:ret) { false }
|
|
||||||
it { should be_falsy }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it "invoke #system" do
|
|
||||||
instance.should_receive(:system).with(/--dry-run/)
|
|
||||||
subject
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "TdAgent" do
|
describe "TdAgent", td_agent_required: true do
|
||||||
let(:described_class) { Fluentd::Agent::TdAgent } # override nested described_class behavior as https://github.com/rspec/rspec-core/issues/1114
|
let(:described_class) { Fluentd::Agent::TdAgent } # override nested described_class behavior as https://github.com/rspec/rspec-core/issues/1114
|
||||||
|
|
||||||
it_should_behave_like "Fluentd::Agent has common behavior"
|
it_should_behave_like "Fluentd::Agent has common behavior"
|
||||||
@ -153,30 +130,6 @@ describe Fluentd::Agent do
|
|||||||
expect(File.read(backup_file)).to eq File.read(instance.config_file)
|
expect(File.read(backup_file)).to eq File.read(instance.config_file)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#dryrun" do
|
|
||||||
subject { instance.dryrun }
|
|
||||||
|
|
||||||
describe "valid/invalid" do
|
|
||||||
before { instance.stub(:detached_command).and_return(ret) }
|
|
||||||
|
|
||||||
context "valid config" do
|
|
||||||
let(:ret) { true }
|
|
||||||
it { should be_truthy }
|
|
||||||
end
|
|
||||||
|
|
||||||
context "invalid config" do
|
|
||||||
let(:ret) { false }
|
|
||||||
it { should be_falsy }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it "invoke #system" do
|
|
||||||
# --dry-run check on Mac, configtest for Unix
|
|
||||||
instance.should_receive(:detached_command).with(/(--dry-run|configtest)/)
|
|
||||||
subject
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -62,6 +62,12 @@ RSpec.configure do |config|
|
|||||||
# rspec 2.99
|
# rspec 2.99
|
||||||
config.infer_spec_type_from_file_location!
|
config.infer_spec_type_from_file_location!
|
||||||
|
|
||||||
|
unless File.directory?("/opt/td-agent")
|
||||||
|
# including td-agent specific tests, so some tests will fail if the system has no td-agent
|
||||||
|
warn "\n\nSkipping td-agent specific tests (system has no td-agent)\n\n"
|
||||||
|
config.filter_run_excluding :td_agent_required => true
|
||||||
|
end
|
||||||
|
|
||||||
config.after(:suite) do
|
config.after(:suite) do
|
||||||
FileUtils.rm_rf FluentdUI.data_dir
|
FileUtils.rm_rf FluentdUI.data_dir
|
||||||
end
|
end
|
||||||
|
@ -124,5 +124,58 @@ shared_examples_for "Fluentd::Agent has common behavior" do |klass|
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#dryrun" do
|
||||||
|
let(:root) { FluentdUI.data_dir + "/tmp/agentspec/" }
|
||||||
|
let(:dummy_log_file) { root + "dummy.log" }
|
||||||
|
let(:dummy_pid_file) { root + "dummy.pid" }
|
||||||
|
|
||||||
|
before do
|
||||||
|
FileUtils.mkdir_p root
|
||||||
|
instance.stub(:log_file).and_return(dummy_log_file)
|
||||||
|
instance.stub(:pid_file).and_return(dummy_pid_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "valid/invalid" do
|
||||||
|
let(:config_path) { Rails.root.join("tmp", "fluent-test.conf").to_s }
|
||||||
|
before { File.write(config_path, config) }
|
||||||
|
after { File.unlink(config_path) }
|
||||||
|
|
||||||
|
context "valid config" do
|
||||||
|
let(:config) { <<-CONF.strip_heredoc }
|
||||||
|
<source>
|
||||||
|
type forward
|
||||||
|
</source>
|
||||||
|
CONF
|
||||||
|
|
||||||
|
context "with `!`" do
|
||||||
|
subject { instance.dryrun!(config_path) }
|
||||||
|
it { expect { subject }.to_not raise_error }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "without `!`" do
|
||||||
|
subject { instance.dryrun(config_path) }
|
||||||
|
it { should be_truthy }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "invalid config" do
|
||||||
|
let(:config) { <<-CONF.strip_heredoc }
|
||||||
|
<source>
|
||||||
|
type forward
|
||||||
|
CONF
|
||||||
|
|
||||||
|
context "with `!`" do
|
||||||
|
subject { instance.dryrun!(config_path) }
|
||||||
|
it { expect { subject }.to raise_error(Fluentd::Agent::ConfigError) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "without `!`" do
|
||||||
|
subject { instance.dryrun(config_path) }
|
||||||
|
it { should be_falsy }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user