-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathidt-installer
More file actions
executable file
·508 lines (451 loc) · 17.9 KB
/
idt-installer
File metadata and controls
executable file
·508 lines (451 loc) · 17.9 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/bin/bash
#------------------------------------------------------------------------------
# Script: idt-installer
#------------------------------------------------------------------------------
# IBM Cloud Developer Tools - CLI installer script for MacOS and Linux systems
#------------------------------------------------------------------------------
# Copyright (c) 2018, International Business Machines. All Rights Reserved.
#------------------------------------------------------------------------------
VERSION="1.2.3"
PROG="IBM Cloud Developer Tools for Linux/MacOS - Installer"
INSTALLER_URL="https://ibm.biz/idt-installer"
GIT_URL="https://github.com/IBM-Cloud/ibm-cloud-developer-tools"
SLACK_URL="https://slack-invite-ibm-cloud-tech.mybluemix.net/"
IDT_INSTALL_BMX_URL="https://clis.ng.bluemix.net/install"
IDT_INSTALL_BMX_REPO_NAME="Bluemix"
IDT_INSTALL_BMX_REPO_URL="https://plugins.ng.bluemix.net"
#------------------------------------------------------------------------------
function help {
cat <<-!!EOF
${PROG}
Usage: idt-installer [<args>]
Where <args> is:
install [Default] Perform full install (or update) of all needed CLIs and Plugins
uninstall Uninstall full IBM Cloud CLI env, including 'bx', and plugins
help | -h | -? Show this help
--force Force updates of dependencies and other settings during update
--trace Eanble verbose tracing of all activity
If "install" (or no action provided), a full CLI installation (or update) will occur:
1. Pre-req check for 'git', 'docker', 'kubectl', and 'helm'
2. Install latest IBM Cloud 'bx' CLI
3. Install all required plugins
4. Defines 'idt' shortcut to improve useability.
- idt : Shortcut for normal "bx dev" command
- idt update : Runs this installer checking for and installing any updates
- idt uninstall : Uninstalls IDT, 'bx' cli, and all plugins
Chat with us on Slack: ${SLACK_URL}, channel #developer-tools
Submit any issues to : ${GIT_URL}/issues
!!EOF
}
#------------------------------------------------------------------------------
#-- ${FUNCNAME[1]} == Calling function's name
#-- Colors escape seqs
YEL='\033[1;33m'
CYN='\033[0;36m'
GRN='\033[1;32m'
RED='\033[1;31m'
NRM='\033[0m'
function log {
echo -e "${CYN}[${FUNCNAME[1]}]${NRM} $*"
}
function warn {
echo -e "${CYN}[${FUNCNAME[1]}]${NRM} ${YEL}WARN${NRM}: $*"
}
function error {
echo -e "${CYN}[${FUNCNAME[1]}]${NRM} ${RED}ERROR${NRM}: $*"
exit -1
}
function prompt {
label=${1}
default=${2}
if [[ -z $default ]]; then
echo -en "${label}: ${CYN}" > /dev/tty
else
echo -en "${label} [$default]: ${CYN}" > /dev/tty
fi
read -r
echo -e "${NRM}" > /dev/tty
#-- Use $REPLY to get user's input
}
#------------------------------------------------------------------------------
function uninstall {
if [[ -t 0 ]]; then #-- are we in a terminal?
echo
prompt "Please confirm you want to uninstall IBM Cloud Developer Tools (y/N)?"
if [[ "$REPLY" != [Yy]* ]]; then
log "Uninstall aborted at user request"
return
fi
fi
warn "Starting Uninstall..."
[ "$SUDO" ] && log "You may be prompted for 'sudo' password."
#-- Run the following regardless
$SUDO rm -f /usr/local/bin/bluemix
$SUDO rm -f /usr/local/bin/bx
$SUDO rm -f /usr/local/bin/bluemix-analytics
$SUDO rm -rf /usr/local/Bluemix
#-- Taken from bluemix CLI brew uninstaller
if [[ -f /etc/profile ]]; then
$SUDO sed -E -i ".bluemix_uninstall_bak" \
-e '/^### Added by the Bluemix CLI$/d' \
-e '/^source \/usr\/local\/Bluemix\/bx\/bash_autocomplete$/d' \
/etc/profile
fi
if [[ -f ~/.bashrc ]]; then
sed -E -i ".bluemix_uninstall_bak" \
-e '/^### Added by the Bluemix CLI$/d' \
-e '/^source \/usr\/local\/Bluemix\/bx\/bash_autocomplete$/d' \
~/.bashrc
fi
if [[ -f ~/.zshrc ]]; then
sed -E -i ".bluemix_uninstall_bak" \
-e '/^### Added by the Bluemix CLI$/d' \
-e '/^source \/usr\/local\/Bluemix\/bx\/zsh_autocomplete$/d' \
~/.zshrc
fi
env_setup remove
rm -rf ~/.bluemix
log "Uninstall finished."
}
#------------------------------------------------------------------------------
function install {
if [[ -n "$(which idt)" ]]; then
log "Starting Installation..."
else
log "Starting Update..."
fi
#-- Check if internal IBM setup
if [[ -n "$(which bx)" ]]; then
read -r repo url <<< $(bx plugin repos | grep stage1)
if [[ -n "$repo" ]]; then
echo
prompt "Use IBM internal '$repo' repos for install/updates (Y/n)?"
echo
if [[ "$REPLY" != [Nn]* ]]; then
IDT_INSTALL_BMX_URL="https://clis.stage1.ng.bluemix.net/install"
IDT_INSTALL_BMX_REPO_NAME="${repo}"
IDT_INSTALL_BMX_REPO_URL="${url}"
fi
fi
fi
[ "$SUDO" ] && log "Note: You may be prompted for your 'sudo' password during install."
install_deps
install_bx
install_plugins
env_setup add
log "Install finished."
}
#------------------------------------------------------------------------------
function install_deps {
#-- check for/install brew for macos
case "$PLATFORM" in
"Darwin")
install_darwin_deps
;;
"Linux")
if [[ "${DISTRO}" == *Ubuntu* || "${DISTRO}" == *Debian* ]]; then
install_deps_with_apt_get
elif [[ "${DISTRO}" == *Red*Hat* || "${DISTRO}" == *CentOS* ]]; then
install_deps_with_yum
else
error "This script has not been updated for use with your linux distribution (${DISTRO})"
fi
;;
esac
}
#------------------------------------------------------------------------------
function install_darwin_deps {
log "Checking for external dependency: brew"
if [[ -z "$(which brew)" && -n "$(which ruby)" ]]; then
log "'brew' installer not found, attempting to install..."
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
log "'brew' installation completed."
fi
if [[ "$FORCE" == true ]]; then
log "Updating 'brew'..."
brew update
fi
#-- GIT:
log "Installing/updating external dependency: git"
if [[ -z "$(which git)" ]]; then
brew install git
log "Please review any setup requirements for 'git' from: https://git-scm.com/downloads"
elif [[ "$FORCE" == true ]]; then
brew upgrade git
fi
#-- Docker:
log "Installing/updating external dependency: docker"
if [[ -z "$(which docker)" ]]; then
brew cask install docker
log "Please review any setup requirements for 'docker' from: https://docs.docker.com/engine/installation/"
elif [[ "$FORCE" == true ]]; then
brew cask reinstall docker
fi
#-- kubectl:
log "Installing/updating external dependency: kubectl"
if [[ -z "$(which kubectl)" || "$FORCE" == true ]]; then
curl --progress-bar -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl
$SUDO mv ./kubectl /usr/local/bin/kubectl
$SUDO chmod +x /usr/local/bin/kubectl
log "Please review any setup requirements for 'kubectl' from: https://kubernetes.io/docs/tasks/tools/install-kubectl/"
fi
#-- helm:
log "Installing/updating external dependency: helm"
if [[ -z "$(which helm)" ]]; then
brew install kubernetes-helm
log "Please review any setup requirements for 'helm' from: https://github.com/kubernetes/helm/blob/master/docs/install.md"
elif [[ "$FORCE" == true ]]; then
brew upgrade kubernetes-helm
fi
}
#------------------------------------------------------------------------------
function install_deps_with_apt_get {
log "Checking for and updating 'apt-get' support on Linux"
if [[ -z "$(which apt-get)" ]]; then
error "'apt-get' is not found. That's the only Debian/Ubuntu linux installer I know, sorry."
fi
#-- CURL:
log "Installing/updating external dependency: curl"
if [[ -z "$(which curl)" || "$FORCE" == true ]]; then
$SUDO apt-get -y install curl
fi
#-- GIT:
log "Installing/updating external dependency: git"
if [[ -z "$(which git)" || "$FORCE" == true ]]; then
$SUDO apt-get -y install git
log "Please review any setup requirements for 'git' from: https://git-scm.com/downloads"
fi
#-- Docker:
install_docker
#-- kubectl:
log "Installing/updating external dependency: kubectl"
if [[ -z "$(which kubectl)" || "$FORCE" == true ]]; then
curl --progress-bar -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
$SUDO mv ./kubectl /usr/local/bin/kubectl
$SUDO chmod +x /usr/local/bin/kubectl
log "Please review any setup requirements for 'kubectl' from: https://kubernetes.io/docs/tasks/tools/install-kubectl/"
fi
#-- helm:
log "Installing/updating external dependency: helm"
if [[ -z "$(which helm)" || "$FORCE" == true ]]; then
curl -fsSL https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get | bash
log "Please review any setup requirements for 'helm' from: https://github.com/kubernetes/helm/blob/master/docs/install.md"
fi
}
#------------------------------------------------------------------------------
function install_deps_with_yum {
log "Checking for and updating 'apt-get' support on Linux"
if [[ -z "$(which yum)" ]]; then
error "'yum' is not found. That's the only RedHat/Centos linux installer I know, sorry."
fi
#-- CURL:
log "Installing/updating external dependency: curl"
if [[ -z "$(which curl)" || "$FORCE" == true ]]; then
$SUDO yum -y install curl
fi
#-- GIT:
log "Installing/updating external dependency: git"
if [[ -z "$(which git)" || "$FORCE" == true ]]; then
$SUDO yum install -y git
log "Please review any setup requirements for 'git' from: https://git-scm.com/downloads"
fi
#-- Docker:
install_docker
#-- kubectl:
log "Installing/updating external dependency: kubectl"
if [[ -z "$(which kubectl)" || "$FORCE" == true ]]; then
printf "[kubernetes]\nname=Kubernetes\nbaseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64\nenabled=1\ngpgcheck=1\nrepo_gpgcheck=1\ngpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg\n" > /etc/yum.repos.d/kubernetes.repo
$SUDO yum install -y kubectl
log "Please review any setup requirements for 'kubectl' from: https://kubernetes.io/docs/tasks/tools/install-kubectl/"
fi
#-- helm:
log "Installing/updating external dependency: helm"
if [[ -z "$(which helm)" || "$FORCE" == true ]]; then
curl -fsSL https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get | bash
log "Please review any setup requirements for 'helm' from: https://github.com/kubernetes/helm/blob/master/docs/install.md"
fi
}
#------------------------------------------------------------------------------
function install_docker {
#-- Docker:
log "Installing/updating external dependency: docker"
if [[ -z "$(which docker)" || "$FORCE" == true ]]; then
curl -fsSL get.docker.com | $SUDO sh -
if [ "$SUDO" ]; then
# Allow docker to run as a non-root user (if not running as root).
sudo groupadd docker 2>/dev/null
sudo usermod -aG docker $USER 2>/dev/null
else
log 'If you want to run docker without sudo run: "sudo groupadd docker && sudo usermod -aG docker $USER"'
fi
log "Please review any setup requirements for 'docker' from: https://docs.docker.com/engine/installation/"
fi
docker version
}
#------------------------------------------------------------------------------
function install_bx {
if [[ -z "$(which bx)" ]]; then
log "Installing IBM Cloud 'bx' CLI for platform '${PLATFORM}'..."
case "$PLATFORM" in
"Darwin")
log "Downloading and installing IBM Cloud 'bx' CLI from: ${IDT_INSTALL_BMX_URL}/osx"
sh <(curl -fsSL ${IDT_INSTALL_BMX_URL}/osx)
;;
"Linux")
log "Downloading and installing IBM Cloud 'bx' CLI from: ${IDT_INSTALL_BMX_URL}/linux"
sh <(curl -fsSL ${IDT_INSTALL_BMX_URL}/linux)
;;
esac
log "IBM Cloud 'bx' CLI install finished."
else #-- Upgrade
log "Updating existing IBM Cloud 'bx' CLI..."
bx update
fi
log "Running 'bx --version'..."
bx --version
}
#------------------------------------------------------------------------------
function install_plugins {
#-- BX plugins to process
PLUGINS=(
"cloud-functions"
"container-registry"
"container-service"
"dev"
"sdk-gen"
)
log "Installing/updating IBM Cloud CLI plugins used by IDT..."
for plugin in "${PLUGINS[@]}"; do
log "Checking status of plugin: ${plugin}"
read -r p ver <<< "$(bx plugin list | grep "^${plugin} ")"
if [[ -z "$p" ]]; then
log "Installing plugin '$plugin'"
bx plugin install -r "${IDT_INSTALL_BMX_REPO_NAME}" "$plugin"
else
log "Updating plugin '$plugin' from version '$ver'"
bx plugin update -r "${IDT_INSTALL_BMX_REPO_NAME}" "$plugin"
fi
done
log "Running 'bx plugin list'..."
bx plugin list
log "Finished installing/updating plugins"
}
#------------------------------------------------------------------------------
function env_setup {
idt_prog="/usr/local/bin/idt"
env_file=""
if [[ -f ~/.bashrc ]] ; then env_file=~/.bashrc
elif [[ -f ~/.bash_profile ]]; then env_file=~/.bash_profile
elif [[ -f ~/.profile ]] ; then env_file=~/.profile
fi
#-- Clear up any old aliases
if [[ -n "$(grep 'alias idt="bx dev"' "$env_file")" ]]; then
log "Removing old 'idt' aliases from: ${env_file}"
sed -E -i ".idt_uninstall_bak" \
-e '/^#-- Added by the IDT Installer$/d' \
-e '/^alias idt=\"bx dev\"$/d' \
-e '/^alias idt-update=/d' \
-e '/^alias idt-uninstall=/d' \
${env_file}
warn "Please restart your shell so old 'idt' alias does not get picked up!"
warn "Symptom is: running 'idt update' results in 'update is not a defined command'."
fi
if [[ "$1" == "add" ]]; then
idt_launch_ver=$(grep "# Version:" /usr/local/bin/idt 2>/dev/null | cut -d':' -f2)
if [[ ! -f "$idt_prog" || "$FORCE" == true || "${idt_launch_ver}" != "$VERSION" ]]; then
cat <<-!!EOF > ~/idt
#!/bin/bash
#-----------------------------------------------------------
# IBM Cloud Developer Tools (IDT)
# Version:${VERSION}
# Wrapper for the 'bx dev' command, and external helpers.
#-----------------------------------------------------------
# Syntax:
# idt - Run 'bx dev <args>'
# idt update [--trace] [--force] - Update IDT and deps
# idt uninstall [--trace] - Uninstall IDT
#-----------------------------------------------------------
if [[ "\$1" == "update" || "\$1" == "uninstall" ]]; then
echo "IDT launcher action: \$1"
tmp=\$(mktemp -d 2>/dev/null || mktemp -d -t 'idttmpdir')
echo "Fetching latest installer to: \$tmp/idt-installer"
curl -sL https://ibm.biz/idt-installer -o \$tmp/idt-installer
bash -- \$tmp/idt-installer \$*
rm -r \$tmp
else
bx dev \$*
fi
#-----------------------------------------------------------
!!EOF
$SUDO mv ~/idt $idt_prog
$SUDO chmod 755 $idt_prog
log "The following shortcuts defined to access the IBM Cloud Developer Tools CLI:"
log " ${GRN}idt${NRM} : Main command, shorthand for '${CYN}bx dev${NRM}'"
log " ${GRN}idt update${NRM} : Update your IBM Cloud Developer Tools to the latest version"
log " ${GRN}idt uninstall${NRM} : Uninstall the IBM Cloud Developer Tools"
fi
elif [[ "$1" == "remove" ]]; then
$SUDO rm -f $idt_prog
else
error "Internal error - called with invalid parameter: ${1}"
fi
}
#------------------------------------------------------------------------------
# MAIN
#------------------------------------------------------------------------------
function main {
log "--==[ ${GRN}${PROG}, v${VERSION}${NRM} ]==--"
(( SECS = SECONDS ))
TMPDIR=${TMPDIR:-"/tmp"}
PLATFORM=$(uname)
ACTION=""
# Only use sudo if not running as root:
[ "$(id -u)" -ne 0 ] && SUDO=sudo || SUDO=""
#-- Parse args
while [[ $# -gt 0 ]]; do
case "$1" in
"--trace")
warn "Enabling verbose tracing of all activity"
set -x
;;
"--force")
FORCE=true
warn "Forcing updates for all dependencies and other settings"
;;
"update") ACTION="install";;
"install") ACTION="install";;
"uninstall") ACTION="uninstall";;
"help") ACTION="help";;
esac
shift
done
case "$PLATFORM" in
"Darwin")
;;
"Linux")
# Linux distro, e.g "Ubuntu", "RedHatEnterpriseWorkstation", "RedHatEnterpriseServer", "CentOS", "Debian"
DISTRO=$(lsb_release -ds 2>/dev/null || cat /etc/*release 2>/dev/null | head -n1 || uname -om || echo "")
if [[ "$DISTRO" != *Ubuntu* && "$DISTRO" != *RedHat* && "$DISTRO" != *CentOS* && "$DISTRO" != *Debian* ]]; then
warn "Linux has only been tested on Ubuntu, RedHat, Centos & Debian distrubutions please let us know if you use this utility on other Distros"
fi
;;
*)
warn "Only MacOS and Linux systems are supported by this installer."
warn "For Windows, please follow manual installation instructions at:"
warn "${GIT_URL}"
error "Unsupported platform: ${PLATFORM}"
;;
esac
case "$ACTION" in
"") install;;
"install") install;;
"uninstall") uninstall;;
*) help;;
esac
(( SECS = SECONDS - SECS ))
log "--==[ ${GRN}Total time: ${SECS} seconds${NRM} ]==--"
}
#------------------------------------------------------------------------------
#-- Kick things off
#------------------------------------------------------------------------------
main "$@"