This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate.sh
executable file
·146 lines (124 loc) · 3.46 KB
/
generate.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
set -e
# usage: $0 path/to/config.sh [target/path/]
# default config
defaultServer='example.com'
declare -a listens=( 80 ) resolvers=()
declare -A redirects=() forcedProtos=() simpleProxies=() simpleStatics=() sslCerts=() extraConfigs=()
config="$(dirname "$BASH_SOURCE")"
if [ "$1" ]; then
config="$1"
fi
if [ -d "$config" ]; then
config="${config%/}/config.sh"
fi
targetDir="$(dirname "$config")"
if [ "$2" ]; then
targetDir="$2"
fi
echo "Loading $config ..."
source "$config"
target="$targetDir/default.conf"
declare -A allHostsA=()
for serverName in "${!redirects[@]}" "${!forcedProtos[@]}" "${!simpleProxies[@]}" "${!simpleStatics[@]}" "${!sslCerts[@]}"; do
allHostsA[$serverName]=1
done
unset allHostsA[$defaultServer]
allHosts=( "${!allHostsA[@]}" )
IFS=$'\n'
allHosts=( $(echo "${allHosts[@]}" | xargs -n1 | sort) )
unset IFS
allHosts=( "$defaultServer" "${allHosts[@]}" )
echo "Generating into $target ..."
cat > "$target" <<EOH
# GENERATED BY $BASH_SOURCE via $config - DO NOT MODIFY DIRECTLY
EOH
if [ "${#resolvers[@]}" -gt 0 ]; then
echo "resolver ${resolvers[*]};" >> "$target"
fi
for serverName in "${allHosts[@]}"; do
sslCert="${sslCerts[$serverName]}"
redirectTo="${redirects[$serverName]}"
doForceProto="${forcedProtos[$serverName]}"
proxyTo="${simpleProxies[$serverName]}"
staticFiles="${simpleStatics[$serverName]}"
extraConfig="${extraConfigs[$serverName]}"
cat >> "$target" <<EOB
server {
EOB
for listen in "${listens[@]}"; do
if [[ "$listen" == *ssl* ]]; then
# ignore "ssl" enabled listen directives unless we have corresponding SSL configuration
# otherwise, we get:
# [error] 6#6: *13 no "ssl_certificate" is defined in server listening on SSL port while SSL handshaking, client: x.x.x.x, server: 0.0.0.0:443
if [ -z "$sslCert" ]; then
continue
fi
fi
cat >> "$target" <<EOB
listen $listen;
EOB
done
if [ "$sslCert" ]; then
ssl=( $sslCert ) # split on whitespace
cert="${ssl[0]}"
key="${ssl[1]}"
cat >> "$target" <<EOB
ssl_certificate $cert;
ssl_certificate_key $key;
include conf.d/ssl.include;
EOB
fi
cat >> "$target" <<EOB
server_name $serverName;
include conf.d/set-proto.include;
location / {
# start a default "location" block to allow for "add_header", etc.
EOB
if [ "$doForceProto" ]; then
cat >> "$target" <<EOB
set \$force_proto "$doForceProto";
include conf.d/force-proto.include;
EOB
fi
if [ "$redirectTo" ]; then
redirectTo='$proto://'"$redirectTo"'$request_uri' # TODO decide if this should support "$redirectTo" already having '$' in it somewhere, and thus not doing this prepend/append
cat >> "$target" <<EOB
return 301 $redirectTo;
EOB
fi
if [ "$staticFiles" ]; then
cat >> "$target" <<EOB
root $staticFiles;
index index.html index.htm index;
try_files \$uri \$uri/ \$uri.html =404;
add_header Cache-Control "public";
EOB
fi
if [ "$proxyTo" ]; then
cat >> "$target" <<EOB
# let the proxied server handle whether this "body" is too large
client_max_body_size 0;
# use a variable so NGINX is forced to resolve this at request time instead of at startup
set \$proxy_to $proxyTo;
proxy_pass \$proxy_to;
include conf.d/proxy-pass.include;
EOB
fi
cat >> "$target" <<EOB
}
EOB
if [ "$extraConfig" ]; then
extraConfig="${extraConfig#$'\n'}"
extraConfig="${extraConfig%$'\n'}"
{
echo
echo "$extraConfig"
} >> "$target"
fi
cat >> "$target" <<EOB
}
EOB
done
echo "Copying *.include to $targetDir/"
cp -v "$(dirname "$BASH_SOURCE")"/*.include "$targetDir"