dc-agent/setup.sh

184 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Check root
if [ `whoami` != "root" ];then
echo -e "\nYou need to be root to perform this command. \n"
exit 4
fi
os=$(uname | tr '[:upper:]' '[:lower:]')
platform() {
local support_os=("darwin" "linux" "freebsd")
if [[ "$os" == ${support_os[0]} || "$os" == ${support_os[1]} || "$os" == ${support_os[2]} ]]; then
echo $os
else
echo "no"
fi
}
arch() {
local out="386"
local res=`uname -m`
case $res in
"x86_64") out="amd64"
;;
"x86") out="386"
;;
"aarch64") out="arm64"
;;
*) out=$res
;;
esac
echo $out
}
grepCross() {
local out=$(grep --version | grep -o "FreeBSD")
if [[ $out == "FreeBSD" ]]; then
echo $(grep -Eo "$@")
else
echo $(grep -Po "$@")
fi
}
cmdhas() {
type "$1" > /dev/null 2>&1
}
download() {
if cmdhas "curl"; then
curl --compressed -q "$@"
elif cmdhas "wget"; then
# Emulate curl with wget
ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \
-e 's/-L //' \
-e 's/--compressed //' \
-e 's/-I /--server-response /' \
-e 's/-s /-q /' \
-e 's/-o /-O /' \
-e 's/-C - /-c /')
# shellcheck disable=SC2086
eval wget $ARGS
fi
}
Install() {
local platformValue=`platform`
local archValue=`arch`
if [[ platformValue == "no" ]]; then
echo -e "\nThis operating system is not supported.\n"
exit 10
elif [[ platformValue == "freebsd" ]]; then
echo -e "\nFreeBSD system is not supported for the time being.\n"
exit 10
fi
local res=$(download -s https://server-0.sercretcore.cn/api/download\?arch=${archValue}\&platform=${platformValue})
local status=$(echo "${res}" | grepCross '"status":.' | sed 's/"status"://g')
if [[ "${status}" == "1" ]]; then
local downloadURL=$(echo "${res}" | grepCross '"downloadURL":".*?[^\\]",' | sed 's/"downloadURL":"//g' | sed 's/",//g')
local fileSha256=$(echo "${res}" | grepCross '"sha256":".*?[^\\]",' | sed 's/"sha256":"//g' | sed 's/",//g')
mkdir -p /usr/local/dc-agent/bin
mkdir -p /usr/local/dc-agent/log
download "${downloadURL}" -o "/usr/local/dc-agent/bin/dc-agent"
if cmdhas "sha256sum"; then
checkSha256=$(sha256sum /usr/local/dc-agent/bin/dc-agent | awk '{ print $1 }' | tr '[A-Z]' '[a-z]')
if [[ "${checkSha256}" != "${fileSha256}" ]]; then
echo -e "${checkSha256}\n"
echo -e "${fileSha256}\n"
echo -e "\nError: File sha256 check failed.\n"
exit 1
fi
else
echo -e "\nError: sha256sum command not found.\n"
exit 1
fi
echo -e "\n"
sudo chmod 755 /usr/local/dc-agent/bin/dc-agent
sudo /usr/local/dc-agent/bin/dc-agent install
sudo /usr/local/dc-agent/bin/dc-agent start
echo -e "\nInstall success!\n"
else
echo -e "\nServer connection failed, please check your network connection.\n"
fi
}
Uninstall() {
sudo /usr/local/dc-agent/bin/dc-agent stop
sudo /usr/local/dc-agent/bin/dc-agent remove
PIDFILE="/var/run/dc-agent.pid"
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
sudo kill -QUIT $PID
fi
# Remove all file
sudo rm -rf /usr/local/dc-agent
echo -e "\nUninstall success!\n"
}
echo "
DC-Agent
https://github.com/yi-ge/dc-agent
------------------------------------------------------
MIT License
Copyright (c) 2019 Yige
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the \"Software\"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"
if [[ "$1" == "--install" ]]; then
Install
elif [[ "$1" == "--uninstall" ]]; then
Uninstall
else
echo "What do you want to do?"
select var in "Install (I accept the MIT License.)" "Uninstall" "Exit"; do
break;
done
echo "You have selected $var."
if [[ $var == "Install (I accept the MIT License.)" ]]; then
Install
elif [[ $var == "Uninstall" ]]; then
Uninstall
else
exit 0
fi
fi