-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_redis_cli.sh
More file actions
148 lines (120 loc) · 4.77 KB
/
install_redis_cli.sh
File metadata and controls
148 lines (120 loc) · 4.77 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
#!/bin/bash
# Create a temporary directory and store its name in a variable.
TEMPD=$(mktemp -d)
# Exit if the temp directory wasn't created successfully.
if [ ! -e "$TEMPD" ]; then
>&2 read -p "Failed to create temp directory. Press any button to exit."
exit 1
fi
# Make sure the temp directory gets removed on script exit.
trap "exit 1" HUP INT PIPE QUIT TERM
trap 'rm -rf "$TEMPD"' EXIT
# These three variables must match the redis tarball file name
REDIS_DOWNLOAD_URL="https://download.redis.io/redis-stable.tar.gz"
REDIS_FILE_NAME="redis-stable.tar.gz"
REDIS_FOLDER_NAME="redis-stable"
# Path to built redis-cli executable (make)
REDIS_CLI_BUILT_PATH="src/redis-cli"
# Where the redis-cli executable will be copied to (must start with and not finish with /)
DESTINATION_FOLDER="/usr/local/bin"
DESTINATION_FILE="$DESTINATION_FOLDER/redis-cli"
printBanner() {
clear
echo "=================================================="
echo "============ Auto install redis-cli ============"
echo "=================================================="
echo ""
}
download() {
url="$1"
filename="$2"
if [ -x "$(which curl)" ]; then
curl -sL "$url" -o "$filename"
elif [ -x "$(which wget)" ]; then
wget -q "$url" -O "$filename"
else
read -p "Could not find curl or wget, please install one of them. Press any button to exit."
exit 1
fi
}
userInput='\0'
readKeys() {
validKeys=("$@")
while true; do
read -s -n 1 userInput <&1
for validKey in "${validKeys[@]}"; do
if [ "${validKey,,}" = "${userInput,,}" ]; then
userInput="${validKey,,}"
return 0
fi
done
done
}
# Access the temporary directory
cd "$TEMPD" || exit 1
step=0
# Dummy command to get sudo permissions
sudo printf ""
printBanner
echo "$((++step)). Downloading and installing necessary dependencies..."
sudo apt-get update >/dev/null && \
sudo apt-get install gcc make openssl libssl-dev -y >/dev/null
echo "Finished installing dependencies."
echo "$((++step)). Downloading and extracting redis source files..."
download "$REDIS_DOWNLOAD_URL" "$REDIS_FILE_NAME" && \
tar xzf "$REDIS_FILE_NAME"
# Exit if the redis compressed file wasn't extracted successfully
if [ ! -e "$REDIS_FOLDER_NAME" ]; then
>&2 read -p "Failed to extract redis-cli tarball! Press any button to exit."
exit 1
fi
echo "Finished download and extraction of redis source files."
# Access the extracted redis files
cd "$REDIS_FOLDER_NAME" || exit 1
echo "$((++step)). Building redis-cli from source files, sit back and relax, this operation can take a while..."
start_time=$(date +%s.%2N)
# Build the actual redis files, which will also build our desired 'redis-cli'
make BUILD_TLS=yes &>/dev/null
end_time=$(date +%s.%2N)
# Exit if the redis wasn't built successfully
if [ ! -e "$REDIS_CLI_BUILT_PATH" ]; then
echo ""
>&2 read -p "Failed to build redis-cli! Press any button to exit."
exit 1
fi
echo "Build time of redis-cli: $(echo "scale=2; $end_time - $start_time" | bc)s"
new_redis_version=$("$REDIS_CLI_BUILT_PATH" --version | grep -E -o "[0-9\.]+")
# Verify if the redis-cli already exists, and if so, ask the user if he really wants to get rid of the old file
if [ -f "$DESTINATION_FILE" ]; then
current_redis_version=$("$DESTINATION_FILE" --version | grep -E -o "[0-9\.]+")
printBanner && \
printf "WARNING: You already have a redis-cli in your %s folder.\nCurrent redis-cli version: %s. New redis-cli version: %s.\nDo you want to replace it? (y/n) " "$DESTINATION_FILE" "$current_redis_version" "$new_redis_version"
readKeys "y" "n"
if [ "$userInput" != "y" ]; then
printBanner && echo "Fair enough, bye..."
sleep 3
clear && exit 1
else
echo ""
echo "$((++step)). Removing old redis-cli file..."
sudo rm "$DESTINATION_FILE"
if [ -f "$DESTINATION_FILE" ]; then
read -p "Failed to remove redis-cli file from '$DESTINATION_FOLDER' folder. Press any button to exit."
exit 1
fi
echo "Successfully removed old redis-cli file."
fi
fi
echo "$((++step)). Copying redis-cli file to $DESTINATION_FOLDER folder..."
# Copy the built 'redis-cli' to the binary folder so we can use it
sudo cp "$REDIS_CLI_BUILT_PATH" "$DESTINATION_FOLDER"
if [ ! -e "$DESTINATION_FILE" ]; then
read -p "Failed to copy redis-cli file to '$DESTINATION_FOLDER' folder. Press any button to exit."
exit 1
fi
echo "$((++step)). Successfully copied redis-cli file, setting its permissions to 755..."
# Set 'redis-cli' permissions so we can execute it as normal user
sudo chmod 755 "$DESTINATION_FILE"
echo ""
read -p "Finished installation of redis-cli version $new_redis_version, press any button to exit..."
exit 0