Class: HAProxy::Parser
- Inherits:
-
Object
- Object
- HAProxy::Parser
- Defined in:
- lib/haproxy/parser.rb
Instance Attribute Summary collapse
-
#current_backend ⇒ Object
Returns the value of attribute current_backend.
-
#current_frontend ⇒ Object
Returns the value of attribute current_frontend.
-
#current_section ⇒ Object
Returns the value of attribute current_section.
-
#current_section_name ⇒ Object
Returns the value of attribute current_section_name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #append_option(name, option_string) ⇒ Object
- #append_server(name, ip, port, option_string) ⇒ Object
-
#initialize(options = nil) ⇒ Parser
constructor
A new instance of Parser.
- #normalize_option_string(option_string) ⇒ Object
-
#parse(filename) ⇒ Object
This is starting to suck.
- #reset_parser_flags ⇒ Object
- #start_backend(config, name) ⇒ Object
- #start_frontend(config, name) ⇒ Object
- #start_section(config, name) ⇒ Object
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( = nil) ||= {} = { :verbose => false }.merge() self. = self.verbose = [:verbose] reset_parser_flags end |
Instance Attribute Details
#current_backend ⇒ Object
Returns the value of attribute current_backend.
4 5 6 |
# File 'lib/haproxy/parser.rb', line 4 def current_backend @current_backend end |
#current_frontend ⇒ Object
Returns the value of attribute current_frontend.
4 5 6 |
# File 'lib/haproxy/parser.rb', line 4 def current_frontend @current_frontend end |
#current_section ⇒ Object
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_name ⇒ Object
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 |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/haproxy/parser.rb', line 3 def @options end |
#verbose ⇒ Object
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) = normalize_option_string(option_string) puts " => Adding #{current_section_name} option : #{name} = #{.inspect}" if verbose current_section[name] ||= [] self.current_section[name] << unless .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 = normalize_option_string(option_string) self.current_backend.servers[name] = Server.new(name, ip, port, ) 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) = option_string.strip.split = .first if .size <= 1 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_flags ⇒ Object
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 |