Last active
September 9, 2019 01:01
-
-
Save justinholmes/8985347 to your computer and use it in GitHub Desktop.
Haproxy config generator using Marathon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'json' | |
require 'net/http' | |
url = URI.parse("http://marathon.apps.company.com/v2/tasks") | |
req = Net::HTTP::Get.new(url.path) | |
req.add_field("Accept", "application/json") | |
res = Net::HTTP.new(url.host, url.port).start do |http| | |
http.request(req) | |
end | |
data = res.body | |
lb = Hash.new { |hash, key| hash[key] = [] } | |
result = JSON.parse(data) | |
result['tasks'].each { |task| | |
key = task['appId'] | |
value = "#{task['host']}" + ":" + "#{task['ports'].first}@@@@#{task['id']}" | |
if lb.has_key?(key) | |
lb[key] << value | |
else | |
lb[key] = [value] | |
end | |
} | |
print <<"EOF"; | |
global | |
daemon | |
log 127.0.0.1 local0 | |
log 127.0.0.1 local1 notice | |
maxconn 4096 | |
defaults | |
log global | |
retries 3 | |
maxconn 2000 | |
contimeout 500 | |
clitimeout 500 | |
srvtimeout 500 | |
mode http | |
errorfile 503 /etc/haproxy/errors/503.http | |
listen stats | |
bind 0.0.0.0:9090 | |
balance | |
mode http | |
stats enable | |
stats auth admin:admin | |
backend default | |
server Local 192.168.1.5:80 check | |
backend mesos | |
server Local localhost:5050 check | |
backend marathon | |
server Local localhost:8080 check | |
backend aurora | |
server Local localhost:8081 check | |
frontend http-in | |
bind *:80 | |
acl is_mesos hdr_end(host) -i mesos.apps.example.com | |
use_backend mesos if is_mesos | |
acl is_marathon hdr_end(host) -i marathon.apps.example.com | |
acl is_aurora hdr_end(host) -i aurora.apps.example.com | |
use_backend marathon if is_marathon | |
use_backend aurora if is_aurora | |
EOF | |
lb.each { |app| | |
puts "acl is_#{app.first} hdr_end(host) -i #{app.first}.apps.example.com" | |
puts "use_backend #{app.first} if is_#{app.first}" | |
puts "default_backend default" | |
} | |
lb.each { |app| | |
print <<"EOF" | |
backend #{app.first} | |
mode http | |
option tcplog | |
option httpchk GET / | |
balance leastconn | |
EOF | |
app.last.each {|server| | |
server_split = server.split("@@@@") | |
id = server_split.last | |
hostAndPort= server_split.first | |
puts "server #{id} #{hostAndPort} check" | |
} | |
puts " " | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment