# This file was generated by chef-alfresco

# General Configurations: global, timeouts
<% node['haproxy']['general_config'].each do |line| -%>
<%= line %>
<% end -%>

# Frontends

<% if node['haproxy']['frontends'] -%>
  <% node['haproxy']['frontends'].each do |frontendName,frontend| -%>
frontend <%= frontendName %>
    <% if frontend['entries'] -%>
      <% frontend['entries'].each do |entry| -%>
<%= entry %>
      <% end -%>
    <% end -%>

# Backend ACLs
    <% if frontend['acls'] -%>
      <% frontend['acls'].each do |backendName,acls| -%>
        <% if acls -%>
          <% acls.each do |acl| -%>
acl is_<%= backendName %> <%= acl %>
          <% end -%>
        <% end -%>
      <% end -%>
    <% end -%>

# Generic ACLs - only applies to http frontend (a bit hacky, I know)
    <% if node['haproxy']['acls'] and frontendName == 'http' -%>
      <% node['haproxy']['acls'].each do |acl| -%>
acl <%= acl %>
      <% end -%>

# Other Configurations
      <% if node['haproxy']['other_config'] -%>
        <% node['haproxy']['other_config'].each do |other_config| -%>
<%= other_config %>
        <% end -%>
      <% end -%>

# Redirects
      <% if node['haproxy']['redirects'] -%>
        <% node['haproxy']['redirects'].each do |redirect| -%>
<%= redirect %>
        <% end -%>
      <% end -%>

# Use backends
      <% if frontend['acls'] -%>
        <% frontend['acls'].each do |backendName,acls| -%>
use_backend <%= backendName %> if is_<%= backendName %>
        <% end -%>
      <% end -%>

# Default Backend
default_backend <%= node['haproxy']['default_backend'] %>
    <% end -%>
  <% end -%>
<% end -%>

# Backends list

<% if node['haproxy']['backends'] -%>
  <% node['haproxy']['backends'].each do |backendName,backend| -%>
    <% if node['haproxy']['enabled_backends'].include? backendName -%>
# <%= backendName %> backend
backend <%= backendName %>
      <% if backend['entries'] -%>
        <% backend['entries'].each do |entry| -%>
<%= entry %>
        <% end -%>
      <% end -%>
      <% if node['alfresco']['components'].include? backendName or
        (node['alfresco']['components'].include? 'repo' and backendName == 'alfresco') -%>
        server local_<%= backendName %> 127.0.0.1:<%= backend['port'] %> check inter 5000
      <% end -%>
      <% else -%>
        <% if backend['nodes'] -%>
          <% backend['nodes'].each do |availabilityZone,availabilityZoneNodes| -%>
            <% if availabilityZone['current'] == true -%>
# First, the nodes of the same availability zone
              <% availabilityZoneNodes.each do |nodeHost,nodeIp| -%>
  server <%= nodeHost %> <%= nodeIp %>:<%= backend['port'] %> check inter 5000
              <% end -%>
            <% end -%>
            <% unless availabilityZone['current'] == true -%>
# Then, the nodes of the other availability zone
              <% availabilityZoneNodes.each do |nodeHost,nodeIp| -%>
  server <%= nodeHost %> <%= nodeIp %>:<%= backend['port'] %> check inter 5000
              <% end -%>
            <% end -%>
          <% end -%>
        <% end -%>
    <% end -%>
  <% end -%>
<% end -%>
