pve9-repo-fix/RDP/install-rdp-api.sh
Claude e7d5dd02d2 Improve RDP API installer with rerun detection
Changes:
- Detect if RDP Toggle API is already installed and running
- Show test menu immediately on rerun (no reinstallation)
- Add option 3 (status check) and option 4 (exit) to test menu
- Extract test menu to reusable function show_test_menu()
- Both fresh install and rerun show same interactive test menu

Now users can easily test RDP toggle by simply rerunning the installer script.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 03:00:55 +00:00

188 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
# RDP Toggle API 설치 스크립트
# curl -fsSL https://git.0bin.in/thug0bin/pve9-repo-fix/raw/branch/main/RDP/install-rdp-api.sh | bash
set -e
# IP 주소 가져오기 (Headscale 우선, 없으면 로컬 IP)
get_primary_ip() {
# Headscale VPN IP 확인 (100.64.x.x 대역)
local headscale_ip=$(hostname -I | tr ' ' '\n' | grep '^100\.64\.' | head -n1)
if [ -n "$headscale_ip" ]; then
echo "$headscale_ip"
return 0
fi
# Headscale IP가 없으면 로컬 IP (첫 번째)
local local_ip=$(hostname -I | awk '{print $1}')
if [ -n "$local_ip" ]; then
echo "$local_ip"
return 0
fi
# 그것도 없으면 localhost
echo "127.0.0.1"
}
# 테스트 메뉴 함수
show_test_menu() {
echo ""
echo "=========================================="
echo "RDP API를 테스트하시겠습니까?"
echo " 1) RDP 모드로 전환 테스트"
echo " 2) Shell 모드로 전환 테스트"
echo " 3) 현재 상태 확인"
echo " 4) 종료"
echo ""
echo -n "선택 [1/2/3/4]: "
read -r test_choice </dev/tty
case "$test_choice" in
1)
echo ""
echo "RDP 모드로 전환 중..."
if curl -s -X POST http://localhost:8090/toggle \
-H 'Content-Type: application/json' \
-d '{"mode":"rdp"}' > /dev/null 2>&1; then
echo "✅ RDP 모드로 전환 완료!"
echo ""
echo "현재 상태:"
curl -s http://localhost:8090/status | python3 -m json.tool 2>/dev/null || curl -s http://localhost:8090/status
else
echo "❌ RDP 모드 전환 실패. 서비스 상태를 확인하세요:"
echo " systemctl status rdp-toggle-api"
fi
;;
2)
echo ""
echo "Shell 모드로 전환 중..."
if curl -s -X POST http://localhost:8090/toggle \
-H 'Content-Type: application/json' \
-d '{"mode":"shell"}' > /dev/null 2>&1; then
echo "✅ Shell 모드로 전환 완료!"
echo ""
echo "현재 상태:"
curl -s http://localhost:8090/status | python3 -m json.tool 2>/dev/null || curl -s http://localhost:8090/status
else
echo "❌ Shell 모드 전환 실패. 서비스 상태를 확인하세요:"
echo " systemctl status rdp-toggle-api"
fi
;;
3)
echo ""
echo "현재 상태 확인 중..."
curl -s http://localhost:8090/status | python3 -m json.tool 2>/dev/null || curl -s http://localhost:8090/status
;;
4)
echo ""
echo "종료합니다."
return 0
;;
*)
echo ""
echo "잘못된 선택입니다."
;;
esac
echo ""
}
# 설치 디렉토리 설정
INSTALL_DIR="/opt/rdp-toggle-api"
VENV_DIR="$INSTALL_DIR/venv"
GITEA_BASE_URL="https://git.0bin.in/thug0bin/pve9-repo-fix/raw/branch/main/RDP"
# 이미 설치되어 있는지 확인
if systemctl is-active --quiet rdp-toggle-api.service && [ -f "$INSTALL_DIR/rdp-toggle-api.py" ]; then
echo ""
echo "=========================================="
echo "✅ RDP Toggle API가 이미 설치되어 실행 중입니다!"
echo "=========================================="
echo ""
echo "📍 API 서버: http://$(get_primary_ip):8090"
echo "📁 설치 위치: $INSTALL_DIR"
echo ""
# 바로 테스트 메뉴 보여주기
show_test_menu
echo ""
echo "=========================================="
echo "완료!"
echo "=========================================="
echo ""
exit 0
fi
echo "RDP Toggle API 설치 시작..."
# Python 및 venv 설치
echo "Python 및 필수 패키지 설치 중..."
apt update
apt install -y python3 python3-venv python3-pip curl
# 설치 디렉토리 생성
mkdir -p "$INSTALL_DIR"
# 가상환경 생성
echo "가상환경 생성 중..."
python3 -m venv "$VENV_DIR"
# 가상환경에서 패키지 설치
echo "Python 패키지 설치 중..."
"$VENV_DIR/bin/pip" install --upgrade pip
"$VENV_DIR/bin/pip" install fastapi==0.115.5 uvicorn==0.32.1 python-multipart==0.0.20 pydantic==2.10.3
# API 파일 다운로드
echo "API 서버 파일 다운로드 중..."
curl -fsSL "$GITEA_BASE_URL/rdp-toggle-api.py" -o "$INSTALL_DIR/rdp-toggle-api.py"
chmod +x "$INSTALL_DIR/rdp-toggle-api.py"
# systemd 서비스 생성
echo "systemd 서비스 생성 중..."
cat > /etc/systemd/system/rdp-toggle-api.service << EOF
[Unit]
Description=RDP Toggle API Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=$INSTALL_DIR
ExecStart=$VENV_DIR/bin/python $INSTALL_DIR/rdp-toggle-api.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
# 서비스 활성화 및 시작
echo "서비스 활성화 및 시작 중..."
systemctl daemon-reload
systemctl enable rdp-toggle-api.service
systemctl start rdp-toggle-api.service
# 잠시 대기 후 상태 확인
sleep 2
echo ""
echo "=========================================="
echo "RDP Toggle API 설치 완료!"
echo "=========================================="
echo ""
echo "📍 API 서버: http://$(get_primary_ip):8090"
echo "📁 설치 위치: $INSTALL_DIR"
echo ""
# 테스트 메뉴 보여주기
show_test_menu
echo ""
echo "=========================================="
echo "설치 및 설정이 완료되었습니다!"
echo "=========================================="
echo ""