require 'yaml' require 'flydata/util/encryptor' module Fluent module DataEntryPreferenceConfigurable @@supported_custom_confs = Hash.new{|h,k| h[k] = {}} def self.included(base) base.extend ClassMethods base.class_eval do config_param :custom_conf_path, :string, default: nil config_param :key, :string, default: nil end end def load_custom_conf(file_path = @custom_conf_path) custom_conf = if file_path and File.exists?(file_path) YAML.load_file(file_path) else nil end @@supported_custom_confs.each do |type, settings| settings.each do |key, option| apply_custom_conf(custom_conf, key, type, option) if custom_conf apply_custom_option(key, option) end end end def apply_custom_conf(conf, key, type, option) if conf[type.to_s] and value = conf[type.to_s][key.to_s] var_name = option[:var_name] || key instance_variable_set(:"@#{var_name}", value) end end def apply_custom_option(key, option) var_name = option[:var_name] || key original_value = instance_variable_get(:"@#{var_name}") value = original_value option.each do |option_name, option_value| value = Fluent::DataEntryPreferenceConfigurable.replace_value_with_option( key, value, option_name, option_value, key: @key) end if original_value != value instance_variable_set(:"@#{var_name}", value) end end module ClassMethods def custom_config_param(key, type, option = {}) conf = class_variable_get(:@@supported_custom_confs) conf[type.to_sym][key.to_sym] = option end end def self.replace_value_with_option(param_name, param_value, option_name, option_value, opts = {}) ret = param_value case option_name when :encrypted if option_value ret = Flydata::Util::Encryptor.decrypt( param_value, opts[:key], param_name) end end ret end end module MysqlBinlogFlydataInputPreference CUSTOM_CONFIG_PARAMS = { mysql_data_entry_preference: { database: {}, tables: {}, tables_append_only: {}, host: {}, username: {}, password: {encrypted: true}, ssl_ca_content: {}, ssl_cipher: {}, }, } def self.included(base) base.class_eval do include DataEntryPreferenceConfigurable CUSTOM_CONFIG_PARAMS.each do |type, custom_conf| custom_conf.each do |key, option| custom_config_param key, type, option end end end end end end