Skip to content

feat(ibm): improve ibm vpc baremetal behaviour #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2024
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1!7.1.0
1!7.2.0
53 changes: 47 additions & 6 deletions pycloudlib/ibm/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pycloudlib.ibm._util import get_first as _get_first
from pycloudlib.ibm._util import iter_resources as _iter_resources
from pycloudlib.ibm._util import wait_until as _wait_until
from pycloudlib.ibm.errors import IBMException
from pycloudlib.ibm.errors import IBMCapacityException, IBMException
from pycloudlib.instance import BaseInstance

if TYPE_CHECKING:
Expand Down Expand Up @@ -399,7 +399,9 @@ def execute_instance_action(
return client.create_instance_action(id, action.value, force=force)
if self == self.BARE_METAL_SERVER:
if action == _Action.STOP:
return client.stop_bare_metal_server(id)
return client.stop_bare_metal_server(
id, type="hard" if force else "soft"
)
if action == _Action.START:
return client.start_bare_metal_server(id)
if action == _Action.REBOOT:
Expand Down Expand Up @@ -709,12 +711,22 @@ def _refresh_instance(self) -> dict:
self._instance = self._get_instance(self.id).get_result()
return self._instance

def _wait_for_status(self, status: _Status, sleep_seconds: int = 300):
def _wait_for_status(
self,
status: _Status,
sleep_seconds: int = 300,
side_effect_fn=None,
):
def check_status_and_do_side_effect():
if side_effect_fn:
side_effect_fn()
return self._refresh_instance()["status"] == status.value

_wait_until(
lambda: self._refresh_instance()["status"] == status.value,
check_status_and_do_side_effect,
timeout_seconds=sleep_seconds,
timeout_msg_fn=lambda: (
"Expected {status.value} state, but found"
f"Expected {status.value} state, but found"
f" {self._instance['status']} "
f"after waiting {sleep_seconds} seconds. "
"Check IBM VPC console for more details."
Expand Down Expand Up @@ -746,9 +758,38 @@ def start(self, wait=True):
if wait:
self.wait()

def _check_instance_failed_status(self) -> None:
"""
Check if the instance failed to start and raise an exception if so.

Raises:
IBMCapacityException: If the instance failed to start due to
capacity issues.
IBMException: If the instance failed to start due to other reasons.
"""
if self._instance["status"] == _Status.FAILED.value:
if any(
"capacity" in reason["code"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a link pointing for docs covering this? If so, would you mind adding it for reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any docs for this. I discovered this by manually launching and inspecting instance status via the API/SDK.

for reason in self._instance["status_reasons"]
):
raise IBMCapacityException(
f"Out of capacity! Instance {self.id} failed to start: "
f"{self._instance['status_reasons'][0]['message']}"
)
# Raise generic IBM exception if not capacity related
raise IBMException(
f"Instance {self.id} failed to start: "
f"{self._instance['status_reasons'][0]['message']}"
)

def _wait_for_instance_start(self, **kwargs):
"""Wait for the cloud instance to be up."""
self._wait_for_status(_Status.RUNNING)
# if self.b
self._wait_for_status(
_Status.RUNNING,
sleep_seconds=900,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good for now. But, the 900 should be configurable from a consumer POV, as it might be not flexible enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll look at adding this sometime soon. Thanks!!

side_effect_fn=self._check_instance_failed_status,
)

def wait_for_delete(self, sleep_seconds=30, raise_on_fail=False):
"""Wait for instance to be deleted."""
Expand Down
Loading