forked from mawinkler/c1-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-proxy.sh
executable file
·76 lines (62 loc) · 2.3 KB
/
deploy-proxy.sh
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
#!/bin/bash
set -e
# Source helpers
. ./playground-helpers.sh
# Get config
SERVICE=$1
SERVICE_NAME="$(jq -r --arg SVC $SERVICE '.services[] | select(.name==$SVC) | .proxy_service_name' config.json)"
SERVICE_NAMESPACE="$(jq -r --arg SVC $SERVICE '.services[] | select(.name==$SVC) | .namespace' config.json)"
SERVICE_PORT="$(jq -r --arg SVC $SERVICE '.services[] | select(.name==$SVC) | .proxy_service_port' config.json)"
LISTEN_PORT="$(jq -r --arg SVC $SERVICE '.services[] | select(.name==$SVC) | .proxy_listen_port' config.json)"
#######################################
# Creates the proxy configuration
# for the given service
# Globals:
# SERVICE_NAMESPACE
# SERVICE_NAME
# SERVICE_PORT
# LISTEN_PORT
# Arguments:
# None
# Outputs:
# None
#######################################
function create_proxy_configuration {
SERVICE_HOST=''
while [ "$SERVICE_HOST" == '' ]
do
SERVICE_HOST=$(kubectl get svc -n ${SERVICE_NAMESPACE} ${SERVICE_NAME} \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}')
sleep 2
done
# sudo unlink /etc/nginx/sites-enabled/default
printf '%s\n' "Configure passthrough for ${SERVICE_NAME}"
LINE="include /etc/nginx/passthrough.conf;"
FILE='/etc/nginx/nginx.conf'
grep -qF -- "$LINE" "$FILE" || echo ${LINE} | sudo tee -a ${FILE}
if [ ! -f /tmp/passthrough.conf ]; then
cp templates/passthrough.conf /tmp/passthrough.conf
fi
if grep -Fq "upstream ${SERVICE_NAME} " /tmp/passthrough.conf
then
printf '%s\n' "Proxy already configured for ${SERVICE_NAME}"
exit 0
else
FRAGMENT=$(cat templates/passthrough-fragment.conf)
sed -i "s|###|${FRAGMENT}|" /tmp/passthrough.conf
sed -i "s|_SERVICE|${SERVICE_NAME}|g" /tmp/passthrough.conf
sed -i "s|_DESTINATION_HOST|${SERVICE_HOST}|" /tmp/passthrough.conf
sed -i "s|_DESTINATION_PORT|${SERVICE_PORT}|" /tmp/passthrough.conf
sed -i "s|_LISTEN_PORT|${LISTEN_PORT}|" /tmp/passthrough.conf
sudo cp /tmp/passthrough.conf /etc/nginx/passthrough.conf
fi
}
create_proxy_configuration
printf '%s\n' "Remove default site 🍭"
sudo rm -f /etc/nginx/sites-enabled/default
printf '%s\n' "Apply nginx proxy configuration 🍭"
sudo nginx -t
sudo service nginx restart
if is_linux ; then
echo "Service ${SERVICE} on: http(s)://$(hostname -I | awk '{print $1}'):${LISTEN_PORT}"
fi