import paramiko import os host = "80.65.208.73" user = "root" password = "G7#Zp9!Qv@M2e$XwR8H^K" ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user, password=password, timeout=30) sftp = ssh.open_sftp() def sync_directory(local_dir, remote_dir): print(f"--- Syncing directory: {local_dir} -> {remote_dir} ---") count = 0 for root, dirs, files in os.walk(local_dir): # Ignore heavy build and dependency directories dirs[:] = [d for d in dirs if d not in ('node_modules', '.next', '__pycache__', '.git', '.venv', 'build', '.turbo', 'dist')] rel_path = os.path.relpath(root, local_dir) target_remote_dir = remote_dir if rel_path == '.' else os.path.join(remote_dir, rel_path).replace("\\", "/") try: sftp.stat(target_remote_dir) except IOError: ssh.exec_command(f"mkdir -p '{target_remote_dir}'") for file in files: if file.endswith('.pyc') or file.endswith('.DS_Store'): continue local_file = os.path.join(root, file) remote_file = os.path.join(target_remote_dir, file).replace("\\", "/") try: sftp.put(local_file, remote_file) count += 1 except Exception as e: print(f"Error uploading {local_file} -> {remote_file}: {e}") print(f"Uploaded {count} files for {local_dir}") def run_remote(cmd): print(f"Executing remote: {cmd}") stdin, stdout, stderr = ssh.exec_command(cmd) out = stdout.read().decode() err = stderr.read().decode() if out: print("STDOUT:", out.strip()) if err: print("STDERR:", err.strip()) return out, err base_dir = "/mnt/fam/SERVER/IfixKartEcommerce" # 1. Sync entire Backend codebase sync_directory(os.path.join(base_dir, "Backend/app"), "/var/www/fastapi/ifixkart/app") # 2. Sync entire Storefront codebase sync_directory(os.path.join(base_dir, "storefrontnewone/storefront"), "/var/www/html/ifixkart") # 3. Sync entire Admin ERP Dashboard codebase sync_directory(os.path.join(base_dir, "New admin panel/ikixkart-dashboard"), "/var/www/html/ifixkartdev") # 4. Trigger Remote Builds & Process Restarts print("--- Triggering Remote Storefront Build & PM2 Restart ---") run_remote("cd /var/www/html/ifixkart && npm run build && pm2 restart ifixkart-ecommerce") print("--- Triggering Remote Admin Dashboard Build & PM2 Restart ---") run_remote("cd /var/www/html/ifixkartdev && npm run build && pm2 restart ifixkart-admin") print("--- Running Database Column Migration on Remote Host ---") run_remote("cd /var/www/fastapi/ifixkart && PYTHONPATH=. venv/bin/python3 -c \"from app.core.database.db_session import SessionLocal; from sqlalchemy import text; db=SessionLocal(); db.execute(text('ALTER TABLE products ADD COLUMN show_specifications TINYINT(1) NOT NULL DEFAULT 1')); db.commit(); print('DB Column show_specifications added successfully!')\" || true") print("--- Restarting FastAPI Backend Service if applicable ---") run_remote("pkill -f 'uvicorn app.main:app' || true") sftp.close() ssh.close() print("Entire Codebase Deployment Complete!")