7
7
from urllib .parse import ParseResult , urlparse
8
8
9
9
import aiohttp
10
- from aiohttp import ClientConnectorError , ClientResponseError , ClientSession , InvalidURL
11
10
from aleph .sdk import AlephHttpClient
12
11
from aleph .sdk .conf import settings
13
12
from aleph_message .models import InstanceMessage
@@ -52,16 +51,16 @@ def sanitize_url(url: str) -> str:
52
51
Sanitized URL.
53
52
"""
54
53
if not url :
55
- raise InvalidURL ("Empty URL" )
54
+ raise aiohttp . InvalidURL ("Empty URL" )
56
55
parsed_url : ParseResult = urlparse (url )
57
56
if parsed_url .scheme not in ["http" , "https" ]:
58
- raise InvalidURL (f"Invalid URL scheme: { parsed_url .scheme } " )
57
+ raise aiohttp . InvalidURL (f"Invalid URL scheme: { parsed_url .scheme } " )
59
58
if parsed_url .hostname in FORBIDDEN_HOSTS :
60
59
logger .debug (
61
60
f"Invalid URL { url } hostname { parsed_url .hostname } is in the forbidden host list "
62
61
f"({ ', ' .join (FORBIDDEN_HOSTS )} )"
63
62
)
64
- raise InvalidURL ("Invalid URL host" )
63
+ raise aiohttp . InvalidURL ("Invalid URL host" )
65
64
return url
66
65
67
66
@@ -90,7 +89,7 @@ async def fetch_crn_info(node_url: str) -> dict | None:
90
89
system : dict = await resp .json ()
91
90
info ["machine_usage" ] = MachineUsage .parse_obj (system )
92
91
return info
93
- except InvalidURL as e :
92
+ except aiohttp . InvalidURL as e :
94
93
logger .debug (f"Invalid CRN URL: { url } : { e } " )
95
94
except TimeoutError as e :
96
95
logger .debug (f"Timeout while fetching CRN: { url } : { e } " )
@@ -117,7 +116,7 @@ async def fetch_vm_info(message: InstanceMessage, node_list: NodeInfo) -> tuple[
117
116
Returns:
118
117
VM information.
119
118
"""
120
- async with ClientSession () as session :
119
+ async with aiohttp . ClientSession () as session :
121
120
hold = not message .content .payment or message .content .payment .type == PaymentType ["hold" ]
122
121
crn_hash = safe_getattr (message , "content.requirements.node.node_hash" )
123
122
firmware = safe_getattr (message , "content.environment.trusted_execution.firmware" )
@@ -135,23 +134,20 @@ async def fetch_vm_info(message: InstanceMessage, node_list: NodeInfo) -> tuple[
135
134
# Fetch from the scheduler API directly if no payment or no receiver (hold-tier non-confidential)
136
135
if hold and not confidential :
137
136
try :
137
+ url = f"https://scheduler.api.aleph.cloud/api/v0/allocation/{ message .item_hash } "
138
138
info ["allocation_type" ] = help_strings .ALLOCATION_AUTO
139
- allocation = await fetch_json (
140
- session ,
141
- f"https://scheduler.api.aleph.cloud/api/v0/allocation/{ message .item_hash } " ,
142
- )
143
- nodes = await fetch_json (
144
- session ,
145
- "https://scheduler.api.aleph.cloud/api/v0/nodes" ,
146
- )
139
+ allocation = await fetch_json (session , url )
140
+ url = "https://scheduler.api.aleph.cloud/api/v0/nodes"
141
+ nodes = await fetch_json (session , url )
147
142
info ["ipv6_logs" ] = allocation ["vm_ipv6" ]
148
143
for node in nodes ["nodes" ]:
149
144
if node ["ipv6" ].split ("::" )[0 ] == ":" .join (str (info ["ipv6_logs" ]).split (":" )[:4 ]):
150
145
info ["crn_url" ] = node ["url" ].rstrip ("/" )
151
146
return message .item_hash , info
152
- except Exception :
147
+ except ( aiohttp . ClientResponseError , aiohttp . ClientConnectorError ) as e :
153
148
info ["ipv6_logs" ] = help_strings .VM_SCHEDULED
154
149
info ["crn_url" ] = help_strings .CRN_PENDING
150
+ logger .debug (f"Error while calling Scheduler API ({ url } ): { e } " )
155
151
else :
156
152
# Fetch from the CRN API if PAYG-tier or confidential
157
153
info ["allocation_type" ] = help_strings .ALLOCATION_MANUAL
@@ -165,18 +161,15 @@ async def fetch_vm_info(message: InstanceMessage, node_list: NodeInfo) -> tuple[
165
161
info ["ipv6_logs" ] = str (interface .ip + 1 )
166
162
return message .item_hash , info
167
163
info ["ipv6_logs" ] = help_strings .VM_NOT_READY if confidential else help_strings .VM_NOT_AVAILABLE_YET
168
- except (ClientResponseError , ClientConnectorError ) as e :
164
+ except (aiohttp . ClientResponseError , aiohttp . ClientConnectorError ) as e :
169
165
info ["ipv6_logs" ] = f"Not available. Server error: { e } "
170
166
return message .item_hash , info
171
167
172
168
173
169
async def find_crn_of_vm (vm_id : str ) -> Optional [str ]:
174
170
async with AlephHttpClient (api_server = settings .API_HOST ) as client :
175
- try :
176
- message : InstanceMessage = await client .get_message (item_hash = ItemHash (vm_id ), message_type = InstanceMessage )
177
- node_list : NodeInfo = await _fetch_nodes ()
178
- _ , info = await fetch_vm_info (message , node_list )
179
- return str (info ["crn_url" ])
180
- except Exception :
181
- pass
182
- return None
171
+ message : InstanceMessage = await client .get_message (item_hash = ItemHash (vm_id ), message_type = InstanceMessage )
172
+ node_list : NodeInfo = await _fetch_nodes ()
173
+ _ , info = await fetch_vm_info (message , node_list )
174
+ is_valid = info ["crn_url" ] and info ["crn_url" ] != help_strings .CRN_PENDING
175
+ return str (info ["crn_url" ]) if is_valid else None
0 commit comments