Update routers/deploy.py
Browse files- routers/deploy.py +0 -251
routers/deploy.py
CHANGED
|
@@ -18,11 +18,6 @@ router = APIRouter()
|
|
| 18 |
|
| 19 |
deployed_projects = {}
|
| 20 |
|
| 21 |
-
|
| 22 |
-
GITHUB_WEBHOOK_SECRET = os.getenv("GITHUB_WEBHOOK_SECRET", "your_github_webhook_secret_here_CHANGE_THIS")
|
| 23 |
-
if GITHUB_WEBHOOK_SECRET == "your_github_webhook_secret_here_CHANGE_THIS":
|
| 24 |
-
print("WARNING: GITHUB_WEBHOOK_SECRET is not set. Webhook security is compromised.")
|
| 25 |
-
|
| 26 |
# --- Helper Functions ---
|
| 27 |
|
| 28 |
# Function to recursively find a file (case-insensitive) within a directory
|
|
@@ -39,147 +34,6 @@ def _find_file_in_project(filename: str, root_dir: str) -> str | None:
|
|
| 39 |
return None
|
| 40 |
|
| 41 |
# Function to build and deploy a Docker container from a project path
|
| 42 |
-
async def _build_and_deploy(project_id: str, project_path: str, app_name: str, existing_container_name: str = None):
|
| 43 |
-
"""
|
| 44 |
-
Handles the Docker build and deployment process for a given project.
|
| 45 |
-
If an existing_container_name is provided, it attempts to stop and remove it first.
|
| 46 |
-
Manages ngrok tunnels for the deployed application.
|
| 47 |
-
"""
|
| 48 |
-
docker_client = docker.from_env()
|
| 49 |
-
|
| 50 |
-
# Define consistent naming for Docker image and container
|
| 51 |
-
image_name = f"{app_name.lower()}_{project_id[:8]}"
|
| 52 |
-
container_name = f"{image_name}_container"
|
| 53 |
-
|
| 54 |
-
try:
|
| 55 |
-
# Step 1: Clean up old containers and images if they exist
|
| 56 |
-
# Stop and remove the previously deployed container for this project
|
| 57 |
-
if existing_container_name:
|
| 58 |
-
print(f"Attempting to stop and remove existing container: {existing_container_name}")
|
| 59 |
-
try:
|
| 60 |
-
old_container = docker_client.containers.get(existing_container_name)
|
| 61 |
-
old_container.stop(timeout=5) # Give 5 seconds to stop gracefully
|
| 62 |
-
old_container.remove(force=True)
|
| 63 |
-
print(f"Successfully stopped and removed old container: {existing_container_name}")
|
| 64 |
-
except docker.errors.NotFound:
|
| 65 |
-
print(f"Existing container {existing_container_name} not found, proceeding with new deployment.")
|
| 66 |
-
except Exception as e:
|
| 67 |
-
print(f"Error stopping/removing old container {existing_container_name}: {e}")
|
| 68 |
-
|
| 69 |
-
# Remove any exited or created containers that might be lingering from previous runs
|
| 70 |
-
# (This is a general cleanup, not specific to this project_id, but good practice)
|
| 71 |
-
for c in docker_client.containers.list(all=True):
|
| 72 |
-
if c.status in ["created", "exited"]: # Only remove non-running containers
|
| 73 |
-
# Be cautious: only remove containers clearly associated with this deployment logic
|
| 74 |
-
# For more robust logic, might check labels or names more strictly
|
| 75 |
-
if c.name.startswith(f"{app_name.lower()}_{project_id[:8]}") or c.name.startswith(f"ngrok-"):
|
| 76 |
-
print(f"Removing leftover container {c.name} ({c.id}) with status {c.status}")
|
| 77 |
-
try:
|
| 78 |
-
c.remove(force=True)
|
| 79 |
-
except Exception as e:
|
| 80 |
-
print(f"Error removing leftover container {c.name}: {e}")
|
| 81 |
-
|
| 82 |
-
# Step 2: Build Docker image
|
| 83 |
-
print(f"Building Docker image from {project_path} with tag: {image_name}")
|
| 84 |
-
image, build_logs_generator = docker_client.images.build(path=project_path, tag=image_name, rm=True)
|
| 85 |
-
# Process build logs (can be streamed to UI in a real application)
|
| 86 |
-
for log_line in build_logs_generator:
|
| 87 |
-
if 'stream' in log_line:
|
| 88 |
-
print(f"[BUILD LOG] {log_line['stream'].strip()}")
|
| 89 |
-
elif 'error' in log_line:
|
| 90 |
-
print(f"[BUILD ERROR] {log_line['error'].strip()}")
|
| 91 |
-
|
| 92 |
-
print(f"Docker image built successfully: {image.id}")
|
| 93 |
-
|
| 94 |
-
# Step 3: Run new Docker container
|
| 95 |
-
print(f"Running new container {container_name} from image {image_name}")
|
| 96 |
-
container = docker_client.containers.run(
|
| 97 |
-
image=image_name,
|
| 98 |
-
ports={"8080/tcp": None}, # Docker will assign a random host port for 8080/tcp
|
| 99 |
-
name=container_name,
|
| 100 |
-
detach=True, # Run in background
|
| 101 |
-
mem_limit="512m", # Limit memory usage
|
| 102 |
-
nano_cpus=1_000_000_000, # Limit CPU usage to 1 full core (1 billion nano-CPUs)
|
| 103 |
-
read_only=True, # Make container filesystem read-only (except tmpfs)
|
| 104 |
-
tmpfs={"/tmp": ""}, # Mount an in-memory tmpfs for /tmp directory
|
| 105 |
-
user="1001:1001" # Run as a non-root user (important for security)
|
| 106 |
-
)
|
| 107 |
-
print(f"Container started with ID: {container.id}")
|
| 108 |
-
|
| 109 |
-
# Wait a moment for the container to fully start and expose its port
|
| 110 |
-
time.sleep(5) # Increased sleep to give the application within the container more time
|
| 111 |
-
|
| 112 |
-
# Retrieve the dynamically assigned host port for the container's 8080 port
|
| 113 |
-
port_info = docker_client.api.port(container.id, 8080)
|
| 114 |
-
if not port_info:
|
| 115 |
-
# If port 8080 is not exposed, the container likely failed to start or is not exposing correctly
|
| 116 |
-
print(f"Error: Port 8080 not exposed by container {container.id}. Inspecting container logs...")
|
| 117 |
-
try:
|
| 118 |
-
container_logs = container.logs().decode('utf-8')
|
| 119 |
-
print(f"Container logs:\n{container_logs}")
|
| 120 |
-
except Exception as log_e:
|
| 121 |
-
print(f"Could not retrieve container logs: {log_e}")
|
| 122 |
-
container.stop()
|
| 123 |
-
container.remove(force=True)
|
| 124 |
-
raise Exception("Port 8080 not exposed by container or container failed to start correctly. Check container logs.")
|
| 125 |
-
|
| 126 |
-
host_port = port_info[0]['HostPort']
|
| 127 |
-
print(f"Container {container.id} is accessible on host port: {host_port}")
|
| 128 |
-
|
| 129 |
-
# Step 4: Manage ngrok tunnel
|
| 130 |
-
# Check if an ngrok tunnel already exists for this project and close it
|
| 131 |
-
if project_id in deployed_projects and deployed_projects[project_id].get('ngrok_tunnel'):
|
| 132 |
-
existing_tunnel = deployed_projects[project_id]['ngrok_tunnel']
|
| 133 |
-
print(f"Closing existing ngrok tunnel: {existing_tunnel.public_url}")
|
| 134 |
-
try:
|
| 135 |
-
existing_tunnel.disconnect()
|
| 136 |
-
except Exception as ngrok_disconnect_e:
|
| 137 |
-
print(f"Error disconnecting existing ngrok tunnel: {ngrok_disconnect_e}")
|
| 138 |
-
deployed_projects[project_id]['ngrok_tunnel'] = None # Clear the reference
|
| 139 |
-
|
| 140 |
-
# Connect a new ngrok tunnel to the dynamically assigned host port
|
| 141 |
-
print(f"Connecting new ngrok tunnel to host port {host_port}")
|
| 142 |
-
tunnel = ngrok.connect(host_port, bind_tls=True) # bind_tls=True for HTTPS
|
| 143 |
-
public_url = tunnel.public_url
|
| 144 |
-
print(f"Ngrok public URL for {app_name}: {public_url}")
|
| 145 |
-
|
| 146 |
-
# Step 5: Update global state with new deployment details
|
| 147 |
-
# Ensure the project_id exists in deployed_projects before updating
|
| 148 |
-
if project_id not in deployed_projects:
|
| 149 |
-
deployed_projects[project_id] = {} # Initialize if not already present (should be by deploy_from_git)
|
| 150 |
-
|
| 151 |
-
deployed_projects[project_id].update({
|
| 152 |
-
"container_id": container.id,
|
| 153 |
-
"container_name": container_name,
|
| 154 |
-
"ngrok_tunnel": tunnel,
|
| 155 |
-
"public_url": public_url,
|
| 156 |
-
"status": "deployed" # Set status to deployed on success
|
| 157 |
-
})
|
| 158 |
-
|
| 159 |
-
return public_url, container_name
|
| 160 |
-
|
| 161 |
-
except docker.errors.BuildError as e:
|
| 162 |
-
print(f"Docker build error: {e}")
|
| 163 |
-
# Capture and return detailed build logs for better debugging
|
| 164 |
-
build_logs_str = "\n".join([str(log_line.get('stream', '')).strip() for log_line in e.build_log if 'stream' in log_line])
|
| 165 |
-
if project_id in deployed_projects:
|
| 166 |
-
deployed_projects[project_id]["status"] = "failed"
|
| 167 |
-
raise HTTPException(status_code=500, detail=f"Docker build failed: {e.msg}\nLogs:\n{build_logs_str}")
|
| 168 |
-
except docker.errors.ContainerError as e:
|
| 169 |
-
print(f"Docker container runtime error: {e}")
|
| 170 |
-
if project_id in deployed_projects:
|
| 171 |
-
deployed_projects[project_id]["status"] = "failed"
|
| 172 |
-
raise HTTPException(status_code=500, detail=f"Container failed during runtime: {e.stderr.decode()}")
|
| 173 |
-
except docker.errors.APIError as e:
|
| 174 |
-
print(f"Docker API error: {e}")
|
| 175 |
-
if project_id in deployed_projects:
|
| 176 |
-
deployed_projects[project_id]["status"] = "failed"
|
| 177 |
-
raise HTTPException(status_code=500, detail=f"Docker daemon or API error: {e.explanation}")
|
| 178 |
-
except Exception as e:
|
| 179 |
-
print(f"General deployment error: {e}")
|
| 180 |
-
if project_id in deployed_projects:
|
| 181 |
-
deployed_projects[project_id]["status"] = "failed"
|
| 182 |
-
raise HTTPException(status_code=500, detail=f"Deployment process failed unexpectedly: {str(e)}")
|
| 183 |
|
| 184 |
# --- API Endpoints ---
|
| 185 |
|
|
@@ -287,111 +141,6 @@ async def deploy_from_git(repo_url: str = Form(...), app_name: str = Form(...)):
|
|
| 287 |
print(f"Error during initial _build_and_deploy for project {project_id}: {e}")
|
| 288 |
raise HTTPException(status_code=500, detail=f"Initial deployment failed unexpectedly: {str(e)}")
|
| 289 |
|
| 290 |
-
@router.post("/webhook/github")
|
| 291 |
-
async def github_webhook(request: Request, background_tasks: BackgroundTasks):
|
| 292 |
-
"""
|
| 293 |
-
Endpoint to receive GitHub webhook events (e.g., push events) and trigger redeployments.
|
| 294 |
-
"""
|
| 295 |
-
# --- Security: Verify GitHub Webhook Signature ---
|
| 296 |
-
# This is CRUCIAL to ensure the webhook is from GitHub and hasn't been tampered with.
|
| 297 |
-
# For production, DO NOT comment this out.
|
| 298 |
-
signature_header = request.headers.get("X-Hub-Signature-256")
|
| 299 |
-
if not signature_header:
|
| 300 |
-
raise HTTPException(status_code=403, detail="X-Hub-Signature-256 header missing.")
|
| 301 |
-
|
| 302 |
-
# Read the raw request body once to use for hashing
|
| 303 |
-
body = await request.body()
|
| 304 |
-
|
| 305 |
-
try:
|
| 306 |
-
# Calculate expected signature
|
| 307 |
-
sha_name, signature = signature_header.split("=", 1)
|
| 308 |
-
if sha_name != "sha256":
|
| 309 |
-
raise HTTPException(status_code=400, detail="Invalid X-Hub-Signature-256 algorithm. Only sha256 supported.")
|
| 310 |
-
|
| 311 |
-
# Use HMAC-SHA256 with your secret key to hash the raw request body
|
| 312 |
-
# Ensure the secret is encoded to bytes
|
| 313 |
-
mac = hmac.new(GITHUB_WEBHOOK_SECRET.encode("utf-8"), body, hashlib.sha256)
|
| 314 |
-
|
| 315 |
-
# Compare the calculated hash with the signature received from GitHub
|
| 316 |
-
if not hmac.compare_digest(mac.hexdigest(), signature):
|
| 317 |
-
raise HTTPException(status_code=403, detail="Invalid GitHub signature.")
|
| 318 |
-
except Exception as e:
|
| 319 |
-
print(f"Webhook signature verification failed: {e}")
|
| 320 |
-
raise HTTPException(status_code=403, detail="Signature verification failed.")
|
| 321 |
-
|
| 322 |
-
# Parse the JSON payload from the webhook
|
| 323 |
-
payload = await request.json()
|
| 324 |
-
github_event = request.headers.get("X-GitHub-Event")
|
| 325 |
-
|
| 326 |
-
print(f"Received GitHub '{github_event}' webhook for repository: {payload.get('repository', {}).get('full_name')}")
|
| 327 |
-
|
| 328 |
-
# Process only 'push' events
|
| 329 |
-
if github_event != "push":
|
| 330 |
-
return JSONResponse({"message": f"Received '{github_event}' event, but only 'push' events are processed."}, status_code=200)
|
| 331 |
-
|
| 332 |
-
# Get the repository URL from the webhook payload
|
| 333 |
-
repo_url_from_webhook = payload.get("repository", {}).get("html_url") # Prefer html_url or clone_url
|
| 334 |
-
if not repo_url_from_webhook:
|
| 335 |
-
raise HTTPException(status_code=400, detail="Repository URL not found in webhook payload.")
|
| 336 |
-
|
| 337 |
-
# Find the project linked to this repository in our in-memory storage
|
| 338 |
-
project_to_redeploy = None
|
| 339 |
-
project_id_to_redeploy = None
|
| 340 |
-
for project_id, project_data in deployed_projects.items():
|
| 341 |
-
# Match based on repo_url. A more robust solution might normalize URLs or use repository IDs.
|
| 342 |
-
if project_data.get("repo_url") == repo_url_from_webhook:
|
| 343 |
-
project_to_redeploy = project_data
|
| 344 |
-
project_id_to_redeploy = project_id
|
| 345 |
-
break
|
| 346 |
-
|
| 347 |
-
if not project_to_redeploy:
|
| 348 |
-
print(f"No active project found for repository: {repo_url_from_webhook}. Webhook ignored.")
|
| 349 |
-
return JSONResponse({"message": "No associated project found for this repository, ignoring webhook."}, status_code=200)
|
| 350 |
-
|
| 351 |
-
print(f"Received push for {repo_url_from_webhook}. Triggering redeployment for project {project_id_to_redeploy} ({project_to_redeploy['app_name']}).")
|
| 352 |
-
|
| 353 |
-
# Step 1: Pull the latest changes from the Git repository
|
| 354 |
-
project_path = project_to_redeploy["project_path"]
|
| 355 |
-
try:
|
| 356 |
-
repo = git.Repo(project_path)
|
| 357 |
-
origin = repo.remotes.origin
|
| 358 |
-
print(f"Pulling latest changes for {repo_url_from_webhook} into {project_path}")
|
| 359 |
-
origin.pull() # Pull the latest changes from the remote
|
| 360 |
-
print("Latest changes pulled successfully.")
|
| 361 |
-
except git.exc.GitCommandError as e:
|
| 362 |
-
print(f"Failed to pull latest changes for {repo_url_from_webhook}: {e.stderr.decode()}")
|
| 363 |
-
# Update project status to failed if pull fails
|
| 364 |
-
deployed_projects[project_id_to_redeploy]["status"] = "failed"
|
| 365 |
-
return JSONResponse({"error": f"Failed to pull latest changes: {e.stderr.decode()}"}, status_code=500)
|
| 366 |
-
except Exception as e:
|
| 367 |
-
print(f"Unexpected error during git pull: {e}")
|
| 368 |
-
deployed_projects[project_id_to_redeploy]["status"] = "failed"
|
| 369 |
-
return JSONResponse({"error": f"An unexpected error occurred during git pull: {str(e)}"}, status_code=500)
|
| 370 |
-
|
| 371 |
-
# Step 2: Trigger redeployment in a background task
|
| 372 |
-
# Using FastAPI's BackgroundTasks ensures the webhook endpoint returns immediately,
|
| 373 |
-
# preventing timeouts for GitHub, while the redeployment happens asynchronously.
|
| 374 |
-
|
| 375 |
-
# Get the current container name for proper cleanup in _build_and_deploy
|
| 376 |
-
current_container_name = project_to_redeploy.get("container_name")
|
| 377 |
-
|
| 378 |
-
# Add the build and deploy task to background tasks
|
| 379 |
-
background_tasks.add_task(
|
| 380 |
-
_build_and_deploy,
|
| 381 |
-
project_id_to_redeploy,
|
| 382 |
-
project_path,
|
| 383 |
-
project_to_redeploy["app_name"],
|
| 384 |
-
current_container_name # Pass existing container name for cleanup
|
| 385 |
-
)
|
| 386 |
-
|
| 387 |
-
# Update project status to indicate redeployment is in progress
|
| 388 |
-
deployed_projects[project_id_to_redeploy]["status"] = "redeploying"
|
| 389 |
-
|
| 390 |
-
return JSONResponse(
|
| 391 |
-
{"message": f"Redeployment for project {project_id_to_redeploy} initiated from GitHub webhook."},
|
| 392 |
-
background=background_tasks,
|
| 393 |
-
status_code=202 # 202 Accepted: request has been accepted for processing
|
| 394 |
-
)
|
| 395 |
|
| 396 |
# --- Cleanup Endpoint (Optional, for manual testing/management) ---
|
| 397 |
@router.post("/project/delete/{project_id}")
|
|
|
|
| 18 |
|
| 19 |
deployed_projects = {}
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# --- Helper Functions ---
|
| 22 |
|
| 23 |
# Function to recursively find a file (case-insensitive) within a directory
|
|
|
|
| 34 |
return None
|
| 35 |
|
| 36 |
# Function to build and deploy a Docker container from a project path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# --- API Endpoints ---
|
| 39 |
|
|
|
|
| 141 |
print(f"Error during initial _build_and_deploy for project {project_id}: {e}")
|
| 142 |
raise HTTPException(status_code=500, detail=f"Initial deployment failed unexpectedly: {str(e)}")
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
# --- Cleanup Endpoint (Optional, for manual testing/management) ---
|
| 146 |
@router.post("/project/delete/{project_id}")
|