Add connect endpoint for computes

Param to connect to compute after creation
Report compute unauthorized HTTP errors to client
This commit is contained in:
grossmj committed 2021-12-24 13:05:39 +10:30
1 parent 36cf43475d
commit 10fdd8fcf4
10 files changed
+55 -36

No files matched your search

+14 -9
View File
@@ -74,10 +74,6 @@ class Controller:
if host == "0.0.0.0":
host = "127.0.0.1"
name = socket.gethostname()
if name == "gns3vm":
name = "Main server"
self._load_controller_settings()
if server_config.enable_ssl:
@@ -93,7 +89,7 @@ class Controller:
try:
self._local_server = await self.add_compute(
compute_id="local",
name=name,
name=f"{socket.gethostname()} (controller)",
protocol=protocol,
host=host,
console_host=console_host,
@@ -102,6 +98,7 @@ class Controller:
password=server_config.compute_password,
force=True,
connect=True,
wait_connection=False,
ssl_context=self._ssl_context,
)
except ControllerError:
@@ -113,7 +110,12 @@ class Controller:
if computes:
for c in computes:
try:
await self.add_compute(**c, connect=False)
#FIXME: Task exception was never retrieved
await self.add_compute(
compute_id=str(c.compute_id),
connect=False,
**c.dict(exclude_unset=True, exclude={"compute_id", "created_at", "updated_at"}),
)
except (ControllerError, KeyError):
pass # Skip not available servers at loading
@@ -341,7 +343,7 @@ class Controller:
os.makedirs(configs_path, exist_ok=True)
return configs_path
async def add_compute(self, compute_id=None, name=None, force=False, connect=True, **kwargs):
async def add_compute(self, compute_id=None, name=None, force=False, connect=True, wait_connection=True, **kwargs):
"""
Add a server to the dictionary of computes controlled by this controller
@@ -371,8 +373,11 @@ class Controller:
self._computes[compute.id] = compute
# self.save()
if connect:
# call compute.connect() later to give time to the controller to be fully started
asyncio.get_event_loop().call_later(1, lambda: asyncio.ensure_future(compute.connect()))
if wait_connection:
await compute.connect()
else:
# call compute.connect() later to give time to the controller to be fully started
asyncio.get_event_loop().call_later(1, lambda: asyncio.ensure_future(compute.connect()))
self.notification.controller_emit("compute.created", compute.asdict())
return compute
else:
+6 -3
View File
@@ -154,6 +154,7 @@ class Compute:
return self._interfaces_cache
async def update(self, **kwargs):
for kw in kwargs:
if kw not in ("user", "password"):
setattr(self, kw, kwargs[kw])
@@ -373,7 +374,7 @@ class Compute:
pass
@locking
async def connect(self):
async def connect(self, report_failed_connection=False):
"""
Check if remote server is accessible
"""
@@ -383,6 +384,8 @@ class Compute:
log.info(f"Connecting to compute '{self._id}'")
response = await self._run_http_query("GET", "/capabilities")
except ComputeError as e:
if report_failed_connection:
raise
log.warning(f"Cannot connect to compute '{self._id}': {e}")
# Try to reconnect after 5 seconds if server unavailable only if not during tests (otherwise we create a ressource usage bomb)
if not hasattr(sys, "_called_from_test") or not sys._called_from_test:
@@ -491,7 +494,7 @@ class Compute:
# Try to reconnect after 1 second if server unavailable only if not during tests (otherwise we create a ressources usage bomb)
from gns3server.api.server import app
if not app.state.exiting and not hasattr(sys, "_called_from_test"):
log.info(f"Reconnecting to to compute '{self._id}' WebSocket '{ws_url}'")
log.info(f"Reconnecting to compute '{self._id}' WebSocket '{ws_url}'")
asyncio.get_event_loop().call_later(1, lambda: asyncio.ensure_future(self.connect()))
self._cpu_usage_percent = None
@@ -572,7 +575,7 @@ class Compute:
msg = ""
if response.status == 401:
raise ControllerUnauthorizedError(f"Invalid authentication for compute {self.id}")
raise ControllerUnauthorizedError(f"Invalid authentication for compute '{self.name}' [{self.id}]")
elif response.status == 403:
raise ControllerForbiddenError(msg)
elif response.status == 404: