lib/splash/config.rb in prometheus-splash-0.8.1 vs lib/splash/config.rb in prometheus-splash-0.8.2

- old
+ new

@@ -9,62 +9,128 @@ include Splash::Helpers include Splash::Constants include Splash::ConfigUtilities + class ConfigLinter + def initialize + @lints_present = {:logs => [:label, :log, :pattern ]} + @lints_types = {:logs => {:label => Symbol, :log => String, :pattern => String, :retention => Hash}} + end + + def verify(options ={}) + status = :success + missings = [] + type_errors = [] + useless = [] + options[:record].each do |key,value| + useless.push key unless @lints_present[options[:type]].include? key or @lints_types[options[:type]].keys.include? key + type_errors.push key if @lints_types[options[:type]][key] != value.class and (@lints_present[options[:type]].include? key or @lints_types[options[:type]].keys.include? key) + end + @lints_present[options[:type]].each do |item| + missings.push item unless options[:record].keys.include? item + end + + status = :failure if (missings.count > 0) or (type_errors.count > 0) + return {:missings => missings, :type_errors => type_errors, :useless => useless, :status => status} + end + + end + # Class to manage configuration in Splash from Splash::Constants override by Yaml CONFIG class Configuration < Hash include Splash::Constants + attr_accessor :config_from_file + # constructor : read config file and map against Constants def initialize(config_file=CONFIG_FILE) - config_from_file = readconf config_file + @config_file = config_file + hash_config_to_default + + end + + + def hash_config_to_default + @config_from_file = readconf @config_file self[:version] = VERSION self[:author] = "#{AUTHOR} <#{EMAIL}>" self[:copyright] = "#{COPYRIGHT} #{LICENSE}" - self[:prometheus_url] = (config_from_file[:prometheus][:url])? config_from_file[:prometheus][:url] : PROMETHEUS_URL - self[:prometheus_pushgateway_url] = (config_from_file[:prometheus][:pushgateway])? config_from_file[:prometheus][:pushgateway] : PROMETHEUS_PUSHGATEWAY_URL - self[:prometheus_alertmanager_url] = (config_from_file[:prometheus][:alertmanager])? config_from_file[:prometheus][:alertmanager] : PROMETHEUS_ALERTMANAGER_URL + self[:prometheus_url] = (@config_from_file[:prometheus][:url])? @config_from_file[:prometheus][:url] : PROMETHEUS_URL + self[:prometheus_pushgateway_url] = (@config_from_file[:prometheus][:pushgateway])? @config_from_file[:prometheus][:pushgateway] : PROMETHEUS_PUSHGATEWAY_URL + self[:prometheus_alertmanager_url] = (@config_from_file[:prometheus][:alertmanager])? @config_from_file[:prometheus][:alertmanager] : PROMETHEUS_ALERTMANAGER_URL - self[:daemon_process_name] = (config_from_file[:daemon][:process_name])? config_from_file[:daemon][:process_name] : DAEMON_PROCESS_NAME - self[:daemon_logmon_scheduling] = (config_from_file[:daemon][:logmon_scheduling])? config_from_file[:daemon][:logmon_scheduling] : DAEMON_LOGMON_SCHEDULING - self[:daemon_metrics_scheduling] = (config_from_file[:daemon][:metrics_scheduling])? config_from_file[:daemon][:metrics_scheduling] : DAEMON_METRICS_SCHEDULING - self[:daemon_procmon_scheduling] = (config_from_file[:daemon][:procmon_scheduling])? config_from_file[:daemon][:procmon_scheduling] : DAEMON_PROCMON_SCHEDULING - self[:daemon_pid_file] = (config_from_file[:daemon][:files][:pid_file])? config_from_file[:daemon][:files][:pid_file] : DAEMON_PID_FILE - self[:daemon_stdout_trace] = (config_from_file[:daemon][:files][:stdout_trace])? config_from_file[:daemon][:files][:stdout_trace] : DAEMON_STDOUT_TRACE - self[:daemon_stderr_trace] = (config_from_file[:daemon][:files][:stderr_trace])? config_from_file[:daemon][:files][:stderr_trace] : DAEMON_STDERR_TRACE + self[:daemon_process_name] = (@config_from_file[:daemon][:process_name])? @config_from_file[:daemon][:process_name] : DAEMON_PROCESS_NAME + self[:daemon_logmon_scheduling] = (@config_from_file[:daemon][:logmon_scheduling])? @config_from_file[:daemon][:logmon_scheduling] : DAEMON_LOGMON_SCHEDULING + self[:daemon_metrics_scheduling] = (@config_from_file[:daemon][:metrics_scheduling])? @config_from_file[:daemon][:metrics_scheduling] : DAEMON_METRICS_SCHEDULING + self[:daemon_procmon_scheduling] = (@config_from_file[:daemon][:procmon_scheduling])? @config_from_file[:daemon][:procmon_scheduling] : DAEMON_PROCMON_SCHEDULING + self[:daemon_pid_file] = (@config_from_file[:daemon][:files][:pid_file])? @config_from_file[:daemon][:files][:pid_file] : DAEMON_PID_FILE + self[:daemon_stdout_trace] = (@config_from_file[:daemon][:files][:stdout_trace])? @config_from_file[:daemon][:files][:stdout_trace] : DAEMON_STDOUT_TRACE + self[:daemon_stderr_trace] = (@config_from_file[:daemon][:files][:stderr_trace])? @config_from_file[:daemon][:files][:stderr_trace] : DAEMON_STDERR_TRACE - self[:webadmin_port] = (config_from_file[:webadmin][:port])? config_from_file[:webadmin][:port] : WEBADMIN_PORT - self[:webadmin_ip] = (config_from_file[:webadmin][:ip])? config_from_file[:webadmin][:ip] : WEBADMIN_IP - self[:webadmin_proxy] = (config_from_file[:webadmin][:proxy])? config_from_file[:webadmin][:proxy] : WEBADMIN_PROXY - self[:webadmin_process_name] = (config_from_file[:webadmin][:process_name])? config_from_file[:webadmin][:process_name] : WEBADMIN_PROCESS_NAME - self[:webadmin_pid_file] = (config_from_file[:webadmin][:files][:pid_file])? config_from_file[:webadmin][:files][:pid_file] : WEBADMIN_PID_FILE - self[:webadmin_stdout_trace] = (config_from_file[:webadmin][:files][:stdout_trace])? config_from_file[:webadmin][:files][:stdout_trace] : WEBADMIN_STDOUT_TRACE - self[:webadmin_stderr_trace] = (config_from_file[:webadmin][:files][:stderr_trace])? config_from_file[:webadmin][:files][:stderr_trace] : WEBADMIN_STDERR_TRACE + self[:webadmin_port] = (@config_from_file[:webadmin][:port])? @config_from_file[:webadmin][:port] : WEBADMIN_PORT + self[:webadmin_ip] = (@config_from_file[:webadmin][:ip])? @config_from_file[:webadmin][:ip] : WEBADMIN_IP + self[:webadmin_proxy] = (@config_from_file[:webadmin][:proxy])? @config_from_file[:webadmin][:proxy] : WEBADMIN_PROXY + self[:webadmin_process_name] = (@config_from_file[:webadmin][:process_name])? @config_from_file[:webadmin][:process_name] : WEBADMIN_PROCESS_NAME + self[:webadmin_pid_file] = (@config_from_file[:webadmin][:files][:pid_file])? @config_from_file[:webadmin][:files][:pid_file] : WEBADMIN_PID_FILE + self[:webadmin_stdout_trace] = (@config_from_file[:webadmin][:files][:stdout_trace])? @config_from_file[:webadmin][:files][:stdout_trace] : WEBADMIN_STDOUT_TRACE + self[:webadmin_stderr_trace] = (@config_from_file[:webadmin][:files][:stderr_trace])? @config_from_file[:webadmin][:files][:stderr_trace] : WEBADMIN_STDERR_TRACE - self[:pid_path] = (config_from_file[:paths][:pid_path])? config_from_file[:paths][:pid_path] : PID_PATH - self[:trace_path] = (config_from_file[:paths][:trace_path])? config_from_file[:paths][:trace_path] : TRACE_PATH + self[:pid_path] = (@config_from_file[:paths][:pid_path])? @config_from_file[:paths][:pid_path] : PID_PATH + self[:trace_path] = (@config_from_file[:paths][:trace_path])? @config_from_file[:paths][:trace_path] : TRACE_PATH self[:execution_template_tokens] = EXECUTION_TEMPLATE_TOKENS_LIST - self[:execution_template_path] = (config_from_file[:templates][:execution][:path])? config_from_file[:templates][:execution][:path] : EXECUTION_TEMPLATE + self[:execution_template_path] = (@config_from_file[:templates][:execution][:path])? @config_from_file[:templates][:execution][:path] : EXECUTION_TEMPLATE - self[:transports] = {} ; self[:transports].merge! TRANSPORTS_STRUCT ; self[:transports].merge! config_from_file[:transports] if config_from_file[:transports] - self[:backends] = {} ; self[:backends].merge! BACKENDS_STRUCT ; self[:backends].merge! config_from_file[:backends] if config_from_file[:backends] - self[:loggers] = {} ; self[:loggers].merge! LOGGERS_STRUCT ; self[:loggers].merge! config_from_file[:loggers] if config_from_file[:loggers] + self[:transports] = {} ; self[:transports].merge! TRANSPORTS_STRUCT ; self[:transports].merge! @config_from_file[:transports] if @config_from_file[:transports] + self[:backends] = {} ; self[:backends].merge! BACKENDS_STRUCT ; self[:backends].merge! @config_from_file[:backends] if @config_from_file[:backends] + self[:loggers] = {} ; self[:loggers].merge! LOGGERS_STRUCT ; self[:loggers].merge! @config_from_file[:loggers] if @config_from_file[:loggers] - self[:processes] = (config_from_file[:processes])? config_from_file[:processes] : {} - self[:logs] = (config_from_file[:logs])? config_from_file[:logs] : {} - self[:commands] = (config_from_file[:commands])? config_from_file[:commands] : {} - self[:sequences] = (config_from_file[:sequences])? config_from_file[:sequences] : {} - self[:transfers] = (config_from_file[:transfers])? config_from_file[:transfers] : {} + self[:processes] = (@config_from_file[:processes])? @config_from_file[:processes] : {} + self[:logs] = (@config_from_file[:logs])? @config_from_file[:logs] : {} + self[:commands] = (@config_from_file[:commands])? @config_from_file[:commands] : {} + self[:sequences] = (@config_from_file[:sequences])? @config_from_file[:sequences] : {} + self[:transfers] = (@config_from_file[:transfers])? @config_from_file[:transfers] : {} + end + + def add_log(options = {}) + @config_from_file = readconf @config_file + res = ConfigLinter::new.verify(options) + if res[:status] == :success then + if @config_from_file[:logs].select{|item| item[:label] == options[:record][:label]}.count > 0 then + return {:status => :already_exist} + else + res[:useless].each {|item| options[:record].delete item} if options[:clean] + @config_from_file[:logs].push options[:record] + writeconf + hash_config_to_default + return {:status => :success} + end + else + return res + end end + + def delete_log(options = {}) + @config_from_file = readconf @config_file + unless @config_from_file[:logs].select{|item| item[:label] == options[:label]}.count > 0 then + return {:status => :not_found} + else + @config_from_file[:logs].delete_if {|value| options[:label] == value[:label] } + writeconf + hash_config_to_default + return {:status => :success} + end + end + + # @!group accessors on configurations Items # getter for full Config Hash # @return [Hash] def full @@ -270,9 +336,19 @@ # @return [Hash] The config global Hash from YAML def readconf(file = CONFIG_FILE) return YAML.load_file(file)[:splash] end + # write config to file from @config_from_file + # @param [String] file default from CONFIG_FILE + # @return [Bool] if ok + def writeconf(file = CONFIG_FILE) + File.open(file,"w") do |f| + data = {} + data[:splash] = @config_from_file + f.write(data.to_yaml) + end + end end