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

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

Constant Summary

Constants inherited from Local

Local::HTTP_RE, Local::PIPELINE_ID

Instance Attribute Summary

Attributes inherited from Base

#conflict_messages

Instance Method Summary collapse

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.



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

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

Instance Method Details

#config_conflict?Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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)


40
41
42
# File 'lib/logstash/config/source/multi_local.rb', line 40

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

#match?Boolean

Returns:

  • (Boolean)


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

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



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

def pipeline_configs
  pipelines = retrieve_yaml_pipelines()
  pipelines_settings = pipelines.map do |pipeline_settings|
    ::LogStash::PipelineSettings.from_settings(@original_settings.clone).merge(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



83
84
85
# File 'lib/logstash/config/source/multi_local.rb', line 83

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

#read_pipelines_from_yaml(yaml_location) ⇒ Object



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

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



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

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