-
Notifications
You must be signed in to change notification settings - Fork 5
/
install-release.sh
157 lines (130 loc) · 3.97 KB
/
install-release.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
#!/usr/bin/env bash
# Inspired by:
# https://github.com/v2fly/fhs-install-v2ray
# The URL of the script is:
# https://raw.githubusercontent.com/samuelncui/yatm/main/install-release.sh
# Use following command to run:
# bash <(curl -L https://raw.githubusercontent.com/samuelncui/yatm/main/install-release.sh)
curl() {
$(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
}
identify_the_operating_system_and_architecture() {
if [[ "$(uname)" == 'Linux' ]]; then
case "$(uname -m)" in
'amd64' | 'x86_64')
MACHINE='amd64'
;;
*)
echo "error: The architecture is not supported."
exit 1
;;
esac
else
echo "error: This operating system is not supported."
exit 1
fi
}
## Demo function for processing parameters
judgment_parameters() {
while [[ "$#" -gt '0' ]]; do
case "$1" in
'--version')
VERSION="${2:?error: Please specify the correct version.}"
break
;;
'-h' | '--help')
HELP='1'
break
;;
*)
echo "$0: unknown option -- -"
exit 1
;;
esac
shift
done
}
get_current_version() {
CURRENT_VERSION=`cat /opt/yatm/VERSION`
}
get_version() {
if [[ -n "$VERSION" ]]; then
RELEASE_VERSION="v${VERSION#v}"
return 2
fi
# Determine the version number for YATM installed from a local file
if [[ -f '/opt/yatm/VERSION' ]]; then
get_current_version
fi
# Get YATM release version number
TMP_FILE="$(mktemp)"
if ! curl -x "${PROXY}" -sS -i -H "Accept: application/vnd.github.v3+json" -o "$TMP_FILE" 'https://api.github.com/repos/samuelncui/yatm/releases/latest'; then
"rm" "$TMP_FILE"
echo 'error: Failed to get release list, please check your network.'
exit 1
fi
HTTP_STATUS_CODE=$(awk 'NR==1 {print $2}' "$TMP_FILE")
if [[ $HTTP_STATUS_CODE -lt 200 ]] || [[ $HTTP_STATUS_CODE -gt 299 ]]; then
"rm" "$TMP_FILE"
echo "error: Failed to get release list, GitHub API response code: $HTTP_STATUS_CODE"
exit 1
fi
RELEASE_LATEST="$(sed 'y/,/\n/' "$TMP_FILE" | grep 'tag_name' | awk -F '"' '{print $4}')"
"rm" "$TMP_FILE"
RELEASE_VERSION="v${RELEASE_LATEST#v}"
# Compare YATM version numbers
if [[ "$RELEASE_VERSION" != "$CURRENT_VERSION" ]]; then
return 0
fi
return 1
}
download_yatm() {
DOWNLOAD_LINK="https://github.com/samuelncui/yatm/releases/download/$RELEASE_VERSION/yatm-linux-$MACHINE-$RELEASE_VERSION.tar.gz"
echo "Downloading YATM archive: $DOWNLOAD_LINK"
if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "$GZIP_FILE" "$DOWNLOAD_LINK"; then
echo 'error: Download failed! Please check your network or try again.'
return 1
fi
}
# Explanation of parameters in the script
show_help() {
echo "usage:"
echo ' --version Install the specified version of YATM, e.g., --version v0.1.0'
echo ' -h, --help Show help'
exit 0
}
main() {
identify_the_operating_system_and_architecture
judgment_parameters "$@"
# Parameter information
[[ "$HELP" -eq '1' ]] && show_help
# Two very important variables
TMP_DIRECTORY="$(mktemp -d)"
get_version
NUMBER="$?"
if [[ "$NUMBER" -eq '1' ]]; then
echo "info: No new version. The current version of YATM is $CURRENT_VERSION."
exit 0
fi
echo "info: Installing YATM $RELEASE_VERSION for $(uname -m)"
GZIP_FILE="${TMP_DIRECTORY}/yatm-linux-$MACHINE-$RELEASE_VERSION.tar.gz"
download_yatm
if [[ "$?" -eq '1' ]]; then
"rm" -r "$TMP_DIRECTORY"
echo "removed: $TMP_DIRECTORY"
exit 1
fi
mkdir -p /opt/yatm
tar -xvzf ${GZIP_FILE} -C /opt/yatm
if [[ ! -f '/opt/yatm/config.yaml' ]]; then
cp /opt/yatm/config.example.yaml /opt/yatm/config.yaml;
vim /opt/yatm/config.yaml;
echo "Copy example config to /opt/yatm/config.yaml, you may edit it later";
fi
systemctl daemon-reload
systemctl enable /opt/yatm/yatm-httpd.service
systemctl stop yatm-httpd.service
systemctl start yatm-httpd.service
systemctl status yatm-httpd.service
}
main "$@"