Class: HAProxy::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/haproxy/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
10
11
12
13
# File 'lib/haproxy/parser.rb', line 6

def initialize(options = nil)
  options ||= {}
  options = { :verbose => false }.merge(options)

  self.options = options
  self.verbose = options[:verbose]
  reset_parser_flags
end

Instance Attribute Details

#current_backendObject

Returns the value of attribute current_backend.



4
5
6
# File 'lib/haproxy/parser.rb', line 4

def current_backend
  @current_backend
end

#current_frontendObject

Returns the value of attribute current_frontend.



4
5
6
# File 'lib/haproxy/parser.rb', line 4

def current_frontend
  @current_frontend
end

#current_sectionObject

Returns the value of attribute current_section.



4
5
6
# File 'lib/haproxy/parser.rb', line 4

def current_section
  @current_section
end

#current_section_nameObject

Returns the value of attribute current_section_name.



4
5
6
# File 'lib/haproxy/parser.rb', line 4

def current_section_name
  @current_section_name
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/haproxy/parser.rb', line 3

def options
  @options
end

#verboseObject

Returns the value of attribute verbose.



3
4
5
# File 'lib/haproxy/parser.rb', line 3

def verbose
  @verbose
end

Instance Method Details

#append_option(name, option_string) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/haproxy/parser.rb', line 62

def append_option(name, option_string)
  normalized_options = normalize_option_string(option_string)

  puts " => Adding #{current_section_name} option : #{name} = #{normalized_options.inspect}" if verbose

  current_section[name] ||= []
  self.current_section[name] << normalized_options unless normalized_options.nil?
end

#append_server(name, ip, port, option_string) ⇒ Object



71
72
73
74
75
76
# File 'lib/haproxy/parser.rb', line 71

def append_server(name, ip, port, option_string)
  puts " => Adding server: #{name}" if verbose

  server_options = normalize_option_string(option_string)
  self.current_backend.servers[name] = Server.new(name, ip, port, server_options)
end

#normalize_option_string(option_string) ⇒ Object



56
57
58
59
60
# File 'lib/haproxy/parser.rb', line 56

def normalize_option_string(option_string)
  normalized_options = option_string.strip.split
  normalized_options = normalized_options.first if normalized_options.size <= 1
  normalized_options
end

#parse(filename) ⇒ Object

This is starting to suck. Try treetop.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/haproxy/parser.rb', line 22

def parse(filename)
  self.reset_parser_flags

  config = HAProxy::Config.new
  start_backend(config, 'default')
  start_frontend(config, 'default')

  lines = File.readlines(filename)
  lines.each do |line|
    line.strip!
    case line.strip
    when /^(global|defaults)$/
      start_section(config, $1)
    when /^frontend\W+([^\W]+)\W+([^\W:]+):(\d+)/
      start_section(config, 'frontend')
      start_frontend(config, $1)
    when /^backend\W+([^\W]+)/
      start_section(config, 'backend')
      start_backend(config, $1)
    when /^server\W+([^\W]+)\W+([\d\.]+):(\d+)(.*)/
      append_server($1, $2, $3, $4)
    when /^([^\W]+)([^#]*)/ # match other name/value pairs; ignore comments
      append_option($1, $2)
    when /^$/
    when /^#.*/
      puts " => Ignoring comment: #{line}" if verbose
    else
      puts " => Skipping non-matching line: #{line}" if verbose
    end
  end

  config
end

#reset_parser_flagsObject



15
16
17
18
19
# File 'lib/haproxy/parser.rb', line 15

def reset_parser_flags
  self.current_section   = nil
  self.current_backend   = nil
  self.current_frontend  = nil
end

#start_backend(config, name) ⇒ Object



93
94
95
96
97
98
# File 'lib/haproxy/parser.rb', line 93

def start_backend(config, name)
  puts " => Starting backend: #{name}" if verbose

  self.current_backend = Backend.new(name, {})
  config.backends << self.current_backend
end

#start_frontend(config, name) ⇒ Object



86
87
88
89
90
91
# File 'lib/haproxy/parser.rb', line 86

def start_frontend(config, name)
  puts " => Starting frontend: #{name}" if verbose

  self.current_frontend = Frontend.new(name, {})
  config.frontends << self.current_frontend
end

#start_section(config, name) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/haproxy/parser.rb', line 78

def start_section(config, name)
  puts " => Starting option_group: #{name}" if verbose

  config.option_groups[name] ||= {}
  self.current_section_name = name
  self.current_section = config.option_groups[name]
end