From acae88076ca9d777cb767fae5ff3687a462540b9 Mon Sep 17 00:00:00 2001 From: Nicolas Koehl Date: Wed, 26 Feb 2025 17:22:35 +0700 Subject: [PATCH] Enhance static directory handling and job deployment configuration --- NOMAD_JOB_MANAGEMENT_GUIDE.md | 700 +++++++++++++++++++++++++++ __pycache__/run.cpython-313.pyc | Bin 0 -> 902 bytes app/__pycache__/main.cpython-313.pyc | Bin 4254 -> 5741 bytes app/main.py | 74 ++- check_allocations.py | 33 ++ check_job_status.py | 48 ++ deploy_nomad_mcp.py | 224 ++++----- nomad_mcp_job.nomad | 56 +-- 8 files changed, 984 insertions(+), 151 deletions(-) create mode 100644 NOMAD_JOB_MANAGEMENT_GUIDE.md create mode 100644 __pycache__/run.cpython-313.pyc create mode 100644 check_allocations.py create mode 100644 check_job_status.py diff --git a/NOMAD_JOB_MANAGEMENT_GUIDE.md b/NOMAD_JOB_MANAGEMENT_GUIDE.md new file mode 100644 index 0000000..d62a351 --- /dev/null +++ b/NOMAD_JOB_MANAGEMENT_GUIDE.md @@ -0,0 +1,700 @@ +# Nomad Job Management Guide + +This guide explains the complete process of creating, deploying, monitoring, and troubleshooting Nomad jobs using the Nomad MCP service. It's designed to be used by both humans and AI assistants to effectively manage containerized applications in a Nomad cluster. + +## Prerequisites + +- Access to a Nomad cluster +- Nomad MCP service installed and running +- Proper environment configuration (NOMAD_ADDR, NOMAD_NAMESPACE, etc.) +- Python with required packages installed + +## 1. Creating a Nomad Job Specification + +A Nomad job specification defines how your application should run. This can be created in two formats: + +### Option A: Using a .nomad HCL File + +```hcl +job "your-job-name" { + datacenters = ["dc1"] + type = "service" + namespace = "development" + + group "app" { + count = 1 + + network { + port "http" { + to = 8000 + } + } + + task "app-task" { + driver = "docker" + + config { + image = "your-registry/your-image:tag" + ports = ["http"] + command = "python" + args = ["-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] + + # Mount volumes if needed + mount { + type = "bind" + source = "local/app-code" + target = "/app" + readonly = false + } + } + + # Pull code from Git repository if needed + artifact { + source = "git::ssh://git@your-git-server:port/org/repo.git" + destination = "local/app-code" + options { + ref = "main" + sshkey = "your-base64-encoded-ssh-key" + } + } + + env { + # Environment variables + PORT = "8000" + HOST = "0.0.0.0" + LOG_LEVEL = "INFO" + PYTHONPATH = "/app" + + # Add any application-specific environment variables + STATIC_DIR = "/local/app-code/static" + } + + resources { + cpu = 200 + memory = 256 + } + + service { + name = "your-service-name" + port = "http" + tags = [ + "traefik.enable=true", + "traefik.http.routers.your-service.entryPoints=https", + "traefik.http.routers.your-service.rule=Host(`your-service.domain.com`)" + ] + + check { + type = "http" + path = "/api/health" + interval = "10s" + timeout = "2s" + } + } + } + } +} +``` + +### Option B: Using a Python Deployment Script + +```python +#!/usr/bin/env python +import os +import json +from app.services.nomad_client import NomadService + +def main(): + # Initialize the Nomad service + nomad_service = NomadService() + + # Create job specification + job_spec = { + "Job": { + "ID": "your-job-name", + "Name": "your-job-name", + "Type": "service", + "Datacenters": ["dc1"], + "Namespace": "development", + "TaskGroups": [ + { + "Name": "app", + "Count": 1, + "Networks": [ + { + "DynamicPorts": [ + { + "Label": "http", + "To": 8000 + } + ] + } + ], + "Tasks": [ + { + "Name": "app-task", + "Driver": "docker", + "Config": { + "image": "your-registry/your-image:tag", + "ports": ["http"], + "command": "python", + "args": ["-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"], + "mount": [ + { + "type": "bind", + "source": "local/app-code", + "target": "/app", + "readonly": False + } + ] + }, + "Artifacts": [ + { + "GetterSource": "git::ssh://git@your-git-server:port/org/repo.git", + "RelativeDest": "local/app-code", + "GetterOptions": { + "ref": "main", + "sshkey": "your-base64-encoded-ssh-key" + } + } + ], + "Env": { + "PORT": "8000", + "HOST": "0.0.0.0", + "LOG_LEVEL": "INFO", + "PYTHONPATH": "/app", + "STATIC_DIR": "/local/app-code/static" + }, + "Resources": { + "CPU": 200, + "MemoryMB": 256 + }, + "Services": [ + { + "Name": "your-service-name", + "PortLabel": "http", + "Tags": [ + "traefik.enable=true", + "traefik.http.routers.your-service.entryPoints=https", + "traefik.http.routers.your-service.rule=Host(`your-service.domain.com`)" + ], + "Checks": [ + { + "Type": "http", + "Path": "/api/health", + "Interval": 10000000000, # 10 seconds in nanoseconds + "Timeout": 2000000000 # 2 seconds in nanoseconds + } + ] + } + ] + } + ] + } + ] + } + } + + # Start the job + response = nomad_service.start_job(job_spec) + + print(f"Job deployment response: {response}") + + if response.get("status") == "started": + print(f"✅ Job deployed successfully!") + print(f"Job ID: {response.get('job_id')}") + print(f"Evaluation ID: {response.get('eval_id')}") + else: + print(f"❌ Failed to deploy job.") + print(f"Status: {response.get('status')}") + print(f"Message: {response.get('message', 'Unknown error')}") + +if __name__ == "__main__": + main() +``` + +## 2. Deploying the Nomad Job + +### Option A: Using the Nomad CLI + +```bash +# Deploy using a .nomad file +nomad job run your-job-file.nomad + +# Verify the job was submitted +nomad job status your-job-name +``` + +### Option B: Using the Python Deployment Script + +```bash +# Run the deployment script +python deploy_your_job.py +``` + +### Option C: Using the Nomad MCP API + +```bash +# Using curl +curl -X POST http://localhost:8000/api/claude/create-job \ + -H "Content-Type: application/json" \ + -d '{ + "job_id": "your-job-name", + "name": "Your Job Name", + "type": "service", + "datacenters": ["dc1"], + "namespace": "development", + "docker_image": "your-registry/your-image:tag", + "count": 1, + "cpu": 200, + "memory": 256, + "ports": [ + { + "Label": "http", + "Value": 0, + "To": 8000 + } + ], + "env_vars": { + "PORT": "8000", + "HOST": "0.0.0.0", + "LOG_LEVEL": "INFO", + "PYTHONPATH": "/app", + "STATIC_DIR": "/local/app-code/static" + } + }' + +# Using PowerShell +Invoke-RestMethod -Uri "http://localhost:8000/api/claude/create-job" -Method POST -Headers @{"Content-Type"="application/json"} -Body '{ + "job_id": "your-job-name", + "name": "Your Job Name", + "type": "service", + "datacenters": ["dc1"], + "namespace": "development", + "docker_image": "your-registry/your-image:tag", + "count": 1, + "cpu": 200, + "memory": 256, + "ports": [ + { + "Label": "http", + "Value": 0, + "To": 8000 + } + ], + "env_vars": { + "PORT": "8000", + "HOST": "0.0.0.0", + "LOG_LEVEL": "INFO", + "PYTHONPATH": "/app", + "STATIC_DIR": "/local/app-code/static" + } +}' +``` + +## 3. Checking Job Status + +After deploying a job, you should check its status to ensure it's running correctly. + +### Option A: Using the Nomad CLI + +```bash +# Check job status +nomad job status your-job-name + +# Check allocations for the job +nomad job allocs your-job-name + +# Check the most recent allocation +nomad alloc status -job your-job-name +``` + +### Option B: Using the Nomad MCP API + +```bash +# Using curl +curl -X POST http://localhost:8000/api/claude/jobs \ + -H "Content-Type: application/json" \ + -d '{ + "job_id": "your-job-name", + "action": "status", + "namespace": "development" + }' + +# Using PowerShell +Invoke-RestMethod -Uri "http://localhost:8000/api/claude/jobs" -Method POST -Headers @{"Content-Type"="application/json"} -Body '{ + "job_id": "your-job-name", + "action": "status", + "namespace": "development" +}' +``` + +### Option C: Using a Python Script + +```python +#!/usr/bin/env python +from app.services.nomad_client import NomadService + +def main(): + # Initialize the Nomad service + service = NomadService() + + # Get job information + job = service.get_job('your-job-name') + print(f"Job Status: {job.get('Status', 'Unknown')}") + print(f"Job Type: {job.get('Type', 'Unknown')}") + print(f"Job Datacenters: {job.get('Datacenters', [])}") + + # Get allocations + allocations = service.get_allocations('your-job-name') + print(f"\nFound {len(allocations)} allocations") + + if allocations: + latest_alloc = allocations[0] + print(f"Latest allocation ID: {latest_alloc.get('ID', 'Unknown')}") + print(f"Allocation Status: {latest_alloc.get('ClientStatus', 'Unknown')}") + +if __name__ == "__main__": + main() +``` + +## 4. Checking Job Logs + +Logs are crucial for diagnosing issues with your job. Here's how to access them: + +### Option A: Using the Nomad CLI + +```bash +# First, get the allocation ID +nomad job allocs your-job-name + +# Then view the logs for a specific allocation +nomad alloc logs + +# View stderr logs +nomad alloc logs -stderr + +# Follow logs in real-time +nomad alloc logs -f +``` + +### Option B: Using the Nomad MCP API + +```bash +# Using curl +curl -X GET http://localhost:8000/api/claude/job-logs/your-job-name + +# Using PowerShell +Invoke-RestMethod -Uri "http://localhost:8000/api/claude/job-logs/your-job-name" -Method GET +``` + +### Option C: Using a Python Script + +```python +#!/usr/bin/env python +from app.services.nomad_client import NomadService + +def main(): + # Initialize the Nomad service + service = NomadService() + + # Get allocations for the job + allocations = service.get_allocations('your-job-name') + + if allocations: + latest_alloc = allocations[0] + alloc_id = latest_alloc["ID"] + print(f"Latest allocation ID: {alloc_id}") + + # Get logs for the allocation + try: + # Get stdout logs + stdout_logs = service.get_allocation_logs(alloc_id, task="your-task-name", log_type="stdout") + print("\nStandard Output Logs:") + print(stdout_logs) + + # Get stderr logs + stderr_logs = service.get_allocation_logs(alloc_id, task="your-task-name", log_type="stderr") + print("\nStandard Error Logs:") + print(stderr_logs) + except Exception as e: + print(f"Error getting logs: {str(e)}") + else: + print("No allocations found for your-job-name job") + +if __name__ == "__main__": + main() +``` + +## 5. Troubleshooting Common Issues + +### Issue: Job Fails to Start + +1. **Check the job status**: + ```bash + nomad job status your-job-name + ``` + +2. **Examine the allocation status**: + ```bash + nomad alloc status -job your-job-name + ``` + +3. **Check the logs for errors**: + ```bash + # Get the allocation ID first + nomad job allocs your-job-name + # Then check the logs + nomad alloc logs -stderr + ``` + +4. **Common errors and solutions**: + + a. **Missing static directory**: + ``` + RuntimeError: Directory 'static' does not exist + ``` + Solution: Add an environment variable to specify the static directory path: + ```hcl + env { + STATIC_DIR = "/local/app-code/static" + } + ``` + + b. **Invalid mount configuration**: + ``` + invalid mount config for type 'bind': bind source path does not exist + ``` + Solution: Ensure the source path exists or is created by an artifact: + ```hcl + artifact { + source = "git::ssh://git@your-git-server:port/org/repo.git" + destination = "local/app-code" + } + ``` + + c. **Port already allocated**: + ``` + Allocation failed: Failed to place allocation: failed to place alloc: port is already allocated + ``` + Solution: Use dynamic ports or choose a different port: + ```hcl + network { + port "http" { + to = 8000 + } + } + ``` + +### Issue: Application Errors After Deployment + +1. **Check application logs**: + ```bash + nomad alloc logs + ``` + +2. **Verify environment variables**: + ```bash + nomad alloc status + ``` + Look for the "Environment Variables" section. + +3. **Check resource constraints**: + Ensure the job has enough CPU and memory allocated: + ```hcl + resources { + cpu = 200 # Increase if needed + memory = 256 # Increase if needed + } + ``` + +## 6. Updating a Job + +After fixing issues, you'll need to update the job: + +### Option A: Using the Nomad CLI + +```bash +# Update the job with the modified specification +nomad job run your-updated-job-file.nomad +``` + +### Option B: Using the Nomad MCP API + +```bash +# Using PowerShell to restart a job +Invoke-RestMethod -Uri "http://localhost:8000/api/claude/jobs" -Method POST -Headers @{"Content-Type"="application/json"} -Body '{ + "job_id": "your-job-name", + "action": "restart", + "namespace": "development" +}' +``` + +### Option C: Using a Python Script + +```python +#!/usr/bin/env python +from app.services.nomad_client import NomadService + +def main(): + # Initialize the Nomad service + service = NomadService() + + # Get the current job specification + job = service.get_job('your-job-name') + + # Modify the job specification as needed + # For example, update environment variables: + job["TaskGroups"][0]["Tasks"][0]["Env"]["STATIC_DIR"] = "/local/app-code/static" + + # Update the job + response = service.start_job({"Job": job}) + + print(f"Job update response: {response}") + +if __name__ == "__main__": + main() +``` + +## 7. Stopping a Job + +When you're done with a job, you can stop it: + +### Option A: Using the Nomad CLI + +```bash +# Stop a job +nomad job stop your-job-name + +# Stop and purge a job +nomad job stop -purge your-job-name +``` + +### Option B: Using the Nomad MCP API + +```bash +# Using PowerShell +Invoke-RestMethod -Uri "http://localhost:8000/api/claude/jobs" -Method POST -Headers @{"Content-Type"="application/json"} -Body '{ + "job_id": "your-job-name", + "action": "stop", + "namespace": "development", + "purge": true +}' +``` + +### Option C: Using a Python Script + +```python +#!/usr/bin/env python +from app.services.nomad_client import NomadService + +def main(): + # Initialize the Nomad service + service = NomadService() + + # Stop the job + response = service.stop_job('your-job-name', purge=True) + + print(f"Job stop response: {response}") + +if __name__ == "__main__": + main() +``` + +## 8. Complete Workflow Example + +Here's a complete workflow for deploying, monitoring, troubleshooting, and updating a job: + +```python +#!/usr/bin/env python +import time +from app.services.nomad_client import NomadService + +def main(): + # Initialize the Nomad service + service = NomadService() + + # 1. Create and deploy the job + job_spec = { + "Job": { + "ID": "example-app", + "Name": "Example Application", + "Type": "service", + "Datacenters": ["dc1"], + "Namespace": "development", + # ... rest of job specification ... + } + } + + deploy_response = service.start_job(job_spec) + print(f"Deployment response: {deploy_response}") + + # 2. Wait for the job to be scheduled + print("Waiting for job to be scheduled...") + time.sleep(5) + + # 3. Check job status + job = service.get_job('example-app') + print(f"Job Status: {job.get('Status', 'Unknown')}") + + # 4. Get allocations + allocations = service.get_allocations('example-app') + + if allocations: + latest_alloc = allocations[0] + alloc_id = latest_alloc["ID"] + print(f"Latest allocation ID: {alloc_id}") + print(f"Allocation Status: {latest_alloc.get('ClientStatus', 'Unknown')}") + + # 5. Check logs for errors + stderr_logs = service.get_allocation_logs(alloc_id, log_type="stderr") + + # 6. Look for common errors + if "Directory 'static' does not exist" in stderr_logs: + print("Error detected: Missing static directory") + + # 7. Update the job to fix the issue + job["TaskGroups"][0]["Tasks"][0]["Env"]["STATIC_DIR"] = "/local/app-code/static" + update_response = service.start_job({"Job": job}) + print(f"Job update response: {update_response}") + + # 8. Wait for the updated job to be scheduled + print("Waiting for updated job to be scheduled...") + time.sleep(5) + + # 9. Check the updated job status + updated_job = service.get_job('example-app') + print(f"Updated Job Status: {updated_job.get('Status', 'Unknown')}") + else: + print("No allocations found for the job") + +if __name__ == "__main__": + main() +``` + +## 9. Best Practices + +1. **Always check logs after deployment**: Logs are your primary tool for diagnosing issues. + +2. **Use environment variables for configuration**: This makes your jobs more flexible and easier to update. + +3. **Implement health checks**: Health checks help Nomad determine if your application is running correctly. + +4. **Set appropriate resource limits**: Allocate enough CPU and memory for your application to run efficiently. + +5. **Use artifacts for code deployment**: Pull code from a Git repository to ensure consistency. + +6. **Implement proper error handling**: Your application should handle errors gracefully and provide meaningful error messages. + +7. **Use namespaces**: Organize your jobs into namespaces based on environment or team. + +8. **Document your job specifications**: Include comments in your job files to explain configuration choices. + +9. **Implement a CI/CD pipeline**: Automate the deployment process to reduce errors and improve efficiency. + +10. **Monitor job performance**: Use Nomad's monitoring capabilities to track resource usage and performance. + +## 10. Conclusion + +Managing Nomad jobs effectively requires understanding the job lifecycle, from creation to deployment, monitoring, troubleshooting, and updating. By following this guide, you can create robust deployment processes that ensure your applications run reliably in a Nomad cluster. + +Remember that the key to successful job management is thorough testing, careful monitoring, and quick response to issues. With the right tools and processes in place, you can efficiently manage even complex applications in a Nomad environment. \ No newline at end of file diff --git a/__pycache__/run.cpython-313.pyc b/__pycache__/run.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a8f5d1d91a6a93410146983662088a0d7dfb3f8b GIT binary patch literal 902 zcmZ`%&r2IY6rRb>?q+whu^t+l7G$w1q+*P}s0mWAHdULdA!}_93(Gdy#?Z~~GP|*u zlSo1FqL+fuf509JIp);AL5WbEQV=}#X30O$S!2*k-wZSF&HLW_-Uri`Wf4%*zy2sc z4*_r)1YP5Aoc0U^;0sWJ3UdH=Zipd`L%Gl^1~UrKn0_#I1E&!zgu6{{?|z^%Dw?7! zfniqT`u~q)Rj!MiqU_fd{D1rMYPgFo^mfp9&+cTZkXfM2f+nhB<{bdW7yz0?=aMuw z3ewGGCy$H&$6jlkEPNhJ#m~0F+VRN7vYu1j%>?wMAfux<>cJ3@3JCl2Z-E+S!uq<~a0Q3;Y>BRp4+dkJ{ literal 0 HcmV?d00001 diff --git a/app/__pycache__/main.cpython-313.pyc b/app/__pycache__/main.cpython-313.pyc index c0ec8b1b88ffc30078a3c3f298e4909492c1c0b5..50700bd8de9d052c4b881326a1e3ea3436ded252 100644 GIT binary patch delta 1670 zcmbVMU1-}@6uy#WOYvWR94Dzu(;GK`?vi+aA$68?bS+(+Fej7hI%&*Q#g-GTwqzvP zrD4qJQ|rB$%pGZAtON$_V?)Qfr?H1U>|rps4%CQH3VRz2(xvQeuq!)qO!6?8K)UDN z^PTUU`<;9BP0yc0&OL|23efZGA3rYt*0%2K!lt(duHiYR#{=Y^Xpa|+>TdMN-l%u5 zW&*Agfar1h!~JQ)z<8si7eu{G3v()fGrde-zy!f;Yt%yr8({k;(?5#UObDXKn1S(m z0JygRV449#%=F29(SAfm(V_L-b`LYi_(t&>+z68gqkS}uyzR_s=8fneN;?!FAXp}r z3E%MJH%-4c4aOSt+R?;>-aQpDeFYZlJ{m_X{S0O>iy#c@+dT^!kwkIR>?;0kc16R$ zfY+fO1f>jC+Gbb>BZG&56TE{Pkq{;71&oOy7;{58wIti5%YJzAWb*-;Vf`P>LaPyd zFfO(kwo%^Ukue!P@Dm~y##~_PJOCnoxB^|DtKs@C4SnKB_K$r3sgWg&AJPa{aENrW z0ob5#k+ZmSq){SjJLPLy{otY?#-X~xLq+9OAqL}u%*Rwoz5$nRz}N~OyC#UsFqKKF zLORLAw4^A)5;Brf4B;hF8PTlFrE`~p=h+KEdiCwFqT`Q{C1fcDc`=t2WJyf%q6)K| zEO3Zv9BP&teAVXX(rN$xq|>jbNcu%7#l_iFtnN5|QOcm2kEGz#ak%+LNW|{E(W=O$c#Y(ilq!;9*@TefSym&jbFzpO@R>ActwI)|S)J60w98e+WAz|Kob0<(Y*n_xQC*&~lT}7`_L7z47+5$SdHWt2IxcgbzJ+u~jLAt;8-XFL( zaDVvT@I&Ww&)8GX*be0^*z&eg$J8&~rK$N+`@%MLrIKqtQKC-lQlP1|Lb)qc&kkiR zka@CT&)e_N8mlevnk z^NX2RB#L?M1ZLZ<(9KZE72GDKE4Jo6v^$_-ejlzi)sLY@tf;A^JcV37Oe1vNS0|X- zS=BXHs@+VX4y!!s@wvGmI%e@S>XWL#YPS_t?|C94##F&1i6g~;;{tZN__<@wEbG77 z;xos~PpTSg)&uD^bCpx3^_s{`R)|TmsF@J0JdKuQR$n*C8Bre5r4R*7QHN^8Mp~oIxz`R~$QrwY)k5oTti;-ZkqBlcPeAw}Ll=w`1$?J@ow0`_%RR bPb+J|XTGf$E4GcPX}0}$w)*_W=vJCRRI!ii>ruODC=Et0Bw;1(ru@#pjmSiStGTq`#$t+4u zF3B&dyv34HlAEK+c#9=3F*kMcbs-l<^~o~A0W7ze@{1=I3P13^#hRO6npbj*y|gSd zIln0H7IP+$$CjR2lA2dmodqfML?%B+~Tmw%}*)KNwq7=0CGVt yEuOh~y@)5H%!d?421b^4&PL7-<<9B_5gS}C@>?F@XyCla;5pe)%z`Bys2BjWPgdyw diff --git a/app/main.py b/app/main.py index 638824d..4b00a47 100644 --- a/app/main.py +++ b/app/main.py @@ -92,8 +92,78 @@ async def health_check(): return health_status -# Mount static files -app.mount("/", StaticFiles(directory="static", html=True), name="static") +# Find the static directory +def find_static_directory(): + """Find the static directory by checking multiple possible locations.""" + logger.info("Starting static directory search...") + + # First check if STATIC_DIR environment variable is set + static_dir_env = os.getenv("STATIC_DIR") + if static_dir_env: + logger.info(f"STATIC_DIR environment variable found: '{static_dir_env}'") + if os.path.isdir(static_dir_env): + logger.info(f"✅ Confirmed '{static_dir_env}' exists and is a directory") + return static_dir_env + else: + logger.warning(f"❌ STATIC_DIR '{static_dir_env}' does not exist or is not a directory") + # List parent directory contents if possible + parent_dir = os.path.dirname(static_dir_env) + if os.path.exists(parent_dir): + logger.info(f"Contents of parent directory '{parent_dir}':") + try: + for item in os.listdir(parent_dir): + item_path = os.path.join(parent_dir, item) + item_type = "directory" if os.path.isdir(item_path) else "file" + logger.info(f" - {item} ({item_type})") + except Exception as e: + logger.error(f"Error listing parent directory: {str(e)}") + else: + logger.info("STATIC_DIR environment variable not set") + + # Possible locations for the static directory + possible_paths = [ + "static", # Local development + "/app/static", # Docker container + "/local/nomad_mcp/static", # Nomad with artifact + os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "static") # Relative to this file + ] + + logger.info(f"Checking {len(possible_paths)} possible static directory locations:") + + # Check each path and use the first one that exists + for path in possible_paths: + logger.info(f"Checking path: '{path}'") + if os.path.isdir(path): + logger.info(f"✅ Found valid static directory at: '{path}'") + return path + else: + logger.info(f"❌ Path '{path}' does not exist or is not a directory") + + # If no static directory is found, log a warning but don't fail + # This allows the API to still function even without the UI + logger.warning("No static directory found in any of the checked locations. UI will not be available.") + + # Try to create the static directory if STATIC_DIR is set + if static_dir_env: + try: + logger.info(f"Attempting to create static directory at '{static_dir_env}'") + os.makedirs(static_dir_env, exist_ok=True) + if os.path.isdir(static_dir_env): + logger.info(f"✅ Successfully created static directory at '{static_dir_env}'") + return static_dir_env + else: + logger.error(f"Failed to create static directory at '{static_dir_env}'") + except Exception as e: + logger.error(f"Error creating static directory: {str(e)}") + + return None + +# Mount static files if the directory exists +static_dir = find_static_directory() +if static_dir: + app.mount("/", StaticFiles(directory=static_dir, html=True), name="static") +else: + logger.warning("Static files not mounted. API endpoints will still function.") if __name__ == "__main__": import uvicorn diff --git a/check_allocations.py b/check_allocations.py new file mode 100644 index 0000000..2ee52f3 --- /dev/null +++ b/check_allocations.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +""" +Script to check allocations for the nomad-mcp job. +""" + +from app.services.nomad_client import NomadService + +def main(): + service = NomadService() + + # Get allocations for the job + allocations = service.get_allocations('nomad-mcp') + print(f'Found {len(allocations)} allocations') + + if allocations: + latest_alloc = allocations[0] + print(f'Latest allocation ID: {latest_alloc["ID"]}') + print(f'Status: {latest_alloc.get("ClientStatus", "Unknown")}') + + # Get logs for the allocation + try: + logs = service.get_allocation_logs(latest_alloc["ID"]) + print("\nAllocation Logs:") + print(logs.get("stdout", "No stdout logs available")) + print("\nError Logs:") + print(logs.get("stderr", "No stderr logs available")) + except Exception as e: + print(f"Error getting logs: {str(e)}") + else: + print("No allocations found for nomad-mcp job") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/check_job_status.py b/check_job_status.py new file mode 100644 index 0000000..ff09730 --- /dev/null +++ b/check_job_status.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +""" +Script to check the status of the Nomad MCP job and its allocations. +""" + +from app.services.nomad_client import NomadService + +def main(): + print("Checking Nomad MCP job status...") + + # Initialize the Nomad service + service = NomadService() + + # Get job information + job = service.get_job('nomad-mcp') + print(f"Job Status: {job.get('Status', 'Unknown')}") + print(f"Job Type: {job.get('Type', 'Unknown')}") + print(f"Job Datacenters: {job.get('Datacenters', [])}") + + # Get allocations + allocations = service.get_allocations('nomad-mcp') + print(f"\nFound {len(allocations)} allocations") + + if allocations: + latest_alloc = allocations[0] + print(f"Latest allocation ID: {latest_alloc.get('ID', 'Unknown')}") + print(f"Allocation Status: {latest_alloc.get('ClientStatus', 'Unknown')}") + + # Check for task states + task_states = latest_alloc.get('TaskStates', {}) + if task_states: + print("\nTask States:") + for task_name, state in task_states.items(): + print(f" - {task_name}: {state.get('State', 'Unknown')}") + + # Check for events + events = state.get('Events', []) + if events: + print(f" Events:") + for event in events[-3:]: # Show last 3 events + print(f" - {event.get('Time', 'Unknown')}: {event.get('Type', 'Unknown')} - {event.get('DisplayMessage', 'No message')}") + + # Check health endpoint + print("\nService should be available at: https://nomad_mcp.dev.meisheng.group") + print("You can check the health endpoint at: https://nomad_mcp.dev.meisheng.group/api/health") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/deploy_nomad_mcp.py b/deploy_nomad_mcp.py index c038f33..ad6fd1a 100644 --- a/deploy_nomad_mcp.py +++ b/deploy_nomad_mcp.py @@ -6,127 +6,13 @@ Script to deploy the Nomad MCP service using our own Nomad client. import os import sys import json +import subprocess from dotenv import load_dotenv from app.services.nomad_client import NomadService # Load environment variables from .env file load_dotenv() -def read_job_spec(file_path): - """Read the Nomad job specification from a file.""" - try: - with open(file_path, 'r') as f: - content = f.read() - - # Convert HCL to JSON (simplified approach) - # In a real scenario, you might want to use a proper HCL parser - # This is a very basic approach that assumes the job spec is valid - job_id = "nomad-mcp" - - # Create a basic job structure - job_spec = { - "ID": job_id, - "Name": job_id, - "Type": "service", - "Datacenters": ["jm"], - "Namespace": "development", - "TaskGroups": [ - { - "Name": "app", - "Count": 1, - "Networks": [ - { - "DynamicPorts": [ - { - "Label": "http", - "To": 8000 - } - ] - } - ], - "Tasks": [ - { - "Name": "nomad-mcp", - "Driver": "docker", - "Config": { - "image": "registry.dev.meisheng.group/nomad_mcp:20250226", - "ports": ["http"], - "command": "python", - "args": ["-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"], - "mount": [ - { - "type": "bind", - "source": "local/nomad_mcp", - "target": "/app", - "readonly": False - } - ] - }, - "Artifacts": [ - { - "source": "git::ssh://git@gitea.service.mesh:2222/Mei_Sheng_Textiles/nomad_mcp.git", - "destination": "local/nomad_mcp", - "options": { - "ref": "main", - "sshkey": "LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFBQUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUFNd0FBQUF0emMyZ3RaVwpReU5UVXhPUUFBQUNBY01oYkNPVXhFOHBYQ3d5UEh0ZFR4aThHU0pzNEdTNXZ6ZTR6Tm1ueUYvUUFBQUtCQm5RZi9RWjBICi93QUFBQXR6YzJndFpXUXlOVFV4T1FBQUFDQWNNaGJDT1V4RThwWEN3eVBIdGRUeGk4R1NKczRHUzV2emU0ek5tbnlGL1EKQUFBRURreWwzQlZlek9YUWZUNzZac0NkYTZPNTFnMExsb25EMEd6L2Y4SHh3dzRCd3lGc0k1VEVUeWxjTEREOGUxMVBHTAp3WkltemdaTG0vTjdqTTJhZklYOUFBQUFHR1JsY0d4dmVTQnJaWGtnWm05eUlHNXZiV0ZrWDIxamNBRUNBd1FGCi0tLS0tRU5EIE9QRU5TU0ggUFJJVkFURSBLRVktLS0tLQo=" - } - } - ], - "Env": { - "NOMAD_ADDR": "http://pjmldk01.ds.meisheng.group:4646", - "NOMAD_NAMESPACE": "development", - "NOMAD_SKIP_VERIFY": "true", - "PORT": "8000", - "HOST": "0.0.0.0", - "LOG_LEVEL": "INFO", - "RELOAD": "true", - "PYTHONPATH": "/app" - }, - "Resources": { - "CPU": 200, - "MemoryMB": 256 - }, - "Services": [ - { - "Name": "nomad-mcp", - "PortLabel": "http", - "Tags": [ - "traefik.enable=true", - "traefik.http.routers.nomad-mcp.entryPoints=https", - "traefik.http.routers.nomad-mcp.rule=Host(`nomad_mcp.dev.meisheng.group`)", - "traefik.http.routers.nomad-mcp.middlewares=proxyheaders@consulcatalog" - ], - "Checks": [ - { - "Type": "http", - "Path": "/api/health", - "Interval": 10000000000, - "Timeout": 2000000000, - "CheckRestart": { - "Limit": 3, - "Grace": 60000000000 - } - } - ] - } - ] - } - ] - } - ], - "Update": { - "MaxParallel": 1, - "MinHealthyTime": 30000000000, - "HealthyDeadline": 300000000000, - "AutoRevert": True - } - } - - return job_spec - except Exception as e: - print(f"Error reading job specification: {str(e)}") - sys.exit(1) - def main(): print("Deploying Nomad MCP service using our own Nomad client...") @@ -142,12 +28,112 @@ def main(): # Initialize the Nomad service nomad_service = NomadService() - # Read the job specification - job_spec = read_job_spec("nomad_mcp_job.nomad") - print("Job specification loaded successfully.") + # Use the HCL file directly with the Nomad CLI + print("Registering and starting the nomad-mcp job...") + + # Create a job specification directly + job_spec = { + "Job": { + "ID": "nomad-mcp", + "Name": "nomad-mcp", + "Type": "service", + "Datacenters": ["jm"], + "Namespace": "development", + "TaskGroups": [ + { + "Name": "app", + "Count": 1, + "Networks": [ + { + "DynamicPorts": [ + { + "Label": "http", + "To": 8000 + } + ] + } + ], + "Tasks": [ + { + "Name": "nomad-mcp", + "Driver": "docker", + "Config": { + "image": "registry.dev.meisheng.group/nomad_mcp:20250226", + "ports": ["http"], + "command": "python", + "args": ["-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"], + "mount": [ + { + "type": "bind", + "source": "local/nomad_mcp", + "target": "/app", + "readonly": False + } + ] + }, + "Artifacts": [ + { + "GetterSource": "git::ssh://git@gitea.service.mesh:2222/Mei_Sheng_Textiles/nomad_mcp.git", + "RelativeDest": "local/nomad_mcp", + "GetterOptions": { + "ref": "main", + "sshkey": "LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFBQUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUFNd0FBQUF0emMyZ3RaVwpReU5UVXhPUUFBQUNBY01oYkNPVXhFOHBYQ3d5UEh0ZFR4aThHU0pzNEdTNXZ6ZTR6Tm1ueUYvUUFBQUtCQm5RZi9RWjBICi93QUFBQXR6YzJndFpXUXlOVFV4T1FBQUFDQWNNaGJDT1V4RThwWEN3eVBIdGRUeGk4R1NKczRHUzV2emU0ek5tbnlGL1EKQUFBRURreWwzQlZlek9YUWZUNzZac0NkYTZPNTFnMExsb25EMEd6L2Y4SHh3dzRCd3lGc0k1VEVUeWxjTERJOGUxMVBHTAp3WkltemdaTG0vTjdqTTJhZklYOUFBQUFHR1JsY0d4dmVTQnJaWGtnWm05eUlHNXZiV0ZrWDIxamNBRUNBd1FGCi0tLS0tRU5EIE9QRU5TU0ggUFJJVkFURSBLRVktLS0tLQo=" + } + } + ], + "Env": { + "NOMAD_ADDR": "http://pjmldk01.ds.meisheng.group:4646", + "NOMAD_NAMESPACE": "development", + "NOMAD_SKIP_VERIFY": "true", + "PORT": "8000", + "HOST": "0.0.0.0", + "LOG_LEVEL": "INFO", + "RELOAD": "true", + "PYTHONPATH": "/app", + "STATIC_DIR": "/local/nomad_mcp/static" + }, + "Resources": { + "CPU": 200, + "MemoryMB": 256 + }, + "Services": [ + { + "Name": "nomad-mcp", + "PortLabel": "http", + "Tags": [ + "traefik.enable=true", + "traefik.http.routers.nomad-mcp.entryPoints=https", + "traefik.http.routers.nomad-mcp.rule=Host(`nomad_mcp.dev.meisheng.group`)", + "traefik.http.routers.nomad-mcp.middlewares=proxyheaders@consulcatalog" + ], + "Checks": [ + { + "Type": "http", + "Path": "/api/health", + "Interval": 10000000000, + "Timeout": 2000000000, + "CheckRestart": { + "Limit": 3, + "Grace": 60000000000 + } + } + ] + } + ] + } + ] + } + ], + "Update": { + "MaxParallel": 1, + "MinHealthyTime": 30000000000, + "HealthyDeadline": 300000000000, + "AutoRevert": True + } + } + } # Start the job - print("Registering and starting the nomad-mcp job...") response = nomad_service.start_job(job_spec) print("\nJob registration response:") diff --git a/nomad_mcp_job.nomad b/nomad_mcp_job.nomad index 0655c06..522ba4e 100644 --- a/nomad_mcp_job.nomad +++ b/nomad_mcp_job.nomad @@ -1,5 +1,5 @@ job "nomad-mcp" { - datacenters = ["jm"] + datacenters = ["dc1"] type = "service" namespace = "development" @@ -16,12 +16,11 @@ job "nomad-mcp" { driver = "docker" config { - image = "registry.dev.meisheng.group/nomad_mcp:20250226" + image = "python:3.11-slim" ports = ["http"] command = "python" args = ["-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] - # Mount the local directory containing the code mount { type = "bind" source = "local/nomad_mcp" @@ -30,34 +29,37 @@ job "nomad-mcp" { } } - # Pull code from Gitea artifact { - source = "git::ssh://git@gitea.service.mesh:2222/Mei_Sheng_Textiles/nomad_mcp.git" + source = "git::https://gitea.dev.meisheng.group/nkohl/nomad_mcp.git" destination = "local/nomad_mcp" - options { - ref = "main" # or whichever branch/tag you want to use - sshkey = "LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFBQUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUFNd0FBQUF0emMyZ3RaVwpReU5UVXhPUUFBQUNBY01oYkNPVXhFOHBYQ3d5UEh0ZFR4aThHU0pzNEdTNXZ6ZTR6Tm1ueUYvUUFBQUtCQm5RZi9RWjBICi93QUFBQXR6YzJndFpXUXlOVFV4T1FBQUFDQWNNaGJDT1V4RThwWEN3eVBIdGRUeGk4R1NKczRHUzV2emU0ek5tbnlGL1EKQUFBRURreWwzQlZlek9YUWZUNzZac0NkYTZPNTFnMExsb25EMEd6L2Y4SHh3dzRCd3lGc0k1VEVUeWxjTEREOGUxMVBHTAp3WkltemdaTG0vTjdqTTJhZklYOUFBQUFHR1JsY0d4dmVTQnJaWGtnWm05eUlHNXZiV0ZrWDIxamNBRUNBd1FGCi0tLS0tRU5EIE9QRU5TU0ggUFJJVkFURSBLRVktLS0tLQo=" - } } env { - # Nomad connection settings NOMAD_ADDR = "http://pjmldk01.ds.meisheng.group:4646" NOMAD_NAMESPACE = "development" - NOMAD_SKIP_VERIFY = "true" - - # API settings - PORT = "8000" - HOST = "0.0.0.0" - - # Logging level - LOG_LEVEL = "INFO" - - # Enable to make development easier - RELOAD = "true" - - # Set PYTHONPATH to include the app directory + LOG_LEVEL = "DEBUG" PYTHONPATH = "/app" + STATIC_DIR = "/local/nomad_mcp/static" + } + + # Add a template to create the static directory if it doesn't exist + template { + data = <