Skip to content

Commit

Permalink
feat: add install release script
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelncui committed Sep 27, 2023
1 parent e2ebddd commit c212209
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cp ./cmd/httpd/yatm-httpd.service ./output/
cp ./cmd/httpd/config.example.yaml ./output/
cp ./LICENSE ./output/
cp ./README.md ./output/
echo "${RELEASE_VERSION}" > ./output/VERSION

# docker run --rm -v $(pwd):/app golang:1.21 sh -c "cd /app && bash "
# docker run --rm -v $(pwd):/app node:20-slim sh -c "cd /app && bash build_frontend.sh"
Expand Down
151 changes: 151 additions & 0 deletions install-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!/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/ltfs
mkdir -p /opt/yatm
tar -xvzf ${GZIP_FILE} -C /opt/yatm

systemctl daemon-reload
systemctl enable /opt/yatm/yatm-httpd.service
systemctl restart yatm-httpd.service
systemctl status yatm-httpd.service
}

main "$@"

0 comments on commit c212209

Please sign in to comment.