Skip to content

Commit f0f0592

Browse files
committed
Report console host from payload
1 parent d76c024 commit f0f0592

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

firecracker.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import sys
6060
from pathlib import Path
6161
from typing import Any, Dict, Optional, Union
62+
from urllib.parse import urlparse
6263

6364
import requests
6465
from requests.auth import HTTPBasicAuth
@@ -136,13 +137,15 @@ def __init__(
136137
verify: Union[bool, str] = True,
137138
auth: Optional[HTTPBasicAuth] = None,
138139
cert: Optional[Union[str, tuple]] = None,
140+
console_host: Optional[str] = None,
139141
) -> None:
140142
self.base_url = base_url
141143
self.token = token
142144
self.timeout = int(timeout or 30)
143145
self.verify = verify
144146
self.auth = auth
145147
self.cert = cert
148+
self.console_host = console_host or ""
146149

147150
class Ctx(object):
148151
"""Execution context parsed from JSON + CLI timeout (Python 3.6 compatible)."""
@@ -181,14 +184,11 @@ def _to_ctx(config_path: str, timeout: int):
181184
token = token.strip() or None
182185

183186
# If the provided URL already includes an explicit port, don't append another
184-
if "://" in url:
185-
# Extract the authority part after scheme:// and check for ':' in host:port
186-
authority = url.split("://", 1)[1]
187-
has_explicit_port = ":" in authority.split("/")[0]
188-
else:
189-
has_explicit_port = False
190-
# If no scheme, assume http for completeness
187+
if "://" not in url:
191188
url = f"http://{url}"
189+
parsed = urlparse(url)
190+
authority = parsed.netloc or ""
191+
has_explicit_port = ":" in authority
192192

193193
base_url = f"{url}/v1" if has_explicit_port else f"{url}:{port}/v1"
194194

@@ -273,7 +273,8 @@ def _to_ctx(config_path: str, timeout: int):
273273
elif isinstance(client_key, str) and client_key:
274274
_fail("TLS client key provided but client certificate missing")
275275

276-
agent = Agent(base_url, token, int(timeout or 30), verify=verify, auth=auth, cert=cert)
276+
console_host = (host.get("console_host") or parsed.hostname or "").strip()
277+
agent = Agent(base_url, token, int(timeout or 30), verify=verify, auth=auth, cert=cert, console_host=console_host)
277278
return Ctx(data, vm_name, agent)
278279

279280
# -------------------------- HTTP helpers --------------------------
@@ -366,6 +367,10 @@ def op_console(ctx):
366367
"""POST /v1/vms/{name}/console — obtain VNC bridge connection info."""
367368
url = f"{ctx.agent.base_url}/vms/{ctx.vm_name}/console"
368369
data = _json_or_fail(_req("POST", url, ctx.agent))
370+
console_host = getattr(ctx.agent, "console_host", "") or ""
371+
resp_host = data.get("host")
372+
if console_host and (not resp_host or resp_host in {"0.0.0.0", "127.0.0.1", "::"}):
373+
data["host"] = console_host
369374
_ok(data)
370375

371376
# -------------------------- main --------------------------

0 commit comments

Comments
 (0)