refactor: reorganize project structure for better maintainability

- Move Docker files to build/docker/
- Move CI/CD configs to build/ci/
- Move deployment configs to deploy/ (helm, k8s, argocd)
- Move config files to config/
- Move scripts to tools/
- Consolidate assets to assets/ (Reflex compatible)
- Add data/ directory for local data (gitignored)
- Update all path references in Makefile, Dockerfile, CI configs
- Add comprehensive README files for build/ and deploy/
- Update project documentation

Benefits:
- Clear separation of concerns
- Cleaner root directory
- Better developer experience
- Enterprise-grade structure
- Improved maintainability
This commit is contained in:
Ehsan.Asadi
2025-12-30 21:20:32 +03:30
parent 954387a8cf
commit 6820f0ee4f
45 changed files with 1737 additions and 361 deletions

126
tools/scripts/diagnose-502.sh Executable file
View File

@@ -0,0 +1,126 @@
#!/bin/bash
# Diagnostic script for 502 Bad Gateway error
set -e
NAMESPACE=${1:-production}
APP_NAME="peikarband"
echo "🔍 Diagnosing 502 Bad Gateway for $APP_NAME in namespace $NAMESPACE"
echo "=========================================="
echo ""
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 1. Check Pods
echo "1⃣ Checking Pods..."
echo "-------------------"
PODS=$(kubectl get pods -n $NAMESPACE -l app.kubernetes.io/name=$APP_NAME --no-headers 2>/dev/null || echo "")
if [ -z "$PODS" ]; then
echo -e "${RED}❌ No pods found!${NC}"
exit 1
fi
kubectl get pods -n $NAMESPACE -l app.kubernetes.io/name=$APP_NAME
echo ""
# Check pod status
POD_STATUS=$(kubectl get pods -n $NAMESPACE -l app.kubernetes.io/name=$APP_NAME -o jsonpath='{.items[0].status.phase}' 2>/dev/null || echo "Unknown")
POD_NAME=$(kubectl get pods -n $NAMESPACE -l app.kubernetes.io/name=$APP_NAME -o jsonpath='{.items[0].metadata.name}' 2>/dev/null || echo "")
if [ "$POD_STATUS" != "Running" ]; then
echo -e "${RED}❌ Pod is not Running! Status: $POD_STATUS${NC}"
echo ""
echo "Pod events:"
kubectl describe pod -n $NAMESPACE $POD_NAME | tail -20
echo ""
fi
# 2. Check Service
echo "2⃣ Checking Service..."
echo "----------------------"
kubectl get svc -n $NAMESPACE -l app.kubernetes.io/name=$APP_NAME
echo ""
# 3. Check Ingress
echo "3⃣ Checking Ingress..."
echo "-----------------------"
kubectl get ingress -n $NAMESPACE -l app.kubernetes.io/name=$APP_NAME
echo ""
# 4. Check Pod Logs
if [ -n "$POD_NAME" ]; then
echo "4⃣ Recent Pod Logs (last 30 lines)..."
echo "--------------------------------------"
kubectl logs -n $NAMESPACE $POD_NAME --tail=30 || echo "Could not fetch logs"
echo ""
fi
# 5. Check Readiness/Liveness
if [ -n "$POD_NAME" ]; then
echo "5⃣ Checking Probe Status..."
echo "----------------------------"
READY=$(kubectl get pod -n $NAMESPACE $POD_NAME -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}' 2>/dev/null || echo "Unknown")
echo "Ready: $READY"
# Check container status
CONTAINER_STATUS=$(kubectl get pod -n $NAMESPACE $POD_NAME -o jsonpath='{.status.containerStatuses[0].ready}' 2>/dev/null || echo "Unknown")
echo "Container Ready: $CONTAINER_STATUS"
echo ""
fi
# 6. Test from inside pod
if [ -n "$POD_NAME" ] && [ "$POD_STATUS" == "Running" ]; then
echo "6⃣ Testing /ping endpoint from inside pod..."
echo "---------------------------------------------"
kubectl exec -n $NAMESPACE $POD_NAME -- curl -s http://localhost:8000/ping || echo -e "${RED}❌ /ping failed!${NC}"
echo ""
fi
# 7. Check Service Endpoints
echo "7⃣ Checking Service Endpoints..."
echo "---------------------------------"
SVC_NAME=$(kubectl get svc -n $NAMESPACE -l app.kubernetes.io/name=$APP_NAME -o jsonpath='{.items[0].metadata.name}' 2>/dev/null || echo "")
if [ -n "$SVC_NAME" ]; then
kubectl get endpoints -n $NAMESPACE $SVC_NAME
ENDPOINTS=$(kubectl get endpoints -n $NAMESPACE $SVC_NAME -o jsonpath='{.subsets[0].addresses[*].ip}' 2>/dev/null || echo "")
if [ -z "$ENDPOINTS" ]; then
echo -e "${RED}❌ No endpoints! Service cannot route traffic.${NC}"
echo "This is likely because readiness probe is failing."
fi
echo ""
fi
# 8. Check Resources
if [ -n "$POD_NAME" ]; then
echo "8⃣ Checking Resource Usage..."
echo "------------------------------"
kubectl top pod -n $NAMESPACE $POD_NAME 2>/dev/null || echo "Metrics not available"
echo ""
fi
# 9. Check Events
echo "9⃣ Recent Events..."
echo "-------------------"
kubectl get events -n $NAMESPACE --sort-by='.lastTimestamp' | grep $APP_NAME | tail -10 || echo "No recent events"
echo ""
# 10. NetworkPolicy check
echo "🔟 Checking NetworkPolicy..."
echo "---------------------------"
kubectl get networkpolicy -n $NAMESPACE -l app.kubernetes.io/name=$APP_NAME || echo "No NetworkPolicy found"
echo ""
echo "=========================================="
echo "✅ Diagnosis complete!"
echo ""
echo "Common fixes:"
echo "1. If pod is CrashLoopBackOff: Check logs and resource limits"
echo "2. If no endpoints: Readiness probe is failing - check /ping endpoint"
echo "3. If NetworkPolicy exists: Check if it allows ingress traffic"
echo "4. If resources exhausted: Increase limits in values-production.yaml"

