42
43
44
45
46
47
48
49
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/hapi/application/app.rb', line 42
def render(config)
content = ""
f = File.open($haproxy_config, "r")
in_gen_section = false
f.each_line do |line|
if line =~ /HAPROXY_API_GENERATED/
if in_gen_section
in_gen_section = false
else
in_gen_section = true
end
else
if !in_gen_section
content += line
end
end
end
f.close
content += "# HAPROXY_API_GENERATED - START\n"
config['frontend'].each_pair do |name, frontend|
content += "frontend #{name}\n"
content += " bind 0.0.0.0:#{frontend['port']}\n"
if frontend.has_key? 'acls'
port = ''
frontend['acls'].each_pair do |acl,backend_port|
clean_name = acl.gsub('/','_')
content += " acl url_#{clean_name} path_beg #{acl}\n"
content += " use_backend #{name}-#{backend_port}-backend if url_#{clean_name}\n"
port = backend_port
end
content += " default_backend #{name}-#{port}-backend\n"
else
content += " default_backend #{name}-backend\n"
end
content += "\n"
end
config['backend'].each_pair do |name, backend|
content += "backend #{name}\n"
content += " balance #{backend['lbmethod']}\n"
if backend.has_key?('options')
backend['options'].each_pair do |k,v|
content += " option #{k} #{v}\n"
end
end
port = backend['port']
server_options = ""
if backend.has_key?('server_options')
backend['server_options'].each_pair do |k,v|
server_options += "#{k} #{v} "
end
end
backend['servers'].each do |server|
content += " server #{server}:#{port} #{server}:#{port} #{server_options} \n"
end
content += "\n"
end
content += "# HAPROXY_API_GENERATED - END\n"
return content
end
|