From c833682cc886bc714d4c3ea07266f409b2072f2e Mon Sep 17 00:00:00 2001 From: uu59 Date: Tue, 8 Jul 2014 18:20:26 +0900 Subject: [PATCH] Add basis out_mongo setting --- app/assets/stylesheets/common.css.scss | 7 ++ .../fluentd/settings/out_mongo_controller.rb | 36 ++++++++++ app/models/fluentd/setting/out_mongo.rb | 63 +++++++++++++++++ .../fluentd/settings/in_tail/_form.html.haml | 1 - .../settings/out_mongo/_form.html.haml | 67 +++++++++++++++++++ .../settings/out_mongo/finish.html.haml | 4 ++ .../fluentd/settings/out_mongo/show.html.haml | 3 + config/routes.rb | 4 ++ 8 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 app/controllers/fluentd/settings/out_mongo_controller.rb create mode 100644 app/models/fluentd/setting/out_mongo.rb create mode 100644 app/views/fluentd/settings/out_mongo/_form.html.haml create mode 100644 app/views/fluentd/settings/out_mongo/finish.html.haml create mode 100644 app/views/fluentd/settings/out_mongo/show.html.haml diff --git a/app/assets/stylesheets/common.css.scss b/app/assets/stylesheets/common.css.scss index f55adf1..6144ce4 100644 --- a/app/assets/stylesheets/common.css.scss +++ b/app/assets/stylesheets/common.css.scss @@ -116,3 +116,10 @@ label { cursor: pointer; } + + +.ignore-rails-error-div { + .field_with_errors { + display: inline; + } +} diff --git a/app/controllers/fluentd/settings/out_mongo_controller.rb b/app/controllers/fluentd/settings/out_mongo_controller.rb new file mode 100644 index 0000000..ffc6802 --- /dev/null +++ b/app/controllers/fluentd/settings/out_mongo_controller.rb @@ -0,0 +1,36 @@ +class Fluentd::Settings::OutMongoController < ApplicationController + before_action :login_required + before_action :find_fluentd + + def show + @setting = Fluentd::Setting::OutMongo.new({ + host: "127.0.0.1", + port: 27017, + capped: true, + capped_size: "100m", + }) + end + + def finish + @setting = Fluentd::Setting::OutMongo.new(setting_params) + unless @setting.valid? + return render "show" + end + + @fluentd.agent.config_append @setting.to_conf + if @fluentd.agent.running? + unless @fluentd.agent.restart + @setting.errors.add(:base, @fluentd.agent.log_tail(1).first) + return render "show" + end + end + redirect_to fluentd_setting_path(@fluentd) + end + + private + + def setting_params + params.require(:fluentd_setting_out_mongo).permit(*Fluentd::Setting::OutMongo::KEYS) + end + +end diff --git a/app/models/fluentd/setting/out_mongo.rb b/app/models/fluentd/setting/out_mongo.rb new file mode 100644 index 0000000..0ed00c0 --- /dev/null +++ b/app/models/fluentd/setting/out_mongo.rb @@ -0,0 +1,63 @@ +class Fluentd + module Setting + class OutMongo + include ActiveModel::Model + + KEYS = [ + :host, :port, :database, :collection, :capped, :capped_size, :capped_max, :user, :password, :tag_mapped, + :buffer_type, :buffer_queue_limit, :buffer_chunk_limit, :flush_interval, :retry_wait, :retry_limit, :max_retry_wait, :num_threads, + ].freeze + + attr_accessor(*KEYS) + + validates :host, presence: true + validates :port, presence: true + validates :database, presence: true + validate :validate_capped + validate :validate_collection + + def to_conf + <<-XML.strip_heredoc.gsub(/^[ ]*\n/m, "") + + type mongo + #{print_if_present :host} + #{print_if_present :port} + #{print_if_present :database} + #{print_if_present :collection} + #{print_if_present :user} + #{print_if_present :password} + + #{self.capped.present? ? "capped" : ""} + #{print_if_present :capped_size} + #{print_if_present :capped_max} + + #{self.tag_mapped.present? ? "tag_mapped" : ""} + #{print_if_present :buffer_type} + #{print_if_present :buffer_queue_limit} + #{print_if_present :buffer_chunk_limit} + #{print_if_present :flush_interval} + #{print_if_present :retry_wait} + #{print_if_present :retry_limit} + #{print_if_present :max_retry_wait} + #{print_if_present :num_threads} + + XML + end + + def print_if_present(key) + send(key).present? ? "#{key} #{send(key)}" : "" + end + + def validate_capped + return true if capped.blank? + errors.add(:capped_size, :blank) if capped_size.blank? + end + + def validate_collection + if tag_mapped.blank? && collection.blank? + errors.add(:collection, :blank) + end + end + end + end +end diff --git a/app/views/fluentd/settings/in_tail/_form.html.haml b/app/views/fluentd/settings/in_tail/_form.html.haml index 8e1c488..3e06dd6 100644 --- a/app/views/fluentd/settings/in_tail/_form.html.haml +++ b/app/views/fluentd/settings/in_tail/_form.html.haml @@ -1,4 +1,3 @@ - - @setting.errors.full_messages.each do |e| %div.alert.alert-danger= e diff --git a/app/views/fluentd/settings/out_mongo/_form.html.haml b/app/views/fluentd/settings/out_mongo/_form.html.haml new file mode 100644 index 0000000..a540f9b --- /dev/null +++ b/app/views/fluentd/settings/out_mongo/_form.html.haml @@ -0,0 +1,67 @@ +- @setting.errors.full_messages.each do |msg| + = msg + += form_for(@setting, url: finish_fluentd_setting_out_mongo_path(@fluentd), html: {class: "ignore-rails-error-div"}) do |f| + .form-group + = f.label :host + = f.text_field :host + .form-group + = f.label :port + = f.text_field :port + .form-group + = f.label :database + = f.text_field :database + .form-group + = f.label :collection + = f.text_field :collection + .form-group + = f.label :tag_mapped + = f.check_box :tag_mapped, {}, "checked", nil + .form-group + = f.label :user + = f.text_field :user + .form-group + = f.label :password + = f.text_field :password + + .well.well-sm + %h4{"data-toggle" => "collapse", "href" => "#advanced-setting"} + = icon('fa-caret-down') + = t('terms.advanced_setting') + #advanced-setting.collapse + .form-group + = f.label :capped + = f.check_box :capped, {}, "checked", nil + .form-group + = f.label :capped_size + = f.text_field :capped_size + .form-group + = f.label :capped_max + = f.text_field :capped_max + .form-group + = f.label :buffer_type + = f.text_field :buffer_type + .form-group + = f.label :buffer_queue_limit + = f.text_field :buffer_queue_limit + .form-group + = f.label :buffer_chunk_limit + = f.text_field :buffer_chunk_limit + .form-group + = f.label :flush_interval + = f.text_field :flush_interval + .form-group + = f.label :retry_wait + = f.text_field :retry_wait + .form-group + = f.label :retry_limit + = f.text_field :retry_limit + .form-group + = f.label :max_retry_wait + = f.text_field :max_retry_wait + .form-group + = f.label :num_threads + = f.text_field :num_threads + = f.submit t('fluentd.common.finish') , class: "btn btn-lg btn-primary pull-right" + = f.submit t('terms.prev'), class: "btn btn-lg btn-default", name: "back" + diff --git a/app/views/fluentd/settings/out_mongo/finish.html.haml b/app/views/fluentd/settings/out_mongo/finish.html.haml new file mode 100644 index 0000000..3f5a71e --- /dev/null +++ b/app/views/fluentd/settings/out_mongo/finish.html.haml @@ -0,0 +1,4 @@ +- page_title t(".page_title") + += render "form" + diff --git a/app/views/fluentd/settings/out_mongo/show.html.haml b/app/views/fluentd/settings/out_mongo/show.html.haml new file mode 100644 index 0000000..38ae0d9 --- /dev/null +++ b/app/views/fluentd/settings/out_mongo/show.html.haml @@ -0,0 +1,3 @@ +- page_title t(".page_title") + += render "form" diff --git a/config/routes.rb b/config/routes.rb index 858c8f0..44bcb60 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,10 @@ Rails.application.routes.draw do post "confirm" post "finish" end + + resource :out_mongo, only: ["show"], module: :settings, controller: :out_mongo do + post "finish" + end end end