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
134 changes: 103 additions & 31 deletions sima_cli/cloudex/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def _authorize_supported_host():
authorize_admin()


def _authorize_tunnel_cleanup(session):
if disconnect_requires_admin(session):
console.print("[cyan]→[/cyan] Authorizing local tunnel cleanup")
_authorize_supported_host()


class StageProgress:
"""A compact spinner for terminals and readable milestones for logs."""

Expand Down Expand Up @@ -323,10 +329,25 @@ def _interactive_session_selection_available():
return sys.stdin.isatty()


def _choose_sessions(sessions):
"""Select one or more local connection records without probing them."""
def _select_checkboxes(choices, *, message, instruction, mandatory=True):
from InquirerPy import inquirer

return set(inquirer.checkbox(
message=message,
choices=choices,
instruction=instruction,
mandatory=mandatory,
enabled_symbol="[x]",
disabled_symbol="[ ]",
pointer="❯",
transformer=lambda selected: (
"{} selected".format(len(selected)) if selected else "None selected"
),
).execute() or [])


def _choose_sessions(sessions):
"""Select one or more local connection records without probing them."""
choices = []
for session in sessions:
device = session.get("device", "DevKit " + _short(session.get("allocation_id")))
Expand All @@ -337,18 +358,11 @@ def _choose_sessions(sessions):
"name": "{} · {} · {} · {}".format(device, session_label, target, expiry),
"value": session["session_id"],
})
selected_ids = inquirer.checkbox(
selected = _select_checkboxes(
choices,
message="Select CloudEx connections to disconnect:",
choices=choices,
instruction="Space to select, Enter to disconnect selected connections",
enabled_symbol="[x]",
disabled_symbol="[ ]",
pointer="❯",
transformer=lambda selected: (
"{} selected".format(len(selected)) if selected else "None selected"
),
).execute() or []
selected = set(selected_ids)
)
return [session for session in sessions if session["session_id"] in selected]


Expand Down Expand Up @@ -382,9 +396,7 @@ def disconnect_command(session_id, disconnect_all):
for session in selected:
label = session.get("device", "DevKit " + _short(session.get("allocation_id")))
try:
if disconnect_requires_admin(session):
console.print("[cyan]→[/cyan] Authorizing local tunnel cleanup")
_authorize_supported_host()
_authorize_tunnel_cleanup(session)
with StageProgress("Disconnecting {}".format(label)) as progress:
result = disconnect_session(session, store, progress, authorize=False)
if result == "stale":
Expand Down Expand Up @@ -614,29 +626,89 @@ def request_command(device, cluster, duration, environment, verbose):
raise click.ClickException(str(exc)) from exc


def _choose_allocations_to_release(api):
inventory = api.call("GET", "/devkits")
allocations = inventory.get("allocations", [])
if not allocations:
console.print("You have no CloudEx allocations to release.")
return []

devices = {(d["cluster_id"], d["id"]): d.get("name") for d in inventory.get("devices", [])}
choices = []
for allocation in allocations:
target = allocation["target_id"]
name = devices.get((allocation["cluster_id"], target)) or target
choices.append({
"name": "{} · {} · {}".format(name, _short(allocation["session_id"]), allocation["status"]),
"value": allocation["session_id"],
})
selected = _select_checkboxes(
choices,
message="Select CloudEx allocations to release:",
instruction="Space to select, Enter to release selected allocations",
mandatory=False,
)
if not selected:
console.print("[dim]No CloudEx allocations selected.[/dim]")
return [allocation["session_id"] for allocation in allocations if allocation["session_id"] in selected]


def _release_allocation(allocation_id, store, get_api):
failures = []
local_cleanup_blocked = False
for session in store.list():
if session.get("allocation_id") == allocation_id:
try:
# Rich redirects terminal output during progress rendering.
# Ask for sudo before starting the spinner, as disconnect does.
_authorize_tunnel_cleanup(session)
with StageProgress("Disconnecting CloudEx") as progress:
disconnect_session(session, store, progress, authorize=False)
except CloudExError as exc:
failures.append("Tunnel cleanup failed: {}. Local retry state retained; retry sima-cli -i cloudex disconnect.".format(exc))
local_cleanup_blocked |= disconnect_requires_admin(session)
if local_cleanup_blocked:
failures.append("Lease release skipped because its local CloudEx tunnel is still running.")
return failures
try:
get_api().call("DELETE", "/devkits/allocations/" + allocation_id)
except CloudExError as exc:
failures.append("Lease release failed: {}".format(exc))
else:
console.print("Lease released; device availability returns after cleanup is confirmed.")
return failures


