feat:shell

This commit is contained in:
Harold 2025-11-05 10:52:15 +08:00
parent b120857f50
commit d76975a6c6
2 changed files with 299 additions and 0 deletions

View file

@ -0,0 +1,127 @@
Xiaozhi Server Startup Script Usage
====================================
QUICK START
-----------
1. Start server only (most common for development):
./start.sh --region <region> --action server
./start.sh -r <region> -a server
2. Start all services (first time or full restart):
./start.sh --region <region> --action all
./start.sh -r <region> -a all
COMMAND LINE OPTIONS
--------------------
-r, --region <region> Deployment region (required)
Values: sg, us, local
-a, --action <action> Action to perform (required)
Values: server, all
-h, --help Show help message
REGION OPTIONS
--------------
sg - Singapore deployment (uses docker-compose_sg_all.yml)
REGION=SINGAPORE, secret: a1c50356-4307-4bb7-8c16-c661d06ec17b
us - US West 2 deployment (uses docker-compose_us_all.yml)
REGION=US-WEST-2, secret: ff22d758-79dd-47ff-a897-c6e39983e0e1
local - Local development (uses docker-compose_local_all.yml)
REGION=LOCAL, secret: from custom_config.yaml
ACTION OPTIONS
--------------
server - Restart ONLY xiaozhi-esp32-server
Other services (web, db, redis) keep running
Fast restart for code iteration
✓ Preserves database data
✓ Preserves Redis cache
✓ Avoids web service restart
all - Start/restart ALL services
Runs: docker-compose up -d
Use when:
- First time setup
- After modifying docker-compose files
- Need to restart web/db/redis
COMMON USAGE EXAMPLES
---------------------
# Development iteration (most frequent)
./start.sh --region local --action server # Local dev, restart server only
./start.sh -r sg -a server # Singapore, restart server only
# First time or full restart
./start.sh --region local --action all # Start everything locally
./start.sh -r sg -a all # Deploy to Singapore
./start.sh -r us -a all # Deploy to US West 2
# Show help
./start.sh --help
./start.sh -h
WORKFLOW EXAMPLE
----------------
1. Initial setup:
./start.sh --region local --action all
2. Make code changes in xiaozhi-server
3. Quick restart to test:
./start.sh -r local -a server
4. View logs:
docker-compose -f docker-compose_local_all.yml logs -f xiaozhi-esp32-server
5. Repeat steps 2-4 for rapid development
TIPS
----
* Server restart takes ~10 seconds
* All services restart takes ~1-2 minutes
* Database and Redis data persist between restarts
* Check container status: docker ps --filter name=xiaozhi
* Stop all services: docker-compose -f docker-compose_<region>_all.yml down
* Both long (--region) and short (-r) options are supported
TROUBLESHOOTING
---------------
Q: "Error: Invalid region 'XX'"
A: Use only: sg, us, or local (case insensitive)
Q: "Error: Invalid action 'XX'"
A: Use only: server or all (case insensitive)
Q: "Error: Unknown option"
A: Check syntax: ./start.sh --region <region> --action <action>
Or use short form: ./start.sh -r <region> -a <action>
Q: "Error: Compose file not found"
A: Make sure you run the script from xiaozhi-server directory
Q: Container not starting
A: Check logs with:
docker-compose -f docker-compose_<region>_all.yml logs xiaozhi-esp32-server
Q: Need to rebuild image after code changes
A: docker-compose -f docker-compose_<region>_all.yml build xiaozhi-esp32-server
Then run: ./start.sh <region> server

172
main/xiaozhi-server/start.sh Executable file
View file

