Skip to content

Commit

Permalink
Fix/override env (#40)
Browse files Browse the repository at this point in the history
* Added option to override env

* removed env_name from backend and frontends

* fix tests
  • Loading branch information
aleeriz authored Nov 23, 2017
1 parent e1103ee commit 1ed63ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
23 changes: 14 additions & 9 deletions templates/haproxy.ctmp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ listen stats
stats uri {{ consul_haproxy_stats_uri }}
{% endif %}

{% if environment_name is defined %}
{% set env_name = environment_name %}
{% else %}
{% set env_name = fallback_environment %}
{% endif %}

# consul_services
{% if consul_consumer_services is defined %}
{% for consumer_service in consul_consumer_services %}
Expand All @@ -63,11 +57,22 @@ listen stats
{% else %}
{% set service_name = consumer_service %}
{% endif %}

{% set env_name = consumer_service.environment_overridedefault(false) %}

{% if not env_name %}
{% if environment_name is defined %}
{% set env_name = environment_name %}
{% else %}
{% set env_name = fallback_environment %}
{% endif %}
{% endif %}

{% set tags_contains = consumer_service.tags_containsdefault(false) %}
{% set tag_regex = consumer_service.tag_regexdefault(false) %}
{% set haproxy_setting = consul_services[service_name].haproxy | default({}) %}
# Service Config for {{ service_name }}
backend {{ service_name }}-{{ env_name }}
backend {{ service_name }}
mode {{ haproxy_setting.service_mode | default('http') }}
<% scratch.Set "serviceFound" "False" -%>
<% scratch.Set "localPort" "{{ consul_services[service_name].local_port | default(consul_services[service_name].port) }}" -%>
Expand Down Expand Up @@ -117,9 +122,9 @@ backend {{ service_name }}-{{ env_name }}
{% if tags_contains %}<%else%> # Did not match tags_contains {{ tags_contains }}<%end%>{% endif %}
<%- end %>
<%- end %>
frontend {{ service_name }}-{{ env_name }}
frontend {{ service_name }}
mode {{ haproxy_setting.service_mode | default('http') }}
bind localhost:<% scratch.Get "localPort" %>
default_backend {{ service_name }}-{{ env_name }}
default_backend {{ service_name }}
{% endfor %}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,25 @@

describe 'HAProxy server backend should be listed and up' do
let(:pre_command) { 'sleep 2' }
describe command "echo 'show stat' | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep hellofresh-testing,hellofresh | grep UP" do
describe command "echo 'show stat' | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep hellofresh,hellofresh | grep UP" do
its(:exit_status) { should eq 0 }
end
end

describe 'hellofresh backend should have default weight' do
describe command 'echo "get weight hellofresh-testing/`cat /etc/haproxy/haproxy.cfg | grep "server hellofresh" | awk \'{print $2}\'`" | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep 100' do
describe command 'echo "get weight hellofresh/`cat /etc/haproxy/haproxy.cfg | grep "server hellofresh" | awk \'{print $2}\'`" | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep 100' do
its(:stdout) { should contain '100 \(initial 100\)'}
end
end

describe 'superdb backend should exist' do
describe command "echo 'show stat' | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep superdb-testing,BACKEND" do
describe command "echo 'show stat' | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep superdb,BACKEND" do
its(:exit_status) { should eq 0 }
end
end

describe 'superssh-testing backend should have set weight' do
describe command 'echo "get weight superssh-testing/`cat /etc/haproxy/haproxy.cfg | grep "server superssh-testing" | awk \'{print $2}\'`" | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep 77' do
describe command 'echo "get weight superssh/`cat /etc/haproxy/haproxy.cfg | grep "server superssh-testing" | awk \'{print $2}\'`" | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep 77' do
its(:stdout) { should contain '77 \(initial 77\)'}
end
end
Expand Down

0 comments on commit 1ed63ff

Please sign in to comment.