Files
nomad_mcp/deploy_nomad_mcp.py
2025-02-26 16:10:35 +07:00

171 lines
7.3 KiB
Python

#!/usr/bin/env python
"""
Script to deploy the Nomad MCP service using our own Nomad client.
"""
import os
import sys
import json
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...")
# Check if NOMAD_ADDR is configured
nomad_addr = os.getenv("NOMAD_ADDR")
if not nomad_addr:
print("Error: NOMAD_ADDR is not configured in .env file.")
sys.exit(1)
print(f"Connecting to Nomad at: {nomad_addr}")
try:
# 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.")
# Start the job
print("Registering and starting the nomad-mcp job...")
response = nomad_service.start_job(job_spec)
print("\nJob registration response:")
print(json.dumps(response, indent=2))
if response.get("status") == "started":
print("\n✅ Nomad MCP service deployed successfully!")
print(f"Job ID: {response.get('job_id')}")
print(f"Evaluation ID: {response.get('eval_id')}")
print("\nThe service will be available at: https://nomad_mcp.dev.meisheng.group")
else:
print("\n❌ Failed to deploy Nomad MCP service.")
print(f"Status: {response.get('status')}")
print(f"Message: {response.get('message', 'Unknown error')}")
except Exception as e:
print(f"Error deploying Nomad MCP service: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
main()