Skip to content

Commit b073576

Browse files
philogicaehoh
authored andcommitted
Fixes after review
1 parent 219f047 commit b073576

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

src/aleph_client/commands/instance/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def validate_ssh_pubkey_file(file: Union[str, Path]) -> Path:
148148
choices=[ptype.value for ptype in PaymentType] + ["nft"],
149149
default=PaymentType.superfluid.value,
150150
)
151-
payment_type = PaymentType(payment_type) if payment_type != "nft" else PaymentType.hold
151+
payment_type = PaymentType.hold if payment_type == "nft" else PaymentType(payment_type)
152152
is_stream = payment_type != PaymentType.hold
153153

154154
super_token_chains = get_chains_with_super_token()
@@ -568,7 +568,7 @@ async def _show_instances(messages: List[InstanceMessage], node_list: NodeInfo):
568568
if info["confidential"]
569569
else Text.assemble("Type: ", Text("Regular", style="grey50"))
570570
)
571-
chain = Text.assemble("Chain: ", Text(info["chain"], style="cyan"))
571+
chain = Text.assemble("Chain: ", Text(str(info["chain"]), style="cyan"))
572572
instance = Text.assemble(
573573
"Item Hash ↓\t Name: ", name, "\n", item_hash_link, "\n", payment, " ", confidential, "\n", chain
574574
)
@@ -819,7 +819,9 @@ async def logs(
819819
log_data = json.loads(log)
820820
if "message" in log_data:
821821
echo(log_data["message"])
822-
except:
822+
except aiohttp.ClientConnectorError as e:
823+
echo(f"Unable to connect to domain: {domain}\nError: {e}")
824+
except aiohttp.ClientResponseError:
823825
echo(f"No VM associated with {vm_id} are currently running on {domain}")
824826

825827

src/aleph_client/commands/instance/network.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from urllib.parse import ParseResult, urlparse
88

99
import aiohttp
10-
from aiohttp import ClientConnectorError, ClientResponseError, ClientSession, InvalidURL
1110
from aleph.sdk import AlephHttpClient
1211
from aleph.sdk.conf import settings
1312
from aleph_message.models import InstanceMessage
@@ -52,16 +51,16 @@ def sanitize_url(url: str) -> str:
5251
Sanitized URL.
5352
"""
5453
if not url:
55-
raise InvalidURL("Empty URL")
54+
raise aiohttp.InvalidURL("Empty URL")
5655
parsed_url: ParseResult = urlparse(url)
5756
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}")
5958
if parsed_url.hostname in FORBIDDEN_HOSTS:
6059
logger.debug(
6160
f"Invalid URL {url} hostname {parsed_url.hostname} is in the forbidden host list "
6261
f"({', '.join(FORBIDDEN_HOSTS)})"
6362
)
64-
raise InvalidURL("Invalid URL host")
63+
raise aiohttp.InvalidURL("Invalid URL host")
6564
return url
6665

6766

@@ -90,7 +89,7 @@ async def fetch_crn_info(node_url: str) -> dict | None:
9089
system: dict = await resp.json()
9190
info["machine_usage"] = MachineUsage.parse_obj(system)
9291
return info
93-
except InvalidURL as e:
92+
except aiohttp.InvalidURL as e:
9493
logger.debug(f"Invalid CRN URL: {url}: {e}")
9594
except TimeoutError as e:
9695
logger.debug(f"Timeout while fetching CRN: {url}: {e}")
@@ -117,7 +116,7 @@ async def fetch_vm_info(message: InstanceMessage, node_list: NodeInfo) -> tuple[
117116
Returns:
118117
VM information.
119118
"""
120-
async with ClientSession() as session:
119+
async with aiohttp.ClientSession() as session:
121120
hold = not message.content.payment or message.content.payment.type == PaymentType["hold"]
122121
crn_hash = safe_getattr(message, "content.requirements.node.node_hash")
123122
firmware = safe_getattr(message, "content.environment.trusted_execution.firmware")
@@ -135,23 +134,20 @@ async def fetch_vm_info(message: InstanceMessage, node_list: NodeInfo) -> tuple[
135134
# Fetch from the scheduler API directly if no payment or no receiver (hold-tier non-confidential)
136135
if hold and not confidential:
137136
try:
137+
url = f"https://scheduler.api.aleph.cloud/api/v0/allocation/{message.item_hash}"
138138
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)
147142
info["ipv6_logs"] = allocation["vm_ipv6"]
148143
for node in nodes["nodes"]:
149144
if node["ipv6"].split("::")[0] == ":".join(str(info["ipv6_logs"]).split(":")[:4]):
150145
info["crn_url"] = node["url"].rstrip("/")
151146
return message.item_hash, info
152-
except Exception:
147+
except (aiohttp.ClientResponseError, aiohttp.ClientConnectorError) as e:
153148
info["ipv6_logs"] = help_strings.VM_SCHEDULED
154149
info["crn_url"] = help_strings.CRN_PENDING
150+
logger.debug(f"Error while calling Scheduler API ({url}): {e}")
155151
else:
156152
# Fetch from the CRN API if PAYG-tier or confidential
157153
info["allocation_type"] = help_strings.ALLOCATION_MANUAL
@@ -165,18 +161,15 @@ async def fetch_vm_info(message: InstanceMessage, node_list: NodeInfo) -> tuple[
165161
info["ipv6_logs"] = str(interface.ip + 1)
166162
return message.item_hash, info
167163
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:
169165
info["ipv6_logs"] = f"Not available. Server error: {e}"
170166
return message.item_hash, info
171167

172168

173169
async def find_crn_of_vm(vm_id: str) -> Optional[str]:
174170
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

src/aleph_client/commands/instance/superfluid.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async def update_flow(account: ETHAccount, receiver: str, flow: Decimal, update_
5757
# Reduce the existing flow
5858
new_flow_rate_wei = current_flow_rate_wei - flow_rate_wei
5959
# Ensure to not leave infinitesimal flows
60+
# Often, there were 1-10 wei remaining in the flow rate, which prevented the flow from being deleted
6061
if new_flow_rate_wei > 99:
6162
new_flow_rate_ether = from_wei(new_flow_rate_wei)
6263
return await account.update_flow(receiver, new_flow_rate_ether)

0 commit comments

Comments
 (0)