-
Notifications
You must be signed in to change notification settings - Fork 34
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1!7.1.0 | ||
1!7.2.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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: | ||
|
@@ -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." | ||
|
@@ -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"] | ||
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 | ||
a-dubs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._wait_for_status( | ||
_Status.RUNNING, | ||
sleep_seconds=900, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good for now. But, the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.""" | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.