forked from jacksoncage/varnish-docker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse
More file actions
82 lines (65 loc) · 2 KB
/
parse
File metadata and controls
82 lines (65 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
################################################################
################################################################
## Luis Elizondo <lelizondo@gmail.com>
## Apache License 2.0
##
## This file will parse the Environment variables created by
## node containers linked to the varnish container
## and automatically create a default.vcl configuration file
##
## The configuration file will create a load balancer and add
## each of the node containers linked.
##
## Varnish is not configured to use the reverse proxy features
## that it has, it just a load balancer.
##
################################################################
################################################################
# Get the Environment variables and save them in the variable envs
envs=`printenv`
# Remove the default.vcl file
rm /etc/varnish/default.vcl
# Loop through all of our variables
for env in $envs
do
# separate the name of the variable from the value
IFS== read name value <<< "$env"
# if the variable has PORT_3000_TCP_ADDR it means this is a
# variable created by a node container linked to the varnish
# container
if [[ $name == *PORT_3000_TCP_ADDR* ]]; then
# create a backend for each node container found in the variables
cat >> /etc/varnish/default.vcl << EOF
backend ${name} {
.host = "${value}";
.port = "3000";
}
EOF
fi
done
# once we have all the containers ready, we create the
# load balancer, since we're gonna loop again, we just
# create the first line
cat >> /etc/varnish/default.vcl << EOF
director lb round-robin {
EOF
# loop again to add each backend created
for env in $envs
do
IFS== read name value <<< "$env"
if [[ $name == *PORT_3000_TCP_ADDR* ]]; then
# create each backend in the load balancer
cat >> /etc/varnish/default.vcl << EOF
{ .backend = ${name}; }
EOF
fi
done
# close the load balancer line
# and add the balancer as the req.backend
cat >> /etc/varnish/default.vcl << EOF
}
sub vcl_recv {
set req.backend = lb;
}
EOF