Skip to content

Commit

Permalink
feat(deployment): Clean backend IP fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Dec 12, 2024
1 parent 8fdf14a commit 9a05e51
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
3 changes: 2 additions & 1 deletion backend/src/interfaces/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ class GetAgentSecretMessage(BaseModel):


class GetAgentResponse(PublicAgentData):
pass
instance_ip: str | None


class GetAgentSecretResponse(BaseModel):
secret: str


class AgentPythonPackageManager(str, Enum):
poetry = "poetry"
pip = "pip"
9 changes: 0 additions & 9 deletions backend/src/interfaces/aleph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,3 @@ class AlephVolume(BaseModel):
mount: str
ref: str
use_latest: bool


class AlephNodeInfo:
def __init__(self, **kwargs):
self.data = kwargs.get("data", {})
self.nodes = self.data.get("corechannel", {}).get("resource_nodes", [])
self.nodes.sort(key=lambda x: x.get("score", 0), reverse=True)
self.core_node = self.data.get("corechannel", {}).get("nodes", [])
self.core_node.sort(key=lambda x: x.get("score", 0), reverse=True)
11 changes: 8 additions & 3 deletions backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,15 @@ async def get_agent_public_info(agent_id: str) -> GetAgentResponse:
)
agent = agents[0]

try:
ip_address = await fetch_instance_ip(agent.instance_hash)
except ValueError:
ip_address = None

return GetAgentResponse(
id=agent.id,
instance_hash=agent.instance_hash,
instance_ip=ip_address,
last_update=agent.last_update,
subscription_id=agent.subscription_id,
)
Expand Down Expand Up @@ -205,11 +211,10 @@ async def update(

try:
hostname = await fetch_instance_ip(agent.instance_hash)
print(hostname)
except ValueError:
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="Instance IPv6 not found, it's probably not allocated yet.",
detail="Instance IPv6 address not found, it probably isn't allocated yet. Please try again in a few minutes.",
)

# Create a Paramiko SSH client
Expand All @@ -233,7 +238,7 @@ async def update(

# Execute a command
_stdin, stdout, stderr = ssh_client.exec_command(
f"wget {deploy_script_url} -O /tmp/deploy-agent.sh -q --no-cached && chmod +x /tmp/deploy-agent.sh && /tmp/deploy-agent.sh {python_version} {package_manager.value}"
f"wget {deploy_script_url} -O /tmp/deploy-agent.sh -q --no-cache && chmod +x /tmp/deploy-agent.sh && /tmp/deploy-agent.sh {python_version} {package_manager.value}"
)

output = stdout.read().decode("utf-8")
Expand Down
6 changes: 5 additions & 1 deletion backend/src/utils/aleph.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ async def fetch_instance_ip(item_hash) -> str:
resp.raise_for_status()
allocation = await resp.json()
return allocation["vm_ipv6"]
except (aiohttp.ClientResponseError, aiohttp.ClientConnectorError):
except (
aiohttp.ClientResponseError,
aiohttp.ClientConnectorError,
aiohttp.ConnectionTimeoutError,
):
raise ValueError()

0 comments on commit 9a05e51

Please sign in to comment.