添加 deploy.sh
This commit is contained in:
268
deploy.sh
Normal file
268
deploy.sh
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# deploy.sh - Deployment script for SiteProxy
|
||||||
|
# Usage: ./deploy.sh [start|stop|restart|logs|status]
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# 颜色定义
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
BLUE='\033[0;34m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
|
# 配置
|
||||||
|
PROJECT_NAME="siteproxy"
|
||||||
|
COMPOSE_FILE="docker-compose.yml"
|
||||||
|
ENV_FILE=".env"
|
||||||
|
ENV_EXAMPLE=".env.example"
|
||||||
|
|
||||||
|
# 打印带颜色的消息
|
||||||
|
print_info() {
|
||||||
|
echo -e "${BLUE}ℹ${NC} $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_success() {
|
||||||
|
echo -e "${GREEN}✓${NC} $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_warning() {
|
||||||
|
echo -e "${YELLOW}⚠${NC} $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_error() {
|
||||||
|
echo -e "${RED}✗${NC} $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 打印标题
|
||||||
|
print_header() {
|
||||||
|
echo ""
|
||||||
|
echo -e "${BLUE}================================${NC}"
|
||||||
|
echo -e "${BLUE} $1${NC}"
|
||||||
|
echo -e "${BLUE}================================${NC}"
|
||||||
|
echo ""
|
||||||
|
}
|
||||||
|
|
||||||
|
# 检查依赖
|
||||||
|
check_dependencies() {
|
||||||
|
print_info "Checking dependencies..."
|
||||||
|
|
||||||
|
if ! command -v docker &> /dev/null; then
|
||||||
|
print_error "Docker is not installed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! command -v docker-compose &> /dev/null; then
|
||||||
|
print_error "Docker Compose is not installed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_success "All dependencies found"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 检查环境文件
|
||||||
|
check_env_file() {
|
||||||
|
print_info "Checking environment configuration..."
|
||||||
|
|
||||||
|
if [ ! -f "$ENV_FILE" ]; then
|
||||||
|
print_warning "$ENV_FILE not found"
|
||||||
|
|
||||||
|
if [ -f "$ENV_EXAMPLE" ]; then
|
||||||
|
print_info "Copying $ENV_EXAMPLE to $ENV_FILE"
|
||||||
|
cp "$ENV_EXAMPLE" "$ENV_FILE"
|
||||||
|
print_warning "Please edit $ENV_FILE and configure your settings"
|
||||||
|
print_warning "Especially change AUTH_PASSWORD and SESSION_SECRET"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
print_error "$ENV_EXAMPLE not found"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 检查是否使用默认密码
|
||||||
|
if grep -q "your_secure_password_here" "$ENV_FILE"; then
|
||||||
|
print_error "Default password detected in $ENV_FILE"
|
||||||
|
print_warning "Please change AUTH_PASSWORD before deploying"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if grep -q "change_this_to_random_string" "$ENV_FILE"; then
|
||||||
|
print_error "Default session secret detected in $ENV_FILE"
|
||||||
|
print_warning "Please change SESSION_SECRET before deploying"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_success "Environment configuration OK"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 启动服务
|
||||||
|
start_service() {
|
||||||
|
print_header "Starting $PROJECT_NAME"
|
||||||
|
|
||||||
|
check_dependencies
|
||||||
|
check_env_file
|
||||||
|
|
||||||
|
print_info "Building and starting containers..."
|
||||||
|
docker-compose -f "$COMPOSE_FILE" up -d --build
|
||||||
|
|
||||||
|
print_success "Service started successfully"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
print_info "Service is running at: http://localhost:8080"
|
||||||
|
print_info "View logs with: ./deploy.sh logs"
|
||||||
|
print_info "Check status with: ./deploy.sh status"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 停止服务
|
||||||
|
stop_service() {
|
||||||
|
print_header "Stopping $PROJECT_NAME"
|
||||||
|
|
||||||
|
print_info "Stopping containers..."
|
||||||
|
docker-compose -f "$COMPOSE_FILE" down
|
||||||
|
|
||||||
|
print_success "Service stopped"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 重启服务
|
||||||
|
restart_service() {
|
||||||
|
print_header "Restarting $PROJECT_NAME"
|
||||||
|
|
||||||
|
stop_service
|
||||||
|
sleep 2
|
||||||
|
start_service
|
||||||
|
}
|
||||||
|
|
||||||
|
# 查看日志
|
||||||
|
view_logs() {
|
||||||
|
print_header "Viewing $PROJECT_NAME Logs"
|
||||||
|
|
||||||
|
print_info "Press Ctrl+C to exit"
|
||||||
|
echo ""
|
||||||
|
docker-compose -f "$COMPOSE_FILE" logs -f --tail=100
|
||||||
|
}
|
||||||
|
|
||||||
|
# 查看状态
|
||||||
|
check_status() {
|
||||||
|
print_header "$PROJECT_NAME Status"
|
||||||
|
|
||||||
|
if docker-compose -f "$COMPOSE_FILE" ps | grep -q "Up"; then
|
||||||
|
print_success "Service is running"
|
||||||
|
echo ""
|
||||||
|
docker-compose -f "$COMPOSE_FILE" ps
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# 检查健康状态
|
||||||
|
if docker-compose -f "$COMPOSE_FILE" ps | grep -q "healthy"; then
|
||||||
|
print_success "Health check: PASSED"
|
||||||
|
else
|
||||||
|
print_warning "Health check: PENDING or FAILED"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 显示资源使用
|
||||||
|
echo ""
|
||||||
|
print_info "Resource usage:"
|
||||||
|
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" $(docker-compose -f "$COMPOSE_FILE" ps -q)
|
||||||
|
|
||||||
|
else
|
||||||
|
print_warning "Service is not running"
|
||||||
|
echo ""
|
||||||
|
docker-compose -f "$COMPOSE_FILE" ps
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 更新服务
|
||||||
|
update_service() {
|
||||||
|
print_header "Updating $PROJECT_NAME"
|
||||||
|
|
||||||
|
print_info "Pulling latest changes..."
|
||||||
|
git pull
|
||||||
|
|
||||||
|
print_info "Rebuilding containers..."
|
||||||
|
docker-compose -f "$COMPOSE_FILE" build --no-cache
|
||||||
|
|
||||||
|
print_info "Restarting service..."
|
||||||
|
docker-compose -f "$COMPOSE_FILE" up -d
|
||||||
|
|
||||||
|
print_success "Update complete"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 清理
|
||||||
|
cleanup() {
|
||||||
|
print_header "Cleaning up $PROJECT_NAME"
|
||||||
|
|
||||||
|
print_warning "This will remove all containers, volumes, and images"
|
||||||
|
read -p "Are you sure? (y/N) " -n 1 -r
|
||||||
|
echo
|
||||||
|
|
||||||
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||||
|
print_info "Stopping and removing containers..."
|
||||||
|
docker-compose -f "$COMPOSE_FILE" down -v
|
||||||
|
|
||||||
|
print_info "Removing images..."
|
||||||
|
docker-compose -f "$COMPOSE_FILE" down --rmi all
|
||||||
|
|
||||||
|
print_success "Cleanup complete"
|
||||||
|
else
|
||||||
|
print_info "Cleanup cancelled"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 显示帮助
|
||||||
|
show_help() {
|
||||||
|
echo "Usage: ./deploy.sh [command]"
|
||||||
|
echo ""
|
||||||
|
echo "Commands:"
|
||||||
|
echo " start Start the service"
|
||||||
|
echo " stop Stop the service"
|
||||||
|
echo " restart Restart the service"
|
||||||
|
echo " logs View service logs"
|
||||||
|
echo " status Check service status"
|
||||||
|
echo " update Update and restart service"
|
||||||
|
echo " cleanup Remove all containers and images"
|
||||||
|
echo " help Show this help message"
|
||||||
|
echo ""
|
||||||
|
echo "Examples:"
|
||||||
|
echo " ./deploy.sh start"
|
||||||
|
echo " ./deploy.sh logs"
|
||||||
|
echo " ./deploy.sh status"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 主逻辑
|
||||||
|
main() {
|
||||||
|
case "${1:-start}" in
|
||||||
|
start)
|
||||||
|
start_service
|
||||||
|
;;
|
||||||
|
stop)
|
||||||
|
stop_service
|
||||||
|
;;
|
||||||
|
restart)
|
||||||
|
restart_service
|
||||||
|
;;
|
||||||
|
logs)
|
||||||
|
view_logs
|
||||||
|
;;
|
||||||
|
status)
|
||||||
|
check_status
|
||||||
|
;;
|
||||||
|
update)
|
||||||
|
update_service
|
||||||
|
;;
|
||||||
|
cleanup)
|
||||||
|
cleanup
|
||||||
|
;;
|
||||||
|
help|--help|-h)
|
||||||
|
show_help
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
print_error "Unknown command: $1"
|
||||||
|
echo ""
|
||||||
|
show_help
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# 执行主函数
|
||||||
|
main "$@"
|
||||||
Reference in New Issue
Block a user