-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathgce-startup-script
executable file
·51 lines (44 loc) · 1.27 KB
/
gce-startup-script
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
#!/bin/bash
# metadata prints the value associated with the named metadata key on this VM.
metadata() {
curl -s http://metadata/computeMetadata/v1/instance/attributes/$1 -H "Metadata-Flavor: Google" >/tmp/meta
if ! grep 'Error 404' /tmp/meta >/dev/null; then
cat /tmp/meta
fi
rm -f /tmp/meta
}
set -e
# redirector should be the gs:// URL of the go-import-redirector binary.
redirector=$(metadata redirector)
# import should be the import path, like rsc.io/*
# repo should be the repo path, like https://github.com/rsc/*
import=$(metadata import)
repo=$(metadata repo)
# initialize /work with redirector binary
mkdir -p /work
gsutil cp $redirector /work/redirector
chmod +x /work/redirector
cd /work
# if metadata for 'certs' is present, copy certificates from
# <certs>/<host>.crt and <certs>/<host>.key,
# where host is the host name in the import path.
set +e
host=$(echo $import | sed 's;/.*;;')
certs=$(metadata certs)
opt=""
if [ "$certs" != "" ]; then
opt=-tls
gsutil cp $certs/$host.crt .
gsutil cp $certs/$host.key .
fi
letsencrypt=$(metadata letsencrypt)
if [ "$letsencrypt" != "" ]; then
opt="-letsencrypt=$letsencrypt"
fi
# run redirector in a loop, sleeping 10 seconds between restarts
while true
do
./redirector $opt "$import" "$repo"
sleep 10
done >log 2>&1 &
exit 0