Add out_forward setting basis (wip)

This commit is contained in:
uu59 2014-07-16 13:26:27 +09:00
parent fec145a9d6
commit 3ddb412846
12 changed files with 217 additions and 8 deletions

View File

@ -0,0 +1,7 @@
(function(){
"use strict";
$(function(){
});
})();

View File

@ -123,3 +123,9 @@ label {
display: inline; display: inline;
} }
} }
.form-group {
.form-group {
margin-left: 30px; // used at fluentd/settings/out_forward, nested config form
}
}

View File

@ -0,0 +1,34 @@
class Fluentd::Settings::OutForwardController < ApplicationController
before_action :login_required
before_action :find_fluentd
def show
@setting = Fluentd::Setting::OutForward.new({
})
end
def finish
@setting = Fluentd::Setting::OutForward.new(setting_params)
unless @setting.valid?
return render "show"
end
@fluentd.agent.config_append @setting.to_config
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_forward).permit(*Fluentd::Setting::OutForward::KEYS).merge(
params.require(:fluentd_setting_out_forward).permit(:server => Fluentd::Setting::OutForward::Server::KEYS)
)
end
end

View File

@ -9,6 +9,18 @@ module SettingsHelper
html << form.check_box(key, {}, "true", "false") html << form.check_box(key, {}, "true", "false")
when :choice when :choice
html << form.select(key, form.object.values_of(key), opts) html << form.select(key, form.object.values_of(key), opts)
when :nested
child_data = form.object.class.children[key]
klass = child_data[:class]
children = form.object.send(key) || {"0" => {}}
children.each_pair do |index, child|
# TODO: allow append/delete for multiple child
form.fields_for("#{key}[#{index}]", klass.new(child), class: "nested-column #{child_data[:multiple] ? "multiple" : ""} well well-sm") do |ff|
klass::KEYS.each do |k|
html << field(ff, k)
end
end
end
else else
html << form.text_field(key) html << form.text_field(key)
end end

View File

@ -2,9 +2,10 @@ class Fluentd
module Setting module Setting
module Common module Common
extend ActiveSupport::Concern extend ActiveSupport::Concern
include ActiveModel::Model
module ClassMethods module ClassMethods
attr_accessor :values, :types attr_accessor :values, :types, :children
def choice(key, values) def choice(key, values)
@values ||= {} @values ||= {}
@ -12,6 +13,22 @@ class Fluentd
set_type(:choice, [key]) set_type(:choice, [key])
end end
def nested(key, klass, options = {})
# e.g.:
# <match>
# type forward
# <server>
# ..
# </server>
# </match>
@children ||= {}
@children[key] = {
class: klass,
options: options,
}
set_type(:nested, [key])
end
def booleans(*keys) def booleans(*keys)
# e.g.: # e.g.:
# use_ssl true # use_ssl true
@ -36,12 +53,16 @@ class Fluentd
end end
end end
def child_class(key)
self.class.children[key][:class]
end
def values_of(key) def values_of(key)
self.class.values[key] || [] self.class.values.try(:[], key) || []
end end
def column_type(key) def column_type(key)
self.class.types[key] || "string" self.class.types.try(:[], key) || "string"
end end
def conf(key) def conf(key)
@ -50,6 +71,26 @@ class Fluentd
boolenan(key) boolenan(key)
when :flag when :flag
flag(key) flag(key)
when :nested
klass = child_class(key)
send(key).map do |(_, child)|
# send("servers")
#
# "servers" => {
# "0" => {
# "name" => "foo",
# "host" => "bar",
# ..
# },
# "1" => {
# ..
# }
# }
child_instance = klass.new(child)
unless child_instance.empty_value?
"\n" + child_instance.to_config(key).gsub(/^/m, " ")
end
end.join
else else
print_if_present(key) print_if_present(key)
end end
@ -77,17 +118,33 @@ class Fluentd
send(key).presence == "true" ? key.to_s : "" send(key).presence == "true" ? key.to_s : ""
end end
def to_config def empty_value?
config = ""
self.class.const_get(:KEYS).each do |key|
config << conf(key)
end
config.empty?
end
def to_config(elm_name = nil)
indent = " " indent = " "
config = "<match #{match}>\n" if elm_name
config << "#{indent}type #{plugin_type_name}\n" config = "<#{elm_name}>\n"
else
config = "<match #{match}>\n"
config << "#{indent}type #{plugin_type_name}\n"
end
self.class.const_get(:KEYS).each do |key| self.class.const_get(:KEYS).each do |key|
next if key == :match next if key == :match
config << indent config << indent
config << conf(key) config << conf(key)
config << "\n" config << "\n"
end end
config << "</match>\n" if elm_name
config << "</#{elm_name}>\n"
else
config << "</match>\n"
end
config.gsub(/^[ ]*\n/m, "") config.gsub(/^[ ]*\n/m, "")
end end
end end

