Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions press/frappe_compute_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def provision_virtual_machine(
},
)

def check_machine_availability(self, machine_type, instance_id: str | None = None):
return self.client.get_api(
"orchestrator.api.utils.check_machine_availability",
{"machine_type": machine_type, "instance_id": instance_id},
)

def sync_virtual_machine(self, instance_id: str):
return self.client.get_api("orchestrator.api.virtual_machine.sync", {"instance_id": instance_id})

Expand Down
16 changes: 15 additions & 1 deletion press/press/doctype/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,16 @@ def get_hetzner_client(self):
api_token = self.get_password("hetzner_api_token")
return Client(token=api_token)

def _check_frappe_compute_machine_availability(
self, machine_type: str | list, instance_id: str | None = None
):
api_secret = self.get_password("frappe_compute_api_secret")

client = FrappeComputeClient(
url=self.frappe_compute_base_url, api_key=self.frappe_compute_api_key, api_secret=api_secret
)
return client.check_machine_availability(machine_type, instance_id=instance_id)

def _check_aws_machine_availability(self, machine_type: str | list) -> bool | dict[str, bool]:
"""Check if instance offering in the region is present"""
client = self.get_aws_client()
Expand Down Expand Up @@ -1282,14 +1292,18 @@ def _check_hetzner_machine_availability(self, machine_type: str | list) -> bool
return results

@frappe.whitelist()
def check_machine_availability(self, machine_type: str | list) -> bool | dict[str, bool]:
def check_machine_availability(
self, machine_type: str | list, instance_id: str | None = None
) -> bool | dict[str, bool]:
"Check availability of machine in the region before allowing provision"
if self.cloud_provider == "AWS EC2":
return self._check_aws_machine_availability(machine_type)
if self.cloud_provider == "OCI":
return self._check_oci_machine_availability(machine_type)
if self.cloud_provider == "Hetzner":
return self._check_hetzner_machine_availability(machine_type)
if self.cloud_provider == "Frappe Compute":
return self._check_frappe_compute_machine_availability(machine_type, instance_id)

return True

Expand Down
3 changes: 2 additions & 1 deletion press/press/doctype/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,8 @@ def can_change_plan( # noqa: C901
)

cluster: Cluster = frappe.get_doc("Cluster", self.cluster)
if not cluster.check_machine_availability(new_plan.instance_type):
instance_id = frappe.db.get_value("Virtual Machine", self.virtual_machine, "instance_id")
if not cluster.check_machine_availability(new_plan.instance_type, instance_id):
frappe.throw(
f"Cannot change plan right now since the instance type {new_plan.instance_type} is not available. Try again later."
)
Expand Down
Loading