forked from sphen13/munki-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
406 lines (329 loc) · 12.2 KB
/
run
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/bin/bash
nginxConfig="/etc/nginx/nginx.conf"
# build nginx config
cat <<EOF > ${nginxConfig}
worker_processes 1;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
# optimize for large files
sendfile on;
directio 512;
aio on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 90;
# log format definition
log_format rt_cache '\$remote_addr - \$upstream_cache_status [\$time_local] '
'"\$request" \$status \$body_bytes_sent '
'"\$http_referer" "\$http_user_agent"';
access_log /var/log/nginx/access.log rt_cache;
# open file caching
open_file_cache max=2000 inactive=5m;
open_file_cache_valid 5m;
open_file_cache_min_uses 1;
open_file_cache_errors on;
# MIME type handling
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
types {
application/x-plist plist;
}
# Don't include the nginx version number, etc
server_tokens off;
# Gzip Settings
gzip on;
gzip_min_length 1000;
gzip_types application/x-javascript text/css application/javascript text/javascript text/plain text/xml;
# caching
proxy_cache_path /cache/pkgs keys_zone=MUNKI_PKGS:5m levels=1:2 use_temp_path=off max_size=${MAX_SIZE} inactive=${EXPIRE_PKGS};
proxy_cache_path /cache/icons keys_zone=MUNKI_ICONS:5m levels=1:2 use_temp_path=off inactive=${EXPIRE_ICONS};
proxy_cache_path /cache/apple keys_zone=APPLE_SUS:5m levels=1:2 use_temp_path=off inactive=${EXPIRE_SUS};
proxy_cache_path /cache/other keys_zone=MUNKI_DEFAULT:5m levels=1:2 use_temp_path=off inactive=${EXPIRE_OTHER};
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_cache_valid 200 302 404 ${EXPIRE_OTHER};
proxy_cache_revalidate on;
proxy_cache_lock on;
proxy_cache_lock_timeout 5m;
server {
listen ${PORT};
EOF
# enable SSL if asked
if [[ ${SSL} ]]; then
cat <<EOF >> ${nginxConfig}
ssl on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/cert.key;
EOF
fi
# server_name if provided
if [[ ${SERVER_NAME} ]]; then
echo " server_name ${SERVER_NAME};" >> ${nginxConfig}
echo "" >> ${nginxConfig}
echo " proxy_set_header Host \$server_name;" >> ${nginxConfig}
else
echo " proxy_set_header Host \$host;" >> ${nginxConfig}
fi
# continue
cat <<EOF >> ${nginxConfig}
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host \$server_name;
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
proxy_cache MUNKI_DEFAULT;
location = / {
return 204;
}
EOF
# gruntwork staging server detection
if [[ ${GRUNTWORK} ]]; then
echo "managed_installs" > /usr/share/nginx/html/gruntwork-only
cat <<EOF >> ${nginxConfig}
location ${MUNKI_ROOT}/manifests/gruntwork-only {
alias /usr/share/nginx/html/gruntwork-only;
}
EOF
fi
# continue
cat <<EOF >> ${nginxConfig}
location ${MUNKI_ROOT}/catalogs/ {
proxy_pass ${UPSTREAM_SERVER}${MUNKI_ROOT}/catalogs/;
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host \$server_name;
EOF
# server_name if provided
if [[ ${SERVER_NAME} ]]; then
echo " proxy_set_header Host \$server_name;" >> ${nginxConfig}
else
echo " proxy_set_header Host \$host;" >> ${nginxConfig}
fi
# gruntwork staging server authorization
if [[ ${GRUNTWORK} ]]; then
echo " proxy_set_header Authorization \"Basic ${GRUNTWORK}\";" >> ${nginxConfig}
fi
# continue
cat <<EOF >> ${nginxConfig}
}
location ${MUNKI_ROOT}/icons/ {
proxy_pass ${UPSTREAM_SERVER}${MUNKI_ROOT}/icons/;
proxy_cache MUNKI_ICONS;
proxy_cache_valid 200 302 404 ${EXPIRE_ICONS};
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host \$server_name;
EOF
# server_name if provided
if [[ ${SERVER_NAME} ]]; then
echo " proxy_set_header Host \$server_name;" >> ${nginxConfig}
else
echo " proxy_set_header Host \$host;" >> ${nginxConfig}
fi
# gruntwork staging server authorization
if [[ ${GRUNTWORK} ]]; then
echo " proxy_set_header Authorization \"Basic ${GRUNTWORK}\";" >> ${nginxConfig}
fi
# continue
cat <<EOF >> ${nginxConfig}
}
location ${MUNKI_ROOT}/client_resources/ {
proxy_pass ${UPSTREAM_SERVER}${MUNKI_ROOT}/client_resources/;
proxy_cache MUNKI_ICONS;
proxy_cache_valid 200 302 404 ${EXPIRE_ICONS};
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host \$server_name;
EOF
# server_name if provided
if [[ ${SERVER_NAME} ]]; then
echo " proxy_set_header Host \$server_name;" >> ${nginxConfig}
else
echo " proxy_set_header Host \$host;" >> ${nginxConfig}
fi
# gruntwork staging server authorization
if [[ ${GRUNTWORK} ]]; then
echo " proxy_set_header Authorization \"Basic ${GRUNTWORK}\";" >> ${nginxConfig}
fi
# continue
cat <<EOF >> ${nginxConfig}
}
location ${MUNKI_ROOT}/manifests/ {
proxy_pass ${UPSTREAM_SERVER}${MUNKI_ROOT}/manifests/;
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host \$server_name;
EOF
# server_name if provided
if [[ ${SERVER_NAME} ]]; then
echo " proxy_set_header Host \$server_name;" >> ${nginxConfig}
else
echo " proxy_set_header Host \$host;" >> ${nginxConfig}
fi
# gruntwork staging server authorization
if [[ ${GRUNTWORK} ]]; then
echo " proxy_set_header Authorization \"Basic ${GRUNTWORK}\";" >> ${nginxConfig}
fi
# continue
cat <<EOF >> ${nginxConfig}
}
location ${MUNKI_ROOT}/pkgs/ {
proxy_pass ${UPSTREAM_SERVER}${MUNKI_ROOT}/pkgs/;
proxy_cache MUNKI_PKGS;
proxy_cache_valid 200 206 302 ${EXPIRE_PKGS};
slice ${SLICE};
proxy_cache_key \$host\$uri\$is_args\$args\$slice_range;
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
proxy_set_header Range \$slice_range;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host \$server_name;
EOF
# server_name if provided
if [[ ${SERVER_NAME} ]]; then
echo " proxy_set_header Host \$server_name;" >> ${nginxConfig}
else
echo " proxy_set_header Host \$host;" >> ${nginxConfig}
fi
# gruntwork staging server authorization
if [[ ${GRUNTWORK} ]]; then
echo " proxy_set_header Authorization \"Basic ${GRUNTWORK}\";" >> ${nginxConfig}
fi
# split up server block for sus if needed
if [[ ${SERVER_NAME_SUS} ]]; then
# reset our variables
SERVER_NAME=${SERVER_NAME_SUS}
UPSTREAM_SERVER=${UPSTREAM_SUS_SERVER}
cat <<EOF >> ${nginxConfig}
}
}
server {
listen ${PORT};
EOF
# enable SSL if asked
if [[ ${SSL_SUS} ]]; then
cat <<EOF >> ${nginxConfig}
ssl on;
ssl_certificate /etc/ssl/cert_sus.pem;
ssl_certificate_key /etc/ssl/cert_sus.key;
EOF
fi
# server_name if provided
if [[ ${SERVER_NAME_SUS} ]]; then
echo " server_name ${SERVER_NAME_SUS};" >> ${nginxConfig}
echo "" >> ${nginxConfig}
echo " proxy_set_header Host \$server_name;" >> ${nginxConfig}
else
echo " proxy_set_header Host \$host;" >> ${nginxConfig}
fi
# continue
cat <<EOF >> ${nginxConfig}
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host \$server_name;
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
proxy_cache MUNKI_DEFAULT;
location = / {
return 204;
}
EOF
else
echo " }" >> ${nginxConfig}
fi
# continue
cat <<EOF >> ${nginxConfig}
location ${SUS_ROOT}/ {
if ( \$http_user_agent ~ "Darwin/11" ){
rewrite ^${SUS_ROOT}/index(.*)\.sucatalog$ ${SUS_ROOT}/content/catalogs/others/index-lion-snowleopard-leopard.merged-1$1.sucatalog last;
}
if ( \$http_user_agent ~ "Darwin/12" ){
rewrite ^${SUS_ROOT}/index(.*)\.sucatalog$ ${SUS_ROOT}/content/catalogs/others/index-mountainlion-lion-snowleopard-leopard.merged-1$1.sucatalog last;
}
if ( \$http_user_agent ~ "Darwin/13" ){
rewrite ^${SUS_ROOT}/index(.*)\.sucatalog$ ${SUS_ROOT}/content/catalogs/others/index-10.9-mountainlion-lion-snowleopard-leopard.merged-1$1.sucatalog last;
}
if ( \$http_user_agent ~ "Darwin/14" ){
rewrite ^${SUS_ROOT}/index(.*)\.sucatalog$ ${SUS_ROOT}/content/catalogs/others/index-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1$1.sucatalog last;
}
if ( \$http_user_agent ~ "Darwin/15" ){
rewrite ^${SUS_ROOT}/index(.*)\.sucatalog$ ${SUS_ROOT}/content/catalogs/others/index-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1$1.sucatalog last;
}
if ( \$http_user_agent ~ "Darwin/16" ){
rewrite ^${SUS_ROOT}/index(.*)\.sucatalog$ ${SUS_ROOT}/content/catalogs/others/index-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1$1.sucatalog last;
}
if ( \$http_user_agent ~ "Darwin/17" ){
rewrite ^${SUS_ROOT}/index(.*)\.sucatalog$ ${SUS_ROOT}/content/catalogs/others/index-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1$1.sucatalog last;
}
if ( \$http_user_agent ~ "Darwin/18" ){
rewrite ^${SUS_ROOT}/index(.*)\.sucatalog$ ${SUS_ROOT}/content/catalogs/others/index-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1$1.sucatalog last;
}
}
location ${SUS_ROOT}/content/catalogs/ {
proxy_pass ${UPSTREAM_SERVER}${SUS_ROOT}/content/catalogs/;
}
location ${SUS_ROOT}/content/downloads/ {
proxy_pass ${UPSTREAM_SERVER}${SUS_ROOT}/content/downloads/;
proxy_cache APPLE_SUS;
proxy_cache_valid 200 206 302 ${EXPIRE_SUS};
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
slice ${SLICE};
proxy_cache_key \$host\$uri\$is_args\$args\$slice_range;
proxy_set_header Range \$slice_range;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host \$server_name;
proxy_http_version 1.1;
EOF
# server_name if provided
if [[ ${SERVER_NAME} ]]; then
echo " proxy_set_header Host \$server_name;" >> ${nginxConfig}
else
echo " proxy_set_header Host \$host;" >> ${nginxConfig}
fi
# continue
cat <<EOF >> ${nginxConfig}
}
}
}
EOF
# enable avahi if the user wants it
if [[ ${AVAHI_HOST} ]]; then
# configure avahi
cat <<EOF > /etc/avahi/services/munki.service
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">proxy</name>
<service>
<type>_munki._tcp</type>
<port>${PORT}</port>
<txt-record>protocol=http</txt-record>
<txt-record>path=${MUNKI_ROOT}</txt-record>
</service>
</service-group>
EOF
sed -i -e "s/^#host-name=.*/host-name=${AVAHI_HOST}/" /etc/avahi/avahi-daemon.conf
sed -i -e "s/^#domain-name=.*/domain-name=${AVAHI_DOMAIN}/" /etc/avahi/avahi-daemon.conf
# start avahi
echo "Starting mDNS service"
avahi-daemon -D --no-drop-root
fi
# run nginx
nginx -g 'daemon off;'
# if we get this far nginx failed - lets output the config
cat -n /etc/nginx/nginx.conf