77from typing import Any , Callable , Coroutine , Literal , Union
88
99import aiohttp .web_exceptions
10+ import pydantic
1011from aiohttp import web
1112from aiohttp .web_urldispatcher import UrlMappingMatchInfo
1213from aleph_message .exceptions import UnknownHashError
1819from pydantic .main import BaseModel
1920
2021from aleph .vm .models import VmExecution
22+ from aleph .vm .orchestrator .run import create_vm_execution
2123from aleph .vm .pool import VmPool
2224
2325logger = logging .getLogger (__name__ )
@@ -28,7 +30,7 @@ def is_token_still_valid(timestamp):
2830 Checks if a token has exprired based on its timestamp
2931 """
3032 current_datetime = datetime .now (tz = timezone .utc )
31- target_datetime = datetime .fromisoformat (timestamp , tz = timezone . utc )
33+ target_datetime = datetime .fromisoformat (timestamp )
3234
3335 return target_datetime > current_datetime
3436
@@ -51,7 +53,7 @@ class SignedPubKeyPayload(BaseModel):
5153 # alg: Literal["ECDSA"]
5254 domain : str
5355 address : str
54- expires : str
56+ expires : str
5557
5658 @property
5759 def json_web_key (self ) -> Jwk :
@@ -219,7 +221,6 @@ def get_execution_or_404(ref: ItemHash, pool: VmPool) -> VmExecution:
219221
220222@require_jwk_authentication
221223async def stream_logs (request : web .Request , authenticated_sender : str ) -> web .StreamResponse :
222- # TODO: Add user authentication
223224 vm_hash = get_itemhash_or_400 (request .match_info )
224225 pool : VmPool = request .app ["vm_pool" ]
225226 execution = get_execution_or_404 (vm_hash , pool = pool )
@@ -260,7 +261,6 @@ async def operate_expire(request: web.Request, authenticated_sender: str) -> web
260261 """Stop the virtual machine, smoothly if possible.
261262
262263 A timeout may be specified to delay the action."""
263- # TODO: Add user authentication
264264 vm_hash = get_itemhash_or_400 (request .match_info )
265265 timeout = float (ItemHash (request .match_info ["timeout" ]))
266266 if not 0 < timeout < timedelta (days = 10 ).total_seconds ():
@@ -317,9 +317,14 @@ async def operate_reboot(request: web.Request, authenticated_sender: str) -> web
317317 logger .debug (f"Unauthorized sender { authenticated_sender } for { vm_hash } " )
318318 return web .Response (status = 401 , body = "Unauthorized sender" )
319319
320- # TODO: implement this endpoint
321- logger .info (f"Rebooting { execution .vm_hash } " )
322- return web .Response (status = 200 , body = f"Rebooted { execution .vm_hash } " )
320+ if execution .is_running :
321+ logger .info (f"Rebooting { execution .vm_hash } " )
322+ await pool .stop_vm (vm_hash )
323+ pool .forget_vm (vm_hash )
324+ await create_vm_execution (vm_hash = vm_hash , pool = pool )
325+ return web .Response (status = 200 , body = f"Rebooted VM with ref { vm_hash } " )
326+ else :
327+ return web .Response (status = 200 , body = "Starting VM (was not running) with ref {vm_hash}" )
323328
324329
325330@require_jwk_authentication
0 commit comments