View File

@ -0,0 +1,48 @@
class Fluentd
module Setting
class OutForward
class Server
include Common
KEYS = [
:name, :host, :port, :weight, :standby
].freeze
attr_accessor(*KEYS)
flags :standby
validates :host, presence: true
end
include Common
KEYS = [
:match,
:send_timeout, :recover_wait, :heartbeat_type, :heartbeat_interval,
:phi_threshold, :hard_timeout,
:server
].freeze
attr_accessor(*KEYS)
choice :heartbeat_type, %w(udp tcp)
nested :server, Server
validates :match, presence: true
validate :validate_at_least_one_server
validate :validate_nested_values
def validate_at_least_one_server
# FIXME: real validation
true
end
def validate_nested_values
# FIXME: real validation with child class instance
self.class.children.inject(true) do |result, child|
# result & child.valid?
end
true
end
end
end
end

View File

@ -0,0 +1,22 @@
- @setting.errors.full_messages.each do |msg|
= msg
= form_for(@setting, url: finish_fluentd_setting_out_forward_path(@fluentd), html: {class: "ignore-rails-error-div"}) do |f|
= field(f, :match)
= field(f, :server)
.well.well-sm
%h4{"data-toggle" => "collapse", "href" => "#advanced-setting"}
= icon('fa-caret-down')
= t('terms.advanced_setting')
#advanced-setting.collapse
= field(f, :send_timeout)
= field(f, :recover_wait)
= field(f, :heartbeat_type)
= field(f, :heartbeat_interval)
= field(f, :phi_threshold)
= field(f, :hard_timeout)
= 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"

View File

@ -0,0 +1,6 @@
- page_title t(".page_title")
.well
= raw t('fluentd.settings.out_forward.option_guide')
= render "form"

View File

@ -36,3 +36,7 @@
= link_to(fluentd_setting_out_mongo_path(@fluentd)) do = link_to(fluentd_setting_out_mongo_path(@fluentd)) do
= icon('fa-file-text-o fa-lg') = icon('fa-file-text-o fa-lg')
= t("fluentd.common.setup_out_mongo") = t("fluentd.common.setup_out_mongo")
%p
= link_to(fluentd_setting_out_forward_path(@fluentd)) do
= icon('fa-file-text-o fa-lg')
= t("fluentd.common.setup_out_forward")

View File

@ -96,6 +96,7 @@ en:
setup_in_syslog: Setting Syslog readling setup_in_syslog: Setting Syslog readling
setup_out_td: Setting Treasure Data writing setup_out_td: Setting Treasure Data writing
setup_out_mongo: Setting MongoDB writing setup_out_mongo: Setting MongoDB writing
setup_out_forward: forward(TODO)
finish: Update config finish: Update config
fluentd_info: Setting info fluentd_info: Setting info
recent_errors: "Recently %{days} days errors" recent_errors: "Recently %{days} days errors"
@ -128,9 +129,12 @@ en:
out: Output source setting out: Output source setting
edit: edit:
<<: *fluentd_common <<: *fluentd_common
out_forward:
option_guide: |
show:
page_title: out_forward(TODO)
out_td: out_td:
option_guide: | option_guide: |
fluent-plugin-td install is required.<br />
show: show:
page_title: Write to Treasure Data setting page_title: Write to Treasure Data setting
out_mongo: out_mongo:

View File

@ -96,6 +96,7 @@ ja:
setup_in_syslog: syslog読み込みの設定 setup_in_syslog: syslog読み込みの設定
setup_out_td: Treasure Dataへの書き出し設定 setup_out_td: Treasure Dataへの書き出し設定
setup_out_mongo: MongoDBへの書き出し設定 setup_out_mongo: MongoDBへの書き出し設定
setup_out_forward: forward(TODO)
finish: 設定する finish: 設定する
fluentd_info: 設定情報 fluentd_info: 設定情報
recent_errors: "直近 %{days} 日内のエラー" recent_errors: "直近 %{days} 日内のエラー"
@ -129,6 +130,10 @@ ja:
out: 出力ソースの設定 out: 出力ソースの設定
edit: edit:
<<: *fluentd_common <<: *fluentd_common
out_forward:
option_guide: |
show:
page_title: out_forward(TODO)
out_td: out_td:
option_guide: | option_guide: |
fluent-plugin-tdプラグインのインストールが必要です。<br /> fluent-plugin-tdプラグインのインストールが必要です。<br />

View File

@ -33,6 +33,10 @@ Rails.application.routes.draw do
resource :out_s3, only: ["show"], module: :settings, controller: :out_s3 do resource :out_s3, only: ["show"], module: :settings, controller: :out_s3 do
post "finish" post "finish"
end end
resource :out_forward, only: ["show"], module: :settings, controller: :out_forward do
post "finish"
end
end end
end end