-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathubuntu-provision.sh
277 lines (237 loc) · 8.19 KB
/
ubuntu-provision.sh
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
#!/bin/bash
#
# File: ubuntu-provision.sh
# Description: Installs jazz prerequisites on a Ubuntu 18.04.2 LTS ec2-instance.
# if you already have the prerequisites installed, just run Installer.py
# directly from the from the cloned jazz-installer folder.
# Variables section
# Disable shellcheck warnings about sudo and output redirection, doesn't apply here
# shellcheck disable=SC2024
#TODO consider using pyenv
TERRAFORM_URL="https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip"
ATLASSIAN_CLI_URL="https://bobswift.atlassian.net/wiki/download/attachments/16285777/atlassian-cli-6.7.1-distribution.zip"
INSTALLER_GITHUB_URL="https://github.com/tmobile/jazz-installer.git"
# Installation directory
INSTALL_DIR=$(pwd)
#Since Ubuntu 18.04.2 use default Python3
PYEXE='python3'
# Log file to record the installation logs
LOG_FILE_NAME=provision_setup.out
LOG_FILE=$(realpath "$INSTALL_DIR"/"$LOG_FILE_NAME")
JAZZ_INSTALLER_BRANCH="master"
# Default verbosity of the installation
VERBOSE=0
NC='\033[0m'
GREEN='\033[0;32m'
RED='\033[0;31m'
print_info()
{
# shellcheck disable=SC2059
printf "\\r${GREEN}$1${NC}\\n" 1>&3 2>&4
}
print_error()
{
# shellcheck disable=SC2059
printf "\\r${RED}$1${NC}\\n" 1>&3 2>&4
}
#Spin wheel
function spin_wheel () {
pid=$1 # Process Id of the previous running command
message=$2
spin='-\|/'
printf "\\r%s...." "$message" 1>&3 2>&4
i=0
while ps -p "$pid" > /dev/null
do
i=$(( (i+1) %4 ))
# shellcheck disable=SC2059
printf "\\r${GREEN}$message....${spin:$i:1}" 1>&3 2>&4
sleep .05
done
while s=$(ps -p "$pid" -o s=) && [[ "$s" && "$s" != 'Z' ]]; do
sleep 1
done
exitcode=$?
if [ $exitcode -gt 0 ]
then
print_error "$message....Failed"
exit
else
print_info "$message....Completed"
fi
}
trap 'printf "${RED}\nCancelled....\n${NC}" 1>&3 2>&4; exit' 2
trap '' SIGTSTP
function install_packages () {
# Download and Installing Softwares required for Jazz ubuntu-provision
# Java JDK
# Unzip
# Terraform
# Atlassian CLI
# Python36
# Python requirements from requirements.txt
#Fork output redirection so we can control output if VERBOSE is set
exec 3>&1
exec 4>&2
# print verbosity mode during installation
if [ "$1" == 1 ]; then
print_info "You have started the installer in verbose mode" 1>&3 2>&4
elif [ "$1" == 0 ]; then
# Redirecting the stdout and stderr to /dev/null for non-verbose installation
exec 1>/dev/null
exec 2>/dev/null
print_info "You have started the installer in non-verbose mode" 1>&3 2>&4
fi
print_info "You may view the detailed installation logs at $LOG_FILE" 1>&3 2>&4
# Update apt repo.
sudo apt update
alias python='echo $PYEXE'
spin_wheel $! "Update apt repo"
# Install git
if command -v git > /dev/null; then
print_info "Git already installed, using it"
else
sudo apt install -y git >> "$LOG_FILE" &
spin_wheel $! "Installing git"
fi
# Download and Install java
if command -v java > /dev/null; then
print_info "Java already installed, using it"
else
sudo apt install -y openjdk-8-jdk >> "$LOG_FILE" &
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/
spin_wheel $! "Installing Java 1.8"
fi
#TODO is this required? it shouldn't be
# Download and Install jq
if command -v jq > /dev/null; then
print_info "JQ already installed, using it"
else
sudo apt install -y jq >> "$LOG_FILE" &
spin_wheel $! "Installing jq"
fi
# Download and Install unzip
if command -v unzip > /dev/null; then
print_info "Unzip already installed, using it"
else
sudo apt install -y unzip >> "$LOG_FILE" &
spin_wheel $! "Installing unzip"
fi
# Create a temporary folder .
# Here we will have all the temporary files needed and delete it at the end
sudo rm -rf "$INSTALL"_DIR/jazz_tmp
mkdir "$INSTALL_DIR"/jazz_tmp
#Download and Install Terraform
curl -v -L "$TERRAFORM_URL" -o "$INSTALL_DIR"/jazz_tmp/terraform.zip >> "$LOG_FILE" &
spin_wheel $! "Downloading terraform"
sudo unzip -o "$INSTALL_DIR"/jazz_tmp/terraform.zip -d /usr/bin >> "$LOG_FILE" &
spin_wheel $! "Installing terraform"
#Downloading and Install atlassian-cli
curl -L "$ATLASSIAN_CLI_URL" -o "$INSTALL_DIR"/jazz_tmp/atlassian-cli-6.7.1-distribution.zip >> "$LOG_FILE" &
spin_wheel $! "Downloading atlassian-cli"
unzip -o "$INSTALL_DIR"/jazz_tmp/atlassian-cli-6.7.1-distribution.zip -d "$INSTALL_DIR"/jazz_tmp/ >> "$LOG_FILE" &
spin_wheel $! "Fetching atlassian-cli"
# Installing python36+
if command -v $PYEXE > /dev/null; then
print_info "python already installed, using it"
else
sudo apt install -y python3.6 >> "$LOG_FILE" &
spin_wheel $! "Installing python36"
fi
#Download and install pip
if command -v $PYEXE -m pip > /dev/null; then
print_info "pip already installed, using it"
else
sudo apt install -y python3-pip >> "$LOG_FILE" &
spin_wheel $! "Installing pip"
alias pip=pip3
fi
pip3 install virtualenv
sudo apt-get install -y python3-venv >> "$LOG_FILE" &
spin_wheel $! "Installing virtualenv"
$PYEXE -m venv jazzenv >> "$LOG_FILE" &
spin_wheel $! "Creating virtualenv"
# shellcheck disable=SC1091
source jazzenv/bin/activate
#Get Jazz installer code base
if [ -z "$JAZZ_INSTALLER_BRANCH" ]; then
echo "Jazz branch to clone"
print_info "Skipping installer repo clone based on CLI flag"
else
sudo rm -rf jazz-installer
git clone -b "$JAZZ_INSTALLER_BRANCH" "$INSTALLER_GITHUB_URL" --depth 1 >> "$LOG_FILE" &
spin_wheel $! "Fetching jazz-installer repo"
fi
# If you're running this standalone, you'll need the subfolder
if [ -e jazz-installer/requirements.txt ]
then
pip install -r jazz-installer/requirements.txt >> "$LOG_FILE" &
spin_wheel $! "Installing pip dependencies"
else
pip install -r requirements.txt >> "$LOG_FILE" &
spin_wheel $! "Installing pip dependencies"
fi
#Undo output redirection and close unused file descriptors.
exec 1>&3 3>&-
exec 2>&4 4>&-
}
# Running the selector
while [ $# -gt 0 ] ; do
case "$1" in
-h|--help)
echo "Installs jazz prerequisites on a ubuntu ec2-instance."
echo ""
echo "./ubuntu-provision.sh [options]"
echo ""
echo "options:"
echo "-ib, --installer-branch [optional] ubuntu-provision repo branch to use. Defaults to 'master'"
echo "-nc, --no-clone [optional] Skip cloning the jazz-installer repo into the current directory. Default: repo is cloned."
echo "-v, --verbose 1|0 [optional] Enable/Disable verbose ubuntu-provision logs. Default:0(Disabled)"
echo "-t, --tags Key=stackName,Value=production [optional] Specify as space separated key/value pairs"
echo "-h, --help [optional] Describe help"
exit 0 ;;
-ib|--installer-branch)
shift
# shellcheck disable=SC2236
if [ ! -z "$1" ] ; then
JAZZ_INSTALLER_BRANCH="$1"
else
echo "No arguments supplied for installer branch name."
echo "Usage: ./ubuntu-provision.sh -ib installer_branch_name"
exit 1
fi
shift ;;
-nc|--no-clone)
shift
JAZZ_INSTALLER_BRANCH=''
shift ;;
-v|--verbose)
shift
if [[ ("$#" -gt 0) && ("$1" == "1") ]] || [[ ("$#" -gt 0) && ("$1" == "0") ]] ; then
VERBOSE="$1"
else
echo "Please specify 1 or 0 for verbosity. Enable/Disable verbose ubuntu-provision logs. Default:0(Disabled)"
echo "Usage: ./ubuntu-provision --verbose 1"
exit 1
fi
shift ;;
-t|--tags)
shift
while [ "$#" -gt 0 ] ; do
if [[ "$1" =~ Key=.*,Value=.* ]] && [[ ! "$1" =~ ";" ]] ; then
arr+=("$1")
elif [[ $1 == -* ]] ; then break
else
echo "Please specify tags in format: Key=stackName,Value=production"
echo "Usage: ./ubuntu-provision --tags Key=stackName,Value=production Key=department,Value=devops"
exit 1
fi
shift
done ;;
*)
echo "Invalid flag!"
echo "Please run './ubuntu-provision.sh -h' to see all the available options."
exit 1 ;;
esac
done
install_packages "$VERBOSE"