View File

@@ -0,0 +1,69 @@
#!/bin/bash
# Update .web/env.json with API_URL from environment variable at runtime
set -e
API_URL="${API_URL:-http://localhost:8000}"
# Extract protocol, host, and port from API_URL
if [[ "$API_URL" =~ ^(https?://)([^:/]+)(:([0-9]+))? ]]; then
PROTOCOL="${BASH_REMATCH[1]}"
HOST="${BASH_REMATCH[2]}"
PORT="${BASH_REMATCH[4]:-8000}"
# Remove trailing slash
API_URL="${API_URL%/}"
# Update .web/env.json
if [ -f "/app/.web/env.json" ]; then
# Use Python to properly update JSON
python3 <<EOF
import json
import os
api_url = os.environ.get('API_URL', 'http://localhost:8000')
api_url = api_url.rstrip('/')
# Determine protocol and host
if api_url.startswith('https://'):
ws_protocol = 'wss://'
http_protocol = 'https://'
elif api_url.startswith('http://'):
ws_protocol = 'ws://'
http_protocol = 'http://'
else:
ws_protocol = 'ws://'
http_protocol = 'http://'
api_url = f'http://{api_url}'
# Remove protocol prefix for host extraction
host = api_url.replace('https://', '').replace('http://', '').split('/')[0]
# Read existing env.json
with open('/app/.web/env.json', 'r') as f:
env_data = json.load(f)
# Update URLs
env_data['PING'] = f'{http_protocol}{host}/ping'
env_data['EVENT'] = f'{ws_protocol}{host}/_event'
env_data['UPLOAD'] = f'{http_protocol}{host}/_upload'
env_data['AUTH_CODESPACE'] = f'{http_protocol}{host}/auth-codespace'
env_data['HEALTH'] = f'{http_protocol}{host}/_health'
env_data['ALL_ROUTES'] = f'{http_protocol}{host}/_all_routes'
# Write back
with open('/app/.web/env.json', 'w') as f:
json.dump(env_data, f)
print(f"Updated .web/env.json with API_URL: {api_url}")
EOF
else
echo "Warning: /app/.web/env.json not found"
fi
else
echo "Warning: Invalid API_URL format: $API_URL"
fi
# Execute the original command
exec "$@"

39
tools/setup.py Normal file
View File

@@ -0,0 +1,39 @@
"""Setup configuration for Peikarband platform."""
from setuptools import setup, find_packages
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
with open("requirements.txt", "r", encoding="utf-8") as fh:
requirements = [line.strip() for line in fh if line.strip() and not line.startswith("#")]
setup(
name="peikarband",
version="0.1.0",
author="Peikarband Team",
author_email="support@peikarband.ir",
description="پلتفرم جامع مدیریت هاستینگ و زیرساخت ابری",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/yourusername/peikarband",
packages=find_packages(where="src"),
package_dir={"": "src"},
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
python_requires=">=3.11",
install_requires=requirements,
entry_points={
"console_scripts": [
"peikarband=src.presentation.web.app:main",
],
},
)