-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·177 lines (153 loc) · 4.83 KB
/
install.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
#! /usr/bin/env bash
set -u
# Check if script is run non-interactively (e.g. CI)
# If it is run non-interactively we should not prompt for passwords.
if [[ ! -t 0 || -n ${CI-} ]]; then
NONINTERACTIVE=1
fi
TEMP_LOCAL="/tmp/opta_local"
abort() {
printf "%s\n" "$1"
exit 1
}
# Compares two version numbers.
# Returns 0 if the versions are equal, 1 if the first version is higher, and 2 if the second version is higher.
# Source Code: https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
# You can run the test cases by using the below command:
# /bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/nsarupr/28a5af20ef5462cdd1f4c95739203246/raw/2cc50b6bd2e973bee8c6ef6269f731a78862bb37/compare_version.sh)"
compare_version() {
if [[ $1 == "$2" ]]; then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i = ${#ver1[@]}; i < ${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i = 0; i < ${#ver1[@]}; i++)); do
if [[ -z ${ver2[i]} ]]; then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]})); then
echo 1
return
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
echo 2
return
fi
done
echo 0
return
}
trim_version() {
VERSION="$1"
firstChar=${VERSION:0:1}
if [[ ${firstChar} == "v" || ${firstChar} == "V" ]]; then
VERSION=${VERSION:1}
fi
echo "${VERSION}"
}
check_prerequisites() {
echo "Checking Prerequisites..."
# declare -a prereq # Throws "unbound variable" error on Ubuntu 20.04 LTS Focal Fossa on Line #38
hard_prereq=()
soft_prereq=()
if ! unzip_loc="$(type -p unzip)" || [[ -z $unzip_loc ]]; then
hard_prereq+=(unzip)
fi
if ! curl_loc="$(type -p curl)" || [[ -z $curl_loc ]]; then
hard_prereq+=(curl)
fi
if ! terraform_loc="$(type -p terraform)" || [[ -z $terraform_loc ]]; then
soft_prereq+=(terraform)
fi
if ! docker_loc="$(type -p docker)" || [[ -z $docker_loc ]]; then
soft_prereq+=(docker)
fi
if [[ ${#hard_prereq[@]} -gt 0 ]]; then
abort "Please install the following prerequisites: (${hard_prereq[*]})"
fi
if [[ ${#soft_prereq[@]} -gt 0 ]]; then
echo "Opta would require (${soft_prereq[*]}) to run properly. Please install these."
fi
}
# Check OS
OS="$(uname)"
echo "Welcome to the opta installer."
check_prerequisites
# Set VERSION
VERSION="${VERSION:-}"
if [[ -z ${VERSION} ]]; then
# Determine latest VERSION
echo "Determining latest version"
VERSION="$(curl -s https://api.github.com/repos/run-x/opta/releases/latest | grep 'tag_name' | grep -oP '[0-9.]+')"
else
VERSION=$(trim_version "${VERSION}")
fi
echo "Going to install opta v${VERSION}"
if [[ ${OS} == "Linux" ]]; then
SPECIFIC_OS_ID=$(grep "ID=" /etc/os-release | awk -F"=" '{print $2;exit}' | tr -d '"')
if [[ ${SPECIFIC_OS_ID} == "amzn" ]] || [[ ${SPECIFIC_OS_ID} == "centos" ]]; then
PACKAGE=https://github.com/run-x/opta/releases/download/v$VERSION/opta_centos.zip
else
PACKAGE=https://github.com/run-x/opta/releases/download/v$VERSION/opta_linux.zip
fi
elif [[ ${OS} == "Darwin" ]]; then
PACKAGE=https://github.com/run-x/opta/releases/download/v$VERSION/opta_mac.zip
else
abort "Opta is only supported on macOS and Linux."
fi
echo "Downloading installation package..."
curl -s -L "${PACKAGE}" -o /tmp/opta.zip --fail
if [[ $? != 0 ]]; then
echo "Version ${VERSION} not found."
echo "Please check the available versions at https://github.com/run-x/opta/releases."
exit 1
fi
echo "Downloaded"
rm -rf $TEMP_LOCAL
mkdir $TEMP_LOCAL
if [[ -d ~/.opta ]]; then
if [[ -n ${NONINTERACTIVE-} ]]; then
echo "Opta already installed. Overwriting..."
if [[ -d ~/.opta/local ]]; then
mv ~/.opta/local $TEMP_LOCAL
fi
rm -rf ~/.opta
else
read -p "Opta already installed. Overwrite? " -n 1 -r
echo
if [[ ${REPLY} =~ ^[Yy]$ ]]; then
if [[ -d ~/.opta/local ]]; then
mv ~/.opta/local $TEMP_LOCAL
fi
rm -rf ~/.opta
else
rm -rf /tmp/opta.zip
exit 0
fi
fi
fi
echo "Sleeping..."
sleep 15
echo "Installing..."
unzip -q /tmp/opta.zip -d ~/.opta
rm -rf /tmp/opta.zip
if [[ -d $TEMP_LOCAL/local ]]; then
mv $TEMP_LOCAL/local ~/.opta/
fi
rm -rf $TEMP_LOCAL
chmod u+x ~/.opta/opta
RUNPATH=~/.opta
# Add symlink if possible, or tell the user to use sudo for symlinking
if ln -fs ~/.opta/opta /usr/local/bin/opta 2>/dev/null; then
echo "Opta symlinked to /usr/local/bin/opta; You can now type 'opta' in the terminal to run it."
else
echo "Please symlink the opta binary to one of your path directories; for example using 'sudo ln -fs $RUNPATH/opta /usr/local/bin/opta'"
echo "Alternatively, you could add the .opta installation directory to your path like so"
echo "export PATH=\$PATH:${RUNPATH}"
echo "to your terminal profile."
fi