ai_api / app.py
Yogesh
initial deploy
cd8bd0a
Raw
History Blame Contribute Delete
2.12 kB
import os
import subprocess
import sys
import urllib.request
import tarfile
print("=== Starting OmniRoute Installation ===")
# Try to check Node.js version, and download v22.13.0 if missing or older
try:
node_version = subprocess.check_output(["node", "--version"], stderr=subprocess.DEVNULL).decode().strip()
print(f"Detected system Node.js version: {node_version}")
major_version = int(node_version.split(".")[0].replace("v", ""))
except Exception:
node_version = None
major_version = 0
if major_version < 22:
print("Node.js version is older than v22 or missing. Downloading Node.js v22.13.0 (x64)...")
node_url = "https://nodejs.org/dist/v22.13.0/node-v22.13.0-linux-x64.tar.xz"
tar_path = "/tmp/node.tar.xz"
node_dir = os.path.abspath("node_runtime")
if not os.path.exists(node_dir):
print(f"Downloading from {node_url}...")
urllib.request.urlretrieve(node_url, tar_path)
print("Extracting Node.js binary...")
os.makedirs(node_dir, exist_ok=True)
subprocess.run(["tar", "-xJf", tar_path, "-C", node_dir, "--strip-components=1"], check=True)
print("Extraction complete!")
# Configure PATH to use the custom Node.js binaries
node_bin = os.path.join(node_dir, "bin")
os.environ["PATH"] = f"{node_bin}:{os.environ.get('PATH', '')}"
print(f"Updated PATH: {os.environ['PATH']}")
# Verify the updated version
new_version = subprocess.check_output(["node", "--version"]).decode().strip()
print(f"New configured Node.js version: {new_version}")
# 1. Install Node.js dependencies
print("Installing npm packages...")
subprocess.run(["npm", "install"], check=True)
# 2. Build the Next.js production build
print("Building Next.js application...")
subprocess.run(["npm", "run", "build"], check=True)
# 3. Configure environment variables for port 7860
os.environ["PORT"] = "7860"
os.environ["DATA_DIR"] = os.path.abspath("data")
# 4. Start the OmniRoute server
print("Starting OmniRoute server on port 7860...")
subprocess.run(["node", "scripts/dev/run-standalone.mjs"], check=True)