Class: LogStash::Config::Loader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, debug_config = false) ⇒ Loader

Returns a new instance of Loader.



6
7
8
9
# File 'lib/logstash/config/loader.rb', line 6

def initialize(logger, debug_config=false)
  @logger = logger
  @debug_config = debug_config
end

Instance Attribute Details

#debug_configObject

Returns the value of attribute debug_config.



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

def debug_config
  @debug_config
end

Instance Method Details

#fetch_config(uri) ⇒ Object

def load_config



90
91
92
93
94
95
96
# File 'lib/logstash/config/loader.rb', line 90

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

#format_config(config_path, config_string) ⇒ Object



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

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



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

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.agent.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



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
82
83
84
85
86
87
88
# File 'lib/logstash/config/loader.rb', line 53

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.agent.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"
    if @debug_config
      @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 @debug_config
    @logger.debug? && @logger.debug("\nThe following is the merged configuration")
    @logger.debug? && @logger.debug("\n" + config + "\n\n")
  end
  return config
end