Skip to content

Commit 52fcf03

Browse files
committed
Merge pull request #429 from CodeNow/refactor-proxy
Refactor Proxied Hosts
2 parents 6d2398c + 4842e6d commit 52fcf03

10 files changed

Lines changed: 111 additions & 197 deletions

File tree

ansible/eru.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- hosts: consul
33
- hosts: mongodb
44
- hosts: redis
5+
- hosts: socket-server-proxy
56

67
- hosts: eru
78
vars_files:
@@ -10,9 +11,7 @@
1011
- { role: notify, tags: [ notify ] }
1112
- { role: builder, tags: [ build ] }
1213
- role: container_start
13-
14-
- hosts: socket-server-proxy
15-
vars_files:
16-
- group_vars/alpha-eru.yml
17-
roles:
18-
- role: eru
14+
- role: nginx-proxied-service
15+
nginx_host: "{{ groups['socket-server-proxy'][0] }}"
16+
target_ip_address: "{{ hostvars[groups['eru'][0]]['ansible_default_ipv4']['address'] }}"
17+
templates: [ 11-eru-server.conf ]

ansible/roles/eru/tasks/main.yml

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
# these are pretty hacky, but it should work
3+
# Get port information from the hosted service
4+
- name: get eru ports
5+
when: name == "eru"
6+
tags: [ configure_proxy, deploy ]
7+
become: true
8+
shell: |
9+
for c in $(docker ps | awk '/eru/{ print $1 }'); do
10+
docker port $c 5501 | cut -d ':' -f 2
11+
docker port $c 5502 | cut -d ':' -f 2
12+
done
13+
args:
14+
executable: /bin/bash
15+
register: eru_target_ports
16+
17+
- name: get socket server ports
18+
when: name == "api-socket-server"
19+
tags: [ configure_proxy, deploy ]
20+
become: true
21+
shell: |
22+
for c in $(docker ps | awk '/api-socket-server/{ print $1 }'); do
23+
docker port $c 80 | cut -d ':' -f 2
24+
done
25+
args:
26+
executable: /bin/bash
27+
register: socket_target_ports
28+
29+
# everything from this point on is deligated to the nginx host
30+
31+
- name: print target ports
32+
delegate_to: "{{ nginx_host }}"
33+
tags: [ configure_proxy, deploy ]
34+
debug:
35+
msg: |
36+
eru ports -- {{ eru_target_ports }}
37+
socket ports -- {{ socket_target_ports }}
38+
39+
- name: print target IP address
40+
delegate_to: "{{ nginx_host }}"
41+
tags: [ configure_proxy, deploy ]
42+
debug:
43+
msg: ip -- {{ target_ip_address }}
44+
45+
- name: assert nginx config directory
46+
delegate_to: "{{ nginx_host }}"
47+
tags: [ configure_proxy, deploy ]
48+
become: yes
49+
file:
50+
state: directory
51+
dest: /etc/nginx
52+
53+
- name: assert nginx sites-available directory
54+
delegate_to: "{{ nginx_host }}"
55+
tags: [ configure_proxy, deploy ]
56+
become: yes
57+
file:
58+
state: directory
59+
dest: /etc/nginx/sites-available
60+
61+
- name: assert nginx sites-enable directory
62+
delegate_to: "{{ nginx_host }}"
63+
tags: [ configure_proxy, deploy ]
64+
become: yes
65+
file:
66+
state: directory
67+
dest: /etc/nginx/sites-enable
68+
69+
- name: put configuration in place
70+
delegate_to: "{{ nginx_host }}"
71+
tags: [ configure_proxy, deploy ]
72+
become: yes
73+
template:
74+
src: "{{ item }}"
75+
dest: /etc/nginx/sites-available/{{ item }}
76+
with_items: "{{ templates }}"
77+
78+
- name: link configuration to enable
79+
delegate_to: "{{ nginx_host }}"
80+
tags: [ configure_proxy, deploy ]
81+
become: yes
82+
file:
83+
state: link
84+
dest: /etc/nginx/sites-enabled/{{ item }}
85+
src: /etc/nginx/sites-available/{{ item }}
86+
with_items: "{{ templates }}"
87+
88+
- name: reload nginx
89+
delegate_to: "{{ nginx_host }}"
90+
tags: [ configure_proxy, deploy ]
91+
become: yes
92+
shell: >
93+
docker ps |
94+
awk '/nginx/{ print $1 }' |
95+
xargs -n 1 docker kill --signal SIGHUP
96+
args:
97+
executable: /bin/bash

ansible/roles/socket-proxy/templates/00-nginx-status.conf renamed to ansible/roles/nginx-proxied-service/templates/00-nginx-status.conf

File renamed without changes.

ansible/roles/socket-proxy/templates/01-socket-server.conf renamed to ansible/roles/nginx-proxied-service/templates/01-socket-server.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ map $http_upgrade $connection_upgrade {
55

66
upstream socketserver {
77
sticky;
8-
{% for port in ports.stdout_lines -%}
9-
server {{ socket_server_hostname }}:{{ port }};
8+
{% for port in socket_target_ports.stdout_lines -%}
9+
server {{ target_ip_address }}:{{ port }};
1010
{% endfor %}
1111
}
1212

ansible/roles/eru/templates/11-eru-server.conf renamed to ansible/roles/nginx-proxied-service/templates/11-eru-server.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ server {
3535

3636
location / {
3737
expires 300;
38-
proxy_pass http://{{ eru_server_hostname }}:{{ ports.stdout_lines[0] | trim }};
38+
proxy_pass http://{{ target_ip_address }}:{{ eru_target_ports.stdout_lines[0] | trim }};
3939
proxy_set_header Host $host;
4040
proxy_set_header x-real-ip $remote_addr;
4141
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
@@ -44,7 +44,7 @@ server {
4444
}
4545

4646
location /graphql {
47-
proxy_pass http://{{ eru_server_hostname }}:{{ ports.stdout_lines[1] | trim }};
47+
proxy_pass http://{{ target_ip_address }}:{{ eru_target_ports.stdout_lines[1] | trim }};
4848
proxy_set_header Host $host;
4949
proxy_set_header x-real-ip $remote_addr;
5050
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;

ansible/roles/socket-proxy/files/nginx.conf

Lines changed: 0 additions & 74 deletions
This file was deleted.

ansible/roles/socket-proxy/handlers/main.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

ansible/roles/socket-proxy/tasks/main.yml

Lines changed: 0 additions & 50 deletions
This file was deleted.

ansible/socket-server.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- hosts: redis
88
- hosts: swarm-manager
99
- hosts: consul
10+
- hosts: socket-server-proxy
1011

1112
- hosts: socket-server
1213
vars_files:
@@ -22,10 +23,7 @@
2223
- { role: tls-client, tls_service: mongodb, tags: [ tls ] }
2324
- { role: datadog, tags: [ datadog ] }
2425
- { role: container_start, number_of_containers: 8 }
25-
26-
- hosts: socket-server-proxy
27-
vars_files:
28-
- group_vars/alpha-proxy-socket-server.yml
29-
roles:
30-
- role: socket-proxy
31-
- role: container_restart
26+
- role: nginx-proxied-service
27+
nginx_host: "{{ groups['socket-server-proxy'][0] }}"
28+
target_ip_address: "{{ hostvars[groups['socket-server'][0]]['ansible_default_ipv4']['address'] }}"
29+
templates: [ 01-socket-server.conf ]

0 commit comments

Comments
 (0)