require 'fileutils' require 'flydata/fluent-plugins/preference' module Flydata module Preference class DataEntryPreference CONFS_HOME = File.join(FLYDATA_HOME, 'confs') CUSTOM_CONFIG_PARAMS = { RedshiftMysqlDataEntry: ::Fluent::MysqlBinlogFlydataInputPreference::CUSTOM_CONFIG_PARAMS } class << self # data_entry must be hash def load_conf(data_entry) path = conf_path(data_entry) raise "Conf file does not exist. path:#{path}" unless File.exists?(path) custom_conf = YAML::load(File.open(path, 'r')) data_entry.each_key do |k| v = custom_conf[k] case v when Hash data_entry[k].merge!(v) when NilClass else data_entry[k] = v end end filter_data_entry(data_entry) data_entry end def filter_data_entry(de) configurable_params = CUSTOM_CONFIG_PARAMS[de['type'].to_sym] return de if configurable_params.nil? or configurable_params.empty? configurable_params.each do |pref_name, param_info| # skip if the data entry doesn't have the information de_pref = de[pref_name.to_s] next unless de_pref param_info.each do |param_name, options| next if options.nil? or options.empty? param_name_str = param_name.to_s next if de_pref[param_name_str].nil? or de_pref[param_name_str].empty? options.each do |option_name, option_value| de_pref[param_name_str] = Fluent::DataEntryPreferenceConfigurable.replace_value_with_option( param_name_str, de_pref[param_name_str], option_name, option_value, key: de['data_port_key']) end end end end def copy_template(data_entry) return unless File.exists?(conf_template_path(data_entry)) FileUtils.mkdir_p(CONFS_HOME) FileUtils.cp(conf_template_path(data_entry), conf_path(data_entry)+".tmpl") # Do not overwrite a current conf file unless File.exists?(conf_path(data_entry)) FileUtils.cp(conf_template_path(data_entry), conf_path(data_entry)) end end def conf_path(data_entry) "#{File.join(CONFS_HOME, data_entry['name'])}.conf" end def conf_exists?(data_entry) File.exists?(conf_path(data_entry)) end def conf_name(data_entry) "#{data_entry['name']}.conf" end def configurable?(data_entry) File.exists?(conf_template_path(data_entry)) end def conf_template_path(data_entry) File.join(FLYDATA_TMPL_DIR, conf_template_name(data_entry)) end def conf_template_name(data_entry) type = ActiveSupport::Inflector.underscore(data_entry['type']) "#{type}.conf.tmpl" end def create(type) custom_config_params = CUSTOM_CONFIG_PARAMS[type] return nil unless custom_config_params end end end end end