Class: LogStash::Config::Source::MultiLocal

Inherits:
Local
  • Object
show all
Includes:
Util::Loggable, Util::SubstitutionVariables
Defined in:
lib/logstash/config/source/multi_local.rb

Constant Summary

Constants included from Util::SubstitutionVariables

Util::SubstitutionVariables::SUBSTITUTION_PLACEHOLDER_REGEX

Constants inherited from Local

Local::HTTP_RE, Local::PIPELINE_ID

Instance Attribute Summary

Attributes inherited from Base

#conflict_messages

Instance Method Summary collapse

Methods included from Util::SubstitutionVariables

#deep_replace, #replace_placeholders

Methods inherited from Base

#both_module_configs?, #config_path, #config_path?, #config_path_setting, #config_reload_automatic, #config_reload_automatic?, #config_reload_automatic_setting, #config_string, #config_string?, #config_string_setting, #modules, #modules?, #modules_cli, #modules_cli?, #modules_cli_setting, #modules_defined?, #modules_setting

Constructor Details

#initialize(settings) ⇒ MultiLocal

Returns a new instance of MultiLocal.



10
11
12
13
14
# File 'lib/logstash/config/source/multi_local.rb', line 10

def initialize(settings)
  @original_settings = settings
  super(settings)
  @match_warning_done = false
end

Instance Method Details

#config_conflict?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/logstash/config/source/multi_local.rb', line 46

def config_conflict?
  @conflict_messages.clear
  # are there any auto-reload conflicts?
  if !(modules_cli? || modules? || config_string? || config_path?)
    detect_pipelines if !@detect_pipelines_called
    if @detected_marker.nil?
      @conflict_messages << I18n.t("logstash.runner.config-pipelines-failed-read", :path => pipelines_yaml_location)
    elsif @detected_marker == false
      @conflict_messages << I18n.t("logstash.runner.config-pipelines-empty", :path => pipelines_yaml_location)
    elsif @detected_marker.is_a?(Class)
      @conflict_messages << I18n.t("logstash.runner.config-pipelines-invalid", :invalid_class => @detected_marker, :path => pipelines_yaml_location)
    end
  else
    do_warning? && logger.warn("Ignoring the 'pipelines.yml' file because modules or command line options are specified")
  end
  @conflict_messages.any?
end

#detect_duplicate_pipelines(pipelines) ⇒ Object



89
90
91
92
93
94
# File 'lib/logstash/config/source/multi_local.rb', line 89

def detect_duplicate_pipelines(pipelines)
  duplicate_ids = pipelines.group_by {|pipeline| pipeline.get("pipeline.id") }.select {|k, v| v.size > 1 }.map {|k, v| k}
  if duplicate_ids.any?
    raise ConfigurationError.new("Pipelines YAML file contains duplicate pipeline ids: #{duplicate_ids.inspect}. Location: #{pipelines_yaml_location}")
  end
end

#detect_pipelinesObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/logstash/config/source/multi_local.rb', line 96

def detect_pipelines
  result = read_pipelines_from_yaml(pipelines_yaml_location) rescue nil
  if result.is_a?(Array)
    @detected_marker = true
  elsif result.nil?
    @detected_marker = nil
  elsif !result
    @detected_marker = false
  else
    @detected_marker = result.class
  end
  @detect_pipelines_called = true
end

#invalid_pipelines_detected?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/logstash/config/source/multi_local.rb', line 42

def invalid_pipelines_detected?
  !@detected_marker || @detected_marker.is_a?(Class)
end

#match?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/logstash/config/source/multi_local.rb', line 34

def match?
  if modules_cli? || modules? || config_string? || config_path?
    return false
  end
  detect_pipelines if !@detect_pipelines_called
  return !(invalid_pipelines_detected?)
end

#pipeline_configsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logstash/config/source/multi_local.rb', line 16

def pipeline_configs
  pipelines = deep_replace(retrieve_yaml_pipelines())
  pipelines_settings = pipelines.map do |pipeline_settings|
    clone = @original_settings.clone
    clone.merge_pipeline_settings(pipeline_settings)
  end
  detect_duplicate_pipelines(pipelines_settings)
  pipeline_configs = pipelines_settings.map do |pipeline_settings|
    @settings = pipeline_settings
    # this relies on instance variable @settings and the parent class' pipeline_configs
    # method. The alternative is to refactor most of the Local source methods to accept
    # a settings object instead of relying on @settings.
    local_pipeline_configs # create a PipelineConfig object based on @settings
  end.flatten
  @settings = @original_settings
  pipeline_configs
end

#pipelines_yaml_locationObject



85
86
87
# File 'lib/logstash/config/source/multi_local.rb', line 85

def pipelines_yaml_location
  ::File.join(@original_settings.get("path.settings"), "pipelines.yml")
end

#read_pipelines_from_yaml(yaml_location) ⇒ Object



78
79
80
81
82
83
# File 'lib/logstash/config/source/multi_local.rb', line 78

def read_pipelines_from_yaml(yaml_location)
  logger.debug("Reading pipeline configurations from YAML", :location => pipelines_yaml_location)
  ::YAML.load(IO.read(yaml_location))
rescue => e
  raise ConfigurationError.new("Failed to read pipelines yaml file. Location: #{yaml_location}, Exception: #{e.inspect}")
end

#retrieve_yaml_pipelinesObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/logstash/config/source/multi_local.rb', line 64

def retrieve_yaml_pipelines
  # by now, either the config_conflict? or the match? should have ruled out any config problems
  # but we don't rely on this, we can still get IO errors or
  result = read_pipelines_from_yaml(pipelines_yaml_location)
  case result
  when Array
    result
  when false
    raise ConfigurationError.new("Pipelines YAML file is empty. Path: #{pipelines_yaml_location}")
  else
    raise ConfigurationError.new("Pipelines YAML file must contain an array of pipeline configs. Found \"#{result.class}\" in #{pipelines_yaml_location}")
  end
end