diff --git a/app/models/fluentd.rb b/app/models/fluentd.rb index caaac6e..cde0640 100644 --- a/app/models/fluentd.rb +++ b/app/models/fluentd.rb @@ -11,7 +11,6 @@ class Fluentd before_validation :expand_paths COLUMNS = [:id, :variant, :log_file, :pid_file, :config_file] - JSON_PATH = (ENV["FLUENTD_UI_DATA_DIR"].presence || Rails.root.join("db").to_s) + "/#{Rails.env}-fluentd.json" DEFAULT_CONF = <<-CONF.strip_heredoc # http://docs.fluentd.org/articles/in_forward @@ -46,6 +45,16 @@ class Fluentd %w(fluentd_gem td-agent) end + def self.json_path + if ENV["FLUENTD_UI_DATA_DIR"].present? + dir = ENV["FLUENTD_UI_DATA_DIR"] + else + dir = ENV["HOME"] + "/.fluentd-ui/core_data" + end + FileUtils.mkdir_p(dir) + dir + "/#{Rails.env}-fluentd.json" + end + def fluentd? variant == "fluentd_gem" end @@ -138,12 +147,12 @@ class Fluentd def self.instance return unless exists? - attr = JSON.parse(File.read(JSON_PATH)) + attr = JSON.parse(File.read(json_path)) Fluentd.new(attr) end def self.exists? - File.exists?(JSON_PATH) + File.exists?(json_path) end def update_attributes(params) @@ -158,12 +167,12 @@ class Fluentd result[col] = send(col) result end.to_json - File.open(JSON_PATH, "w") do |f| + File.open(self.class.json_path, "w") do |f| f.write json end && ensure_default_config_file end def destroy - File.unlink(JSON_PATH) + File.unlink(self.class.json_path) end end diff --git a/app/models/fluentd/agent/fluentd_gem.rb b/app/models/fluentd/agent/fluentd_gem.rb index 7c75fd2..a44a088 100644 --- a/app/models/fluentd/agent/fluentd_gem.rb +++ b/app/models/fluentd/agent/fluentd_gem.rb @@ -5,12 +5,10 @@ class Fluentd include LocalCommon def self.default_options - # TODO: current default options from fluentd --setup, but root permission required for them. - # It could be more user friendly values { - :pid_file => "/var/run/fluent.pid", - :log_file => "/var/log/fluent.log", - :config_file => "/etc/fluent/fluent.conf", + :pid_file => "#{ENV["HOME"]}/.fluentd-ui/fluent.pid", + :log_file => "#{ENV["HOME"]}/.fluentd-ui/fluent.log", + :config_file => "#{ENV["HOME"]}/.fluentd-ui/fluent.conf", } end diff --git a/app/views/fluentd/_form.html.haml b/app/views/fluentd/_form.html.haml index bd3a8e7..e32f0e8 100644 --- a/app/views/fluentd/_form.html.haml +++ b/app/views/fluentd/_form.html.haml @@ -9,11 +9,23 @@ = @fluentd.variant %div.form-group = f.label :pid_file - = f.text_field :pid_file, class: "form-control" + - if @fluentd.td_agent? + = f.text_field :pid_file, class: "form-control", disabled: true + = f.hidden_field :pid_file + - else + = f.text_field :pid_file, class: "form-control" %div.form-group = f.label :log_file - = f.text_field :log_file, class: "form-control" + - if @fluentd.td_agent? + = f.text_field :log_file, class: "form-control", disabled: true + = f.hidden_field :log_file + - else + = f.text_field :log_file, class: "form-control" %div.form-group = f.label :config_file - = f.text_field :config_file, class: "form-control" + - if @fluentd.td_agent? + = f.text_field :config_file, class: "form-control", disabled: true + = f.hidden_field :config_file + - else + = f.text_field :config_file, class: "form-control" = f.submit btn, class: "btn btn-primary" diff --git a/spec/models/fluentd_spec.rb b/spec/models/fluentd_spec.rb index b201071..e080c49 100644 --- a/spec/models/fluentd_spec.rb +++ b/spec/models/fluentd_spec.rb @@ -56,7 +56,7 @@ describe Fluentd do end let(:fluentd) { build(:fluentd) } - after { File.unlink(Fluentd::JSON_PATH) if File.exist?(Fluentd::JSON_PATH) } + after { File.unlink(Fluentd.json_path) if File.exist?(Fluentd.json_path) } describe "#valid?" do before do