-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolang
378 lines (351 loc) · 13.4 KB
/
golang
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
#!/bin/bash
##############################################################################
#
# golang
# -------------------
# Install or update Go language.
#
# Download Go source tarball and SHA1 hash from https://code.google.com. If
# hashcheck is okay, install and setup Go in $PREFIX/go (/usr/local/go if
# $PREFIX unset), and modify the system default $PATH to include Go's location
# by appending it to /etc/profile if run as root, or ~/.profile otherwise.
#
# For x86_64 and 32-bit Linux, only. Should work on all distros, except for
# RHEL and CentOS.
#
# This script was tested with torsocks, to use this with Tor, do:
# $ sudo usewithtor golang [re]install
#
# @author Isis Agora Lovecruft, 0xa3adb67a2cdb8b35
# @date 3 September 2012
# @version 0.0.5
#
##############################################################################
set -e
GO_VERSION="1.1.1"
USER_AGENT='Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0")'
PREFIX=${PREFIX:-/usr/local}
contact_name=isis
contact_host=patternsinthevoid.net
contact="${contact_name}@${contact_host}"
function usage ()
{
printf "%s [options]\n\n" $(basename $0)
printf "\tinstall Install Go ${GO_VERSION}\n"
printf "\treinstall Update Go to ${GO_VERSION}\n"
printf "\tpath Export PATH to Go & GoTools\n\n"
cat <<EOF
If you have tor and torsocks installed, you can use this script like so:
$ sudo usewithtor golang [re]install
Additional Info
---------------
The 'install' and 'path' options will append to /etc/profile if run as root or
~/.profile otherwise (this is part of the Go installation instructions; it's so
that your GOPATH and GOROOT environment variables are available for use),
whereas the 'reinstall' option will not update your PATH.
If reinstalling, make sure to choose the reinstall option, else you'll end
up with Go in your PATH twice!
EOF
}
## wget is such a piece of shit
googlecode_cert=${HOME}/go.googlecode.com.pem
cat > $googlecode_cert<<EOF
# Certificate chain
# 0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googlecode.com
# i:/C=US/O=Google Inc/CN=Google Internet Authority
# 1 s:/C=US/O=Google Inc/CN=Google Internet Authority
# i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
# ---
# subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googlecode.com
# issuer=/C=US/O=Google Inc/CN=Google Internet Authority
#
-----BEGIN CERTIFICATE-----
MIID8zCCA1ygAwIBAgIKYFQAGQABAACI2DANBgkqhkiG9w0BAQUFADBGMQswCQYD
VQQGEwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzEiMCAGA1UEAxMZR29vZ2xlIElu
dGVybmV0IEF1dGhvcml0eTAeFw0xMzA1MjIxNTQ5MzZaFw0xMzEwMzEyMzU5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M8URx3eVlBfR3bex6gdr5UWTtrRY+zwuG+hNj2IOqNNsGXszr1AAt
dlQCICci26F85I+OeRxpOfo+5LFbdqkS43T46Fbt68SNzwK5WHD3gN/R8yinxam4
h2eohczgVg==
-----END CERTIFICATE-----
EOF
google_cert=${HOME}/code.google.com.pem
cat > $google_cert <<EOF
# Certificate chain
# 0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.google.com
# i:/C=US/O=Google Inc/CN=Google Internet Authority
# 1 s:/C=US/O=Google Inc/CN=Google Internet Authority
# i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
# ---
# subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.google.com
# issuer=/C=US/O=Google Inc/CN=Google Internet Authority
#
-----BEGIN CERTIFICATE-----
MIIGKjCCBZOgAwIBAgIKYFSo5QABAACI/DANBgkqhkiG9w0BAQUFADBGMQswCQYD
VQQGEwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzEiMCAGA1UEAxMZR29vZ2xlIElu
dGVybmV0IEF1dGhvcml0eTAeFw0xMzA1MjIxNTUwMjBaFw0xMzEwMzEyMzU5NTla
MGYxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1N
b3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMRUwEwYDVQQDFAwqLmdv
b2dsZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJrwTkq86I+x0Qwp
k9TWhohc4j+SVTSX9UQOcX0BK6LcAGcM6tCCmerrPjenj8ugQttOq8HlsBs1tYna
Ijwi4YzphBG6pDvllIaYXxOdN4jQYAdVXazRMP5yPe6GITI7eIB2GVUYz1CFH3yV
6jiJkAW9cxyLiu/Nq4eZ/4Cv2vbVAgMBAAGjggP9MIID+TAdBgNVHSUEFjAUBggr
BgEFBQcDAQYIKwYBBQUHAwIwHQYDVR0OBBYEFI62cYbUFPtyWMvEtlXQnK5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VjYXRpb24uY29tggsqLnl0aW1nLmNv
bYILYW5kcm9pZC5jb22CBGcuY2+CBmdvby5nbIIUZ29vZ2xlLWFuYWx5dGljcy5j
b22CCmdvb2dsZS5jb22CEmdvb2dsZWNvbW1lcmNlLmNvbYIKdXJjaGluLmNvbYII
eW91dHUuYmWCC3lvdXR1YmUuY29tghR5b3V0dWJlZWR1Y2F0aW9uLmNvbTANBgkq
hkiG9w0BAQUFAAOBgQBth2xoSd+0Tsw8V096Ix2yPQKj7xz9GOt09ac+vXXazCPE
GVybxZZ5Xf0xxMa7+STBajHYNLgVg7te4rTXs6HaUS/IHri6ZtZ1NtotRWIjk5XR
wFZmH2AKFgdm8A4koiNdn+KuOXt1s6qLgLarDZOD4dr8WatNV1iA3CtiWPDLbQ==
-----END CERTIFICATE-----
EOF
# $1: (bool) USE_WITH_TOR
function install_go ()
{
OUR_ARCH=$(uname -m)
FILE="https://go.googlecode.com/files/go"
DOWN="https://code.google.com/p/go/downloads/detail?name=go"
if [[ "$OUR_ARCH" == "x86_64" ]]; then
ARCH_STRING=".linux-amd64.tar.gz"
elif [[ "$OUR_ARCH" == "i686" ]]; then
ARCH_STRING=".linux-386.tar.gz"
else
printf "Unsupported architecture. Must be 64-bit or 32-bit.\n"
exit 1
fi
save_file=${GO_VERSION}${ARCH_STRING}
tarball=${FILE}$save_file
details=${DOWN}$save_file
GO_SOURCE=${HOME}/go${save_file}
HTML_PAGE=${HOME}/go${GO_VERSION}.tar.gz.html
if ! test -f "$GO_SOURCE" ; then
if test -n "$tarball"; then
printf "Downloading GoTools, which includes gc (the Go compiler)...\n"
printf "Getting GoTools from:\n\t%s\n" ${tarball}
if [[ "${USE_WITH_TOR}" == 1 ]]; then
cd ${HOME} && \
usewithtor wget --progress=bar -U "$USER_AGENT" \
--secure-protocol=TLSv1 --ca-certificate=$googlecode_cert \
-O ${GO_SOURCE} ${tarball}
else
cd ${HOME} && \
wget --timestamping --progress=bar -U "$USER_AGENT" \
--secure-protocol=TLSv1 --ca-certificate=$googlecode_cert \
-O ${GO_SOURCE} ${tarball}
fi
else
printf "Error determining correct download...exiting...\n"
unset USE_WITH_TOR
exit 1
fi
else
printf "Download already completed, attempting to resume install."
fi
if ! test -f "$HTML_PAGE" ; then
printf "Getting reported SHA1 from:\n\t%s\n" ${details}
if [[ "${USE_WITH_TOR}" == 1 ]]; then
cd ${HOME} && \
usewithtor wget -nv --progress=bar -U "$USER_AGENT" \
--secure-protocol=TLSv1 --ca-certificate=$google_cert \
-O $HTML_PAGE ${details}
else
cd ${HOME} && \
wget -nv --progress=bar -U "$USER_AGENT" \
--secure-protocol=TLSv1 --ca-certificate=$google_cert \
-O $HTML_PAGE ${details}
fi
else
printf "We've already got the reported SHA1 from code.google.com...\n"
fi
unset USE_WITH_TOR
theirs=/tmp/go_reported_hash
ours=/tmp/go_our_hash
printf "Checking SHA1 hash of downloaded source...\n"
grep "sha1" $HTML_PAGE | cut -d ">" -f 2 | cut -d "<" -f 1 | cat > $theirs
sha1sum $GO_SOURCE | cut -d " " -f 1 > $ours
printf "Reported SHA1 from Google Code: %s\n" $(cat $theirs )
printf "Computed SHA1 from download: %s\n" $(cat $ours )
HASHDIFF=`diff ${theirs} ${ours}`
if [[ "$HASHDIFF" == "" ]]; then
printf "Installing GoTools ...\n"
tar -C $PREFIX -xzf $GO_SOURCE && \
printf "Installation successful.\n"
else
printf "WARNING: SHA1 sums do not match! Not installing...\n"
printf "Exiting...\n"
exit 1
fi
}
function clean_up ()
{
JUNK="$theirs $ours $HTML_PAGE $GO_SOURCE $googlecode_cert $google_cert "
printf "Cleaning up..."
for file in $JUNK; do
if test -f $file ; then
printf "Removing ${file}..."
rm $file
fi
done
}
function export_paths ()
{
if [ $(id -u) -eq 0 ]; then
printf ""
printf "Modifying system \$PATH to include Go..."
cat >> /etc/profile<<EOF
## Path to GoTools
export GOROOT=$PREFIX/go
export GOPATH=\$HOME/go
export PATH=\$PATH:\$GOROOT/bin:\$GOPATH/bin
EOF
printf "Successfully updated \$PATH in /etc/profile."
else
printf ""
printf "Modifying user \$PATH..."
cat >> $HOME/.profile<<FIN
## Path to GoTools
## place the following in /etc/profile to enable Go for all users:
export GOROOT=$PREFIX/go
export GOPATH=\$HOME/go
export PATH=\$PATH:\$GOROOT/bin:\$GOPATH/bin
FIN
printf "Successfully updated \$PATH in ${HOME}/.profile..."
fi
}
function root_check ()
{
if [ $(id -u) -ne 0 ]; then
printf "Must be run as root."
exit 1
fi
}
function os_check ()
{
if [ $(uname) != "Linux" ]; then
printf "Sorry, this only works for Linux."
exit 1
fi
}
function go_check ()
{
if test -n $(which go) ; then
current=$(go version | cut -d " " -f 3 | cut -b 3- )
printf "Found previous Go installation. Version: %s\n" ${current}
if test -d $PREFIX/go ; then
if [[ "$current" == "${GO_VERSION}" ]] ; then
printf "Already up to date!\n"
exit 0
elif [[ "$current" > "${GO_VERSION}" ]] ; then
printf "Oops! You've got Go %s, while we've got Go %s.\n" ${current} $GO_VERSION
printf "Please mailto:%s and tell Isis to update me.\n" $contact
else
printf "Updating previous Go installation to %s...\n" $GO_VERSION
fi
fi
fi
}
function mercurial_check ()
{
if test -z $(which hg) ; then
cat <<EOF
Go requires mercurial. Please use your distro's package manager to obtain
mercurcial first. Exiting...
EOF
exit
fi
}
USE_WITH_TOR=0
while [[ "x$1" != "x" ]]; do
case $1 in
install )
#root_check
mercurial_check
os_check
go_check
install_go $USE_WITH_TOR
clean_up
export_paths
printf "\n\nInstallation successful!\n"
printf "To get started learning Go, try doing:\n"
printf "\t$ go get code.google.com/p/go-tour/gotour\n"
unset tarball
unset details
exit 0
;;
reinstall )
#root_check
mercurial_check
os_check
go_check
install_go $USE_WITH_TOR
clean_up
printf "\n\nUpgrade successful!\n"
printf "To get started learning Go, try doing:\n"
printf "\t$ go get code.google.com/p/go-tour/gotour\n"
unset tarball
unset details
exit 0
;;
path )
os_check
export_paths
exit 0
;;
tor )
printf "Attempting to force downloads over Tor...\n"
tpo='https://www.torproject.org/download'
tsocks='https://gitweb.torproject.org/torsocks.git'
if [ -z "$(which tor)" ] ; then
printf "It seems you don't have Tor installed!\n"
printf "You can download it here:\n\t%s\n" $tpo
elif [ -z "$(which usewithtor)" ] ; then
cat <<EOF
It seems you don't have torsocks installed!
Unfortunately, if you distribution doesn't already provide a package for it,
you'll likely have to build it from source. The git repository for it is here:
$tsocks
EOF
else
USE_WITH_TOR=1
export USE_WITH_TOR
fi
shift
;;
* )
usage
exit 1
;;
esac
done