diff --git a/pharmq-setup.sh b/pharmq-setup.sh index 6cda6e0..dd6c38f 100644 --- a/pharmq-setup.sh +++ b/pharmq-setup.sh @@ -285,7 +285,7 @@ phase2_tailscale_pve() { phase3_collect_info() { print_phase "Phase 3/10: 약국 정보 수집" - # 명령행 인자가 있으면 사용, 없으면 대화형 입력 + # 명령행 인자가 있으면 사용 if [ -n "$ARGS_NAME" ]; then PHARMACY_NAME="$ARGS_NAME" HIRA_CODE="${ARGS_HIRA:-}" @@ -295,7 +295,65 @@ phase3_collect_info() { MSSQL_SERVER="${ARGS_MSSQL:-192.168.0.201\\PM2014}" print_ok "명령행 인자에서 약국 정보 로드됨" else - # 약국명 (필수) + # PVE의 Tailscale IP로 이미 등록된 약국인지 조회 + local EXISTING_PHARMACY="" + if [ -n "$PVE_VPN_IP" ]; then + print_step "등록된 약국 조회 중... (VPN IP: $PVE_VPN_IP)" + EXISTING_PHARMACY=$(curl -s --connect-timeout 5 "${FARMQ_API}/api/pharmacies" 2>/dev/null | \ + python3 -c " +import json, sys +try: + data = json.load(sys.stdin) + pharmacies = data.get('pharmacies', data) if isinstance(data, dict) else data + if isinstance(pharmacies, list): + for p in pharmacies: + devices = p.get('devices', []) + for d in devices: + if d.get('tailscale_ip') == '$PVE_VPN_IP': + print(json.dumps(p, ensure_ascii=False)) + sys.exit(0) + if p.get('tailscale_ip') == '$PVE_VPN_IP': + print(json.dumps(p, ensure_ascii=False)) + sys.exit(0) +except: + pass +" 2>/dev/null || true) + fi + + if [ -n "$EXISTING_PHARMACY" ]; then + # 기존 약국 정보 로드 + local EXIST_NAME EXIST_CODE + EXIST_NAME=$(echo "$EXISTING_PHARMACY" | python3 -c "import json,sys; print(json.load(sys.stdin).get('pharmacy_name',''))" 2>/dev/null || true) + EXIST_CODE=$(echo "$EXISTING_PHARMACY" | python3 -c "import json,sys; print(json.load(sys.stdin).get('pharmacy_code',''))" 2>/dev/null || true) + + echo "" + echo -e "${GREEN}이 PVE에 등록된 약국을 찾았습니다:${NC}" + echo -e " 약국명: ${WHITE}${EXIST_NAME}${NC}" + echo -e " 코드: ${WHITE}${EXIST_CODE}${NC}" + echo "" + echo -ne "${CYAN}이 약국 정보를 사용하시겠습니까? (Y/n): ${NC}" + read -r USE_EXISTING /dev/null || true) + PHARMACY_ADDRESS=$(echo "$EXISTING_PHARMACY" | python3 -c "import json,sys; print(json.load(sys.stdin).get('address','') or '')" 2>/dev/null || true) + OWNER_NAME=$(echo "$EXISTING_PHARMACY" | python3 -c "import json,sys; print(json.load(sys.stdin).get('owner_name','') or '')" 2>/dev/null || true) + PHARMACY_PHONE=$(echo "$EXISTING_PHARMACY" | python3 -c "import json,sys; print(json.load(sys.stdin).get('phone','') or '')" 2>/dev/null || true) + MSSQL_SERVER="${ARGS_MSSQL:-192.168.0.201\\PM2014}" + print_ok "기존 약국 정보 로드됨: $PHARMACY_CODE ($PHARMACY_NAME)" + + echo "" + print_info "약국명: $PHARMACY_NAME" + print_info "코드: $PHARMACY_CODE" + print_info "MSSQL: $MSSQL_SERVER" + print_ok "Phase 3 완료: 약국 정보 수집됨" + return 0 + fi + fi + + # 신규 약국 — 대화형 입력 PHARMACY_NAME="" while [ -z "$PHARMACY_NAME" ]; do echo -ne "${CYAN}약국명 (필수): ${NC}" @@ -782,6 +840,17 @@ SVCEOF" phase7_register() { print_phase "Phase 7/10: 약국 + 장비 + 계정 등록" + # 이미 약국 코드가 있으면 (Phase 3에서 기존 약국 로드됨) 등록 스킵 + if [ -n "${PHARMACY_CODE:-}" ]; then + print_ok "약국 이미 등록됨: $PHARMACY_CODE ($PHARMACY_NAME) — 등록 스킵" + # Tailscale hostname만 업데이트 + tailscale set --hostname="${PHARMACY_CODE}-pve-pharmq" 2>/dev/null || true + if [ -n "${CT_VMID:-}" ]; then + pct exec "$CT_VMID" -- bash -c "tailscale set --hostname=${PHARMACY_CODE}-ubuntu-api" 2>/dev/null || true + fi + return 0 + fi + # 7-1. 약국 생성 (CT를 primary 장비로) print_step "약국 생성 중... (farmq.db)" local PHARMACY_RESPONSE