@cloudex_group.command("release")
@click.argument("allocation_id")
@click.argument("allocation_id", required=False)
@click.option("--environment", type=click.Choice(["production", "staging"]), default="production", show_default=True, help="Vulcan environment (requires -i).")
@_vulcan_only
def release_command(allocation_id, environment):
"""Release your NeatOps lease and disconnect its local CloudEx sessions."""
"""Disconnect local CloudEx sessions and release their NeatOps allocations.

Omit ALLOCATION_ID in an interactive terminal to select allocations.
"""
try:
allocation_id = _allocation_id(allocation_id)
api = None
if allocation_id is None:
if not _interactive_session_selection_available():
raise click.UsageError("Provide an allocation ID when running without an interactive terminal. "
"Use sima-cli -i cloudex devices to find your allocation IDs.")
api = _neatops_api(environment)
allocation_ids = _choose_allocations_to_release(api)
if not allocation_ids:
return
else:
allocation_ids = [allocation_id]
allocation_ids = [_allocation_id(value) for value in allocation_ids]
store = _store()
failures = []
for session in store.list():
if session.get("allocation_id") == allocation_id:
try:
with StageProgress("Disconnecting CloudEx") as progress:
disconnect_session(session, store, progress)
except CloudExError as exc:
failures.append("Tunnel cleanup failed: {}. Local retry state retained; retry sima-cli -i cloudex disconnect.".format(exc))
try:
_neatops_api(environment).call("DELETE", "/devkits/allocations/" + allocation_id)
except CloudExError as exc:
failures.append("Lease release failed: {}".format(exc))
else:
console.print("Lease released; device availability returns after cleanup is confirmed.")
batch = len(allocation_ids) > 1
for allocation_id in allocation_ids:
if batch:
console.print("Releasing allocation {}".format(allocation_id))
errors = _release_allocation(allocation_id, store, lambda: api or _neatops_api(environment))
failures.extend("{}: {}".format(allocation_id, error) if batch else error for error in errors)
if failures:
raise CloudExError("; ".join(failures))
except CloudExError as exc:
Expand Down
129 changes: 129 additions & 0 deletions tests/unit/test_cloudex_neatops.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,135 @@ def test_release_disconnects_only_matching_local_allocation(command_api, monkeyp
command_api.call.assert_called_once_with("DELETE", "/devkits/allocations/" + AID)


def test_release_authorizes_active_tunnel_before_progress_and_releases_after_disconnect(command_api, monkeypatch):
session = {"allocation_id": AID, "forwarder_pid": 123}
store = Mock()
store.list.return_value = [session]
monkeypatch.setattr(commands, "_store", lambda: store)
monkeypatch.setattr(commands, "disconnect_requires_admin", lambda _: True)
events = []
commands._authorize_supported_host.side_effect = lambda: events.append("sudo")

class TrackingProgress(commands.StageProgress):
def __enter__(self):
events.append("progress")
return super().__enter__()

monkeypatch.setattr(commands, "StageProgress", TrackingProgress)
disconnect = Mock(side_effect=lambda *args, **kwargs: events.append("disconnect"))
monkeypatch.setattr(commands, "disconnect_session", disconnect)
command_api.call.side_effect = lambda *_: events.append("release")

result = CliRunner().invoke(commands.cloudex_group, ["-i", "release", AID])

assert result.exit_code == 0, result.output
assert events == ["sudo", "progress", "disconnect", "release"]
assert disconnect.call_args.kwargs == {"authorize": False}
assert "Authorizing local tunnel cleanup" in result.output


@pytest.mark.parametrize("sudo_denied", [True, False])
def test_release_keeps_lease_if_local_tunnel_is_still_running(command_api, monkeypatch, sudo_denied):
store = Mock()
store.list.return_value = [{"allocation_id": AID, "forwarder_pid": 123}]
monkeypatch.setattr(commands, "_store", lambda: store)
monkeypatch.setattr(commands, "disconnect_requires_admin", lambda _: True)
if sudo_denied:
commands._authorize_supported_host.side_effect = CloudExError("administrator authorization was not granted")
disconnect = Mock(side_effect=CloudExError("local tunnel cleanup is still pending"))
monkeypatch.setattr(commands, "disconnect_session", disconnect)

result = CliRunner().invoke(commands.cloudex_group, ["-i", "release", AID])

assert result.exit_code != 0
assert "Lease release skipped because its local CloudEx tunnel is still running." in result.output
assert disconnect.call_count == (0 if sudo_denied else 1)
command_api.call.assert_not_called()


def test_release_without_id_releases_only_the_selected_allocation(command_api, monkeypatch):
other_id = "b" * 32
first = {"session_id": AID, "cluster_id": DEVICE["cluster_id"], "target_id": DEVICE["id"], "status": "active"}
second = {"session_id": other_id, "cluster_id": "removed-appliance", "target_id": "offline-kit", "status": "ready"}
command_api.call.side_effect = [{"devices": [DEVICE], "allocations": [first, second]}, {}]
monkeypatch.setattr(commands, "_interactive_session_selection_available", lambda: True)
checkbox = Mock()
checkbox.return_value.execute.return_value = [other_id]
monkeypatch.setattr("InquirerPy.inquirer.checkbox", checkbox)
store = Mock()
store.list.return_value = [{"allocation_id": AID}, {"allocation_id": other_id}]
monkeypatch.setattr(commands, "_store", lambda: store)
disconnect = Mock()
monkeypatch.setattr(commands, "disconnect_session", disconnect)

result = CliRunner().invoke(commands.cloudex_group, ["-i", "release", "--environment", "staging"])

assert result.exit_code == 0, result.output
choices = checkbox.call_args.kwargs["choices"]
assert [choice["value"] for choice in choices] == [AID, other_id]
assert DEVICE["name"] in choices[0]["name"]
assert "offline-kit" in choices[1]["name"]
assert [c.args for c in command_api.call.call_args_list] == [
("GET", "/devkits"), ("DELETE", "/devkits/allocations/" + other_id),
]
disconnect.assert_called_once()
assert disconnect.call_args.args[0] == {"allocation_id": other_id}


@pytest.mark.parametrize("has_allocation", [False, True])
def test_release_without_selection_does_not_delete(command_api, monkeypatch, has_allocation):
allocation = {"session_id": AID, "cluster_id": DEVICE["cluster_id"], "target_id": DEVICE["id"], "status": "active"}
command_api.call.side_effect = [{"devices": [DEVICE], "allocations": [allocation] if has_allocation else []}]
monkeypatch.setattr(commands, "_interactive_session_selection_available", lambda: True)
checkbox = Mock()
checkbox.return_value.execute.return_value = []
monkeypatch.setattr("InquirerPy.inquirer.checkbox", checkbox)
store = Mock()
monkeypatch.setattr(commands, "_store", store)

result = CliRunner().invoke(commands.cloudex_group, ["-i", "release"])

assert result.exit_code == 0, result.output
if has_allocation:
assert "No CloudEx allocations selected" in result.output
assert checkbox.call_args.kwargs["mandatory"] is False
else:
assert "no CloudEx allocations" in result.output
checkbox.assert_not_called()
command_api.call.assert_called_once_with("GET", "/devkits")
store.assert_not_called()


def test_release_checklist_continues_after_a_selected_lease_fails(command_api, monkeypatch):
other_id = "b" * 32
allocations = [{"session_id": aid, "cluster_id": "appliance", "target_id": aid[:8], "status": "active"}
for aid in (AID, other_id, "c" * 32)]
command_api.call.side_effect = [{"allocations": allocations}, CloudExError("Unavailable"), {}]
monkeypatch.setattr(commands, "_interactive_session_selection_available", lambda: True)
monkeypatch.setattr(commands, "_store", lambda: Mock(list=Mock(return_value=[])))
checkbox = Mock()
checkbox.return_value.execute.return_value = [AID, other_id]
monkeypatch.setattr("InquirerPy.inquirer.checkbox", checkbox)

result = CliRunner().invoke(commands.cloudex_group, ["-i", "release"])

assert result.exit_code != 0
assert "{}: Lease release failed: Unavailable".format(AID) in result.output
assert "Lease released;" in result.output
assert [c.args for c in command_api.call.call_args_list] == [
("GET", "/devkits"), ("DELETE", "/devkits/allocations/" + AID),
("DELETE", "/devkits/allocations/" + other_id),
]


def test_release_without_id_requires_a_terminal(command_api):
result = CliRunner().invoke(commands.cloudex_group, ["-i", "release"])

assert result.exit_code == 2
assert "Provide an allocation ID" in result.output
command_api.call.assert_not_called()


def test_cancelled_browser_login_keeps_previous_credentials(api, monkeypatch):
api._save(tokens())
before = api.path.read_bytes()
Expand Down
Loading