Skip to content

Commit a1db78c

Browse files
authored
feat: Add update function to get-higress.sh (#37)
1 parent c6fafa9 commit a1db78c

3 files changed

Lines changed: 165 additions & 10 deletions

File tree

bin/update.sh

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright (c) 2023 Alibaba Group Holding Ltd.
4+
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
9+
# http:www.apache.org/licenses/LICENSE-2.0
10+
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
cd "$(dirname -- "$0")"
18+
ROOT=$(dirname -- "$(pwd -P)")
19+
COMPOSE_ROOT="$ROOT/compose"
20+
cd - >/dev/null
21+
22+
source "$ROOT/bin/base.sh"
23+
24+
CURRENT_VERSION="0.0.0"
25+
if [ -f "$ROOT/VERSION" ]; then
26+
CURRENT_VERSION="$(cat "$ROOT/VERSION")"
27+
fi
28+
29+
initArch() {
30+
ARCH=$(uname -m)
31+
case $ARCH in
32+
armv5*) ARCH="armv5";;
33+
armv6*) ARCH="armv6";;
34+
armv7*) ARCH="arm";;
35+
aarch64) ARCH="arm64";;
36+
x86) ARCH="386";;
37+
x86_64) ARCH="amd64";;
38+
i686) ARCH="386";;
39+
i386) ARCH="386";;
40+
esac
41+
}
42+
43+
initOS() {
44+
OS="$(uname|tr '[:upper:]' '[:lower:]')"
45+
case "$OS" in
46+
# Minimalist GNU for Windows
47+
mingw*|cygwin*) OS='windows';;
48+
esac
49+
}
50+
51+
parseArgs() {
52+
POSITIONAL_ARGS=()
53+
54+
while [[ $# -gt 0 ]]; do
55+
case $1 in
56+
*)
57+
POSITIONAL_ARGS+=("$1") # save positional arg
58+
shift
59+
;;
60+
esac
61+
done
62+
63+
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
64+
}
65+
66+
outputWelcomeMessage() {
67+
echo '
68+
___ ___ ___ ________ ________ _______ ________ ________
69+
|\ \|\ \|\ \|\ ____\|\ __ \|\ ___ \ |\ ____\ |\ ____\
70+
\ \ \\\ \ \ \ \ \___|\ \ \|\ \ \ __/|\ \ \___|_\ \ \___|_
71+
\ \ __ \ \ \ \ \ __\ \ _ _\ \ \_|/_\ \_____ \\ \_____ \
72+
\ \ \ \ \ \ \ \ \|\ \ \ \\ \\ \ \_|\ \|____|\ \\|____|\ \
73+
\ \__\ \__\ \__\ \_______\ \__\\ _\\ \_______\____\_\ \ ____\_\ \
74+
\|__|\|__|\|__|\|_______|\|__|\|__|\|_______|\_________\\_________\
75+
\|_________\|_________|
76+
'
77+
echo "Higress is updated successfully."
78+
echo ""
79+
echo "If Higress is running during update, you will need to restart it to use the new version."
80+
echo ""
81+
echo "Happy Higressing!"
82+
}
83+
84+
update() {
85+
echo "Updating..."
86+
87+
updateImageTags
88+
}
89+
90+
updateImageTags() {
91+
if [ ! -f "$COMPOSE_ROOT/.env_new" ]; then
92+
return
93+
fi
94+
sed -i -e "/.\+_TAG=.*/d" "$COMPOSE_ROOT/.env"
95+
while read -r line; do
96+
if [[ "$line" != *"_TAG="* ]]; then
97+
continue
98+
fi
99+
echo "$line" >> "$COMPOSE_ROOT/.env"
100+
done < "$COMPOSE_ROOT/.env_new"
101+
rm -f "$COMPOSE_ROOT/.env_new"
102+
}
103+
104+
initArch
105+
initOS
106+
update
107+
outputWelcomeMessage

compose/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ NACOS_PASSWORD=''
88
NACOS_DATA_ENC_KEY='0123456789abcdef0123456789abcdef'
99
NACOS_SERVER_TAG='v2.2.3'
1010
HIGRESS_RUNNER_TAG='0.0.3'
11-
HIGRESS_API_SERVER_TAG='0.0.9'
11+
HIGRESS_API_SERVER_TAG='0.0.10'
1212
HIGRESS_CONTROLLER_TAG='1.1.1'
1313
HIGRESS_PILOT_TAG='1.1.1'
1414
HIGRESS_GATEWAY_TAG='1.1.1'

src/get-higress.sh

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
export VERSION
1818

19+
MODE="install"
20+
1921
HAS_CURL="$(type "curl" &> /dev/null && echo true || echo false)"
2022
HAS_WGET="$(type "wget" &> /dev/null && echo true || echo false)"
2123
HAS_DOCKER="$(type "docker" &> /dev/null && echo true || echo false)"
@@ -24,6 +26,7 @@ parseArgs() {
2426
CONFIG_ARGS=()
2527

2628
DESTINATION=""
29+
MODE="install"
2730

2831
if [[ $1 != "-"* ]]; then
2932
DESTINATION="$1"
@@ -36,6 +39,10 @@ parseArgs() {
3639
outputUsage
3740
exit 0
3841
;;
42+
-u|--update)
43+
MODE="update"
44+
shift
45+
;;
3946
*)
4047
CONFIG_ARGS+=("$1")
4148
shift
@@ -48,15 +55,19 @@ parseArgs() {
4855

4956
validateArgs() {
5057
if [ -d "$DESTINATION" ]; then
51-
[ "$(ls -A "$DESTINATION")" ] && echo "The target folder \"$DESTINATION\" is not empty." && exit 1
58+
if [ "$(ls -A "$DESTINATION")" -a "$MODE" != "update" ]; then
59+
echo "The target folder \"$DESTINATION\" is not empty. Add \"-u\" to update an existed Higress instance." && exit 1
60+
fi
5261
if [ ! -w "$DESTINATION" ]; then
53-
echo "The target folder \"$DESTINATION\" is not writeable."
54-
exit 1
62+
echo "The target folder \"$DESTINATION\" is not writeable." && exit 1
5563
fi
5664
else
65+
if [ "$MODE" == "update" ]; then
66+
echo "The target folder \"$DESTINATION\" for update doesn't exist." && exit 1
67+
fi
5768
mkdir -p "$DESTINATION"
5869
if [ $? -ne 0 ]; then
59-
exit -1
70+
exit 1
6071
fi
6172
fi
6273

@@ -67,7 +78,7 @@ validateArgs() {
6778

6879
outputUsage() {
6980
echo "Usage: $(basename -- "$0") [DIR] [OPTIONS...]"
70-
echo 'Install Higress (standalone version) into the DIR (the current directory by default).'
81+
echo 'Install Higress (standalone version) into the DIR ("./higress" by default).'
7182
echo '
7283
-c, --config-url=URL URL of the config storage
7384
Use Nacos with format: nacos://192.168.0.1:8848
@@ -104,6 +115,8 @@ outputUsage() {
104115
--console-port=CONSOLE-PORT
105116
the port used to visit Higress Console
106117
default to 8080 if unspecified
118+
-u, --update update an existed Higress instance.
119+
no user configuration will be changed during update.
107120
-h, --help give this help list'
108121
}
109122

@@ -191,17 +204,45 @@ download() {
191204
# install installs the product.
192205
install() {
193206
tar -zx --exclude="docs" --exclude="src" --exclude="test" -f "$HIGRESS_TMP_FILE" -C "$DESTINATION" --strip-components=1
207+
echo -n "$VERSION" > "$DESTINATION/VERSION"
194208
bash "$DESTINATION/bin/configure.sh" --auto-start ${CONFIG_ARGS[@]}
195209
}
196210

211+
# update updates the product.
212+
update() {
213+
CURRENT_VERSION="0.0.0"
214+
if [ -f "$DESTINATION/VERSION" ]; then
215+
CURRENT_VERSION="$(cat "$DESTINATION/VERSION")"
216+
fi
217+
if [ "$CURRENT_VERSION" == "$VERSION" ]; then
218+
echo "Higress is already up-to-date."
219+
exit 0
220+
fi
221+
222+
BACKUP_FOLDER="$(cd ${DESTINATION}/.. ; pwd)"
223+
BACKUP_FILE="${BACKUP_FOLDER}/higress_backup_$(date '+%Y%m%d%H%M%S').tar.gz"
224+
tar -zc -f "$BACKUP_FILE" -C "$DESTINATION" .
225+
echo "The current version is packed here: $BACKUP_FILE"
226+
echo ""
227+
228+
download
229+
echo ""
230+
231+
tar -zx --exclude="docs" --exclude="src" --exclude="test" --exclude="compose/.env" -f "$HIGRESS_TMP_FILE" -C "$DESTINATION" --strip-components=1
232+
tar -zx -f "$HIGRESS_TMP_FILE" -C "$DESTINATION" --transform='s/env/env_new/g' --strip-components=1 "higress-standalone-${VERSION#v}/compose/.env"
233+
bash "$DESTINATION/bin/update.sh"
234+
echo -n "$VERSION" > "$DESTINATION/VERSION"
235+
return
236+
}
237+
197238
# fail_trap is executed if an error occurs.
198239
fail_trap() {
199240
result=$?
200241
if [ "$result" != "0" ]; then
201242
if [ -n "$INPUT_ARGUMENTS" ]; then
202-
echo "Failed to install Higress with the arguments provided: $INPUT_ARGUMENTS"
243+
echo "Failed to ${MODE} Higress with the arguments provided: $INPUT_ARGUMENTS"
203244
else
204-
echo "Failed to install Higress"
245+
echo "Failed to ${MODE} Higress"
205246
fi
206247
echo -e "\tFor support, go to https://github.com/alibaba/higress."
207248
fi
@@ -227,6 +268,13 @@ initOS
227268
verifySupported
228269

229270
checkDesiredVersion
230-
download
231-
install
271+
case "$MODE" in
272+
update)
273+
update
274+
;;
275+
*)
276+
download
277+
install
278+
;;
279+
esac
232280
cleanup

0 commit comments

Comments
 (0)