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,780 @@
|
|
|
1
|
+
"""Unified firmware command module."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from importlib import import_module
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Annotated, Any, cast
|
|
8
|
+
|
|
9
|
+
import typer
|
|
10
|
+
|
|
11
|
+
from network_toolkit.common.command_helpers import CommandContext
|
|
12
|
+
from network_toolkit.common.defaults import DEFAULT_CONFIG_PATH
|
|
13
|
+
from network_toolkit.common.logging import setup_logging
|
|
14
|
+
from network_toolkit.common.styles import StyleName
|
|
15
|
+
from network_toolkit.config import load_config
|
|
16
|
+
from network_toolkit.exceptions import NetworkToolkitError
|
|
17
|
+
from network_toolkit.platforms import (
|
|
18
|
+
check_operation_support,
|
|
19
|
+
get_platform_file_extensions,
|
|
20
|
+
get_platform_operations,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
MAX_LIST_PREVIEW = 10
|
|
24
|
+
|
|
25
|
+
# Create the firmware subapp
|
|
26
|
+
firmware_app = typer.Typer(
|
|
27
|
+
name="firmware",
|
|
28
|
+
help="Firmware management operations",
|
|
29
|
+
no_args_is_help=True,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@firmware_app.command("upgrade")
|
|
34
|
+
def upgrade(
|
|
35
|
+
target_name: Annotated[str, typer.Argument(help="Device or group name")],
|
|
36
|
+
firmware_file: Annotated[Path, typer.Argument(help="Path to firmware file")],
|
|
37
|
+
precheck_sequence: Annotated[
|
|
38
|
+
str, typer.Option("--precheck-sequence", help="Pre-check sequence name")
|
|
39
|
+
] = "pre_maintenance",
|
|
40
|
+
skip_precheck: Annotated[
|
|
41
|
+
bool, typer.Option("--skip-precheck", help="Skip pre-check sequence")
|
|
42
|
+
] = False,
|
|
43
|
+
config_file: Annotated[
|
|
44
|
+
Path, typer.Option("--config", "-c", help="Configuration file path")
|
|
45
|
+
] = DEFAULT_CONFIG_PATH,
|
|
46
|
+
verbose: Annotated[
|
|
47
|
+
bool, typer.Option("--verbose", "-v", help="Enable verbose output")
|
|
48
|
+
] = False,
|
|
49
|
+
) -> None:
|
|
50
|
+
"""Upgrade firmware on network devices.
|
|
51
|
+
|
|
52
|
+
Uploads and installs firmware upgrade on the specified device or group.
|
|
53
|
+
"""
|
|
54
|
+
setup_logging("DEBUG" if verbose else "INFO")
|
|
55
|
+
ctx = CommandContext(config_file=config_file, verbose=verbose, output_mode=None)
|
|
56
|
+
style_manager = ctx.style_manager
|
|
57
|
+
|
|
58
|
+
try:
|
|
59
|
+
if not firmware_file.exists() or not firmware_file.is_file():
|
|
60
|
+
ctx.output_manager.print_text(
|
|
61
|
+
style_manager.format_message(
|
|
62
|
+
f"Error: Firmware file not found: {firmware_file}",
|
|
63
|
+
StyleName.ERROR,
|
|
64
|
+
)
|
|
65
|
+
)
|
|
66
|
+
raise typer.Exit(1)
|
|
67
|
+
|
|
68
|
+
config = load_config(config_file)
|
|
69
|
+
module = import_module("network_toolkit.cli")
|
|
70
|
+
device_session = cast(Any, module).DeviceSession
|
|
71
|
+
|
|
72
|
+
devices = config.devices or {}
|
|
73
|
+
groups = config.device_groups or {}
|
|
74
|
+
is_device = target_name in devices
|
|
75
|
+
is_group = target_name in groups
|
|
76
|
+
|
|
77
|
+
if not (is_device or is_group):
|
|
78
|
+
ctx.output_manager.print_text(
|
|
79
|
+
style_manager.format_message(
|
|
80
|
+
f"Error: '{target_name}' not found as device or group in configuration",
|
|
81
|
+
StyleName.ERROR,
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
if devices:
|
|
85
|
+
dev_names = sorted(devices.keys())
|
|
86
|
+
preview = ", ".join(dev_names[:MAX_LIST_PREVIEW])
|
|
87
|
+
if len(dev_names) > MAX_LIST_PREVIEW:
|
|
88
|
+
preview += " ..."
|
|
89
|
+
ctx.output_manager.print_text(
|
|
90
|
+
style_manager.format_message("Known devices:", StyleName.INFO)
|
|
91
|
+
+ " "
|
|
92
|
+
+ preview
|
|
93
|
+
)
|
|
94
|
+
if groups:
|
|
95
|
+
grp_names = sorted(groups.keys())
|
|
96
|
+
preview = ", ".join(grp_names[:MAX_LIST_PREVIEW])
|
|
97
|
+
if len(grp_names) > MAX_LIST_PREVIEW:
|
|
98
|
+
preview += " ..."
|
|
99
|
+
ctx.output_manager.print_text(
|
|
100
|
+
style_manager.format_message("Known groups:", StyleName.WARNING)
|
|
101
|
+
+ " "
|
|
102
|
+
+ preview
|
|
103
|
+
)
|
|
104
|
+
raise typer.Exit(1)
|
|
105
|
+
|
|
106
|
+
def process_device(dev: str) -> bool:
|
|
107
|
+
try:
|
|
108
|
+
devices = config.devices or {}
|
|
109
|
+
if dev not in devices:
|
|
110
|
+
ctx.output_manager.print_text(
|
|
111
|
+
style_manager.format_message(
|
|
112
|
+
f"Error: Device '{dev}' not found in configuration",
|
|
113
|
+
StyleName.ERROR,
|
|
114
|
+
)
|
|
115
|
+
)
|
|
116
|
+
return False
|
|
117
|
+
|
|
118
|
+
device_config = devices[dev]
|
|
119
|
+
device_type = device_config.device_type
|
|
120
|
+
|
|
121
|
+
# Check if platform supports firmware upgrade BEFORE connecting
|
|
122
|
+
is_supported, error_msg = check_operation_support(
|
|
123
|
+
device_type, "firmware_upgrade"
|
|
124
|
+
)
|
|
125
|
+
if not is_supported:
|
|
126
|
+
ctx.output_manager.print_text(
|
|
127
|
+
style_manager.format_message(
|
|
128
|
+
f"Error on {dev}: {error_msg}", StyleName.ERROR
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
return False
|
|
132
|
+
|
|
133
|
+
# Check supported file extensions before connecting
|
|
134
|
+
supported_exts = get_platform_file_extensions(device_type)
|
|
135
|
+
if firmware_file.suffix.lower() not in supported_exts:
|
|
136
|
+
ext_list = ", ".join(supported_exts)
|
|
137
|
+
platform_name = {
|
|
138
|
+
"mikrotik_routeros": "MikroTik RouterOS",
|
|
139
|
+
"cisco_ios": "Cisco IOS",
|
|
140
|
+
"cisco_iosxe": "Cisco IOS-XE",
|
|
141
|
+
}.get(device_type, device_type)
|
|
142
|
+
ctx.output_manager.print_text(
|
|
143
|
+
style_manager.format_message(
|
|
144
|
+
f"Error: Invalid firmware file for {platform_name}. "
|
|
145
|
+
f"Expected {ext_list}, got {firmware_file.suffix}",
|
|
146
|
+
StyleName.ERROR,
|
|
147
|
+
)
|
|
148
|
+
)
|
|
149
|
+
return False
|
|
150
|
+
|
|
151
|
+
# Connect to device and proceed with operation
|
|
152
|
+
with device_session(dev, config) as session:
|
|
153
|
+
platform_ops = get_platform_operations(session)
|
|
154
|
+
|
|
155
|
+
if precheck_sequence and not skip_precheck:
|
|
156
|
+
ctx.output_manager.print_text(
|
|
157
|
+
style_manager.format_message(
|
|
158
|
+
f"Running precheck sequence '{precheck_sequence}' on {dev}...",
|
|
159
|
+
StyleName.INFO,
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
# Run sequence commands
|
|
163
|
+
seq_cmds: list[str] = []
|
|
164
|
+
dcfg = devices.get(dev)
|
|
165
|
+
if (
|
|
166
|
+
dcfg
|
|
167
|
+
and dcfg.command_sequences
|
|
168
|
+
and precheck_sequence in dcfg.command_sequences
|
|
169
|
+
):
|
|
170
|
+
seq_cmds = dcfg.command_sequences[precheck_sequence]
|
|
171
|
+
elif (
|
|
172
|
+
config.global_command_sequences
|
|
173
|
+
and precheck_sequence in config.global_command_sequences
|
|
174
|
+
):
|
|
175
|
+
seq_cmds = config.global_command_sequences[
|
|
176
|
+
precheck_sequence
|
|
177
|
+
].commands
|
|
178
|
+
|
|
179
|
+
for cmd in seq_cmds:
|
|
180
|
+
session.execute_command(cmd)
|
|
181
|
+
|
|
182
|
+
ctx.output_manager.print_text(
|
|
183
|
+
style_manager.format_message(
|
|
184
|
+
f"Uploading firmware to {dev} and rebooting...",
|
|
185
|
+
StyleName.WARNING,
|
|
186
|
+
)
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
transport_type = config.get_transport_type(dev)
|
|
190
|
+
try:
|
|
191
|
+
platform_name_obj = platform_ops.get_platform_name()
|
|
192
|
+
platform_name = str(platform_name_obj)
|
|
193
|
+
except Exception: # pragma: no cover
|
|
194
|
+
platform_name = "unknown"
|
|
195
|
+
|
|
196
|
+
ctx.output_manager.print_text(
|
|
197
|
+
style_manager.format_message("Platform:", StyleName.WARNING)
|
|
198
|
+
+ f" {platform_name}"
|
|
199
|
+
)
|
|
200
|
+
ctx.output_manager.print_text(
|
|
201
|
+
style_manager.format_message("Transport:", StyleName.WARNING)
|
|
202
|
+
+ f" {transport_type}"
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
# Use platform-specific firmware upgrade
|
|
206
|
+
ok = platform_ops.firmware_upgrade(
|
|
207
|
+
local_firmware_path=firmware_file
|
|
208
|
+
)
|
|
209
|
+
if ok:
|
|
210
|
+
ctx.output_manager.print_text(
|
|
211
|
+
style_manager.format_message(
|
|
212
|
+
f"OK Firmware upload initiated; device rebooting: {dev}",
|
|
213
|
+
StyleName.SUCCESS,
|
|
214
|
+
)
|
|
215
|
+
)
|
|
216
|
+
return True
|
|
217
|
+
|
|
218
|
+
ctx.output_manager.print_text(
|
|
219
|
+
style_manager.format_message(
|
|
220
|
+
f"FAIL Firmware upgrade failed to start on {dev}",
|
|
221
|
+
StyleName.ERROR,
|
|
222
|
+
)
|
|
223
|
+
)
|
|
224
|
+
return False
|
|
225
|
+
except NetworkToolkitError as e:
|
|
226
|
+
ctx.output_manager.print_text(
|
|
227
|
+
style_manager.format_message(
|
|
228
|
+
f"Error on {dev}: {e.message}", StyleName.ERROR
|
|
229
|
+
)
|
|
230
|
+
)
|
|
231
|
+
if verbose and e.details:
|
|
232
|
+
ctx.output_manager.print_text(
|
|
233
|
+
style_manager.format_message(
|
|
234
|
+
f"Details: {e.details}", StyleName.ERROR
|
|
235
|
+
)
|
|
236
|
+
)
|
|
237
|
+
return False
|
|
238
|
+
except Exception as e: # pragma: no cover
|
|
239
|
+
ctx.output_manager.print_text(
|
|
240
|
+
style_manager.format_message(
|
|
241
|
+
f"Unexpected error on {dev}: {e}", StyleName.ERROR
|
|
242
|
+
)
|
|
243
|
+
)
|
|
244
|
+
return False
|
|
245
|
+
|
|
246
|
+
if is_device:
|
|
247
|
+
ok = process_device(target_name)
|
|
248
|
+
if not ok:
|
|
249
|
+
raise typer.Exit(1)
|
|
250
|
+
return
|
|
251
|
+
|
|
252
|
+
# Handle group
|
|
253
|
+
members: list[str] = []
|
|
254
|
+
try:
|
|
255
|
+
members = config.get_group_members(target_name)
|
|
256
|
+
except Exception:
|
|
257
|
+
grp = groups.get(target_name)
|
|
258
|
+
if grp and getattr(grp, "members", None):
|
|
259
|
+
members = grp.members or []
|
|
260
|
+
|
|
261
|
+
if not members:
|
|
262
|
+
ctx.output_manager.print_text(
|
|
263
|
+
style_manager.format_message(
|
|
264
|
+
f"Error: No devices found in group '{target_name}'",
|
|
265
|
+
StyleName.ERROR,
|
|
266
|
+
)
|
|
267
|
+
)
|
|
268
|
+
raise typer.Exit(1)
|
|
269
|
+
|
|
270
|
+
ctx.output_manager.print_text(
|
|
271
|
+
style_manager.format_message(
|
|
272
|
+
f"Starting firmware upgrade for group '{target_name}' ({len(members)} devices)",
|
|
273
|
+
StyleName.INFO,
|
|
274
|
+
)
|
|
275
|
+
)
|
|
276
|
+
failures = 0
|
|
277
|
+
for dev in members:
|
|
278
|
+
ok = process_device(dev)
|
|
279
|
+
failures += 0 if ok else 1
|
|
280
|
+
|
|
281
|
+
total = len(members)
|
|
282
|
+
ctx.output_manager.print_text(
|
|
283
|
+
style_manager.format_message("Completed:", StyleName.BOLD)
|
|
284
|
+
+ f" {total - failures}/{total} initiated"
|
|
285
|
+
)
|
|
286
|
+
if failures:
|
|
287
|
+
raise typer.Exit(1)
|
|
288
|
+
|
|
289
|
+
except NetworkToolkitError as e:
|
|
290
|
+
ctx.output_manager.print_text(
|
|
291
|
+
style_manager.format_message(f"Error: {e.message}", StyleName.ERROR)
|
|
292
|
+
)
|
|
293
|
+
if verbose and e.details:
|
|
294
|
+
ctx.output_manager.print_text(
|
|
295
|
+
style_manager.format_message(f"Details: {e.details}", StyleName.ERROR)
|
|
296
|
+
)
|
|
297
|
+
raise typer.Exit(1) from None
|
|
298
|
+
except Exception as e: # pragma: no cover
|
|
299
|
+
ctx.output_manager.print_text(
|
|
300
|
+
style_manager.format_message(f"Unexpected error: {e}", StyleName.ERROR)
|
|
301
|
+
)
|
|
302
|
+
raise typer.Exit(1) from None
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
@firmware_app.command("downgrade")
|
|
306
|
+
def downgrade(
|
|
307
|
+
target_name: Annotated[str, typer.Argument(help="Device or group name")],
|
|
308
|
+
firmware_file: Annotated[Path, typer.Argument(help="Path to firmware file")],
|
|
309
|
+
precheck_sequence: Annotated[
|
|
310
|
+
str, typer.Option("--precheck-sequence", help="Pre-check sequence name")
|
|
311
|
+
] = "pre_maintenance",
|
|
312
|
+
skip_precheck: Annotated[
|
|
313
|
+
bool, typer.Option("--skip-precheck", help="Skip pre-check sequence")
|
|
314
|
+
] = False,
|
|
315
|
+
config_file: Annotated[
|
|
316
|
+
Path, typer.Option("--config", "-c", help="Configuration file path")
|
|
317
|
+
] = DEFAULT_CONFIG_PATH,
|
|
318
|
+
verbose: Annotated[
|
|
319
|
+
bool, typer.Option("--verbose", "-v", help="Enable verbose output")
|
|
320
|
+
] = False,
|
|
321
|
+
) -> None:
|
|
322
|
+
"""Downgrade firmware on network devices.
|
|
323
|
+
|
|
324
|
+
Uploads and installs firmware downgrade on the specified device or group.
|
|
325
|
+
"""
|
|
326
|
+
setup_logging("DEBUG" if verbose else "INFO")
|
|
327
|
+
ctx = CommandContext(config_file=config_file, verbose=verbose, output_mode=None)
|
|
328
|
+
style_manager = ctx.style_manager
|
|
329
|
+
|
|
330
|
+
try:
|
|
331
|
+
if not firmware_file.exists() or not firmware_file.is_file():
|
|
332
|
+
ctx.output_manager.print_text(
|
|
333
|
+
style_manager.format_message(
|
|
334
|
+
f"Error: Firmware file not found: {firmware_file}",
|
|
335
|
+
StyleName.ERROR,
|
|
336
|
+
)
|
|
337
|
+
)
|
|
338
|
+
raise typer.Exit(1)
|
|
339
|
+
|
|
340
|
+
config = load_config(config_file)
|
|
341
|
+
module = import_module("network_toolkit.cli")
|
|
342
|
+
device_session = cast(Any, module).DeviceSession
|
|
343
|
+
|
|
344
|
+
devices = config.devices or {}
|
|
345
|
+
groups = config.device_groups or {}
|
|
346
|
+
is_device = target_name in devices
|
|
347
|
+
is_group = target_name in groups
|
|
348
|
+
|
|
349
|
+
if not (is_device or is_group):
|
|
350
|
+
ctx.output_manager.print_text(
|
|
351
|
+
style_manager.format_message(
|
|
352
|
+
f"Error: '{target_name}' not found as device or group in configuration",
|
|
353
|
+
StyleName.ERROR,
|
|
354
|
+
)
|
|
355
|
+
)
|
|
356
|
+
raise typer.Exit(1)
|
|
357
|
+
|
|
358
|
+
def process_device(dev: str) -> bool:
|
|
359
|
+
try:
|
|
360
|
+
devices = config.devices or {}
|
|
361
|
+
if dev not in devices:
|
|
362
|
+
ctx.output_manager.print_text(
|
|
363
|
+
style_manager.format_message(
|
|
364
|
+
f"Error: Device '{dev}' not found in configuration",
|
|
365
|
+
StyleName.ERROR,
|
|
366
|
+
)
|
|
367
|
+
)
|
|
368
|
+
return False
|
|
369
|
+
|
|
370
|
+
device_config = devices[dev]
|
|
371
|
+
device_type = device_config.device_type
|
|
372
|
+
|
|
373
|
+
# Check if platform supports firmware downgrade
|
|
374
|
+
is_supported, error_msg = check_operation_support(
|
|
375
|
+
device_type, "firmware_downgrade"
|
|
376
|
+
)
|
|
377
|
+
if not is_supported:
|
|
378
|
+
ctx.output_manager.print_text(
|
|
379
|
+
style_manager.format_message(
|
|
380
|
+
f"Error on {dev}: {error_msg}", StyleName.ERROR
|
|
381
|
+
)
|
|
382
|
+
)
|
|
383
|
+
return False
|
|
384
|
+
|
|
385
|
+
# Check supported file extensions
|
|
386
|
+
supported_exts = get_platform_file_extensions(device_type)
|
|
387
|
+
if firmware_file.suffix.lower() not in supported_exts:
|
|
388
|
+
ext_list = ", ".join(supported_exts)
|
|
389
|
+
platform_name = {
|
|
390
|
+
"mikrotik_routeros": "MikroTik RouterOS",
|
|
391
|
+
"cisco_ios": "Cisco IOS",
|
|
392
|
+
"cisco_iosxe": "Cisco IOS-XE",
|
|
393
|
+
}.get(device_type, device_type)
|
|
394
|
+
ctx.output_manager.print_text(
|
|
395
|
+
style_manager.format_message(
|
|
396
|
+
f"Error: Invalid firmware file for {platform_name}. "
|
|
397
|
+
f"Expected {ext_list}, got {firmware_file.suffix}",
|
|
398
|
+
StyleName.ERROR,
|
|
399
|
+
)
|
|
400
|
+
)
|
|
401
|
+
return False
|
|
402
|
+
|
|
403
|
+
# Connect to device and perform downgrade
|
|
404
|
+
with device_session(dev, config) as session:
|
|
405
|
+
platform_ops = get_platform_operations(session)
|
|
406
|
+
|
|
407
|
+
if precheck_sequence and not skip_precheck:
|
|
408
|
+
ctx.output_manager.print_text(
|
|
409
|
+
style_manager.format_message(
|
|
410
|
+
f"Running precheck sequence '{precheck_sequence}' on {dev}...",
|
|
411
|
+
StyleName.INFO,
|
|
412
|
+
)
|
|
413
|
+
)
|
|
414
|
+
# Run sequence commands
|
|
415
|
+
seq_cmds: list[str] = []
|
|
416
|
+
dcfg = devices.get(dev)
|
|
417
|
+
if (
|
|
418
|
+
dcfg
|
|
419
|
+
and dcfg.command_sequences
|
|
420
|
+
and precheck_sequence in dcfg.command_sequences
|
|
421
|
+
):
|
|
422
|
+
seq_cmds = dcfg.command_sequences[precheck_sequence]
|
|
423
|
+
elif (
|
|
424
|
+
config.global_command_sequences
|
|
425
|
+
and precheck_sequence in config.global_command_sequences
|
|
426
|
+
):
|
|
427
|
+
seq_cmds = config.global_command_sequences[
|
|
428
|
+
precheck_sequence
|
|
429
|
+
].commands
|
|
430
|
+
|
|
431
|
+
for cmd in seq_cmds:
|
|
432
|
+
session.execute_command(cmd)
|
|
433
|
+
|
|
434
|
+
ctx.output_manager.print_text(
|
|
435
|
+
style_manager.format_message(
|
|
436
|
+
f"Downgrading firmware on {dev} and rebooting...",
|
|
437
|
+
StyleName.WARNING,
|
|
438
|
+
)
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
# Use platform-specific firmware downgrade
|
|
442
|
+
ok = platform_ops.firmware_downgrade(
|
|
443
|
+
local_firmware_path=firmware_file
|
|
444
|
+
)
|
|
445
|
+
if ok:
|
|
446
|
+
ctx.output_manager.print_text(
|
|
447
|
+
style_manager.format_message(
|
|
448
|
+
f"OK Firmware downgrade initiated; device rebooting: {dev}",
|
|
449
|
+
StyleName.SUCCESS,
|
|
450
|
+
)
|
|
451
|
+
)
|
|
452
|
+
return True
|
|
453
|
+
|
|
454
|
+
ctx.output_manager.print_text(
|
|
455
|
+
style_manager.format_message(
|
|
456
|
+
f"FAIL Firmware downgrade failed to start on {dev}",
|
|
457
|
+
StyleName.ERROR,
|
|
458
|
+
)
|
|
459
|
+
)
|
|
460
|
+
return False
|
|
461
|
+
except NetworkToolkitError as e:
|
|
462
|
+
ctx.output_manager.print_text(
|
|
463
|
+
style_manager.format_message(
|
|
464
|
+
f"Error on {dev}: {e.message}", StyleName.ERROR
|
|
465
|
+
)
|
|
466
|
+
)
|
|
467
|
+
if verbose and e.details:
|
|
468
|
+
ctx.output_manager.print_text(
|
|
469
|
+
style_manager.format_message(
|
|
470
|
+
f"Details: {e.details}", StyleName.ERROR
|
|
471
|
+
)
|
|
472
|
+
)
|
|
473
|
+
return False
|
|
474
|
+
except Exception as e: # pragma: no cover
|
|
475
|
+
ctx.output_manager.print_text(
|
|
476
|
+
style_manager.format_message(
|
|
477
|
+
f"Unexpected error on {dev}: {e}", StyleName.ERROR
|
|
478
|
+
)
|
|
479
|
+
)
|
|
480
|
+
return False
|
|
481
|
+
|
|
482
|
+
if is_device:
|
|
483
|
+
ok = process_device(target_name)
|
|
484
|
+
if not ok:
|
|
485
|
+
raise typer.Exit(1)
|
|
486
|
+
return
|
|
487
|
+
|
|
488
|
+
# Handle group
|
|
489
|
+
members: list[str] = []
|
|
490
|
+
try:
|
|
491
|
+
members = config.get_group_members(target_name)
|
|
492
|
+
except Exception:
|
|
493
|
+
grp = groups.get(target_name)
|
|
494
|
+
if grp and getattr(grp, "members", None):
|
|
495
|
+
members = grp.members or []
|
|
496
|
+
|
|
497
|
+
if not members:
|
|
498
|
+
ctx.output_manager.print_text(
|
|
499
|
+
style_manager.format_message(
|
|
500
|
+
f"Error: No devices found in group '{target_name}'",
|
|
501
|
+
StyleName.ERROR,
|
|
502
|
+
)
|
|
503
|
+
)
|
|
504
|
+
raise typer.Exit(1)
|
|
505
|
+
|
|
506
|
+
ctx.output_manager.print_text(
|
|
507
|
+
style_manager.format_message(
|
|
508
|
+
f"Starting firmware downgrade for group '{target_name}' ({len(members)} devices)",
|
|
509
|
+
StyleName.INFO,
|
|
510
|
+
)
|
|
511
|
+
)
|
|
512
|
+
failures = 0
|
|
513
|
+
for dev in members:
|
|
514
|
+
ok = process_device(dev)
|
|
515
|
+
failures += 0 if ok else 1
|
|
516
|
+
|
|
517
|
+
total = len(members)
|
|
518
|
+
ctx.output_manager.print_text(
|
|
519
|
+
style_manager.format_message("Completed:", StyleName.BOLD)
|
|
520
|
+
+ f" {total - failures}/{total} initiated"
|
|
521
|
+
)
|
|
522
|
+
if failures:
|
|
523
|
+
raise typer.Exit(1)
|
|
524
|
+
|
|
525
|
+
except NetworkToolkitError as e:
|
|
526
|
+
ctx.output_manager.print_text(
|
|
527
|
+
style_manager.format_message(f"Error: {e.message}", StyleName.ERROR)
|
|
528
|
+
)
|
|
529
|
+
if verbose and e.details:
|
|
530
|
+
ctx.output_manager.print_text(
|
|
531
|
+
style_manager.format_message(f"Details: {e.details}", StyleName.ERROR)
|
|
532
|
+
)
|
|
533
|
+
raise typer.Exit(1) from None
|
|
534
|
+
except Exception as e: # pragma: no cover
|
|
535
|
+
ctx.output_manager.print_text(
|
|
536
|
+
style_manager.format_message(f"Unexpected error: {e}", StyleName.ERROR)
|
|
537
|
+
)
|
|
538
|
+
raise typer.Exit(1) from None
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
@firmware_app.command("bios")
|
|
542
|
+
def bios(
|
|
543
|
+
target_name: Annotated[str, typer.Argument(help="Device or group name")],
|
|
544
|
+
precheck_sequence: Annotated[
|
|
545
|
+
str, typer.Option("--precheck-sequence", help="Pre-check sequence name")
|
|
546
|
+
] = "pre_maintenance",
|
|
547
|
+
skip_precheck: Annotated[
|
|
548
|
+
bool, typer.Option("--skip-precheck", help="Skip pre-check sequence")
|
|
549
|
+
] = False,
|
|
550
|
+
config_file: Annotated[
|
|
551
|
+
Path, typer.Option("--config", "-c", help="Configuration file path")
|
|
552
|
+
] = DEFAULT_CONFIG_PATH,
|
|
553
|
+
verbose: Annotated[
|
|
554
|
+
bool, typer.Option("--verbose", "-v", help="Enable verbose output")
|
|
555
|
+
] = False,
|
|
556
|
+
) -> None:
|
|
557
|
+
"""Upgrade BIOS on network devices.
|
|
558
|
+
|
|
559
|
+
Upgrades device BIOS/RouterBOOT using platform-specific implementations.
|
|
560
|
+
"""
|
|
561
|
+
setup_logging("DEBUG" if verbose else "INFO")
|
|
562
|
+
ctx = CommandContext(config_file=config_file, verbose=verbose, output_mode=None)
|
|
563
|
+
style_manager = ctx.style_manager
|
|
564
|
+
|
|
565
|
+
try:
|
|
566
|
+
config = load_config(config_file)
|
|
567
|
+
module = import_module("network_toolkit.cli")
|
|
568
|
+
device_session = cast(Any, module).DeviceSession
|
|
569
|
+
|
|
570
|
+
devices = config.devices or {}
|
|
571
|
+
groups = config.device_groups or {}
|
|
572
|
+
is_device = target_name in devices
|
|
573
|
+
is_group = target_name in groups
|
|
574
|
+
|
|
575
|
+
if not (is_device or is_group):
|
|
576
|
+
ctx.output_manager.print_text(
|
|
577
|
+
style_manager.format_message(
|
|
578
|
+
f"Error: '{target_name}' not found as device or group in configuration",
|
|
579
|
+
StyleName.ERROR,
|
|
580
|
+
)
|
|
581
|
+
)
|
|
582
|
+
raise typer.Exit(1)
|
|
583
|
+
|
|
584
|
+
def process_device(dev: str) -> bool:
|
|
585
|
+
try:
|
|
586
|
+
devices = config.devices or {}
|
|
587
|
+
if dev not in devices:
|
|
588
|
+
ctx.output_manager.print_text(
|
|
589
|
+
style_manager.format_message(
|
|
590
|
+
f"Error: Device '{dev}' not found in configuration",
|
|
591
|
+
StyleName.ERROR,
|
|
592
|
+
)
|
|
593
|
+
)
|
|
594
|
+
return False
|
|
595
|
+
|
|
596
|
+
device_config = devices[dev]
|
|
597
|
+
device_type = device_config.device_type
|
|
598
|
+
|
|
599
|
+
# Check if platform supports BIOS upgrade
|
|
600
|
+
is_supported, error_msg = check_operation_support(
|
|
601
|
+
device_type, "bios_upgrade"
|
|
602
|
+
)
|
|
603
|
+
if not is_supported:
|
|
604
|
+
ctx.output_manager.print_text(
|
|
605
|
+
style_manager.format_message(
|
|
606
|
+
f"Error on {dev}: {error_msg}", StyleName.ERROR
|
|
607
|
+
)
|
|
608
|
+
)
|
|
609
|
+
return False
|
|
610
|
+
|
|
611
|
+
# Connect to device and perform BIOS upgrade
|
|
612
|
+
with device_session(dev, config) as session:
|
|
613
|
+
platform_ops = get_platform_operations(session)
|
|
614
|
+
|
|
615
|
+
if precheck_sequence and not skip_precheck:
|
|
616
|
+
ctx.output_manager.print_text(
|
|
617
|
+
style_manager.format_message(
|
|
618
|
+
f"Running precheck sequence '{precheck_sequence}' on {dev}...",
|
|
619
|
+
StyleName.INFO,
|
|
620
|
+
)
|
|
621
|
+
)
|
|
622
|
+
# Run sequence commands
|
|
623
|
+
seq_cmds: list[str] = []
|
|
624
|
+
dcfg = devices.get(dev)
|
|
625
|
+
if (
|
|
626
|
+
dcfg
|
|
627
|
+
and dcfg.command_sequences
|
|
628
|
+
and precheck_sequence in dcfg.command_sequences
|
|
629
|
+
):
|
|
630
|
+
seq_cmds = dcfg.command_sequences[precheck_sequence]
|
|
631
|
+
elif (
|
|
632
|
+
config.global_command_sequences
|
|
633
|
+
and precheck_sequence in config.global_command_sequences
|
|
634
|
+
):
|
|
635
|
+
seq_cmds = config.global_command_sequences[
|
|
636
|
+
precheck_sequence
|
|
637
|
+
].commands
|
|
638
|
+
|
|
639
|
+
for cmd in seq_cmds:
|
|
640
|
+
session.execute_command(cmd)
|
|
641
|
+
|
|
642
|
+
ctx.output_manager.print_text(
|
|
643
|
+
style_manager.format_message(
|
|
644
|
+
f"Upgrading BIOS on {dev} and rebooting...",
|
|
645
|
+
StyleName.WARNING,
|
|
646
|
+
)
|
|
647
|
+
)
|
|
648
|
+
|
|
649
|
+
# Use platform-specific BIOS upgrade
|
|
650
|
+
ok = platform_ops.bios_upgrade()
|
|
651
|
+
if ok:
|
|
652
|
+
ctx.output_manager.print_text(
|
|
653
|
+
style_manager.format_message(
|
|
654
|
+
f"OK BIOS upgrade initiated; device rebooting: {dev}",
|
|
655
|
+
StyleName.SUCCESS,
|
|
656
|
+
)
|
|
657
|
+
)
|
|
658
|
+
return True
|
|
659
|
+
|
|
660
|
+
ctx.output_manager.print_text(
|
|
661
|
+
style_manager.format_message(
|
|
662
|
+
f"FAIL BIOS upgrade failed to start on {dev}",
|
|
663
|
+
StyleName.ERROR,
|
|
664
|
+
)
|
|
665
|
+
)
|
|
666
|
+
return False
|
|
667
|
+
except NetworkToolkitError as e:
|
|
668
|
+
ctx.output_manager.print_text(
|
|
669
|
+
style_manager.format_message(
|
|
670
|
+
f"Error on {dev}: {e.message}", StyleName.ERROR
|
|
671
|
+
)
|
|
672
|
+
)
|
|
673
|
+
if verbose and e.details:
|
|
674
|
+
ctx.output_manager.print_text(
|
|
675
|
+
style_manager.format_message(
|
|
676
|
+
f"Details: {e.details}", StyleName.ERROR
|
|
677
|
+
)
|
|
678
|
+
)
|
|
679
|
+
return False
|
|
680
|
+
except Exception as e: # pragma: no cover
|
|
681
|
+
ctx.output_manager.print_text(
|
|
682
|
+
style_manager.format_message(
|
|
683
|
+
f"Unexpected error on {dev}: {e}", StyleName.ERROR
|
|
684
|
+
)
|
|
685
|
+
)
|
|
686
|
+
return False
|
|
687
|
+
|
|
688
|
+
if is_device:
|
|
689
|
+
ok = process_device(target_name)
|
|
690
|
+
if not ok:
|
|
691
|
+
raise typer.Exit(1)
|
|
692
|
+
return
|
|
693
|
+
|
|
694
|
+
# Handle group
|
|
695
|
+
members: list[str] = []
|
|
696
|
+
try:
|
|
697
|
+
members = config.get_group_members(target_name)
|
|
698
|
+
except Exception:
|
|
699
|
+
grp = groups.get(target_name)
|
|
700
|
+
if grp and getattr(grp, "members", None):
|
|
701
|
+
members = grp.members or []
|
|
702
|
+
|
|
703
|
+
if not members:
|
|
704
|
+
ctx.output_manager.print_text(
|
|
705
|
+
style_manager.format_message(
|
|
706
|
+
f"Error: No devices found in group '{target_name}'",
|
|
707
|
+
StyleName.ERROR,
|
|
708
|
+
)
|
|
709
|
+
)
|
|
710
|
+
raise typer.Exit(1)
|
|
711
|
+
|
|
712
|
+
ctx.output_manager.print_text(
|
|
713
|
+
style_manager.format_message(
|
|
714
|
+
f"Starting BIOS upgrade for group '{target_name}' ({len(members)} devices)",
|
|
715
|
+
StyleName.INFO,
|
|
716
|
+
)
|
|
717
|
+
)
|
|
718
|
+
failures = 0
|
|
719
|
+
for dev in members:
|
|
720
|
+
ok = process_device(dev)
|
|
721
|
+
failures += 0 if ok else 1
|
|
722
|
+
|
|
723
|
+
total = len(members)
|
|
724
|
+
ctx.output_manager.print_text(
|
|
725
|
+
style_manager.format_message("Completed:", StyleName.BOLD)
|
|
726
|
+
+ f" {total - failures}/{total} initiated"
|
|
727
|
+
)
|
|
728
|
+
if failures:
|
|
729
|
+
raise typer.Exit(1)
|
|
730
|
+
|
|
731
|
+
except NetworkToolkitError as e:
|
|
732
|
+
ctx.output_manager.print_text(
|
|
733
|
+
style_manager.format_message(f"Error: {e.message}", StyleName.ERROR)
|
|
734
|
+
)
|
|
735
|
+
if verbose and e.details:
|
|
736
|
+
ctx.output_manager.print_text(
|
|
737
|
+
style_manager.format_message(f"Details: {e.details}", StyleName.ERROR)
|
|
738
|
+
)
|
|
739
|
+
raise typer.Exit(1) from None
|
|
740
|
+
except Exception as e: # pragma: no cover
|
|
741
|
+
ctx.output_manager.print_text(
|
|
742
|
+
style_manager.format_message(f"Unexpected error: {e}", StyleName.ERROR)
|
|
743
|
+
)
|
|
744
|
+
raise typer.Exit(1) from None
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
@firmware_app.command("vendors")
|
|
748
|
+
def vendors() -> None:
|
|
749
|
+
"""Show which vendors support firmware operations.
|
|
750
|
+
|
|
751
|
+
Lists all supported vendors and their firmware operation capabilities.
|
|
752
|
+
"""
|
|
753
|
+
from network_toolkit.platforms.factory import (
|
|
754
|
+
check_operation_support,
|
|
755
|
+
get_supported_platforms,
|
|
756
|
+
)
|
|
757
|
+
|
|
758
|
+
platforms = get_supported_platforms()
|
|
759
|
+
|
|
760
|
+
ctx = CommandContext()
|
|
761
|
+
ctx.print_info("Vendor firmware operation support:")
|
|
762
|
+
|
|
763
|
+
operations = [
|
|
764
|
+
("firmware_upgrade", "Firmware Upgrade"),
|
|
765
|
+
("firmware_downgrade", "Firmware Downgrade"),
|
|
766
|
+
("bios_upgrade", "BIOS Upgrade"),
|
|
767
|
+
]
|
|
768
|
+
|
|
769
|
+
for device_type, vendor_name in platforms.items():
|
|
770
|
+
ctx.print_info(f"\n{vendor_name} ({device_type}):")
|
|
771
|
+
|
|
772
|
+
for op_name, op_display in operations:
|
|
773
|
+
supported, _ = check_operation_support(device_type, op_name)
|
|
774
|
+
status = "✓ Supported" if supported else "✗ Not supported"
|
|
775
|
+
ctx.print_info(f" {op_display}: {status}")
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
def register(app: typer.Typer) -> None:
|
|
779
|
+
"""Register the unified firmware command with the main CLI app."""
|
|
780
|
+
app.add_typer(firmware_app, rich_help_panel="Vendor-Specific Remote Operations")
|