Add golang installation
[bottlenecks.git] / utils / kube_setup / install_golang.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2018 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 usage="Script to install and config golang of specific version.
10
11 usage:
12     bash $(basename "$0") [-h|--help] [-v|--version <version>] [--debug]
13
14 where:
15     -h|--help         show the help text
16     -v|--version      input the version of golang
17     --debug           debug option switch
18 examples:
19     $(basename "$0") -v 1.10.3"
20
21 # Debug option
22 redirect="/dev/null"
23
24 # Process input variables
25 while [[ $# > 0 ]]
26     do
27     key="$1"
28     case $key in
29         -h|--help)
30             echo "$usage"
31             exit 0
32             shift
33         ;;
34         -v|--version)
35             GOLANG_VERSION="$2"
36             shift
37         ;;
38         --debug)
39             redirect="/dev/stdout"
40             shift
41         ;;
42         *)
43             echo "unkown option $1 $2"
44             exit 1
45         ;;
46     esac
47     shift
48 done
49
50 #set -e
51
52 echo "=======Downloading golang of version: ${GOLANG_VERSION}========"
53
54 if [[ -f go${GOLANG_VERSION}.linux-amd64.tar.gz ]]; then
55     rm go${GOLANG_VERSION}.linux-amd64.tar.gz
56 fi
57 curl -O https://storage.googleapis.com/golang/go${GOLANG_VERSION}.linux-amd64.tar.gz >${redirect}
58
59 echo "Installing golang of version: ${GOLANG_VERSION}"
60 if [[ -d /usr/local/go ]]; then
61     rm -rf /usr/local/go
62 fi
63
64 tar -C /usr/local -xzf go${GOLANG_VERSION}.linux-amd64.tar.gz >${redirect}
65
66 if [[ -d $HOME/go ]]; then
67     rm -rf ${HOME}/go
68     mkdir ${HOME}/go
69     mkdir ${HOME}/go/bin
70     mkdir ${HOME}/go/src
71 else
72     mkdir ${HOME}/go
73     mkdir ${HOME}/go/bin
74     mkdir ${HOME}/go/src
75 fi
76
77 echo "Adding golang env to ~/.bashrc"
78 GOROOT=/usr/local/go
79 GOPATH=${HOME}/go
80
81 if [[ $(cat ${HOME}/.bashrc | grep GOROOT) ]]; then
82     echo "golang env alreay in ${HOME}/.bashrc"
83 else
84    cat <<EOF >> ${HOME}/.bashrc
85
86 export GOROOT=/usr/local/go
87 export GOPATH=${HOME}/go
88 export PATH=${PATH}:${GOROOT}/bin:${GOPATH}/bin
89 EOF
90 fi
91
92 export GOROOT=/usr/local/go
93 export GOPATH=${HOME}/go
94 export PATH=${PATH}:${GOROOT}/bin:${GOPATH}/bin
95
96 echo "Running go version command:"
97 go version
98
99 echo "=======Installation of golang-${GOLANG_VERSION} complete======="
100