Skip to content

Instantly share code, notes, and snippets.

@ranjib
Created June 25, 2014 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranjib/0b57a6c44f116851b6db to your computer and use it in GitHub Desktop.
Save ranjib/0b57a6c44f116851b6db to your computer and use it in GitHub Desktop.
Dynamic haproxy using chef based on unprivileged containers (exposing containers port 80)
template '/etc/haproxy/haproxy.cfg' do
extend Helper
variables(
containers: container_ips('goatos'),
start_port: 8000
)
source 'haproxy.cfg.erb'
end
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
defaults
log global
option dontlognull
contimeout 5000
clitimeout 50000
srvtimeout 50000
<% @containers.each |name, ip|%>
listen <%=name.upcase %> :<%= (@start_port+=1) %>
mode http
server <%= name %> <%= ip %>:<%= 80 %>
<% end %>
module Helper
def container_ips
data = {}
user = Etc.getpwnam(name)
config = File.join(user.dir, '.local/share/lxc') # this can be injected from outside as well
LXC.list_containers(config_path: config).each do |n|
ct = LXC::Container.new(n, config)
if ct.running? and (not ct.ip_addresses.empty?)
data[ct.name] = ct.ip_addresses.first
end
end
data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment