Merge pull request #2170 from xymopen/backport

refactor: Clean up install-release.sh
pull/2207/head
Kslr 2020-01-09 00:12:41 +08:00 committed by GitHub
commit 5dd85717fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 104 additions and 83 deletions

View File

@ -9,16 +9,25 @@
# 2: Application error # 2: Application error
# 3: Network error # 3: Network error
# CLI arguments
PROXY=''
HELP=''
FORCE=''
CHECK=''
REMOVE=''
VERSION=''
VSRC_ROOT='/tmp/v2ray'
EXTRACT_ONLY=''
LOCAL=''
LOCAL_INSTALL=''
DIST_SRC='github'
ERROR_IF_UPTODATE=''
CUR_VER="" CUR_VER=""
NEW_VER="" NEW_VER=""
ARCH="" VDIS=''
VDIS="64"
ZIPFILE="/tmp/v2ray/v2ray.zip" ZIPFILE="/tmp/v2ray/v2ray.zip"
V2RAY_RUNNING=0 V2RAY_RUNNING=0
VSRC_ROOT="/tmp/v2ray"
EXTRACT_ONLY=0
ERROR_IF_UPTODATE=0
DIST_SRC="github"
CMD_INSTALL="" CMD_INSTALL=""
CMD_UPDATE="" CMD_UPDATE=""
@ -27,10 +36,6 @@ SOFTWARE_UPDATED=0
SYSTEMCTL_CMD=$(command -v systemctl 2>/dev/null) SYSTEMCTL_CMD=$(command -v systemctl 2>/dev/null)
SERVICE_CMD=$(command -v service 2>/dev/null) SERVICE_CMD=$(command -v service 2>/dev/null)
CHECK=""
FORCE=""
HELP=""
#######color code######## #######color code########
RED="31m" # Error message RED="31m" # Error message
GREEN="32m" # Success message GREEN="32m" # Success message
@ -40,8 +45,7 @@ BLUE="36m" # Info message
######################### #########################
while [[ $# > 0 ]]; do while [[ $# > 0 ]]; do
key="$1" case "$1" in
case $key in
-p|--proxy) -p|--proxy)
PROXY="-x ${2}" PROXY="-x ${2}"
shift # past argument shift # past argument
@ -90,33 +94,49 @@ done
############################### ###############################
colorEcho(){ colorEcho(){
COLOR=$1 echo -e "\033[${1}${@:2}\033[0m" 1>& 2
echo -e "\033[${COLOR}${@:2}\033[0m"
} }
sysArch(){ archAffix(){
ARCH=$(uname -m) case "${1:-"$(uname -m)"}" in
if [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]; then i686|i386)
VDIS="32" echo '32'
elif [[ "$ARCH" == *"armv7"* ]] || [[ "$ARCH" == "armv6l" ]]; then ;;
VDIS="arm" x86_64|amd64)
elif [[ "$ARCH" == *"armv8"* ]] || [[ "$ARCH" == "aarch64" ]]; then echo '64'
VDIS="arm64" ;;
elif [[ "$ARCH" == *"mips64le"* ]]; then *armv7*|armv6l)
VDIS="mips64le" echo 'arm'
elif [[ "$ARCH" == *"mips64"* ]]; then ;;
VDIS="mips64" *armv8*|aarch64)
elif [[ "$ARCH" == *"mipsle"* ]]; then echo 'arm64'
VDIS="mipsle" ;;
elif [[ "$ARCH" == *"mips"* ]]; then *mips64le*)
VDIS="mips" echo 'mips64le'
elif [[ "$ARCH" == *"s390x"* ]]; then ;;
VDIS="s390x" *mips64*)
elif [[ "$ARCH" == "ppc64le" ]]; then echo 'mips64'
VDIS="ppc64le" ;;
elif [[ "$ARCH" == "ppc64" ]]; then *mipsle*)
VDIS="ppc64" echo 'mipsle'
fi ;;
*mips*)
echo 'mips'
;;
*s390x*)
echo 's390x'
;;
ppc64le)
echo 'ppc64le'
;;
ppc64)
echo 'ppc64'
;;
*)
return 1
;;
esac
return 0 return 0
} }
@ -194,27 +214,33 @@ extract(){
return 0 return 0
} }
normalizeVersion() {
if [ -n "$1" ]; then
case "$1" in
v*)
echo "$1"
;;
*)
echo "v$1"
;;
esac
else
echo ""
fi
}
# 1: new V2Ray. 0: no. 2: not installed. 3: check failed. 4: don't check. # 1: new V2Ray. 0: no. 2: not installed. 3: check failed. 4: don't check.
getVersion(){ getVersion(){
if [[ -n "$VERSION" ]]; then if [[ -n "$VERSION" ]]; then
NEW_VER="$VERSION" NEW_VER="$(normalizeVersion "$VERSION")"
if [[ ${NEW_VER} != v* ]]; then
NEW_VER=v${NEW_VER}
fi
return 4 return 4
else else
VER=`/usr/bin/v2ray/v2ray -version 2>/dev/null` VER="$(/usr/bin/v2ray/v2ray -version 2>/dev/null)"
RETVAL="$?" RETVAL=$?
CUR_VER=`echo $VER | head -n 1 | cut -d " " -f2` CUR_VER="$(normalizeVersion "$(echo "$VER" | head -n 1 | cut -d " " -f2)")"
if [[ ${CUR_VER} != v* ]]; then
CUR_VER=v${CUR_VER}
fi
TAG_URL="https://api.github.com/repos/v2ray/v2ray-core/releases/latest" TAG_URL="https://api.github.com/repos/v2ray/v2ray-core/releases/latest"
NEW_VER=`curl ${PROXY} -s ${TAG_URL} --connect-timeout 10| grep 'tag_name' | cut -d\" -f4` NEW_VER="$(normalizeVersion "$(curl ${PROXY} -s "${TAG_URL}" --connect-timeout 10| grep 'tag_name' | cut -d\" -f4)")"
if [[ ${NEW_VER} != v* ]]; then
NEW_VER=v${NEW_VER}
fi
if [[ $? -ne 0 ]] || [[ $NEW_VER == "" ]]; then if [[ $? -ne 0 ]] || [[ $NEW_VER == "" ]]; then
colorEcho ${RED} "Failed to fetch release information. Please check your network or try again." colorEcho ${RED} "Failed to fetch release information. Please check your network or try again."
return 3 return 3
@ -242,9 +268,7 @@ stopV2ray(){
} }
startV2ray(){ startV2ray(){
if [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/lib/systemd/system/v2ray.service" ]; then if [ -n "${SYSTEMCTL_CMD}" ] && [[ -f "/lib/systemd/system/v2ray.service" || -f "/etc/systemd/system/v2ray.service" ]]; then
${SYSTEMCTL_CMD} start v2ray
elif [ -n "${SYSTEMCTL_CMD}" ] && [ -f "/etc/systemd/system/v2ray.service" ]; then
${SYSTEMCTL_CMD} start v2ray ${SYSTEMCTL_CMD} start v2ray
elif [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then elif [ -n "${SERVICE_CMD}" ] && [ -f "/etc/init.d/v2ray" ]; then
${SERVICE_CMD} v2ray start ${SERVICE_CMD} v2ray start
@ -306,33 +330,28 @@ installV2Ray(){
installInitScript(){ installInitScript(){
if [[ -n "${SYSTEMCTL_CMD}" ]];then if [[ -n "${SYSTEMCTL_CMD}" ]] && [[ ! -f "/etc/systemd/system/v2ray.service" && ! -f "/lib/systemd/system/v2ray.service" ]]; then
if [[ ! -f "/etc/systemd/system/v2ray.service" ]]; then
if [[ ! -f "/lib/systemd/system/v2ray.service" ]]; then
cp "${VSRC_ROOT}/systemd/v2ray.service" "/etc/systemd/system/" cp "${VSRC_ROOT}/systemd/v2ray.service" "/etc/systemd/system/"
systemctl enable v2ray.service systemctl enable v2ray.service
fi
fi
return
elif [[ -n "${SERVICE_CMD}" ]] && [[ ! -f "/etc/init.d/v2ray" ]]; then elif [[ -n "${SERVICE_CMD}" ]] && [[ ! -f "/etc/init.d/v2ray" ]]; then
installSoftware "daemon" || return $? installSoftware "daemon" || return $?
cp "${VSRC_ROOT}/systemv/v2ray" "/etc/init.d/v2ray" cp "${VSRC_ROOT}/systemv/v2ray" "/etc/init.d/v2ray"
chmod +x "/etc/init.d/v2ray" chmod +x "/etc/init.d/v2ray"
update-rc.d v2ray defaults update-rc.d v2ray defaults
fi fi
return
} }
Help(){ Help(){
echo "./install-release.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]" cat - 1>& 2 << EOF
echo " -h, --help Show help" ./install-release.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]
echo " -p, --proxy To download through a proxy server, use -p socks5://127.0.0.1:1080 or -p http://127.0.0.1:3128 etc" -h, --help Show help
echo " -f, --force Force install" -p, --proxy To download through a proxy server, use -p socks5://127.0.0.1:1080 or -p http://127.0.0.1:3128 etc
echo " --version Install a particular version, use --version v3.15" -f, --force Force install
echo " -l, --local Install from a local file" --version Install a particular version, use --version v3.15
echo " --remove Remove installed V2Ray" -l, --local Install from a local file
echo " -c, --check Check for update" --remove Remove installed V2Ray
return 0 -c, --check Check for update
EOF
} }
remove(){ remove(){
@ -405,7 +424,9 @@ main(){
[[ "$CHECK" == "1" ]] && checkUpdate && return [[ "$CHECK" == "1" ]] && checkUpdate && return
[[ "$REMOVE" == "1" ]] && remove && return [[ "$REMOVE" == "1" ]] && remove && return
sysArch local ARCH=$(uname -m)
VDIS="$(archAffix)"
# extract local file # extract local file
if [[ $LOCAL_INSTALL -eq 1 ]]; then if [[ $LOCAL_INSTALL -eq 1 ]]; then
colorEcho ${YELLOW} "Installing V2Ray via local file. Please make sure the file is a valid V2Ray package, as we are not able to determine that." colorEcho ${YELLOW} "Installing V2Ray via local file. Please make sure the file is a valid V2Ray package, as we are not able to determine that."
@ -431,7 +452,7 @@ main(){
RETVAL="$?" RETVAL="$?"
if [[ $RETVAL == 0 ]] && [[ "$FORCE" != "1" ]]; then if [[ $RETVAL == 0 ]] && [[ "$FORCE" != "1" ]]; then
colorEcho ${BLUE} "Latest version ${CUR_VER} is already installed." colorEcho ${BLUE} "Latest version ${CUR_VER} is already installed."
if [[ "${ERROR_IF_UPTODATE}" == "1" ]]; then if [ -n "${ERROR_IF_UPTODATE}" ]; then
return 10 return 10
fi fi
return return
@ -445,7 +466,7 @@ main(){
fi fi
fi fi
if [[ "${EXTRACT_ONLY}" == "1" ]]; then if [ -n "${EXTRACT_ONLY}" ]; then
colorEcho ${GREEN} "V2Ray extracted to ${VSRC_ROOT}, and exiting..." colorEcho ${GREEN} "V2Ray extracted to ${VSRC_ROOT}, and exiting..."
return 0 return 0
fi fi