networka 0.1.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- network_toolkit/__about__.py +4 -0
- network_toolkit/__init__.py +8 -0
- network_toolkit/__main__.py +10 -0
- network_toolkit/builtin_sequences/arista_eos/common.yml +53 -0
- network_toolkit/builtin_sequences/mikrotik_routeros/common.yml +52 -0
- network_toolkit/cli.py +265 -0
- network_toolkit/commands/__init__.py +8 -0
- network_toolkit/commands/backup.py +441 -0
- network_toolkit/commands/complete.py +172 -0
- network_toolkit/commands/config.py +1068 -0
- network_toolkit/commands/diff.py +485 -0
- network_toolkit/commands/download.py +197 -0
- network_toolkit/commands/firmware.py +780 -0
- network_toolkit/commands/info.py +377 -0
- network_toolkit/commands/list.py +345 -0
- network_toolkit/commands/routerboard_upgrade.py +204 -0
- network_toolkit/commands/run.py +839 -0
- network_toolkit/commands/schema_unified.py +91 -0
- network_toolkit/commands/sequence.py +17 -0
- network_toolkit/commands/ssh.py +540 -0
- network_toolkit/commands/ssh_engines.py +0 -0
- network_toolkit/commands/ssh_fallback.py +97 -0
- network_toolkit/commands/ssh_platform.py +120 -0
- network_toolkit/commands/upload.py +237 -0
- network_toolkit/commands/vendor_config_backup.py +378 -0
- network_toolkit/common/color_magic.py +177 -0
- network_toolkit/common/command.py +135 -0
- network_toolkit/common/command_base.py +135 -0
- network_toolkit/common/command_helpers.py +214 -0
- network_toolkit/common/credentials.py +86 -0
- network_toolkit/common/defaults.py +17 -0
- network_toolkit/common/errors.py +130 -0
- network_toolkit/common/interactive_confirmation.py +144 -0
- network_toolkit/common/logging.py +47 -0
- network_toolkit/common/output.py +594 -0
- network_toolkit/common/output_clean.py +244 -0
- network_toolkit/common/paths.py +86 -0
- network_toolkit/common/resolver.py +207 -0
- network_toolkit/common/styles.py +199 -0
- network_toolkit/common/table_generator.py +169 -0
- network_toolkit/common/table_providers.py +716 -0
- network_toolkit/config.py +1506 -0
- network_toolkit/credentials.py +288 -0
- network_toolkit/device.py +1658 -0
- network_toolkit/device_transfers.py +170 -0
- network_toolkit/exceptions.py +78 -0
- network_toolkit/ip_device.py +241 -0
- network_toolkit/platforms/__init__.py +21 -0
- network_toolkit/platforms/base.py +267 -0
- network_toolkit/platforms/cisco_ios/__init__.py +6 -0
- network_toolkit/platforms/cisco_ios/constants.py +15 -0
- network_toolkit/platforms/cisco_ios/operations.py +426 -0
- network_toolkit/platforms/cisco_iosxe/__init__.py +6 -0
- network_toolkit/platforms/cisco_iosxe/constants.py +15 -0
- network_toolkit/platforms/cisco_iosxe/operations.py +580 -0
- network_toolkit/platforms/cisco_shared/__init__.py +0 -0
- network_toolkit/platforms/cisco_shared/operations.py +0 -0
- network_toolkit/platforms/factory.py +190 -0
- network_toolkit/platforms/mikrotik_routeros/__init__.py +8 -0
- network_toolkit/platforms/mikrotik_routeros/confirmation_patterns.py +24 -0
- network_toolkit/platforms/mikrotik_routeros/constants.py +31 -0
- network_toolkit/platforms/mikrotik_routeros/operations.py +686 -0
- network_toolkit/results_enhanced.py +364 -0
- network_toolkit/schemas/__init__.py +8 -0
- network_toolkit/sequence_manager.py +239 -0
- network_toolkit/shell_completion/bash_completion_nw.sh +339 -0
- network_toolkit/shell_completion/zsh_completion_nw.zsh +136 -0
- network_toolkit/transport/__init__.py +15 -0
- network_toolkit/transport/factory.py +113 -0
- network_toolkit/transport/interfaces.py +46 -0
- network_toolkit/transport/nornir_inventory.py +173 -0
- network_toolkit/transport/nornir_netmiko.py +277 -0
- network_toolkit/transport/scrapli_sync.py +36 -0
- network_toolkit/tui/__init__.py +18 -0
- network_toolkit/tui/app.py +1275 -0
- network_toolkit/tui/clipboard.py +46 -0
- network_toolkit/tui/compat.py +111 -0
- network_toolkit/tui/constants.py +111 -0
- network_toolkit/tui/controller.py +550 -0
- network_toolkit/tui/data.py +149 -0
- network_toolkit/tui/dialogs.py +196 -0
- network_toolkit/tui/helpers.py +55 -0
- network_toolkit/tui/keymap.py +31 -0
- network_toolkit/tui/layout.py +111 -0
- network_toolkit/tui/models.py +93 -0
- network_toolkit/tui/output_manager.py +372 -0
- network_toolkit/tui/prompt.py +172 -0
- network_toolkit/tui/services.py +285 -0
- networka-0.1.9.dist-info/METADATA +316 -0
- networka-0.1.9.dist-info/RECORD +93 -0
- networka-0.1.9.dist-info/WHEEL +4 -0
- networka-0.1.9.dist-info/entry_points.txt +2 -0
- networka-0.1.9.dist-info/licenses/LICENSE +73 -0
|
@@ -0,0 +1,839 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""`nw run` command implementation."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
7
|
+
from enum import Enum
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from time import perf_counter
|
|
10
|
+
from typing import Annotated, Any
|
|
11
|
+
|
|
12
|
+
import typer
|
|
13
|
+
|
|
14
|
+
# Tables and printing routed via OutputManager helpers
|
|
15
|
+
from network_toolkit.common.command import CommandContext
|
|
16
|
+
from network_toolkit.common.credentials import prompt_for_credentials
|
|
17
|
+
from network_toolkit.common.defaults import DEFAULT_CONFIG_PATH
|
|
18
|
+
from network_toolkit.common.output import (
|
|
19
|
+
OutputMode,
|
|
20
|
+
get_output_mode_from_config,
|
|
21
|
+
set_output_mode,
|
|
22
|
+
)
|
|
23
|
+
from network_toolkit.config import load_config
|
|
24
|
+
from network_toolkit.exceptions import NetworkToolkitError
|
|
25
|
+
from network_toolkit.ip_device import (
|
|
26
|
+
create_ip_based_config,
|
|
27
|
+
extract_ips_from_target,
|
|
28
|
+
get_supported_platforms,
|
|
29
|
+
is_ip_list,
|
|
30
|
+
validate_platform,
|
|
31
|
+
)
|
|
32
|
+
from network_toolkit.results_enhanced import ResultsManager
|
|
33
|
+
from network_toolkit.sequence_manager import SequenceManager
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class RawFormat(str, Enum):
|
|
37
|
+
"""Supported raw output formats."""
|
|
38
|
+
|
|
39
|
+
TXT = "txt"
|
|
40
|
+
JSON = "json"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
PREVIEW_LEN = 200
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def register(app: typer.Typer) -> None:
|
|
47
|
+
@app.command(rich_help_panel="Remote Operations")
|
|
48
|
+
def run(
|
|
49
|
+
target: Annotated[
|
|
50
|
+
str,
|
|
51
|
+
typer.Argument(
|
|
52
|
+
help=(
|
|
53
|
+
"Device/group name, comma-separated list, or IP addresses. "
|
|
54
|
+
"For IPs use --platform to specify device type"
|
|
55
|
+
)
|
|
56
|
+
),
|
|
57
|
+
],
|
|
58
|
+
command_or_sequence: Annotated[
|
|
59
|
+
str,
|
|
60
|
+
typer.Argument(
|
|
61
|
+
help=("RouterOS command to execute or name of a configured sequence"),
|
|
62
|
+
),
|
|
63
|
+
],
|
|
64
|
+
*,
|
|
65
|
+
config_file: Annotated[
|
|
66
|
+
Path,
|
|
67
|
+
typer.Option("--config", "-c", help="Configuration directory or file path"),
|
|
68
|
+
] = DEFAULT_CONFIG_PATH,
|
|
69
|
+
verbose: Annotated[
|
|
70
|
+
bool, typer.Option("--verbose", "-v", help="Enable verbose logging")
|
|
71
|
+
] = False,
|
|
72
|
+
store_results: Annotated[
|
|
73
|
+
bool,
|
|
74
|
+
typer.Option(
|
|
75
|
+
"--store-results", "-s", help="Store command results to files"
|
|
76
|
+
),
|
|
77
|
+
] = False,
|
|
78
|
+
results_dir: Annotated[
|
|
79
|
+
str | None, typer.Option("--results-dir", help="Override results directory")
|
|
80
|
+
] = None,
|
|
81
|
+
output_mode: Annotated[
|
|
82
|
+
OutputMode | None,
|
|
83
|
+
typer.Option(
|
|
84
|
+
"--output-mode",
|
|
85
|
+
"-o",
|
|
86
|
+
help="Output decoration mode: default, light, dark, no-color, raw",
|
|
87
|
+
show_default=False,
|
|
88
|
+
),
|
|
89
|
+
] = None,
|
|
90
|
+
raw: Annotated[
|
|
91
|
+
RawFormat | None,
|
|
92
|
+
typer.Option(
|
|
93
|
+
"--raw",
|
|
94
|
+
help="Legacy raw output mode - use --output-mode raw instead",
|
|
95
|
+
show_default=False,
|
|
96
|
+
hidden=True,
|
|
97
|
+
),
|
|
98
|
+
] = None,
|
|
99
|
+
interactive_auth: Annotated[
|
|
100
|
+
bool,
|
|
101
|
+
typer.Option(
|
|
102
|
+
"--interactive-auth",
|
|
103
|
+
"-i",
|
|
104
|
+
help="Prompt for username and password interactively",
|
|
105
|
+
),
|
|
106
|
+
] = False,
|
|
107
|
+
device_type: Annotated[
|
|
108
|
+
str | None,
|
|
109
|
+
typer.Option(
|
|
110
|
+
"--platform",
|
|
111
|
+
"-p",
|
|
112
|
+
help="Device type when using IP addresses (e.g., mikrotik_routeros). Note: This specifies the network driver type, not hardware platform.",
|
|
113
|
+
),
|
|
114
|
+
] = None,
|
|
115
|
+
port: Annotated[
|
|
116
|
+
int | None,
|
|
117
|
+
typer.Option(
|
|
118
|
+
"--port",
|
|
119
|
+
help="SSH port when using IP addresses (default: 22)",
|
|
120
|
+
),
|
|
121
|
+
] = None,
|
|
122
|
+
transport_type: Annotated[
|
|
123
|
+
str | None,
|
|
124
|
+
typer.Option(
|
|
125
|
+
"--transport",
|
|
126
|
+
"-t",
|
|
127
|
+
help="Transport type to use for connections (currently only scrapli is supported). Defaults to configuration or scrapli.",
|
|
128
|
+
),
|
|
129
|
+
] = None,
|
|
130
|
+
) -> None:
|
|
131
|
+
"""Execute a single command or a sequence on a device or a group."""
|
|
132
|
+
# Validate transport type if provided
|
|
133
|
+
if transport_type is not None:
|
|
134
|
+
from network_toolkit.transport.factory import get_transport_factory
|
|
135
|
+
|
|
136
|
+
try:
|
|
137
|
+
# This will raise ValueError if transport_type is invalid
|
|
138
|
+
get_transport_factory(transport_type)
|
|
139
|
+
except ValueError as e:
|
|
140
|
+
# Use typer echo for early errors before CommandContext is available
|
|
141
|
+
typer.echo(f"Error: {e}", err=True)
|
|
142
|
+
raise typer.Exit(1) from e
|
|
143
|
+
|
|
144
|
+
# Handle legacy raw mode mapping
|
|
145
|
+
if raw is not None:
|
|
146
|
+
output_mode = OutputMode.RAW
|
|
147
|
+
|
|
148
|
+
# Create command context with proper styling/logging
|
|
149
|
+
ctx = CommandContext(
|
|
150
|
+
output_mode=output_mode,
|
|
151
|
+
verbose=verbose,
|
|
152
|
+
config_file=config_file,
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
# Handle interactive authentication if requested
|
|
156
|
+
interactive_creds = None
|
|
157
|
+
if interactive_auth:
|
|
158
|
+
if ctx.output.mode != OutputMode.RAW:
|
|
159
|
+
ctx.print_info("Interactive authentication mode enabled")
|
|
160
|
+
interactive_creds = prompt_for_credentials(
|
|
161
|
+
"Enter username for devices",
|
|
162
|
+
"Enter password for devices",
|
|
163
|
+
"admin", # Default username suggestion
|
|
164
|
+
)
|
|
165
|
+
if ctx.output.mode != OutputMode.RAW:
|
|
166
|
+
ctx.print_success(f"Will use username: {interactive_creds.username}")
|
|
167
|
+
|
|
168
|
+
# Track run timing & reporting state
|
|
169
|
+
started_at = perf_counter()
|
|
170
|
+
printed_results_dir = False
|
|
171
|
+
output_mgr = (
|
|
172
|
+
ctx.output
|
|
173
|
+
) # Use context's output manager (may update after config)
|
|
174
|
+
|
|
175
|
+
def _print_results_dir_once(results_mgr: ResultsManager) -> None:
|
|
176
|
+
"""Print the results directory once if storing is enabled."""
|
|
177
|
+
nonlocal printed_results_dir
|
|
178
|
+
if (
|
|
179
|
+
results_mgr.store_results
|
|
180
|
+
and results_mgr.session_dir
|
|
181
|
+
and not printed_results_dir
|
|
182
|
+
):
|
|
183
|
+
output_mgr.print_results_directory(str(results_mgr.session_dir))
|
|
184
|
+
printed_results_dir = True
|
|
185
|
+
|
|
186
|
+
def _print_run_summary(
|
|
187
|
+
*,
|
|
188
|
+
target_label: str,
|
|
189
|
+
op_type: str, # "Command" | "Sequence"
|
|
190
|
+
name: str,
|
|
191
|
+
duration: float,
|
|
192
|
+
results_mgr: ResultsManager,
|
|
193
|
+
is_group: bool = False,
|
|
194
|
+
totals: tuple[int, int, int] | None = None,
|
|
195
|
+
raw_mode: bool = False,
|
|
196
|
+
) -> None:
|
|
197
|
+
"""
|
|
198
|
+
Render an end-of-run summary.
|
|
199
|
+
|
|
200
|
+
Skips printing for device command in raw output mode.
|
|
201
|
+
"""
|
|
202
|
+
results_dir = (
|
|
203
|
+
str(results_mgr.session_dir)
|
|
204
|
+
if results_mgr.store_results and results_mgr.session_dir
|
|
205
|
+
else None
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
output_mgr.print_summary(
|
|
209
|
+
target=target_label,
|
|
210
|
+
operation_type=op_type,
|
|
211
|
+
name=name,
|
|
212
|
+
duration=duration,
|
|
213
|
+
is_group=is_group,
|
|
214
|
+
totals=totals,
|
|
215
|
+
results_dir=results_dir,
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
def _run_command_on_device(
|
|
219
|
+
device_name: str,
|
|
220
|
+
config: Any,
|
|
221
|
+
cmd: str,
|
|
222
|
+
username_override: str | None = None,
|
|
223
|
+
password_override: str | None = None,
|
|
224
|
+
transport_override: str | None = None,
|
|
225
|
+
) -> tuple[str, str | None, str | None]:
|
|
226
|
+
# Late import to allow tests to patch `network_toolkit.cli.DeviceSession`
|
|
227
|
+
from network_toolkit.cli import DeviceSession
|
|
228
|
+
|
|
229
|
+
try:
|
|
230
|
+
with DeviceSession(
|
|
231
|
+
device_name,
|
|
232
|
+
config,
|
|
233
|
+
username_override,
|
|
234
|
+
password_override,
|
|
235
|
+
transport_override,
|
|
236
|
+
) as session:
|
|
237
|
+
result = session.execute_command(cmd)
|
|
238
|
+
return (device_name, result, None)
|
|
239
|
+
except NetworkToolkitError as e: # pragma: no cover - error path
|
|
240
|
+
error_msg = f"Error on {device_name}: {e.message}"
|
|
241
|
+
if verbose and e.details:
|
|
242
|
+
error_msg += f" | Details: {e.details}"
|
|
243
|
+
if output_mode != OutputMode.RAW:
|
|
244
|
+
output_mgr.print_error(error_msg, device_name)
|
|
245
|
+
return (device_name, None, e.message)
|
|
246
|
+
except Exception as e: # pragma: no cover - unexpected
|
|
247
|
+
error_msg = f"Unexpected error on {device_name}: {e}"
|
|
248
|
+
if output_mode != OutputMode.RAW:
|
|
249
|
+
output_mgr.print_error(error_msg, device_name)
|
|
250
|
+
return (device_name, None, str(e))
|
|
251
|
+
|
|
252
|
+
def _run_sequence_on_device(
|
|
253
|
+
device_name: str,
|
|
254
|
+
config: Any,
|
|
255
|
+
seq_name: str,
|
|
256
|
+
username_override: str | None = None,
|
|
257
|
+
password_override: str | None = None,
|
|
258
|
+
transport_override: str | None = None,
|
|
259
|
+
) -> tuple[str, dict[str, str] | None, str | None]:
|
|
260
|
+
# Import DeviceSession from cli to preserve tests' patch path
|
|
261
|
+
from network_toolkit.cli import DeviceSession
|
|
262
|
+
|
|
263
|
+
try:
|
|
264
|
+
with DeviceSession(
|
|
265
|
+
device_name,
|
|
266
|
+
config,
|
|
267
|
+
username_override,
|
|
268
|
+
password_override,
|
|
269
|
+
transport_override,
|
|
270
|
+
) as session:
|
|
271
|
+
# Use vendor-aware sequence resolution
|
|
272
|
+
sm = SequenceManager(config)
|
|
273
|
+
sequence_commands = sm.resolve(seq_name, device_name)
|
|
274
|
+
if not sequence_commands:
|
|
275
|
+
msg = f"Sequence '{seq_name}' not found for device type"
|
|
276
|
+
return (device_name, None, msg)
|
|
277
|
+
|
|
278
|
+
results_map: dict[str, str] = {}
|
|
279
|
+
for cmd in sequence_commands:
|
|
280
|
+
output = session.execute_command(cmd)
|
|
281
|
+
results_map[cmd] = output
|
|
282
|
+
|
|
283
|
+
return (device_name, results_map, None)
|
|
284
|
+
except NetworkToolkitError as e: # pragma: no cover - error path
|
|
285
|
+
error_msg = f"Error on {device_name}: {e.message}"
|
|
286
|
+
if verbose and e.details:
|
|
287
|
+
error_msg += f" | Details: {e.details}"
|
|
288
|
+
return (device_name, None, e.message)
|
|
289
|
+
except Exception as e: # pragma: no cover - unexpected
|
|
290
|
+
return (device_name, None, str(e))
|
|
291
|
+
|
|
292
|
+
try:
|
|
293
|
+
config = load_config(config_file)
|
|
294
|
+
|
|
295
|
+
# If no CLI output mode given, honor config/general output mode
|
|
296
|
+
# Keep OutputManager as the single source of truth for theming
|
|
297
|
+
if output_mode is None:
|
|
298
|
+
chosen_mode = get_output_mode_from_config(
|
|
299
|
+
getattr(getattr(config, "general", None), "output_mode", None)
|
|
300
|
+
)
|
|
301
|
+
output_mgr = set_output_mode(chosen_mode)
|
|
302
|
+
|
|
303
|
+
# Use output manager for all prints
|
|
304
|
+
|
|
305
|
+
# Check if target is IP addresses and handle accordingly
|
|
306
|
+
if is_ip_list(target):
|
|
307
|
+
if device_type is None:
|
|
308
|
+
supported_platforms = get_supported_platforms()
|
|
309
|
+
platform_list = "\n".join(
|
|
310
|
+
[f" {k}: {v}" for k, v in supported_platforms.items()]
|
|
311
|
+
)
|
|
312
|
+
if raw is None:
|
|
313
|
+
ctx.print_error(
|
|
314
|
+
"When using IP addresses, --platform is required"
|
|
315
|
+
)
|
|
316
|
+
ctx.print_info(f"Supported platforms:\n{platform_list}")
|
|
317
|
+
raise typer.Exit(1)
|
|
318
|
+
|
|
319
|
+
if not validate_platform(device_type):
|
|
320
|
+
supported_platforms = get_supported_platforms()
|
|
321
|
+
platform_list = "\n".join(
|
|
322
|
+
[f" {k}: {v}" for k, v in supported_platforms.items()]
|
|
323
|
+
)
|
|
324
|
+
if raw is None:
|
|
325
|
+
ctx.print_error(f"Invalid device type '{device_type}'")
|
|
326
|
+
ctx.print_info(f"Supported platforms:\n{platform_list}")
|
|
327
|
+
raise typer.Exit(1)
|
|
328
|
+
|
|
329
|
+
# Extract IP addresses and create dynamic config
|
|
330
|
+
ips = extract_ips_from_target(target)
|
|
331
|
+
config = create_ip_based_config(
|
|
332
|
+
ips, device_type, config, port=port, transport_type=transport_type
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
if raw is None:
|
|
336
|
+
ctx.print_info(
|
|
337
|
+
f"Using IP addresses with device type '{device_type}': {', '.join(ips)}"
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
sm = SequenceManager(config)
|
|
341
|
+
# Provide command context for better results folder naming
|
|
342
|
+
cmd_ctx = f"run_{target}_{command_or_sequence}"
|
|
343
|
+
results_mgr = ResultsManager(
|
|
344
|
+
config,
|
|
345
|
+
store_results=store_results,
|
|
346
|
+
results_dir=results_dir,
|
|
347
|
+
command_context=cmd_ctx,
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
# --- Resolve targets (support comma-separated names and IPs) ---
|
|
351
|
+
def resolve_targets(target_expr: str) -> tuple[list[str], list[str]]:
|
|
352
|
+
"""Resolve a target expression to concrete device names.
|
|
353
|
+
|
|
354
|
+
Returns a tuple (devices, unknowns).
|
|
355
|
+
"""
|
|
356
|
+
# If target is IP addresses, the devices were already created
|
|
357
|
+
# with generated names, so we need to get those device names
|
|
358
|
+
if is_ip_list(target_expr):
|
|
359
|
+
ips = extract_ips_from_target(target_expr)
|
|
360
|
+
ip_device_names = [f"ip_{ip.replace('.', '_')}" for ip in ips]
|
|
361
|
+
# All IP devices should exist in config now
|
|
362
|
+
return ip_device_names, []
|
|
363
|
+
|
|
364
|
+
requested = [t.strip() for t in target_expr.split(",") if t.strip()]
|
|
365
|
+
devices: list[str] = []
|
|
366
|
+
unknowns: list[str] = []
|
|
367
|
+
|
|
368
|
+
# helpers
|
|
369
|
+
def _add_device(name: str) -> None:
|
|
370
|
+
if name not in devices:
|
|
371
|
+
devices.append(name)
|
|
372
|
+
|
|
373
|
+
for name in requested:
|
|
374
|
+
if config.devices and name in config.devices:
|
|
375
|
+
_add_device(name)
|
|
376
|
+
continue
|
|
377
|
+
if config.device_groups and name in config.device_groups:
|
|
378
|
+
for m in config.get_group_members(name):
|
|
379
|
+
_add_device(m)
|
|
380
|
+
continue
|
|
381
|
+
unknowns.append(name)
|
|
382
|
+
|
|
383
|
+
return devices, unknowns
|
|
384
|
+
|
|
385
|
+
resolved_devices, unknown_targets = resolve_targets(target)
|
|
386
|
+
|
|
387
|
+
if unknown_targets and not resolved_devices:
|
|
388
|
+
if raw is None:
|
|
389
|
+
output_mgr.print_error(
|
|
390
|
+
f"target(s) not found: {', '.join(unknown_targets)}"
|
|
391
|
+
)
|
|
392
|
+
raise typer.Exit(1)
|
|
393
|
+
elif unknown_targets and raw is None:
|
|
394
|
+
ctx.print_warning(
|
|
395
|
+
f"Warning: ignoring unknown target(s): {', '.join(unknown_targets)}"
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
# Determine target mode
|
|
399
|
+
is_single_device = len(resolved_devices) == 1
|
|
400
|
+
|
|
401
|
+
# Determine if the provided second argument is a known sequence name
|
|
402
|
+
is_sequence = bool(sm.exists(command_or_sequence))
|
|
403
|
+
|
|
404
|
+
# Legacy single-target flags removed; use booleans above directly
|
|
405
|
+
|
|
406
|
+
json_mode = raw == RawFormat.JSON
|
|
407
|
+
|
|
408
|
+
if is_sequence:
|
|
409
|
+
if is_single_device:
|
|
410
|
+
if raw is None:
|
|
411
|
+
ctx.print_info(
|
|
412
|
+
f"Executing sequence '{command_or_sequence}' on device {target}"
|
|
413
|
+
)
|
|
414
|
+
output_mgr.print_blank_line()
|
|
415
|
+
|
|
416
|
+
# Single concrete device
|
|
417
|
+
device_target = resolved_devices[0]
|
|
418
|
+
# Get credential overrides if in interactive mode
|
|
419
|
+
username_override = (
|
|
420
|
+
interactive_creds.username if interactive_creds else None
|
|
421
|
+
)
|
|
422
|
+
password_override = (
|
|
423
|
+
interactive_creds.password if interactive_creds else None
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
device_name, results_map, error = _run_sequence_on_device(
|
|
427
|
+
device_target,
|
|
428
|
+
config,
|
|
429
|
+
command_or_sequence,
|
|
430
|
+
username_override,
|
|
431
|
+
password_override,
|
|
432
|
+
transport_type,
|
|
433
|
+
)
|
|
434
|
+
|
|
435
|
+
if error:
|
|
436
|
+
if raw is None:
|
|
437
|
+
ctx.print_error(f"Error: {error}")
|
|
438
|
+
raise typer.Exit(1)
|
|
439
|
+
|
|
440
|
+
if results_map:
|
|
441
|
+
if raw is not None:
|
|
442
|
+
# Print raw outputs with context per command
|
|
443
|
+
for cmd, output in results_map.items():
|
|
444
|
+
if json_mode:
|
|
445
|
+
output_mgr.print_json(
|
|
446
|
+
{
|
|
447
|
+
"event": "result",
|
|
448
|
+
"device": device_target,
|
|
449
|
+
"cmd": cmd,
|
|
450
|
+
"output": output,
|
|
451
|
+
}
|
|
452
|
+
)
|
|
453
|
+
else:
|
|
454
|
+
output_mgr.print_command_output(
|
|
455
|
+
device_target, cmd, output
|
|
456
|
+
)
|
|
457
|
+
else:
|
|
458
|
+
ctx.print_success(
|
|
459
|
+
f"Sequence Results ({len(results_map)} commands):"
|
|
460
|
+
)
|
|
461
|
+
output_mgr.print_blank_line()
|
|
462
|
+
for i, (cmd, output) in enumerate(results_map.items(), 1):
|
|
463
|
+
output_mgr.print_info(f"Command {i}: {cmd}")
|
|
464
|
+
output_mgr.print_output(output)
|
|
465
|
+
output_mgr.print_separator()
|
|
466
|
+
|
|
467
|
+
if results_mgr.store_results:
|
|
468
|
+
stored_paths = results_mgr.store_sequence_results(
|
|
469
|
+
device_target, command_or_sequence, results_map
|
|
470
|
+
)
|
|
471
|
+
if stored_paths:
|
|
472
|
+
output_mgr.print_blank_line()
|
|
473
|
+
output_mgr.print_info(
|
|
474
|
+
f"Results stored: {stored_paths[-1]}"
|
|
475
|
+
)
|
|
476
|
+
_print_results_dir_once(results_mgr)
|
|
477
|
+
|
|
478
|
+
duration = perf_counter() - started_at
|
|
479
|
+
_print_run_summary(
|
|
480
|
+
target_label=device_target,
|
|
481
|
+
op_type="Sequence",
|
|
482
|
+
name=command_or_sequence,
|
|
483
|
+
duration=duration,
|
|
484
|
+
results_mgr=results_mgr,
|
|
485
|
+
is_group=False,
|
|
486
|
+
raw_mode=raw is not None,
|
|
487
|
+
)
|
|
488
|
+
if json_mode:
|
|
489
|
+
output_mgr.print_json(
|
|
490
|
+
{
|
|
491
|
+
"event": "summary",
|
|
492
|
+
"target": device_target,
|
|
493
|
+
"type": "Sequence",
|
|
494
|
+
"name": command_or_sequence,
|
|
495
|
+
"duration": duration,
|
|
496
|
+
"succeeded": True,
|
|
497
|
+
}
|
|
498
|
+
)
|
|
499
|
+
else:
|
|
500
|
+
group_members = resolved_devices
|
|
501
|
+
if not group_members:
|
|
502
|
+
if raw is None:
|
|
503
|
+
ctx.print_warning(f"No devices resolved for '{target}'.")
|
|
504
|
+
return
|
|
505
|
+
|
|
506
|
+
if raw is None:
|
|
507
|
+
ctx.print_info(
|
|
508
|
+
f"Executing sequence '{command_or_sequence}' on targets '{target}' "
|
|
509
|
+
f"({len(group_members)} devices)"
|
|
510
|
+
)
|
|
511
|
+
ctx.print_info(f"Members: {', '.join(group_members)}")
|
|
512
|
+
output_mgr.print_blank_line()
|
|
513
|
+
|
|
514
|
+
with ThreadPoolExecutor(max_workers=len(group_members)) as executor:
|
|
515
|
+
# Get credential overrides if in interactive mode
|
|
516
|
+
username_override = (
|
|
517
|
+
interactive_creds.username if interactive_creds else None
|
|
518
|
+
)
|
|
519
|
+
password_override = (
|
|
520
|
+
interactive_creds.password if interactive_creds else None
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
seq_future_to_device = {
|
|
524
|
+
executor.submit(
|
|
525
|
+
_run_sequence_on_device,
|
|
526
|
+
device,
|
|
527
|
+
config,
|
|
528
|
+
command_or_sequence,
|
|
529
|
+
username_override,
|
|
530
|
+
password_override,
|
|
531
|
+
transport_type,
|
|
532
|
+
): device
|
|
533
|
+
for device in group_members
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
all_results: list[
|
|
537
|
+
tuple[str, dict[str, str] | None, str | None]
|
|
538
|
+
] = []
|
|
539
|
+
for seq_future in as_completed(seq_future_to_device):
|
|
540
|
+
all_results.append(seq_future.result())
|
|
541
|
+
|
|
542
|
+
if raw is not None:
|
|
543
|
+
# Preserve device order from group for deterministic output
|
|
544
|
+
order_index = {d: i for i, d in enumerate(group_members)}
|
|
545
|
+
for _device_name, device_results, error in sorted(
|
|
546
|
+
all_results, key=lambda x: order_index.get(x[0], 0)
|
|
547
|
+
):
|
|
548
|
+
if error or not device_results:
|
|
549
|
+
# In raw mode, skip errors/no output
|
|
550
|
+
continue
|
|
551
|
+
for cmd, output in device_results.items():
|
|
552
|
+
if json_mode:
|
|
553
|
+
output_mgr.print_json(
|
|
554
|
+
{
|
|
555
|
+
"event": "result",
|
|
556
|
+
"device": _device_name,
|
|
557
|
+
"cmd": cmd,
|
|
558
|
+
"output": output,
|
|
559
|
+
}
|
|
560
|
+
)
|
|
561
|
+
else:
|
|
562
|
+
output_mgr.print_command_output(
|
|
563
|
+
_device_name, cmd, output
|
|
564
|
+
)
|
|
565
|
+
else:
|
|
566
|
+
ctx.print_success("Group Sequence Results")
|
|
567
|
+
for device_name, device_results, error in all_results:
|
|
568
|
+
# Print a simple device header and status lines (no tables)
|
|
569
|
+
output_mgr.print_separator()
|
|
570
|
+
output_mgr.print_info(f"Device: {device_name}")
|
|
571
|
+
if error:
|
|
572
|
+
output_mgr.print_error("Failed", device_name)
|
|
573
|
+
output_mgr.print_error(str(error), device_name)
|
|
574
|
+
else:
|
|
575
|
+
output_mgr.print_success("Success", device_name)
|
|
576
|
+
if device_results:
|
|
577
|
+
output_mgr.print_info(
|
|
578
|
+
f"Commands executed: {len(device_results)}",
|
|
579
|
+
device_name,
|
|
580
|
+
)
|
|
581
|
+
output_mgr.print_blank_line()
|
|
582
|
+
|
|
583
|
+
if results_mgr.store_results:
|
|
584
|
+
# Store per-device results and a group summary file
|
|
585
|
+
for device_name, device_results, error in all_results:
|
|
586
|
+
if device_results and not error:
|
|
587
|
+
results_mgr.store_sequence_results(
|
|
588
|
+
device_name, command_or_sequence, device_results
|
|
589
|
+
)
|
|
590
|
+
stored = results_mgr.store_group_results(
|
|
591
|
+
group_name=target,
|
|
592
|
+
command_or_sequence=command_or_sequence,
|
|
593
|
+
group_results=all_results,
|
|
594
|
+
is_sequence=True,
|
|
595
|
+
)
|
|
596
|
+
if stored:
|
|
597
|
+
_print_results_dir_once(results_mgr)
|
|
598
|
+
|
|
599
|
+
# End-of-run summary for group + sequence
|
|
600
|
+
duration = perf_counter() - started_at
|
|
601
|
+
succeeded = sum(1 for _, r, e in all_results if r and not e)
|
|
602
|
+
failed = sum(1 for _, _, e in all_results if e)
|
|
603
|
+
_print_run_summary(
|
|
604
|
+
target_label=target,
|
|
605
|
+
op_type="Sequence",
|
|
606
|
+
name=command_or_sequence,
|
|
607
|
+
duration=duration,
|
|
608
|
+
results_mgr=results_mgr,
|
|
609
|
+
is_group=True,
|
|
610
|
+
totals=(len(group_members), succeeded, failed),
|
|
611
|
+
raw_mode=raw is not None,
|
|
612
|
+
)
|
|
613
|
+
if json_mode:
|
|
614
|
+
output_mgr.print_json(
|
|
615
|
+
{
|
|
616
|
+
"event": "summary",
|
|
617
|
+
"target": target,
|
|
618
|
+
"type": "Sequence",
|
|
619
|
+
"name": command_or_sequence,
|
|
620
|
+
"duration": duration,
|
|
621
|
+
"total": len(group_members),
|
|
622
|
+
"succeeded": succeeded,
|
|
623
|
+
"failed": failed,
|
|
624
|
+
}
|
|
625
|
+
)
|
|
626
|
+
|
|
627
|
+
return # Done handling sequence
|
|
628
|
+
|
|
629
|
+
# Otherwise, handle as a single command
|
|
630
|
+
if is_single_device:
|
|
631
|
+
if raw is None:
|
|
632
|
+
device_target = resolved_devices[0]
|
|
633
|
+
ctx.print_info(f"Executing command on device {device_target}")
|
|
634
|
+
ctx.print_info(f"Command: {command_or_sequence}")
|
|
635
|
+
output_mgr.print_blank_line()
|
|
636
|
+
|
|
637
|
+
device_target = resolved_devices[0]
|
|
638
|
+
# Get credential overrides if in interactive mode
|
|
639
|
+
username_override = (
|
|
640
|
+
interactive_creds.username if interactive_creds else None
|
|
641
|
+
)
|
|
642
|
+
password_override = (
|
|
643
|
+
interactive_creds.password if interactive_creds else None
|
|
644
|
+
)
|
|
645
|
+
|
|
646
|
+
device_name, result, error = _run_command_on_device(
|
|
647
|
+
device_target,
|
|
648
|
+
config,
|
|
649
|
+
command_or_sequence,
|
|
650
|
+
username_override,
|
|
651
|
+
password_override,
|
|
652
|
+
transport_type,
|
|
653
|
+
)
|
|
654
|
+
if error:
|
|
655
|
+
raise typer.Exit(1)
|
|
656
|
+
|
|
657
|
+
if raw is not None:
|
|
658
|
+
if json_mode:
|
|
659
|
+
output_mgr.print_json(
|
|
660
|
+
{
|
|
661
|
+
"event": "result",
|
|
662
|
+
"device": device_target,
|
|
663
|
+
"cmd": command_or_sequence,
|
|
664
|
+
"output": result,
|
|
665
|
+
}
|
|
666
|
+
)
|
|
667
|
+
else:
|
|
668
|
+
output_mgr.print_command_output(
|
|
669
|
+
device_target, command_or_sequence, result or ""
|
|
670
|
+
)
|
|
671
|
+
else:
|
|
672
|
+
output_mgr.print_command_output(
|
|
673
|
+
device_target, command_or_sequence, result or ""
|
|
674
|
+
)
|
|
675
|
+
|
|
676
|
+
if results_mgr.store_results and raw is None and result:
|
|
677
|
+
stored_path = results_mgr.store_command_result(
|
|
678
|
+
device_target, command_or_sequence, result
|
|
679
|
+
)
|
|
680
|
+
if stored_path:
|
|
681
|
+
output_mgr.print_blank_line()
|
|
682
|
+
output_mgr.print_info(f"Results stored: {stored_path}")
|
|
683
|
+
_print_results_dir_once(results_mgr)
|
|
684
|
+
|
|
685
|
+
duration = perf_counter() - started_at
|
|
686
|
+
_print_run_summary(
|
|
687
|
+
target_label=device_target,
|
|
688
|
+
op_type="Command",
|
|
689
|
+
name=command_or_sequence,
|
|
690
|
+
duration=duration,
|
|
691
|
+
results_mgr=results_mgr,
|
|
692
|
+
is_group=False,
|
|
693
|
+
raw_mode=raw is not None,
|
|
694
|
+
)
|
|
695
|
+
if json_mode:
|
|
696
|
+
output_mgr.print_json(
|
|
697
|
+
{
|
|
698
|
+
"event": "summary",
|
|
699
|
+
"target": device_target,
|
|
700
|
+
"type": "Command",
|
|
701
|
+
"name": command_or_sequence,
|
|
702
|
+
"duration": duration,
|
|
703
|
+
"succeeded": True,
|
|
704
|
+
}
|
|
705
|
+
)
|
|
706
|
+
|
|
707
|
+
else:
|
|
708
|
+
members = resolved_devices
|
|
709
|
+
if not members:
|
|
710
|
+
if raw is None:
|
|
711
|
+
ctx.print_warning(f"No devices resolved for '{target}'.")
|
|
712
|
+
return
|
|
713
|
+
|
|
714
|
+
if raw is None:
|
|
715
|
+
ctx.print_info(
|
|
716
|
+
f"Executing command on targets '{target}' "
|
|
717
|
+
f"({len(members)} devices)"
|
|
718
|
+
)
|
|
719
|
+
ctx.print_info(f"Command: {command_or_sequence}")
|
|
720
|
+
ctx.print_info(f"Members: {', '.join(members)}")
|
|
721
|
+
output_mgr.print_blank_line()
|
|
722
|
+
|
|
723
|
+
with ThreadPoolExecutor(max_workers=len(members)) as executor:
|
|
724
|
+
# Get credential overrides if in interactive mode
|
|
725
|
+
username_override = (
|
|
726
|
+
interactive_creds.username if interactive_creds else None
|
|
727
|
+
)
|
|
728
|
+
password_override = (
|
|
729
|
+
interactive_creds.password if interactive_creds else None
|
|
730
|
+
)
|
|
731
|
+
|
|
732
|
+
future_to_device_cmd = {
|
|
733
|
+
executor.submit(
|
|
734
|
+
_run_command_on_device,
|
|
735
|
+
device,
|
|
736
|
+
config,
|
|
737
|
+
command_or_sequence,
|
|
738
|
+
username_override,
|
|
739
|
+
password_override,
|
|
740
|
+
transport_type,
|
|
741
|
+
): device
|
|
742
|
+
for device in members
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
group_results: list[tuple[str, str | None, str | None]] = []
|
|
746
|
+
for future in as_completed(future_to_device_cmd):
|
|
747
|
+
group_results.append(future.result())
|
|
748
|
+
|
|
749
|
+
if raw is not None:
|
|
750
|
+
# Emit raw results per device (skip errors in raw mode)
|
|
751
|
+
for device_name, out_text, error in group_results:
|
|
752
|
+
if error or out_text is None:
|
|
753
|
+
continue
|
|
754
|
+
if json_mode:
|
|
755
|
+
output_mgr.print_json(
|
|
756
|
+
{
|
|
757
|
+
"event": "result",
|
|
758
|
+
"device": device_name,
|
|
759
|
+
"cmd": command_or_sequence,
|
|
760
|
+
"output": out_text,
|
|
761
|
+
}
|
|
762
|
+
)
|
|
763
|
+
else:
|
|
764
|
+
output_mgr.print_command_output(
|
|
765
|
+
device_name, command_or_sequence, out_text
|
|
766
|
+
)
|
|
767
|
+
else:
|
|
768
|
+
ctx.print_success("Group Command Results:")
|
|
769
|
+
for device_name, out_text, error in group_results:
|
|
770
|
+
# Print a simple device header and status lines (no tables)
|
|
771
|
+
output_mgr.print_separator()
|
|
772
|
+
output_mgr.print_info(f"Device: {device_name}")
|
|
773
|
+
if error:
|
|
774
|
+
output_mgr.print_error("Failed", device_name)
|
|
775
|
+
output_mgr.print_error(str(error), device_name)
|
|
776
|
+
else:
|
|
777
|
+
output_mgr.print_success("Success", device_name)
|
|
778
|
+
if out_text:
|
|
779
|
+
output_mgr.print_blank_line()
|
|
780
|
+
output_mgr.print_output(out_text)
|
|
781
|
+
output_mgr.print_blank_line()
|
|
782
|
+
|
|
783
|
+
if results_mgr.store_results:
|
|
784
|
+
# Store per-device results and a group summary file
|
|
785
|
+
for device_name, out_text, error in group_results:
|
|
786
|
+
if out_text and not error:
|
|
787
|
+
results_mgr.store_command_result(
|
|
788
|
+
device_name, command_or_sequence, out_text
|
|
789
|
+
)
|
|
790
|
+
stored = results_mgr.store_group_results(
|
|
791
|
+
group_name=target,
|
|
792
|
+
command_or_sequence=command_or_sequence,
|
|
793
|
+
group_results=group_results,
|
|
794
|
+
is_sequence=False,
|
|
795
|
+
)
|
|
796
|
+
if stored:
|
|
797
|
+
_print_results_dir_once(results_mgr)
|
|
798
|
+
|
|
799
|
+
# End-of-run summary for group + single command
|
|
800
|
+
duration = perf_counter() - started_at
|
|
801
|
+
succeeded = sum(1 for _, o, e in group_results if o and not e)
|
|
802
|
+
failed = sum(1 for _, _, e in group_results if e)
|
|
803
|
+
_print_run_summary(
|
|
804
|
+
target_label=target,
|
|
805
|
+
op_type="Command",
|
|
806
|
+
name=command_or_sequence,
|
|
807
|
+
duration=duration,
|
|
808
|
+
results_mgr=results_mgr,
|
|
809
|
+
is_group=True,
|
|
810
|
+
totals=(len(members), succeeded, failed),
|
|
811
|
+
raw_mode=raw is not None,
|
|
812
|
+
)
|
|
813
|
+
if json_mode:
|
|
814
|
+
output_mgr.print_json(
|
|
815
|
+
{
|
|
816
|
+
"event": "summary",
|
|
817
|
+
"target": target,
|
|
818
|
+
"type": "Command",
|
|
819
|
+
"name": command_or_sequence,
|
|
820
|
+
"duration": duration,
|
|
821
|
+
"total": len(members),
|
|
822
|
+
"succeeded": succeeded,
|
|
823
|
+
"failed": failed,
|
|
824
|
+
}
|
|
825
|
+
)
|
|
826
|
+
|
|
827
|
+
except NetworkToolkitError as e:
|
|
828
|
+
if raw is None:
|
|
829
|
+
output_mgr.print_error(f"Error: {e.message}")
|
|
830
|
+
if verbose and e.details:
|
|
831
|
+
output_mgr.print_error(f"Details: {e.details}")
|
|
832
|
+
raise typer.Exit(1) from None
|
|
833
|
+
except typer.Exit:
|
|
834
|
+
# Re-raise typer.Exit without catching it as an unexpected error
|
|
835
|
+
raise
|
|
836
|
+
except Exception as e: # pragma: no cover - unexpected
|
|
837
|
+
if raw is None:
|
|
838
|
+
output_mgr.print_error(f"Unexpected error: {e}")
|
|
839
|
+
raise typer.Exit(1) from None
|