@ -0,0 +1,172 @@
#!/bin/bash
# Xiaozhi Server Startup Script
# Usage: ./start.sh --region <region> --action <action>
# Region: sg, us, local
# Action: server, all
set -e
# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Display banner
echo -e "${CYAN}"
echo "=================================="
echo " Xiaozhi Server Startup Script"
echo "=================================="
echo -e "${NC}"
# Function to display usage
show_usage() {
echo -e "${RED}Error: Missing or invalid arguments${NC}"
echo ""
echo "Usage: $0 --region <region> --action <action>"
echo ""
echo "Options:"
echo " -r, --region <region> Deployment region"
echo " Values: sg, us, local"
echo ""
echo " -a, --action <action> Action to perform"
echo " Values: server, all"
echo ""
echo " -h, --help Show this help message"
echo ""
echo "Region details:"
echo " sg - Singapore (REGION=SINGAPORE)"
echo " us - US West 2 (REGION=US-WEST-2)"
echo " local - Local development (REGION=LOCAL)"
echo ""
echo "Action details:"
echo " server - Restart only xiaozhi-esp32-server"
echo " all - Start/restart all services (server, web, db, redis)"
echo ""
echo "Examples:"
echo " $0 --region sg --action server"
echo " $0 -r us -a all"
echo " $0 --region local --action server"
exit 1
}
# Parse command line arguments
REGION=""
ACTION=""
while [[ $# -gt 0 ]]; do
case $1 in
-r|--region)
REGION="$2"
shift 2
;;
-a|--action)
ACTION="$2"
shift 2
;;
-h|--help)
show_usage
;;
*)
echo -e "${RED}Error: Unknown option '$1'${NC}"
echo ""
show_usage
;;
esac
done
# Check if required arguments are provided
if [ -z "$REGION" ] || [ -z "$ACTION" ]; then
show_usage
fi
# Validate region
case $REGION in
sg|SG)
COMPOSE_FILE="docker-compose_sg_all.yml"
REGION_NAME="Singapore"
REGION_ENV="SINGAPORE"
;;
us|US)
COMPOSE_FILE="docker-compose_us_all.yml"
REGION_NAME="US West 2"
REGION_ENV="US-WEST-2"
;;
local|LOCAL)
COMPOSE_FILE="docker-compose_local_all.yml"
REGION_NAME="Local"
REGION_ENV="LOCAL"
;;
*)
echo -e "${RED}Error: Invalid region '${REGION}'${NC}"
echo "Valid regions: sg, us, local"
exit 1
;;
esac
# Validate action
case $ACTION in
server|SERVER)
ACTION_NAME="Server Only"
SERVICES="xiaozhi-esp32-server"
;;
all|ALL)
ACTION_NAME="All Services"
SERVICES=""
;;
*)
echo -e "${RED}Error: Invalid action '${ACTION}'${NC}"
echo "Valid actions: server, all"
exit 1
;;
esac
# Check if compose file exists
if [ ! -f "$COMPOSE_FILE" ]; then
echo -e "${RED}Error: Compose file '${COMPOSE_FILE}' not found${NC}"
exit 1
fi
echo -e "${GREEN}Configuration:${NC}"
echo " Region: ${REGION_NAME} (${REGION_ENV})"
echo " Action: ${ACTION_NAME}"
echo " Compose file: ${COMPOSE_FILE}"
echo ""
# Display current status
echo -e "${BLUE}Current container status:${NC}"
docker ps --filter name=xiaozhi --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo "No containers running"
echo ""
# Confirm action
if [ "$ACTION" = "server" ] || [ "$ACTION" = "SERVER" ]; then
echo -e "${YELLOW}→ Restarting xiaozhi-esp32-server...${NC}"
echo ""
docker-compose -f "$COMPOSE_FILE" restart xiaozhi-esp32-server
else
echo -e "${YELLOW}→ Starting all services...${NC}"
echo ""
docker-compose -f "$COMPOSE_FILE" up -d
fi
echo ""
echo -e "${GREEN}✓ Operation completed${NC}"
echo ""
# Display final status
echo -e "${BLUE}Updated container status:${NC}"
docker ps --filter name=xiaozhi --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
echo ""
# Show logs option
echo -e "${CYAN}To view logs, run:${NC}"
if [ "$ACTION" = "server" ] || [ "$ACTION" = "SERVER" ]; then
echo " docker-compose -f $COMPOSE_FILE logs -f xiaozhi-esp32-server"
else
echo " docker-compose -f $COMPOSE_FILE logs -f"
fi
echo ""