1 line
12 KiB
JSON
1 line
12 KiB
JSON
{"repo": "blackeagle686/help1", "files": {"deploy_basma.sh": {"8c3634fffb4caa03bb4672ee34b6cb75fa65f56d": {"t": 1785604634.2091, "n": 4365, "h": "88f12489773cd7b6", "c": "#!/bin/bash\n\n# Define log file\nLOG_FILE=\"server.log\"\n\n# Define Colors\nGREEN='\\033[0;32m'\nBLUE='\\033[0;34m'\nYELLOW='\\033[1;33m'\nRED='\\033[0;31m'\nCLEAR='\\033[0m'\n\n# Check for \"logs\" command\nif [ \"$1\" == \"logs\" ]; then\n if [ -f \"$LOG_FILE\" ]; then\n echo -e \"${BLUE}=== Displaying logs (Press Ctrl+C to exit) ===${CLEAR}\"\n tail -f \"$LOG_FILE\"\n else\n echo -e \"${RED}[!] Log file $LOG_FILE not found!${CLEAR}\"\n fi\n exit 0\nfi\n\n# Check for \"stop\" command\nif [ \"$1\" == \"stop\" ]; then\n echo -e \"${RED}[*] Stopping Django server and Ngrok...${CLEAR}\"\n if [ -f \".django.pid\" ]; then\n DJANGO_PID=$(cat .django.pid)\n echo -e \"${BLUE}[*] Killing existing Django PID $DJANGO_PID...${CLEAR}\"\n kill -9 \"$DJANGO_PID\" 2>/dev/null || true\n rm -f .django.pid\n else\n pkill -f \"manage.py runserver\" || true\n fi\n\n if [ -f \".ngrok_wrapper.pid\" ]; then\n WRAPPER_PID=$(cat .ngrok_wrapper.pid)\n echo -e \"${BLUE}[*] Killing existing Ngrok wrapper PID $WRAPPER_PID...${CLEAR}\"\n kill -9 \"$WRAPPER_PID\" 2>/dev/null || true\n rm -f .ngrok_wrapper.pid\n else\n pkill -f \"start_ngrok.py\" || true\n fi\n\n if [ -f \".ngrok.pid\" ]; then\n NGROK_PID=$(cat .ngrok.pid)\n echo -e \"${BLUE}[*] Killing existing Ngrok PID $NGROK_PID...${CLEAR}\"\n kill -9 \"$NGROK_PID\" 2>/dev/null || true\n rm -f .ngrok.pid\n fi\n\n echo -e \"${GREEN}[✔] Stopped successfully.${CLEAR}\"\n exit 0\nfi\n\necho -e \"${BLUE}==============================================${CLEAR}\"\necho -e \"${GREEN} تنصيب وتشغيل مشروع بصمة قنا في الخلفية ${CLEAR}\"\necho -e \"${BLUE}==============================================${CLEAR}\"\n\n# Terminate existing sessions to avoid port conflicts\necho -e \"${BLUE}[*] Stopping existing instances...${CLEAR}\"\nif [ -f \".django.pid\" ]; then\n DJANGO_PID=$(cat .django.pid)\n kill -9 \"$DJANGO_PID\" 2>/dev/null || true\n rm -f .django.pid\nelse\n pkill -f \"manage.py runserver\" || true\nfi\n\nif [ -f \".ngrok_wrapper.pid\" ]; then\n WRAPPER_PID=$(cat .ngrok_wrapper.pid)\n kill -9 \"$WRAPPER_PID\" 2>/dev/null || true\n rm -f .ngrok_wrapper.pid\nelse\n pkill -f \"start_ngrok.py\" || true\nfi\n\nif [ -f \".ngrok.pid\" ]; then\n NGROK_PID=$(cat .ngrok.pid)\n kill -9 \"$NGROK_PID\" 2>/dev/null || true\n rm -f .ngrok.pid\nfi\n\n# 1. Check for Virtual Environment\nif [ -d \"venv\" ]; then\n echo -e \"${GREEN}[✔] تم العثور على بيئة العمل الافتراضية (venv)${CLEAR}\"\nelse\n echo -e \"${YELLOW}[!] بيئة العمل الافتراضية غير موجودة. جاري إنشائها...${CLEAR}\"\n python3 -m venv venv\n echo -e \"${GREEN}[✔] تم إنشاء بيئة العمل الافتراضية بنجاح.${CLEAR}\"\nfi\n\n# Activate venv\necho -e \"${BLUE}[*] جاري تفعيل بيئة العمل الافتراضية...${CLEAR}\"\nsource venv/bin/activate\n\n# 2. Install / Upgrade pip and requirements\necho -e \"${BLUE}[*] جاري تحديث pip وتثبيت المكتبات المطلوبة...${CLEAR}\"\npip install --upgrade pip\nif [ -f \"requirements.txt\" ]; then\n pip install -r requirements.txt\n echo -e \"${GREEN}[✔] تم تثبيت جميع المكتبات بنجاح.${CLEAR}\"\nelse\n echo -e \"${RED}[!] ملف requirements.txt غير موجود!${CLEAR}\"\n exit 1\nfi\n\n# 3. Database Migrations and Seeding\necho -e \"${BLUE}[*] جاري تحضير وتطبيق قواعد البيانات (Migrations)...${CLEAR}\"\npython3 manage.py makemigrations portal\npython3 manage.py migrate\necho -e \"${BLUE}[*] جاري استيراد البيانات الافتراضية (Seeding)...${CLEAR}\"\npython3 manage.py seed_data\necho -e \"${GREEN}[✔] تم تطبيق قاعدة البيانات واستيراد البيانات بنجاح.${CLEAR}\"\n\n# 4. Collect Static Files\necho -e \"${BLUE}[*] جاري تجميع الملفات الثابتة (Static Files)...${CLEAR}\"\npython3 manage.py collectstatic --noinput\necho -e \"${GREEN}[✔] تم تجميع الملفات الثابتة بنجاح.${CLEAR}\"\n\n# 5. Launch in background\necho -e \"${BLUE}[*] Starting Django & Ngrok in background...${CLEAR}\"\n# Truncate log file\n> \"$LOG_FILE\"\n\nnohup python3 manage.py runserver 127.0.0.1:8005 >> \"$LOG_FILE\" 2>&1 &\necho $! > .django.pid\n\nnohup python3 start_ngrok.py >> \"$LOG_FILE\" 2>&1 &\necho $! > .ngrok_wrapper.pid\n\necho -e \"${GREEN}[✔] Django and Ngrok started in background!${CLEAR}\"\necho -e \"${YELLOW}Logs are being written to $LOG_FILE${CLEAR}\"\necho -e \"${BLUE}Run: ./deploy_basma.sh logs to view output.${CLEAR}\"\necho -e \"${BLUE}Run: ./deploy_basma.sh stop to stop services.${CLEAR}\"\necho -e \"\\nShowing initial logs (waiting for Ngrok URL...):\"\nsleep 4\ncat \"$LOG_FILE\"\n"}}, "manage.py": {"8e7ac79b9576cb38f460147ec7f3ade64a100c62": {"t": 1785604635.4594393, "n": 662, "h": "8f1e60c02dc8e3e4", "c": "#!/usr/bin/env python\n\"\"\"Django's command-line utility for administrative tasks.\"\"\"\nimport os\nimport sys\n\n\ndef main():\n \"\"\"Run administrative tasks.\"\"\"\n os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')\n try:\n from django.core.management import execute_from_command_line\n except ImportError as exc:\n raise ImportError(\n \"Couldn't import Django. Are you sure it's installed and \"\n \"available on your PYTHONPATH environment variable? Did you \"\n \"forget to activate a virtual environment?\"\n ) from exc\n execute_from_command_line(sys.argv)\n\n\nif __name__ == '__main__':\n main()\n"}}, "start_ngrok.py": {"d778885b078754c5a8b28fba6f3a2fa6be4d0d62": {"t": 1785604637.4903135, "n": 5636, "h": "d10df5c0a9122a6b", "c": "import os\nimport sys\nimport time\nimport signal\nimport json\nimport subprocess\nimport urllib.request\nimport urllib.error\nfrom pyngrok import ngrok\nfrom pyngrok.exception import PyngrokNgrokError\n\nTOKEN = \"3EABDo79ukUCl3wW15jvWgtXrVX_66iCGTm5fEtSHpZBmA3sD\"\nPORT = 8005\nPID_FILE = \".ngrok.pid\"\nNGROK_API_PORTS = [4040, 4041, 4042, 4043]\n\n\ndef _kill_pid(pid):\n \"\"\"Terminate a specific process by PID.\"\"\"\n try:\n os.kill(pid, signal.SIGTERM)\n time.sleep(1)\n try:\n os.kill(pid, signal.SIGKILL)\n except ProcessLookupError:\n pass\n except ProcessLookupError:\n pass\n except Exception as e:\n print(f\"[*] Could not kill PID {pid}: {e}\")\n\n\ndef _kill_saved_pid():\n \"\"\"Kill the ngrok process from our saved PID file.\"\"\"\n if not os.path.exists(PID_FILE):\n return\n try:\n with open(PID_FILE, \"r\") as f:\n old_pid = int(f.read().strip())\n print(f\"[!] Found saved ngrok PID {old_pid}. Terminating...\")\n _kill_pid(old_pid)\n print(f\"[✔] Killed PID {old_pid}\")\n except Exception as e:\n print(f\"[*] Could not kill saved PID: {e}\")\n finally:\n try:\n os.remove(PID_FILE)\n except Exception:\n pass\n\n\ndef _find_and_kill_orphaned_ngrok():\n \"\"\"\n Scan local ngrok API ports (4040-4043) to find an orphaned ngrok process\n that has a tunnel for our port (8005). Disconnect the tunnel and kill\n ONLY that specific ngrok process. Does NOT touch other ngrok instances.\n \"\"\"\n for api_port in NGROK_API_PORTS:\n try:\n url = f\"http://127.0.0.1:{api_port}/api/tunnels\"\n req = urllib.request.urlopen(url, timeout=2)\n data = json.loads(req.read())\n\n our_tunnel_found = False\n for tunnel in data.get(\"tunnels\", []):\n addr = tunnel.get(\"config\", {}).get(\"addr\", \"\")\n if str(PORT) in str(addr):\n our_tunnel_found = True\n name = tunnel.get(\"name\", \"\")\n public_url = tunnel.get(\"public_url\", \"\")\n print(f\"[!] Found orphaned tunnel on API :{api_port} → {public_url}\")\n # Disconnect it via the local API\n try:\n del_req = urllib.request.Request(\n f\"http://127.0.0.1:{api_port}/api/tunnels/{name}\",\n method=\"DELETE\"\n )\n urllib.request.urlopen(del_req, timeout=2)\n print(f\"[✔] Disconnected tunnel '{name}'\")\n except Exception:\n pass\n\n if our_tunnel_found:\n # Kill the ngrok process serving this API port\n try:\n result = subprocess.run(\n [\"fuser\", f\"{api_port}/tcp\"],\n capture_output=True, text=True, timeout=5\n )\n pids = result.stdout.strip().split()\n for p in pids:\n pid = int(p)\n print(f\"[!] Killing orphaned ngrok process (PID {pid})\")\n _kill_pid(pid)\n except Exception:\n # Fallback: try lsof\n try:\n result = subprocess.run(\n [\"lsof\", \"-t\", f\"-i:{api_port}\"],\n capture_output=True, text=True, timeout=5\n )\n for line in result.stdout.strip().split('\\n'):\n if line.strip():\n pid = int(line.strip())\n print(f\"[!] Killing orphaned ngrok process (PID {pid})\")\n _kill_pid(pid)\n except Exception:\n pass\n break # We found and handled our tunnel, stop scanning\n\n except (urllib.error.URLError, ConnectionRefusedError, OSError):\n continue # No ngrok API on this port, try next\n\n\ndef cleanup_existing_tunnels():\n \"\"\"\n Kill only this project's ngrok tunnel/process.\n Does NOT affect other ngrok tunnels running on this machine.\n \"\"\"\n print(\"[*] Cleaning up this project's ngrok tunnel...\")\n _kill_saved_pid()\n _find_and_kill_orphaned_ngrok()\n time.sleep(1)\n print(\"[✔] Cleanup complete.\")\n\n\ndef main():\n # Clean up before setting token and connecting\n cleanup_existing_tunnels()\n\n print(\"[*] Setting ngrok authtoken...\")\n ngrok.set_auth_token(TOKEN)\n\n print(f\"[*] Opening tunnel on port {PORT}...\")\n try:\n tunnel = ngrok.connect(PORT)\n print(\"\\n==============================================\\n\")\n print(f\"🚀 NGROK PUBLIC URL: {tunnel.public_url}\")\n print(\"\\n==============================================\\n\")\n\n # Save the ngrok process PID for future cleanup\n try:\n ngrok_process = ngrok.get_ngrok_process()\n ngrok_pid = ngrok_process.proc.pid\n with open(PID_FILE, \"w\") as f:\n f.write(str(ngrok_pid))\n print(f\"[*] Saved ngrok PID {ngrok_pid} to {PID_FILE}\")\n except Exception as pe:\n print(f\"[!] Could not save ngrok PID: {pe}\")\n\n sys.stdout.flush()\n\n while True:\n time.sleep(1)\n\n except PyngrokNgrokError as e:\n print(f\"[!] Ngrok error: {e}\")\n sys.stdout.flush()\n except Exception as e:\n print(f\"[!] Unexpected error: {e}\")\n sys.stdout.flush()\n\n\nif __name__ == \"__main__\":\n main()"}}}} |