ciscopktrewind 0.2.0__tar.gz
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.
- ciscopktrewind-0.2.0/LICENSE +21 -0
- ciscopktrewind-0.2.0/PKG-INFO +122 -0
- ciscopktrewind-0.2.0/README.md +105 -0
- ciscopktrewind-0.2.0/pyproject.toml +31 -0
- ciscopktrewind-0.2.0/setup.cfg +4 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/__init__.py +3 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/apply.py +94 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/cli.py +709 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/compat.py +92 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/console.py +186 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/lab.py +378 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/model.py +97 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/normalize.py +39 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/parse.py +111 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/plan.py +207 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/pt_setup.py +199 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/redact.py +156 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/rules/__init__.py +1 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/rules/ios.py +196 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/store.py +633 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/transports/__init__.py +64 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/transports/paste.py +49 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/transports/pt.py +245 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/transports/pt_bridge.py +130 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind/verify.py +64 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind.egg-info/PKG-INFO +122 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind.egg-info/SOURCES.txt +29 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind.egg-info/dependency_links.txt +1 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind.egg-info/entry_points.txt +2 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind.egg-info/requires.txt +8 -0
- ciscopktrewind-0.2.0/src/ciscopktrewind.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 djhizon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ciscopktrewind
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Snapshots and verified rollback for Cisco IOS configs, for labs where a typo shouldn't cost an afternoon.
|
|
5
|
+
Author: djhizon
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Provides-Extra: pt
|
|
11
|
+
Requires-Dist: packet-tracer-mcp<0.13,>=0.12; extra == "pt"
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
14
|
+
Requires-Dist: hypothesis>=6.100; extra == "dev"
|
|
15
|
+
Requires-Dist: pyflakes>=3; extra == "dev"
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
<p align="center">
|
|
19
|
+
<img src="https://raw.githubusercontent.com/djhizon/ciscopktrewind/v0.2.0/docs/brand/logo-512.png" width="220" height="220" alt="ciscopktrewind logo">
|
|
20
|
+
</p>
|
|
21
|
+
|
|
22
|
+
<h1 align="center"><b>ciscopktrewind</b></h1>
|
|
23
|
+
|
|
24
|
+
<p align="center"><b>Undo for Cisco IOS configs.</b> Automatic snapshots of every config change, and a restore that proves it worked.</p>
|
|
25
|
+
|
|
26
|
+
<p align="center">
|
|
27
|
+
<img src="https://raw.githubusercontent.com/djhizon/ciscopktrewind/v0.2.0/docs/demo/demo.gif" width="800" alt="Demo: a student breaks R1 in its CLI tab; ciscopktrewind names the command, shows the typo, and undoes it">
|
|
28
|
+
</p>
|
|
29
|
+
|
|
30
|
+
## What it does
|
|
31
|
+
|
|
32
|
+
- **Watches** your whole Packet Tracer lab and records every change.
|
|
33
|
+
- **`undo`** takes back one change on a router or switch, then reads it back to **prove** it worked.
|
|
34
|
+
- **`rewind`** takes the whole lab back to a save point, including cables, PCs and added or deleted devices.
|
|
35
|
+
- Also works with any IOS device by pasting `show running-config`.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
You need Python 3.11 or newer ([python.org](https://www.python.org/downloads/); on Windows tick
|
|
40
|
+
"Add python.exe to PATH") and Cisco Packet Tracer. Then pick one way:
|
|
41
|
+
|
|
42
|
+
**Easiest: one command** (pipx keeps it in its own space and puts `ciscopktrewind` on your PATH)
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pipx install "ciscopktrewind[pt] @ git+https://github.com/djhizon/ciscopktrewind"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
No pipx yet? Mac: `brew install pipx && pipx ensurepath`. Windows:
|
|
49
|
+
`py -m pip install --user pipx` then `py -m pipx ensurepath`. Close and reopen the terminal.
|
|
50
|
+
|
|
51
|
+
**Or with plain pip**
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install "ciscopktrewind[pt] @ git+https://github.com/djhizon/ciscopktrewind"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**Or from a clone** (if you want the code)
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
git clone https://github.com/djhizon/ciscopktrewind
|
|
61
|
+
cd ciscopktrewind
|
|
62
|
+
python3 -m venv .venv
|
|
63
|
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
64
|
+
pip install ".[pt]"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**Then, once:**
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
ciscopktrewind setup # finds Packet Tracer, installs the extension it needs, connects
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Update: `pipx upgrade ciscopktrewind` (clone: `git pull` then `pip install ".[pt]"`).
|
|
74
|
+
Remove: `pipx uninstall ciscopktrewind`. Your history stays in your user folder until you delete it.
|
|
75
|
+
|
|
76
|
+
## Use it
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
ciscopktrewind start # watch the open lab; leave this window open
|
|
80
|
+
ciscopktrewind undo # undo the last change (or: undo R1, undo 6)
|
|
81
|
+
ciscopktrewind history # numbered list of changes and save points
|
|
82
|
+
ciscopktrewind rewind # list save points; then e.g. rewind 18:14
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`ciscopktrewind status` tells you if everything is connected.
|
|
86
|
+
|
|
87
|
+
## Use it with any IOS device
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
ciscopktrewind snapshot working.txt --device R1 # save a show running-config
|
|
91
|
+
ciscopktrewind plan now.txt --to 3f9a1c --device R1 # commands to paste back
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Safe by default
|
|
95
|
+
|
|
96
|
+
- Shows you the plan and asks before typing anything.
|
|
97
|
+
- Saves the current state first, so an undo or rewind can be taken back too.
|
|
98
|
+
- A rewind opens a copy; your own lab file is never overwritten.
|
|
99
|
+
- Never reboots, erases or saves the device.
|
|
100
|
+
- Hides passwords and keys unless you ask for them.
|
|
101
|
+
|
|
102
|
+
## Proven
|
|
103
|
+
|
|
104
|
+
82 out of 82 restores verified on real Packet Tracer, with up to three mistakes at once; 11 out of 11 checks on the full watch, undo and rewind flow; 11 out of 11 on the CLI log (102 typed commands, none lost, the secret never stored). Limits and details: [docs/how-it-works.md](docs/how-it-works.md).
|
|
105
|
+
|
|
106
|
+
## Tested with
|
|
107
|
+
|
|
108
|
+
| | Tested | Should work | Won't work |
|
|
109
|
+
|---|---|---|---|
|
|
110
|
+
| Python | 3.11 to 3.14 | newer | 3.10 or older |
|
|
111
|
+
| Packet Tracer | 9.0.0 (macOS) | 8.2 and newer | before 8.2 |
|
|
112
|
+
| MCP Control Center extension | v0.12.0 (installed by `setup`) | | any other build: `ciscopktrewind setup --repair` |
|
|
113
|
+
| packet-tracer-mcp | 0.12.x (installed with ciscopktrewind) | | other versions |
|
|
114
|
+
| Windows | built and unit-tested | needs a real Packet Tracer run | |
|
|
115
|
+
|
|
116
|
+
`ciscopktrewind status` checks your computer against this table and prints the fix for anything that's off. A weekly check installs everything fresh on the newest versions, so breakage shows up here first: [](https://github.com/djhizon/ciscopktrewind/actions/workflows/fresh-install.yml)
|
|
117
|
+
|
|
118
|
+
## Thanks
|
|
119
|
+
|
|
120
|
+
Built on [Mats2208/MCP-Packet-Tracer](https://github.com/Mats2208/MCP-Packet-Tracer). How AI was used: [AI_USAGE.md](AI_USAGE.md).
|
|
121
|
+
|
|
122
|
+
Licensed under the [MIT License](LICENSE). Not affiliated with Cisco.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/djhizon/ciscopktrewind/v0.2.0/docs/brand/logo-512.png" width="220" height="220" alt="ciscopktrewind logo">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center"><b>ciscopktrewind</b></h1>
|
|
6
|
+
|
|
7
|
+
<p align="center"><b>Undo for Cisco IOS configs.</b> Automatic snapshots of every config change, and a restore that proves it worked.</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<img src="https://raw.githubusercontent.com/djhizon/ciscopktrewind/v0.2.0/docs/demo/demo.gif" width="800" alt="Demo: a student breaks R1 in its CLI tab; ciscopktrewind names the command, shows the typo, and undoes it">
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
## What it does
|
|
14
|
+
|
|
15
|
+
- **Watches** your whole Packet Tracer lab and records every change.
|
|
16
|
+
- **`undo`** takes back one change on a router or switch, then reads it back to **prove** it worked.
|
|
17
|
+
- **`rewind`** takes the whole lab back to a save point, including cables, PCs and added or deleted devices.
|
|
18
|
+
- Also works with any IOS device by pasting `show running-config`.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
You need Python 3.11 or newer ([python.org](https://www.python.org/downloads/); on Windows tick
|
|
23
|
+
"Add python.exe to PATH") and Cisco Packet Tracer. Then pick one way:
|
|
24
|
+
|
|
25
|
+
**Easiest: one command** (pipx keeps it in its own space and puts `ciscopktrewind` on your PATH)
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pipx install "ciscopktrewind[pt] @ git+https://github.com/djhizon/ciscopktrewind"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
No pipx yet? Mac: `brew install pipx && pipx ensurepath`. Windows:
|
|
32
|
+
`py -m pip install --user pipx` then `py -m pipx ensurepath`. Close and reopen the terminal.
|
|
33
|
+
|
|
34
|
+
**Or with plain pip**
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install "ciscopktrewind[pt] @ git+https://github.com/djhizon/ciscopktrewind"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Or from a clone** (if you want the code)
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
git clone https://github.com/djhizon/ciscopktrewind
|
|
44
|
+
cd ciscopktrewind
|
|
45
|
+
python3 -m venv .venv
|
|
46
|
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
47
|
+
pip install ".[pt]"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**Then, once:**
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
ciscopktrewind setup # finds Packet Tracer, installs the extension it needs, connects
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Update: `pipx upgrade ciscopktrewind` (clone: `git pull` then `pip install ".[pt]"`).
|
|
57
|
+
Remove: `pipx uninstall ciscopktrewind`. Your history stays in your user folder until you delete it.
|
|
58
|
+
|
|
59
|
+
## Use it
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
ciscopktrewind start # watch the open lab; leave this window open
|
|
63
|
+
ciscopktrewind undo # undo the last change (or: undo R1, undo 6)
|
|
64
|
+
ciscopktrewind history # numbered list of changes and save points
|
|
65
|
+
ciscopktrewind rewind # list save points; then e.g. rewind 18:14
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`ciscopktrewind status` tells you if everything is connected.
|
|
69
|
+
|
|
70
|
+
## Use it with any IOS device
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
ciscopktrewind snapshot working.txt --device R1 # save a show running-config
|
|
74
|
+
ciscopktrewind plan now.txt --to 3f9a1c --device R1 # commands to paste back
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Safe by default
|
|
78
|
+
|
|
79
|
+
- Shows you the plan and asks before typing anything.
|
|
80
|
+
- Saves the current state first, so an undo or rewind can be taken back too.
|
|
81
|
+
- A rewind opens a copy; your own lab file is never overwritten.
|
|
82
|
+
- Never reboots, erases or saves the device.
|
|
83
|
+
- Hides passwords and keys unless you ask for them.
|
|
84
|
+
|
|
85
|
+
## Proven
|
|
86
|
+
|
|
87
|
+
82 out of 82 restores verified on real Packet Tracer, with up to three mistakes at once; 11 out of 11 checks on the full watch, undo and rewind flow; 11 out of 11 on the CLI log (102 typed commands, none lost, the secret never stored). Limits and details: [docs/how-it-works.md](docs/how-it-works.md).
|
|
88
|
+
|
|
89
|
+
## Tested with
|
|
90
|
+
|
|
91
|
+
| | Tested | Should work | Won't work |
|
|
92
|
+
|---|---|---|---|
|
|
93
|
+
| Python | 3.11 to 3.14 | newer | 3.10 or older |
|
|
94
|
+
| Packet Tracer | 9.0.0 (macOS) | 8.2 and newer | before 8.2 |
|
|
95
|
+
| MCP Control Center extension | v0.12.0 (installed by `setup`) | | any other build: `ciscopktrewind setup --repair` |
|
|
96
|
+
| packet-tracer-mcp | 0.12.x (installed with ciscopktrewind) | | other versions |
|
|
97
|
+
| Windows | built and unit-tested | needs a real Packet Tracer run | |
|
|
98
|
+
|
|
99
|
+
`ciscopktrewind status` checks your computer against this table and prints the fix for anything that's off. A weekly check installs everything fresh on the newest versions, so breakage shows up here first: [](https://github.com/djhizon/ciscopktrewind/actions/workflows/fresh-install.yml)
|
|
100
|
+
|
|
101
|
+
## Thanks
|
|
102
|
+
|
|
103
|
+
Built on [Mats2208/MCP-Packet-Tracer](https://github.com/Mats2208/MCP-Packet-Tracer). How AI was used: [AI_USAGE.md](AI_USAGE.md).
|
|
104
|
+
|
|
105
|
+
Licensed under the [MIT License](LICENSE). Not affiliated with Cisco.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=69"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ciscopktrewind"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Snapshots and verified rollback for Cisco IOS configs, for labs where a typo shouldn't cost an afternoon."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "djhizon" }]
|
|
14
|
+
# Zero runtime dependencies, on purpose (docs/architecture-plan.md, ADR 1).
|
|
15
|
+
dependencies = []
|
|
16
|
+
|
|
17
|
+
[project.scripts]
|
|
18
|
+
ciscopktrewind = "ciscopktrewind.cli:main"
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
# Live Packet Tracer commands (`ciscopktrewind pt ...`). Pinned narrowly: we use the
|
|
22
|
+
# bridge modules inside packet-tracer-mcp, which are not a public API.
|
|
23
|
+
pt = ["packet-tracer-mcp>=0.12,<0.13"]
|
|
24
|
+
dev = ["pytest>=8", "hypothesis>=6.100", "pyflakes>=3"]
|
|
25
|
+
|
|
26
|
+
[tool.setuptools.packages.find]
|
|
27
|
+
where = ["src"]
|
|
28
|
+
|
|
29
|
+
[tool.pytest.ini_options]
|
|
30
|
+
testpaths = ["tests"]
|
|
31
|
+
addopts = "-q"
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Restore a device to a snapshot, safely, and prove it.
|
|
2
|
+
|
|
3
|
+
The order is the safety story:
|
|
4
|
+
1. read the device; refuse if it isn't the snapshot's device (model + serial);
|
|
5
|
+
2. store the current state first, so the restore itself can be undone;
|
|
6
|
+
3. plan; an imported snapshot (pasted, loaded from a file) needs explicit confirmation;
|
|
7
|
+
4. re-read right before typing and refuse if the device changed since the plan;
|
|
8
|
+
5. type the plan (never `end`), then read back and verify.
|
|
9
|
+
Nothing here reports success without step 5 saying so.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
from typing import Literal
|
|
16
|
+
|
|
17
|
+
from .model import RestorePlan, Snapshot
|
|
18
|
+
from .normalize import state_digest
|
|
19
|
+
from .plan import plan, to_commands
|
|
20
|
+
from .store import SnapshotStore, device_key, new_snapshot
|
|
21
|
+
from .transports import CommandResult, DeviceState, Transport, TransportError
|
|
22
|
+
from .verify import VerificationResult, verify
|
|
23
|
+
|
|
24
|
+
Outcome = Literal["verified", "planned", "nothing-to-do", "refused", "failed"]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class RestoreRefused(Exception):
|
|
28
|
+
"""A safety rule stopped the restore before anything was typed."""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class RestoreResult:
|
|
33
|
+
outcome: Outcome
|
|
34
|
+
plan: RestorePlan | None
|
|
35
|
+
applied: tuple[CommandResult, ...]
|
|
36
|
+
verification: VerificationResult | None
|
|
37
|
+
safety_snapshot_id: str | None
|
|
38
|
+
detail: str = ""
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def rejected(self) -> tuple[CommandResult, ...]:
|
|
42
|
+
return tuple(r for r in self.applied if r.status != 0)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def restore(
|
|
46
|
+
transport: Transport,
|
|
47
|
+
target: Snapshot,
|
|
48
|
+
store: SnapshotStore,
|
|
49
|
+
*,
|
|
50
|
+
dry_run: bool = False,
|
|
51
|
+
confirm_imported: bool = False,
|
|
52
|
+
source: str = "ciscopktrewind",
|
|
53
|
+
) -> RestoreResult:
|
|
54
|
+
state = transport.read_state()
|
|
55
|
+
if device_key(state.identity) != device_key(target.identity):
|
|
56
|
+
raise RestoreRefused(
|
|
57
|
+
f"snapshot is for {target.identity.model} {target.identity.serial}, "
|
|
58
|
+
f"device is {state.identity.model} {state.identity.serial}"
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
before = _snapshot_of(state, f"{source}: before restore")
|
|
62
|
+
|
|
63
|
+
# VLANs are planned only when both sides have a table. A snapshot without one
|
|
64
|
+
# (a pasted config) means "unknown", never "delete every VLAN".
|
|
65
|
+
use_vlans = state.vlans is not None and bool(target.vlans)
|
|
66
|
+
p = plan(state.config_text, target.config_text,
|
|
67
|
+
state.vlans if use_vlans else None, target.vlans if use_vlans else None)
|
|
68
|
+
if not p.steps:
|
|
69
|
+
v = verify(state.config_text, state.vlans, target)
|
|
70
|
+
return RestoreResult("nothing-to-do" if v.verified else "failed", p, (), v, None, v.detail)
|
|
71
|
+
if dry_run: # a preview, allowed for any snapshot; types and saves nothing
|
|
72
|
+
return RestoreResult("planned", p, (), None, None, f"{len(p.steps)} step(s) planned")
|
|
73
|
+
if target.origin == "imported" and not confirm_imported:
|
|
74
|
+
raise RestoreRefused("this snapshot was imported, not read from the device; "
|
|
75
|
+
"review the plan and confirm to restore it")
|
|
76
|
+
|
|
77
|
+
now = transport.read_state()
|
|
78
|
+
if state_digest(now.config_text, now.vlans or ()) != state_digest(state.config_text, state.vlans or ()):
|
|
79
|
+
raise RestoreRefused("the device changed since the plan was made; run the restore again")
|
|
80
|
+
store.save(before) # so the restore itself can be undone
|
|
81
|
+
applied = tuple(transport.apply(to_commands(p)))
|
|
82
|
+
try:
|
|
83
|
+
after = transport.read_state()
|
|
84
|
+
rejected = sum(1 for r in applied if r.status != 0)
|
|
85
|
+
v = verify(after.config_text, after.vlans, target, rejected=rejected)
|
|
86
|
+
store.save(_snapshot_of(after, f"{source}: after restore"))
|
|
87
|
+
except TransportError as exc:
|
|
88
|
+
v = verify(None, None, target)
|
|
89
|
+
return RestoreResult("failed", p, applied, v, before.id, f"could not read the device back: {exc}")
|
|
90
|
+
return RestoreResult("verified" if v.verified else "failed", p, applied, v, before.id, v.detail)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _snapshot_of(state: DeviceState, source: str) -> Snapshot:
|
|
94
|
+
return new_snapshot(state.config_text, state.vlans or (), state.identity, source=source)
|