This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathinstall-kd.sh
More file actions
executable file
·411 lines (319 loc) · 9.85 KB
/
install-kd.sh
File metadata and controls
executable file
·411 lines (319 loc) · 9.85 KB
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
407
408
409
410
411
#!/usr/bin/env bash
readonly releaseChannel="%RELEASE_CHANNEL%"
readonly OSXFUSE_URL="https://s3.amazonaws.com/koding-dl/osxfuse-3.5.2.dmg"
readonly VIRTUALBOX_URL_LINUX="http://download.virtualbox.org/virtualbox/5.1.8/VirtualBox-5.1.8-111374-Linux_amd64.run"
readonly VIRTUALBOX_URL_DARWIN="http://download.virtualbox.org/virtualbox/5.1.8/VirtualBox-5.1.8-111374-OSX.dmg"
readonly VAGRANT_URL_LINUX="https://releases.hashicorp.com/vagrant/1.8.7/vagrant_1.8.7_x86_64.deb"
readonly VAGRANT_URL_DARWIN="https://releases.hashicorp.com/vagrant/1.8.7/vagrant_1.8.7.dmg"
VERSION="$(curl -sSL https://koding-kd.s3.amazonaws.com/${releaseChannel}/latest-version.txt)"
PLATFORM="$(uname | tr '[:upper:]' '[:lower:]')"
KD_URL="https://koding-kd.s3.amazonaws.com/${releaseChannel}/kd-0.1.${VERSION}.${PLATFORM}_amd64.gz"
is_virtualbox() {
VBoxHeadless -h 2>&1 | grep -c 'Oracle VM VirtualBox Headless Interface' >/dev/null
}
is_vagrant() {
vagrant version 2>&1 | grep -c 'Installed Version:' >/dev/null
}
# download_file <url> <output file>
download_file() {
curl --location --retry 5 --retry-delay 0 --output "$2" "$1"
}
# prompt_install <install info>
prompt_install() {
if [ -z ${KD_NONINTERACTIVE:-} ]; then
echo
read -p "${1} [y/N] " -n 1 -r < /dev/tty
echo
else
REPLY=$KD_NONINTERACTIVE
fi
# default to no
if [ -z "$REPLY" ]; then
REPLY="n"
fi
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
fi
return 1
}
die() {
echo $* 1>&2
exit 1
}
install_fuse_darwin() {
local fuseDir="/Library/Filesystems/osxfuse.fs"
local fuseDmg=/tmp/osxfuse.dmg
if [[ $PLATFORM == "linux" ]]; then
return 0 # setup fuse on linux?
fi
# check if osxfuse is installed already
if [ -d "$fuseDir" ]; then
return
fi
if ! prompt_install "kd requires osxfuse to work. Do you want to install it now?"; then
die "Installation failed. Please run the installer again."
fi
echo ""
echo "Downloading osxfuse..."
# download osxfuse
if ! download_file "$OSXFUSE_URL" "$fuseDmg"; then
die "error: failed to download OSX Fuse" 2>&1
fi
echo
# attach dmg as a volume
sudo hdiutil attach $fuseDmg
# run the osxfuse installer
sudo installer -pkg "/Volumes/FUSE for macOS/Extras/FUSE for macOS 3.5.2.pkg" -target "/"
# unmount dmg after it's finished
diskutil unmount force "/Volumes/FUSE for macOS"
rm -f "$fuseDmg"
echo "Created $fuseDir"
}
install_vagrant_deps() {
if ! prompt_install "VirtualBox and Vagrant are needed in order to build Vagrant stack. Do you want to install them now?"; then
return
fi
install_virtualbox
install_vagrant
if ! vagrant box list | grep ubuntu/trusty64 >/dev/null; then
vagrant box add ubuntu/trusty64
fi
}
install_virtualbox() {
if is_virtualbox; then
return 0
fi
case "$PLATFORM" in
linux)
install_virtualbox_linux
;;
darwin)
install_virtualbox_darwin
;;
*)
die "error: platform not supported: $PLATFORM"
;;
esac
}
install_virtualbox_linux() {
local vboxRun="/tmp/virtualbox.run"
if command -v apt-get >/dev/null; then
sudo apt-get install -q -y dkms linux-headers-$(uname -r) make build-essential screen
fi
if ! download_file "$VIRTUALBOX_URL_LINUX" "$vboxRun"; then
die "error: failed to download VirtualBox"
fi
chmod +x "$vboxRun"
if ! sudo "$vboxRun"; then
die "error: failed to install VirtualBox"
fi
rm -f "$vboxRun"
}
install_virtualbox_darwin() {
local vboxDmg="/tmp/virtualbox.dmg"
if ! download_file "$VIRTUALBOX_URL_DARWIN" "$vboxDmg"; then
die "error: failed to download VirtualBox"
fi
sudo hdiutil attach "$vboxDmg"
sudo installer -pkg "/Volumes/VirtualBox/VirtualBox.pkg" -target /
diskutil unmount force "/Volumes/VirtualBox"
rm -f "$vboxDmg"
}
install_vagrant() {
if is_vagrant; then
return 0
fi
case "$PLATFORM" in
linux)
install_vagrant_linux
;;
darwin)
install_vagrant_darwin
;;
*)
die "error: platform not supported: $PLATFORM"
;;
esac
}
install_vagrant_linux() {
local vagrantDeb="/tmp/vagrant.deb"
if ! download_file "$VAGRANT_URL_LINUX" "$vagrantDeb"; then
die "error: failed to download vagrant"
fi
if ! sudo dpkg -i "$vagrantDeb"; then
die "error: failed to install vagrant"
fi
rm -f "$vagrantDeb"
}
install_vagrant_darwin() {
local vagrantDmg="/tmp/vagrant.dmg"
if ! download_file "$VAGRANT_URL_DARWIN" "$vagrantDmg"; then
die "error: failed to download vagrant"
fi
sudo hdiutil attach "$vagrantDmg"
sudo installer -pkg "/Volumes/Vagrant/Vagrant.pkg" -target /
diskutil unmount force "/Volumes/Vagrant"
# Workaround for Vagrant 1.8.7, which fails to "box add".
sudo rm -f /opt/vagrant/embedded/bin/curl || true
rm -f "$vagrantDmg"
}
if [ -z "$1" ]; then
cat << EOF
The Koding application installer requires an authentication token from
Koding. Please make sure to copy the full command when running this script.
EOF
# Disabled until learn article exists.
#For documentation on this process, please visit the following URL:
#
# http://learn.koding.com/guides/kd
#
#EOF
exit 1
fi
echo "Hello, this is the Koding application (kd) installer."
echo "This installer requires sudo permissions, please input password if prompted..."
if ! sudo -l 2>&1 >/dev/null; then
cat << EOF
Error: Sudo (root) permission is required to install kd. Please run this
command from an account on this machine with proper permissions.
EOF
exit $err
fi
# For now i'm testing on KodingVMs, so i'm allowing KodingVMs
#sudo route del -host 169.254.169.254 reject 2> /dev/null
#routeErr=$?
#awsApiResponse=`curl http://169.254.169.254/latest/dynamic/instance-identity/document --max-time 5 2> /dev/null`
#if [ "$routeErr" -eq 0 ]; then
# sudo route add -host 169.254.169.254 reject 2> /dev/null
#fi
#if [[ $awsApiResponse == *"614068383889"* ]]; then
# cat << EOF
#Error: This feature is for non-Koding machines
#EOF
# exit 1
#fi
# This check helps us prevent kd from being installed over a managed vm
# where kd would have trouble replacing klient in an unknown environment.
#
# TODO: renable this after oldder version of kd has replaced with self
# updateable one.
#
# if sudo [ -f /opt/kite/klient/klient ]; then
# cat << EOF
# Error: Klient is already installed. Please remove it before installing kd.
# If you are attempting to update kd, please run the following command:
# sudo kd update
# EOF
# exit 1
# fi
# TODO: Support both wget and curl
if ! which curl &>/dev/null; then
echo "Error: curl is required to install kd. Please install curl and run this installer again."
exit 1
fi
case "$PLATFORM" in
darwin|linux)
installDir="/usr/local/bin"
kdFile="/tmp/kd"
kdGz="${kdFile}.gz"
# On some OSX systems, /usr/local/bin doesn't seem to exist. Create it.
if sudo [ ! -d "$installDir" ]; then
sudo mkdir -p $installDir
# /usr/local/bin is normally chowned as `user:admin`,
# eg: `jake:admin` on osx
if [ -n "$USER" ]; then
sudo chown $USER $installDir
fi
echo "Created $installDir"
fi
echo
echo "Downloading kd..."
if ! download_file "$KD_URL" "$kdGz"; then
cat << EOF
Error: Failed to download kd binary. Please check your internet
connection or try again.
EOF
exit 1
fi
if ! sudo gzip -d -f "$kdGz"; then
echo "Error: Failed to extract kd binary." 2>&1
exit 1
fi
sudo chmod +x $kdFile
# TODO(rjeczalik): revisit if we really need to uninstall
# or whether install should handle overwrite instead
sudo $kdFile stop > /dev/null 2>&1
sudo $kdFile uninstall > /dev/null 2>&1
sudo mv $kdFile "${installDir}/kd"
echo "Created ${installDir}/kd"
if [[ "$PLATFORM" == "darwin" ]]; then
# Ensure old klient is not running.
sudo launchctl unload -w /Library/LaunchDaemons/com.koding.klient &>/dev/null || true
rm -f /Library/LaunchDaemons/com.koding.klient &>/dev/null || true
# Check if fuse is needed, and install it if it is.
install_fuse_darwin
fi
if ! is_virtualbox || ! is_vagrant; then
install_vagrant_deps
fi
echo
;;
windows)
cat << EOF
Error: This platform is not supported at this time.
EOF
exit 2
;;
*)
cat << EOF
Error: Failed to identify which platform you are installing from.
EOF
exit 2
;;
esac
kontrolFlag="--baseurl=https://sandbox.koding.com"
if [ "$releaseChannel" = "production" ]; then
kontrolFlag="--baseurl=https://koding.com"
fi
if [ -n "$KONTROLURL" ]; then
echo "Installing with custom Kontrol Url... '$KONTROLURL'"
kontrolFlag="--baseurl=${KONTROLURL%/kontrol/kite}"
fi
# Ignore false negative.
#
# shellcheck disable=SC2024
if ! sudo /usr/local/bin/kd install $kontrolFlag --token "$1" < /dev/tty; then
exit $err
fi
cat << EOF
Success! kd (version ${VERSION}, channel ${releaseChannel}) has been successfully installed.
Please run the following command for more information:
kd -h
EOF
kiteQueryID=$(kd version 2>/dev/null | grep 'Kite Query ID' | cut -d: -f 2 | tr -s ' ')
if ! is_virtualbox && ! is_vagrant; then
cat << EOF
No VirtualBox nor Vagrant is present on your system. In order to use local provisioning
with Vagrant provider ensure they are installed:
* VirtualBox 5.0+ (https://www.virtualbox.org/wiki/Downloads)
* Vagrant 1.7.4+ (https://www.vagrantup.com/downloads.html)
EOF
elif ! is_virtualbox; then
cat << EOF
No VirtualBox is present on your system. In order to use local provisioning
with Vagrant provider ensure it is installed:
* VirtualBox 5.0+ (https://www.virtualbox.org/wiki/Downloads)
EOF
elif ! is_vagrant; then
cat << EOF
No Vagrant is present on your system. In order to use local provisioning
with Vagrant provider ensure it is installed:
* Vagrant 1.7.4+ (https://www.vagrantup.com/downloads.html)
EOF
fi
if [[ -n "$kiteQueryID" ]]; then
cat << EOF
Your Kite Query ID, which you can use as a credential for local provisioning, is:
$kiteQueryID
EOF
fi