Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script #99

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Language : 🇺🇸 English | [🇨🇳 简体中文](./README.zh-CN.md)

Backend for iOS application named [ServerBee](https://apps.apple.com/us/app/serverbee/id6443553714)

<a href="https://www.producthunt.com/posts/serverbee?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-serverbee" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=378908&theme=light" alt="ServerBee - Monitor&#0032;and&#0032;manage&#0032;all&#0032;your&#0032;desktop&#0032;systems | Product Hunt" style="width: 250px; height: 54px;" width="250" height="54" /></a>
Web-based Single-Node, Multi-Node Monitoring and Terminal Management Tools

![GitHub release (latest by date)](https://img.shields.io/github/v/release/ZingerLittleBee/server_bee-backend?style=for-the-badge)
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/ZingerLittleBee/server_bee-backend/release.yml?style=for-the-badge)
Expand All @@ -24,7 +24,10 @@ Mono repository for ServerBee
- serverbee-recorder
- [ServerBee Backend](./web/README.md) (The backend of serverbee, can be used as a standalone application)
- serverbee-web (provide data from server)
- serverbee-deploy (provide **AutoLaunch**、**AutoUpdate**、**DownloadWebModule** for serverbee-web)
- ~~serverbee-deploy (provide **AutoLaunch**、**AutoUpdate**、**DownloadWebModule** for serverbee-web)~~(Deprecated)
- Linux use startup.sh to replace
- `bash <(curl -s https://raw.githubusercontent.com/ZingerLittleBee/server_bee-backend/main/script/startup.sh)`
- macOS、Windows use [ServerMilk](https://github.com/ZingerLittleBee/ServerMilk) to replace

# Documentation

Expand Down
7 changes: 5 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Language : [🇺🇸 English](./README.md) | 🇨🇳 简体中文

iOS 应用 [ServerBee](https://apps.apple.com/us/app/serverbee/id6443553714) 的后端

<a href="https://www.producthunt.com/posts/serverbee?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-serverbee" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=378908&theme=light" alt="ServerBee - Monitor&#0032;and&#0032;manage&#0032;all&#0032;your&#0032;desktop&#0032;systems | Product Hunt" style="width: 250px; height: 54px;" width="250" height="54" /></a>
Web 单机、多机版监控、终端管理工具

![GitHub release (latest by date)](https://img.shields.io/github/v/release/ZingerLittleBee/server_bee-backend?style=for-the-badge)
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/ZingerLittleBee/server_bee-backend/release.yml?style=for-the-badge)
Expand All @@ -24,7 +24,10 @@ ServerBee 的单一代码库
- serverbee-recorder
- [ServerBee Backend](web/README.zh-CN.md) (**ServerBee** 的后端可以作为独立应用程序使用)
- serverbee-web (模块提供来自机器的数据)
- serverbee-deploy (模块提供 **开机启动**、**自动更新**、**下载 Web 模块** 的功能)
- ~~serverbee-deploy (模块提供 **开机启动**、**自动更新**、**下载 Web 模块** 的功能)~~(已弃用)
- Linux 使用 startup.sh 替换
- `bash <(curl -s https://raw.githubusercontent.com/ZingerLittleBee/server_bee-backend/main/script/startup.sh)`
- macOS、Windows 使用 [ServerMilk](https://github.com/ZingerLittleBee/ServerMilk) 替换

## 文档

Expand Down
213 changes: 194 additions & 19 deletions script/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,220 @@

BASE_URL="https://db.serverbee.app/cli"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
ERROR='\033[0;31m'
INFO='\033[0;32m'
WARNING='\033[0;33m'
# No Color
NC='\033[0m'

OS="$(uname)"
ARCH="$(uname -m)"
tuple=""

if [[ "$OS" == "Linux" ]]; then
echo -e "Your operating system is ${GREEN}Linux${NC}"
echo -e "OS: ${INFO}Linux${NC}"
tuple="unknown-linux-musl"
elif [[ "$OS" == "Darwin" ]]; then
echo -e "Your operating system is ${GREEN}MacOS${NC}"
echo -e "OS: ${INFO}macOS${NC}"
tuple="apple-darwin"
else
echo -e "Your operating system is ${RED}not identified${NC}" && exit 1
echo -e "OS ${ERROR}not identified${NC}" && exit 1
fi

if [[ "$ARCH" == "x86_64" ]]; then
echo -e "Your architecture is ${GREEN}AMD${NC}"
echo -e "Architecture: ${INFO}AMD${NC}"
ARCH="x86_64"
elif [[ "$ARCH" == "arm64" || "$ARCH" == *"armv"* ]]; then
echo -e "Your architecture is ${GREEN}ARM${NC}"
echo -e "Architecture: ${INFO}ARM${NC}"
ARCH="aarch64"
else
echo -e "Your architecture is ${RED}not identified${NC}" && exit 1
echo -e "Architecture ${ERROR}not identified${NC}" && exit 1
fi

version=$(curl -s https://status.serverbee.app/api/version)
echo -e "Latest version is ${GREEN}$version${NC}"
start_on_boot() {
if [[ $is_start_on_boot == "y" ]]; then
echo -e "Starting on boot..."
cat > /etc/systemd/system/serverbee-web.service <<EOF
[Unit]
Description=ServerBee Web
After=network-online.target systemd-resolved.service
Wants=network-online.target systemd-resolved.service

[Service]
Type=simple
ExecStart=$PWD/serverbee-web
RemainAfterExit=yes
Restart=always
RestartSec=5s
Environment="RUST_LOG=info"

url="$BASE_URL/$version/serverbee-deploy-$ARCH-$tuple.zip"
echo -e "Downloading ${GREEN}$url${NC}"
[Install]
WantedBy=multi-user.target
EOF

mkdir "serverbee"
cd "serverbee" || exit
curl -s -L "$url" -o serverbee-deploy.zip
unzip -o serverbee-deploy.zip
rm serverbee-deploy.zip
./serverbee-deploy
systemctl enable serverbee-web.service
else
echo -e "${ERROR}Failed to enable auto start on boot, please enable it manually.${NC}"
fi
}

installation() {
echo -e "======================= Installation =========================="

echo -e "${INFO}Starting installation...${NC}"

echo -e "Getting the latest version of ServerBee"
version=$(curl -s https://status.serverbee.app/api/version)
echo -e "Latest version: ${INFO}$version${NC}"

echo -e "\nPress ${INFO}Enter to install ${NC}, or ${ERROR}Ctrl+C to cancel${NC}"
read -r

url="$BASE_URL/$version/serverbee-web-$ARCH-$tuple.zip"
echo -e "Downloading ${INFO}$url${NC}"

mkdir "serverbee"
cd "serverbee" || exit
curl -s -L "$url" -o serverbee-web.zip
unzip -o serverbee-web.zip
rm serverbee-web.zip

echo -e "\nEnter the ${INFO}port${NC} what you want to use:"
read -r port

# check the command netstat exists
if command -v netstat &> /dev/null; then
echo -e "netstat exists, checking if port $port is available..."
# check if port is available, if not, input another port
while true; do
if [[ $(netstat -an | grep -c "$port") -eq 0 ]]; then
break
else
echo -e "${ERROR}Port $port is already in use, please try another port.${NC}"
read -r port
if [[ -z $port ]]; then
echo -e "${ERROR}Port is empty, please try again.${NC}"
read -r port
fi
fi
done
fi

echo -e "Enable auto start on boot? (y/n)"
read -r is_start_on_boot

if [[ "$OS" == "Linux" ]]; then
start_on_boot
fi

nohup ./serverbee-web -p $port >/dev/null 2>&1 &
echo -e "Installation completed."
echo -e "======================= End ==========================="
echo -e "\n"
echo -e "======================= Tips =========================="
echo -e "1. Website UI: http://localhost:$port"
echo -e "2. If server is not running, please check the ${INFO}web.log${NC} in serverbee directory."
echo -e "3. If want to stop the server, please use ${INFO}ps -ef | grep serverbee-web | grep -v grep | awk '{print $2}' | xargs kill -9${NC} to stop the server."
echo -e "========================================================="
}

stop() {
echo -e "======================= Stop =========================="
echo -e "Stopping the service..."
ps -ef | grep serverbee-web | grep -v grep | awk '{print $2}' | xargs kill -9
echo -e "Stopped successfully."
echo -e "======================= End ==========================="
}

update() {
echo -e "======================= Update =========================="
echo -e "Updating the service..."
cd serverbee || { echo -e "${ERROR}Directory serverbee not found. Please run installation.${NC}" && exit 1; }

stop

echo -e "Getting the latest version of ServerBee"
version=$(curl -s https://status.serverbee.app/api/version)
echo -e "Latest version: ${INFO}$version${NC}"

echo -e "\nPress ${INFO}Enter to update ${NC}, or ${ERROR}Ctrl+C to cancel${NC}"
read -r

url="$BASE_URL/$version/serverbee-web-$ARCH-$tuple.zip"
echo -e "Downloading ${INFO}$url${NC}"

curl -s -L "$url" -o serverbee-web.zip
unzip -o serverbee-web.zip
rm serverbee-web.zip

echo -e "Updated successfully."
echo -e "======================= End =========================="
}

uninstallation() {
echo -e "======================= Uninstallation =========================="

echo -e "${INFO}Starting uninstallation...${NC}"
stop

echo -e "Do you want to execute the following steps?"
echo -e "1. Remove ${INFO}the directory of serverbee (including data)${NC}"
echo -e "${WARNING}rm -rf serverbee${NC}"
echo -e "2. Remove ${INFO}the service of serverbee${NC} (if exist)"
echo -e "${WARNING}rm -f /etc/systemd/system/serverbee-web.service${NC}"

echo -e "\nPress ${INFO}Enter to execute ${NC}, or ${ERROR}Ctrl+C to cancel${NC}"
read -r

if [[ -d serverbee ]]; then
rm -rf serverbee
rm -f /etc/systemd/system/serverbee-web.service

else
echo -e "${ERROR}Directory serverbee not found.${NC}" && exit 1
fi

echo -e "${INFO}Uninstallation completed.${NC}"
echo -e "======================= End =========================="
}

log() {
echo -e "======================= Log =========================="
echo -e "Log file: ${INFO}web.log${NC}"

if [ -e "web.log" ]; then
tail -n 200 web.log
else
[ -d "serverbee" ] || { echo -e "${ERROR}Directory serverbee not found.${NC}" && exit 1; }
tail -n 200 serverbee/web.log
fi

echo -e "======================= End =========================="
}

echo "======================= Commands =========================="
echo "Welcome to use ServerBee automatic installation, please select an option:"
options=("Installation" "Update" "Uninstallation" "Log" "Stop" "Exit")
select opt in "${options[@]}"; do
case $opt in
"Installation")
installation
;;
"Update")
update
;;
"Uninstallation")
uninstallation
;;
"Log")
log
;;
"Stop")
stop
;;
"Exit")
break
;;
*) echo "Invalid selection, please try again" ;;
esac
done
Loading