Class: LogStash::Config::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/config/loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Loader

Returns a new instance of Loader.



4
5
6
7
# File 'lib/logstash/config/loader.rb', line 4

def initialize(logger)
  @logger = logger
  @config_debug = LogStash::SETTINGS.get_value("config.debug")
end

Instance Method Details

#fetch_config(uri) ⇒ Object

def load_config



100
101
102
103
104
105
106
# File 'lib/logstash/config/loader.rb', line 100

def fetch_config(uri)
  begin
    Net::HTTP.get(uri) + "\n"
  rescue Exception => e
    fail(I18n.t("logstash.runner.configuration.fetch-failed", :path => uri.to_s, :message => e.message))
  end
end

#format_config(config_path, config_string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/logstash/config/loader.rb', line 9

def format_config(config_path, config_string)
  config_string = config_string.to_s
  if config_path
    # Append the config string.
    # This allows users to provide both -f and -e flags. The combination
    # is rare, but useful for debugging.
    loaded_config = load_config(config_path)
    if loaded_config.empty? && config_string.empty?
      # If loaded config from `-f` is empty *and* if config string is empty we raise an error
      fail(I18n.t("logstash.runner.configuration.file-not-found", :path => config_path))
    end

    # tell the user we are merging, otherwise it is very confusing
    if !loaded_config.empty? && !config_string.empty?
      @logger.info("Created final config by merging config string and config path", :path => config_path)
    end

    config_string = config_string + loaded_config
  else
    # include a default stdin input if no inputs given
    if config_string !~ /input *{/
      config_string += LogStash::Config::Defaults.input
    end
    # include a default stdout output if no outputs given
    if config_string !~ /output *{/
      config_string += LogStash::Config::Defaults.output
    end
  end
  config_string
end

#load_config(path) ⇒ Object



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

def load_config(path)
  begin
    uri = URI.parse(path)

    case uri.scheme
    when nil then
      local_config(path)
    when /http/ then
      fetch_config(uri)
    when "file" then
      local_config(uri.path)
    else
      fail(I18n.t("logstash.runner.configuration.scheme-not-supported", :path => path))
    end
  rescue URI::InvalidURIError
    # fallback for windows.
    # if the parsing of the file failed we assume we can reach it locally.
    # some relative path on windows arent parsed correctly (.\logstash.conf)
    local_config(path)
  end
end

#local_config(path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/logstash/config/loader.rb', line 62

def local_config(path)
  path = ::File.expand_path(path)
  path = ::File.join(path, "*") if ::File.directory?(path)

  config = ""
  if Dir.glob(path).length == 0
    @logger.info("No config files found in path", :path => path)
    return config
  end

  encoding_issue_files = []
  Dir.glob(path).sort.each do |file|
    next unless ::File.file?(file)
    if file.match(/~$/)
      @logger.debug("NOT reading config file because it is a temp file", :config_file => file)
      next
    end
    @logger.debug("Reading config file", :config_file => file)
    cfg = ::File.read(file)
    if !cfg.ascii_only? && !cfg.valid_encoding?
      encoding_issue_files << file
    end
    config << cfg + "\n"
    if @config_debug
      @logger.debug? && @logger.debug("\nThe following is the content of a file", :config_file => file.to_s)
      @logger.debug? && @logger.debug("\n" + cfg + "\n\n")
    end
  end
  if encoding_issue_files.any?
    fail("The following config files contains non-ascii characters but are not UTF-8 encoded #{encoding_issue_files}")
  end
  if @config_debug
    @logger.debug? && @logger.debug("\nThe following is the merged configuration")
    @logger.debug? && @logger.debug("\n" + config + "\n\n")
  end
  return config
end