forked from MIT-CAVE/rc-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·249 lines (227 loc) · 8.52 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
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
#!/bin/bash
#
# Install the RC CLI on Unix Systems
# Constants
readonly CHARS_LINE="============================"
readonly RC_CLI_PATH="${HOME}/.rc-cli"
readonly RC_CLI_SHORT_NAME="RC CLI"
readonly RC_CLI_COMMAND="rc-cli"
readonly RC_CLI_VERSION="0.1.5"
readonly BIN_DIR="/usr/local/bin"
readonly DATA_DIR="data"
readonly SSH_CLONE_URL="git@github.com:MIT-CAVE/rc-cli.git"
readonly HTTPS_CLONE_URL="https://github.com/MIT-CAVE/rc-cli.git"
readonly MIN_DOCKER_VERSION="18.09.00"
readonly MIN_TAR_VERSION="1.22"
readonly MIN_BSDTAR_VERSION="0" # FIXME: Minimum version for which 'xz' compression is supported
err() { # Display an error message
printf "$0: $1\n" >&2
}
# Get the current version of the 'tar' archiving utility
get_tar_version() {
if [[ -n $(which bsdtar) ]]; then
printf "$(bsdtar --version | sed 's/\([a-z]\+\s\)\(.*\)\-.*/\2/g')"
elif [[ -n $(which tar) ]]; then
printf "$(tar --version | grep -m1 -o ").*" | sed "s/) //")"
fi
}
check_os() { # Validate that the current OS
case "$(uname -s)" in
Linux*) machine="Linux";;
Darwin*) machine="Mac";;
*) machine="UNKNOWN"
esac
if [ $machine = "UNKNOWN" ]; then
printf "Error: Unknown operating system.\n"
printf "Please run this command on one of the following:\n"
printf "- MacOS\n- Linux\n- Windows (Using Ubuntu 20.04 on Windows Subsystem for Linux 2 - WSL2)"
exit 1
fi
}
get_compressed_data_info() { # Get information on compressed data to download
DATA_URL="${1:-''}"
compressed_file_name=$(printf "$(basename $DATA_URL)" | sed 's/?.*//')
compressed_file_path="${RC_CLI_PATH}/${compressed_file_name}"
compressed_file_type=$(printf ${compressed_file_path##*.} | sed 's/?dl=1//')
if [[ "$compressed_file_type" = "xz" ]]; then
compressed_file_name_no_ext==${compressed_file_name%.*.*}
compressed_folder_name=${compressed_file_name%.*.*}
else
compressed_file_name_no_ext==${compressed_file_name%.*}
compressed_folder_name=${compressed_file_name%.*}
fi
}
validate_install() {
local PROGRAM_NAME="$1"
local EXIT_BOOL="$2"
local ERROR_STRING="$3"
if [ "$($PROGRAM_NAME --version)" = "" ]; then
err "${PROGRAM_NAME} is not installed. ${ERROR_STRING}"
if [ "${EXIT_BOOL}" = "1" ]; then
exit 1
fi
fi
}
validate_version() {
local PROGRAM_NAME="$1"
local EXIT_BOOL="$2"
local ERROR_STRING="$3"
local MIN_VERSION="$4"
local CURRENT_VERSION="$5"
if [ ! "$(printf '%s\n' "$MIN_VERSION" "$CURRENT_VERSION" | sort -V | head -n1)" = "$MIN_VERSION" ]; then
err "Your current $PROGRAM_NAME version ($CURRENT_VERSION) is too old. ${ERROR_STRING}"
if [ "${EXIT_BOOL}" = "1" ]; then
exit 1
fi
fi
}
check_compression() { # Validate tar compression command is installed
if [[ "${compressed_file_type}" == "xz" ]]; then
[[ -n $(which bsdtar) ]] \
&& min_tar_ver=${MIN_TAR_VERSION} \
|| min_tar_ver=${MIN_BSDTAR_VERSION}
install_tar="\nPlease install version ${min_tar_ver} or greater. \nIf your machine does not support tar, you may consider installing {$RC_CLI_SHORT_NAME} using a zip folder. \nThis requires the unzip function to be installed locally.\n"
validate_install "tar" "1" "${install_tar}"
CURRENT_TAR_VERSION=$(get_tar_version)
validate_version "tar" "1" "${install_tar}" "${min_tar_ver}" "${CURRENT_TAR_VERSION}"
# Can not validate unzip as version pipes out to stderr
elif [[ "${compressed_file_type}" == "zip" ]]; then
: # Do nothing
# install_unzip="\nPlease install unzip."
# validate_install "unzip" "1" "$install_unzip"
else
err "The data file you are installing with is not recognized. \nPlease install the $RC_CLI_SHORT_NAME with a tar.xz or zip file."
exit 1
fi
}
check_docker() { # Validate docker is installed
install_docker="\nPlease install version ${MIN_DOCKER_VERSION} or greater. \nFor more information see: 'https://docs.docker.com/get-docker/'"
validate_install "docker" "1" "$install_docker"
CURRENT_DOCKER_VERSION=$(docker --version | sed -e 's/Docker version \(.*\), build.*/\1/')
validate_version "docker" "1" "$install_docker" "$MIN_DOCKER_VERSION" "$CURRENT_DOCKER_VERSION"
}
check_git() { # Validate git is installed
install_git="\nPlease install git. \nFor more information see: 'https://git-scm.com'"
validate_install "git" "1" "$install_git"
}
check_previous_installation() { # Check to make sure previous installations are removed before continuing
if [ -d "${RC_CLI_PATH}" ]; then
LOCAL_CLI_VERSION=$(<${RC_CLI_PATH}/VERSION)
printf "An existing installation of ${RC_CLI_SHORT_NAME} ($LOCAL_CLI_VERSION) was found \nLocation: ${RC_CLI_PATH}\n"
printf "You are installing ${RC_CLI_SHORT_NAME} ($RC_CLI_VERSION)\n"
if [ "$LOCAL_CLI_VERSION" = "$RC_CLI_VERSION" ] ; then
read -r -p "Would you like to reinstall ${RC_CLI_SHORT_NAME} ($RC_CLI_VERSION)? [y/N] " input
else
read -r -p "Would you like to update to ${RC_CLI_SHORT_NAME} ($RC_CLI_VERSION)? [y/N] " input
fi
case ${input} in
[yY][eE][sS] | [yY])
printf "Removing old installation... "
rm -rf "${RC_CLI_PATH}"
printf "done\n"
;;
[nN][oO] | [nN] | "")
err "Installation canceled"
exit 1
;;
*)
err "Invalid input: Installation canceled."
exit 1
;;
esac
fi
}
install_new() { # Copy the needed files locally
printf "Creating application folder at '${RC_CLI_PATH}'..."
mkdir -p "${RC_CLI_PATH}"
printf "done\n"
printf "${CHARS_LINE}\n"
if [[ $2 = "--dev" ]]; then
CLONE_URL="$SSH_CLONE_URL"
INSTALL_PARAM="--dev"
else
clone_opts="--depth=1"
CLONE_URL="$HTTPS_CLONE_URL"
INSTALL_PARAM=""
fi
git clone "${CLONE_URL}" \
${clone_opts} \
"${RC_CLI_PATH}" > /dev/null
if [ ! -d "${RC_CLI_PATH}" ]; then
err "Git Clone Failed. Installation Canceled"
exit 1
else
printf "INSTALL_PARAM=\"${INSTALL_PARAM}\"\n" > "${RC_CLI_PATH}/CONFIG"
fi
}
copy_compressed_data_down() { # Copy the needed data files locally
# Takes three optional parameters (order matters)
# EG:
# copy_compressed_data_down URL LOCAL_PATH NEW_DIR_NAME
new_dir_name="${3:-$compressed_folder_name}"
printf "Copying data down from $1...\n"
curl -L -o "${compressed_file_path}" "$1" --progress-bar
printf "done\n"
printf "Decompressing downloaded data...\n"
if [[ "${compressed_file_type}" = "xz" ]]; then
tar -xf "${compressed_file_path}" -C "$2"
elif [[ "${compressed_file_type}" = "zip" ]]; then
unzip -qq "${compressed_file_path}" -d "$2"
fi
rm "${compressed_file_path}"
if [[ ! "${2}/${compressed_folder_name}" = "${2}/${new_dir_name}" ]]; then
mv "${2}/${zip_folder_name}" "${2}/${new_dir_name}"
fi
if [ ! -d "${2}/${new_dir_name}" ]; then
err "Unable to access data from ${1}. Installation Canceled"
exit 1
fi
printf "done\n"
}
get_data() { # Copy the needed data files locally
copy_compressed_data_down "$DATA_URL" "${RC_CLI_PATH}" "$DATA_DIR"
printf "Setting data URL locally for future CLI Updates... "
printf "DATA_URL=\"${DATA_URL}\"\n" >> "${RC_CLI_PATH}/CONFIG"
printf "done\n"
}
check_args() {
local cmd_ex="
bash <(curl -s https://raw.githubusercontent.com/MIT-CAVE/rc-cli/main/install.sh) \\
<data-url-here>"
if [[ $# -lt 1 ]]; then
err "Not enough arguments to install the CLI with data. Please specify a DATA_URL \nEG:${cmd_ex}"
exit 1
elif [[ $# -gt 1 && $2 != "--dev" ]]; then
err "Too many arguments for CLI installation. Please only specify a DATA_URL\nEG:${cmd_ex}"
exit 1
fi
}
add_to_path() { # Add the cli to a globally accessable path
printf "${CHARS_LINE}\n"
printf "Making '${RC_CLI_COMMAND}' globally accessable: \nCreating link from '${RC_CLI_PATH}/${RC_CLI_COMMAND}.sh' as '${BIN_DIR}/${RC_CLI_COMMAND}':\n"
if [ ! $(ln -sf "${RC_CLI_PATH}/${RC_CLI_COMMAND}.sh" "${BIN_DIR}/${RC_CLI_COMMAND}") ]; then
printf "WARNING!: Super User priviledges required to complete link! Using 'sudo'.\n"
sudo ln -sf "${RC_CLI_PATH}/${RC_CLI_COMMAND}.sh" "${BIN_DIR}/${RC_CLI_COMMAND}"
fi
printf "done\n"
}
success_message() { # Send a success message to the user on successful installation
printf "${CHARS_LINE}\n"
printf "${RC_CLI_SHORT_NAME} (${RC_CLI_COMMAND}) has been successfully installed \n"
printf "You can verify the installation with 'rc-cli version'\n"
printf "To get started use 'rc-cli help'\n"
}
main() {
check_args "$@"
check_os
get_compressed_data_info "$@"
check_compression
check_docker
check_git
check_previous_installation
install_new "$@"
get_data
add_to_path
success_message
}
main "$@"