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
# File 'lib/logstash/config/loader.rb', line 4

def initialize(logger)
  @logger = logger
end

Instance Method Details

#fetch_config(uri) ⇒ Object

def load_config



83
84
85
86
87
88
89
# File 'lib/logstash/config/loader.rb', line 83

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/logstash/config/loader.rb', line 8

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.
    config_string = config_string + load_config(config_path)
  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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/logstash/config/loader.rb', line 28

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/logstash/config/loader.rb', line 50

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

  if Dir.glob(path).length == 0
    fail(I18n.t("logstash.runner.configuration.file-not-found", :path => path))
  end

  config = ""
  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"
    @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
  if encoding_issue_files.any?
    fail("The following config files contains non-ascii characters but are not UTF-8 encoded #{encoding_issue_files}")
  end
  @logger.debug? && @logger.debug("\nThe following is the merged configuration")
  @logger.debug? && @logger.debug("\n" + config + "\n\n")
  return config
end