sshcnfgr 0.2.0b1__tar.gz → 0.2.0b2__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.
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/CHANGELOG.md +7 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/PKG-INFO +5 -4
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/README.md +4 -3
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/pyproject.toml +1 -1
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/__init__.py +1 -1
- sshcnfgr-0.2.0b2/src/sshcnfgr/generator.py +193 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/gui/main_window.py +235 -22
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/model.py +88 -2
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/store.py +15 -1
- sshcnfgr-0.2.0b1/src/sshcnfgr/generator.py +0 -83
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/.gitignore +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/LICENSE +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/SECURITY.md +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/assets/sshcnfgr.svg +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/cli.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/dedup.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/dedup_screen.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/gui/__init__.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/gui/dedup_dialog.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/identities.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/importer.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/logging_conf.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/paths.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/settings.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/tui.py +0 -0
- {sshcnfgr-0.2.0b1 → sshcnfgr-0.2.0b2}/src/sshcnfgr/tui.tcss +0 -0
|
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (with PEP 440 pre-release tags).
|
|
7
7
|
|
|
8
|
+
## [0.2.0b2] - 2026-11-26
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- **User-defined group ordering with Drag & Drop (Qt6 GUI):** Reorder groups via drag & drop or `▲` / `▼` buttons in the Groups tab; order is preserved in `~/.ssh/sshcnfgr.toml` instead of forced alphabetical sorting.
|
|
12
|
+
- **Grouped OpenSSH config generation (VS Code Remote-SSH friendly):** Active hosts are grouped by their primary (first) group in your custom group order (with clear section comments) so important hosts appear at the top in VS Code and `config.generated`.
|
|
13
|
+
- **Extended group defaults & `Match originalhost` blocks:** Groups now support default `user`, `port`, `identity`, and `extra_options` (e.g. `IdentitiesOnly yes`, `ProxyJump`), emitted as clean OpenSSH `Match originalhost alias1,alias2,...` blocks after host blocks and before `Host *`.
|
|
14
|
+
|
|
8
15
|
## [0.2.0b1] - 2026-11-26
|
|
9
16
|
|
|
10
17
|
### Added
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: sshcnfgr
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.0b2
|
|
4
4
|
Summary: TUI, optional Qt6 GUI, and CLI to manage OpenSSH host configs from a single TOML file
|
|
5
5
|
Project-URL: Homepage, https://github.com/thomsbe/SSHCnfgr
|
|
6
6
|
Project-URL: Repository, https://github.com/thomsbe/SSHCnfgr
|
|
@@ -46,7 +46,7 @@ Description-Content-Type: text/markdown
|
|
|
46
46
|
|
|
47
47
|
[](https://github.com/thomsbe/SSHCnfgr/blob/main/LICENSE)
|
|
48
48
|
[](https://www.python.org/)
|
|
49
|
-
[](https://pypi.org/project/sshcnfgr/)
|
|
50
50
|
|
|
51
51
|
**SSHCnfgr** verwaltet deine OpenSSH-Hosts, Gruppen und Identities über eine einzige, saubere TOML-Datei (`~/.ssh/sshcnfgr.toml`) — per **Textual-TUI**, optionaler **Qt6-Desktop-GUI** (Wayland/X11/macOS/Windows) oder **Click-CLI**.
|
|
52
52
|
|
|
@@ -55,6 +55,7 @@ Description-Content-Type: text/markdown
|
|
|
55
55
|
- **Eine einzige Datei neben `~/.ssh/config`:** Alles liegt in `~/.ssh/sshcnfgr.toml` (auf Linux/macOS automatisch mit `0600`-Rechten, unter Windows in `%USERPROFILE%\.ssh\sshcnfgr.toml`) — ideal für den direkten Start per `uvx`.
|
|
56
56
|
- **TOML als Source of Truth:** Deaktivierte Hosts bleiben in der Datenbank erhalten, landen aber nicht in der generierten OpenSSH-Config (`~/.ssh/config.generated`, ebenfalls `0600`).
|
|
57
57
|
- **Dubletten finden & zusammenführen:** Erkennt Hosts mit gleichem Ziel (`HostName` + effektiver `Port` + effektiver `User`), zeigt Feld-Konflikte übersichtlich an und führt Einträge interaktiv zusammen (übrige Dubletten wahlweise deaktivieren oder löschen).
|
|
58
|
+
- **Gruppen-Reihenfolge (Drag & Drop) & `Match originalhost`-Regeln:** Sortiere Gruppen in der Qt6-GUI frei per Drag & Drop — `config.generated` (und damit auch **VS Code Remote-SSH**) listet Hosts nach ihrer ersten Gruppe in genau dieser Reihenfolge. Gruppen-Defaults (`Identity`, `User`, `Port`, `ProxyJump`, `IdentitiesOnly`, …) werden als saubere OpenSSH-`Match originalhost`-Blöcke erzeugt.
|
|
58
59
|
- **TUI, Qt6-GUI & CLI:** Im Terminal mit Textual, auf dem Desktop optional mit PySide6/Qt6 (Live-Suche, Gruppen-/Identity-Tabs, `extra_options`-Editor, Config-Vorschau) oder skriptbar über die CLI.
|
|
59
60
|
- **Sicherheitsnetz:** Keine Speicherung privater Schlüssel-Inhalte (nur Pfade & `.pub`-Metadaten), atomares Schreiben und automatische Zeitstempel-Backups (`*.bak.YYYYMMDD-HHMMSS`) vor jedem Überschreiben.
|
|
60
61
|
|
|
@@ -62,7 +63,7 @@ Description-Content-Type: text/markdown
|
|
|
62
63
|
|
|
63
64
|
## Schnellstart mit `uv` / `uvx`
|
|
64
65
|
|
|
65
|
-
> **Hinweis zur Beta-Version (`0.2.
|
|
66
|
+
> **Hinweis zur Beta-Version (`0.2.0b2`):** Da es sich um eine Vorabversion handelt, benötigt `uvx` / `uv tool install` aktuell das Flag `--prerelease=allow`. Ab dem regulären `0.2.0`-Release entfällt dieses Flag.
|
|
66
67
|
|
|
67
68
|
### Ohne Installation direkt ausführen (`uvx`)
|
|
68
69
|
|
|
@@ -212,7 +213,7 @@ Weitere Dokumente: [CHANGELOG.md](CHANGELOG.md), [SECURITY.md](SECURITY.md), [CO
|
|
|
212
213
|
|
|
213
214
|
### Quickstart
|
|
214
215
|
```bash
|
|
215
|
-
# Run directly with uvx (during 0.2.
|
|
216
|
+
# Run directly with uvx (during 0.2.0b2 beta, pass --prerelease=allow)
|
|
216
217
|
uvx --prerelease=allow sshcnfgr
|
|
217
218
|
uvx --prerelease=allow --from "sshcnfgr[qt]" sshcnfgr gui
|
|
218
219
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://github.com/thomsbe/SSHCnfgr/blob/main/LICENSE)
|
|
6
6
|
[](https://www.python.org/)
|
|
7
|
-
[](https://pypi.org/project/sshcnfgr/)
|
|
8
8
|
|
|
9
9
|
**SSHCnfgr** verwaltet deine OpenSSH-Hosts, Gruppen und Identities über eine einzige, saubere TOML-Datei (`~/.ssh/sshcnfgr.toml`) — per **Textual-TUI**, optionaler **Qt6-Desktop-GUI** (Wayland/X11/macOS/Windows) oder **Click-CLI**.
|
|
10
10
|
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
- **Eine einzige Datei neben `~/.ssh/config`:** Alles liegt in `~/.ssh/sshcnfgr.toml` (auf Linux/macOS automatisch mit `0600`-Rechten, unter Windows in `%USERPROFILE%\.ssh\sshcnfgr.toml`) — ideal für den direkten Start per `uvx`.
|
|
14
14
|
- **TOML als Source of Truth:** Deaktivierte Hosts bleiben in der Datenbank erhalten, landen aber nicht in der generierten OpenSSH-Config (`~/.ssh/config.generated`, ebenfalls `0600`).
|
|
15
15
|
- **Dubletten finden & zusammenführen:** Erkennt Hosts mit gleichem Ziel (`HostName` + effektiver `Port` + effektiver `User`), zeigt Feld-Konflikte übersichtlich an und führt Einträge interaktiv zusammen (übrige Dubletten wahlweise deaktivieren oder löschen).
|
|
16
|
+
- **Gruppen-Reihenfolge (Drag & Drop) & `Match originalhost`-Regeln:** Sortiere Gruppen in der Qt6-GUI frei per Drag & Drop — `config.generated` (und damit auch **VS Code Remote-SSH**) listet Hosts nach ihrer ersten Gruppe in genau dieser Reihenfolge. Gruppen-Defaults (`Identity`, `User`, `Port`, `ProxyJump`, `IdentitiesOnly`, …) werden als saubere OpenSSH-`Match originalhost`-Blöcke erzeugt.
|
|
16
17
|
- **TUI, Qt6-GUI & CLI:** Im Terminal mit Textual, auf dem Desktop optional mit PySide6/Qt6 (Live-Suche, Gruppen-/Identity-Tabs, `extra_options`-Editor, Config-Vorschau) oder skriptbar über die CLI.
|
|
17
18
|
- **Sicherheitsnetz:** Keine Speicherung privater Schlüssel-Inhalte (nur Pfade & `.pub`-Metadaten), atomares Schreiben und automatische Zeitstempel-Backups (`*.bak.YYYYMMDD-HHMMSS`) vor jedem Überschreiben.
|
|
18
19
|
|
|
@@ -20,7 +21,7 @@
|
|
|
20
21
|
|
|
21
22
|
## Schnellstart mit `uv` / `uvx`
|
|
22
23
|
|
|
23
|
-
> **Hinweis zur Beta-Version (`0.2.
|
|
24
|
+
> **Hinweis zur Beta-Version (`0.2.0b2`):** Da es sich um eine Vorabversion handelt, benötigt `uvx` / `uv tool install` aktuell das Flag `--prerelease=allow`. Ab dem regulären `0.2.0`-Release entfällt dieses Flag.
|
|
24
25
|
|
|
25
26
|
### Ohne Installation direkt ausführen (`uvx`)
|
|
26
27
|
|
|
@@ -170,7 +171,7 @@ Weitere Dokumente: [CHANGELOG.md](CHANGELOG.md), [SECURITY.md](SECURITY.md), [CO
|
|
|
170
171
|
|
|
171
172
|
### Quickstart
|
|
172
173
|
```bash
|
|
173
|
-
# Run directly with uvx (during 0.2.
|
|
174
|
+
# Run directly with uvx (during 0.2.0b2 beta, pass --prerelease=allow)
|
|
174
175
|
uvx --prerelease=allow sshcnfgr
|
|
175
176
|
uvx --prerelease=allow --from "sshcnfgr[qt]" sshcnfgr gui
|
|
176
177
|
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .model import HostDatabase, HostEntry, HostGroup, get_option_ci
|
|
7
|
+
from .paths import GENERATED_CONFIG_PATH, SSH_DIR, ensure_private_dir, set_private_permissions
|
|
8
|
+
from .store import create_backup
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _resolve_direct_host_identity_file(host: HostEntry, db: HostDatabase) -> str | None:
|
|
12
|
+
"""Loest nur host-eigene Identity-Angaben auf (ohne Gruppen-/Global-Defaults)."""
|
|
13
|
+
if host.identity_file:
|
|
14
|
+
return host.identity_file
|
|
15
|
+
if host.identity:
|
|
16
|
+
profile = db.get_identity_by_name(host.identity)
|
|
17
|
+
if profile and profile.enabled:
|
|
18
|
+
return profile.identity_file
|
|
19
|
+
return None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _resolve_group_identity_file(group: HostGroup, db: HostDatabase) -> str | None:
|
|
23
|
+
if not group.identity:
|
|
24
|
+
return None
|
|
25
|
+
profile = db.get_identity_by_name(group.identity)
|
|
26
|
+
if profile and profile.enabled:
|
|
27
|
+
return profile.identity_file
|
|
28
|
+
return None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def render_host_block(
|
|
32
|
+
host: HostEntry,
|
|
33
|
+
db: HostDatabase,
|
|
34
|
+
*,
|
|
35
|
+
inline_group_defaults: bool = False,
|
|
36
|
+
) -> str:
|
|
37
|
+
lines: list[str] = [f"Host {host.name}"]
|
|
38
|
+
lines.append(f" HostName {host.host_name}")
|
|
39
|
+
|
|
40
|
+
if host.user:
|
|
41
|
+
lines.append(f" User {host.user}")
|
|
42
|
+
if host.port is not None:
|
|
43
|
+
lines.append(f" Port {host.port}")
|
|
44
|
+
|
|
45
|
+
global_identity_file = get_option_ci(db.global_options, "IdentityFile")
|
|
46
|
+
resolved_identity_file = (
|
|
47
|
+
db.resolve_identity_file(host)
|
|
48
|
+
if inline_group_defaults
|
|
49
|
+
else _resolve_direct_host_identity_file(host, db)
|
|
50
|
+
)
|
|
51
|
+
# Der globale IdentityFile-Default wird schon im Host-*-Block ausgegeben
|
|
52
|
+
# und muss nicht in jeden Host-Block wiederholt werden.
|
|
53
|
+
if resolved_identity_file and resolved_identity_file != global_identity_file:
|
|
54
|
+
lines.append(f" IdentityFile {resolved_identity_file}")
|
|
55
|
+
|
|
56
|
+
for key, value in sorted(host.extra_options.items(), key=lambda item: item[0].lower()):
|
|
57
|
+
lines.append(f" {key} {value}")
|
|
58
|
+
|
|
59
|
+
return "\n".join(lines)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def render_group_match_block(
|
|
63
|
+
group: HostGroup,
|
|
64
|
+
member_aliases: list[str],
|
|
65
|
+
db: HostDatabase,
|
|
66
|
+
) -> str | None:
|
|
67
|
+
"""Erzeugt einen OpenSSH 'Match originalhost'-Block fuer Gruppen-Defaults."""
|
|
68
|
+
if not member_aliases:
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
option_lines: list[str] = []
|
|
72
|
+
if group.user:
|
|
73
|
+
option_lines.append(f" User {group.user}")
|
|
74
|
+
if group.port is not None:
|
|
75
|
+
option_lines.append(f" Port {group.port}")
|
|
76
|
+
|
|
77
|
+
global_identity_file = get_option_ci(db.global_options, "IdentityFile")
|
|
78
|
+
group_identity_file = _resolve_group_identity_file(group, db)
|
|
79
|
+
if group_identity_file and group_identity_file != global_identity_file:
|
|
80
|
+
option_lines.append(f" IdentityFile {group_identity_file}")
|
|
81
|
+
|
|
82
|
+
for key, value in sorted(group.extra_options.items(), key=lambda item: item[0].lower()):
|
|
83
|
+
option_lines.append(f" {key} {value}")
|
|
84
|
+
|
|
85
|
+
if not option_lines:
|
|
86
|
+
return None
|
|
87
|
+
|
|
88
|
+
header_comment = f"# Gruppe: {group.name}"
|
|
89
|
+
if group.description:
|
|
90
|
+
header_comment += f" ({group.description})"
|
|
91
|
+
|
|
92
|
+
aliases_csv = ",".join(sorted(member_aliases, key=str.lower))
|
|
93
|
+
return "\n".join([header_comment, f"Match originalhost {aliases_csv}", *option_lines])
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def generate_config(
|
|
97
|
+
db: HostDatabase,
|
|
98
|
+
*,
|
|
99
|
+
include_timestamp: bool = True,
|
|
100
|
+
use_group_match: bool = True,
|
|
101
|
+
) -> str:
|
|
102
|
+
header = ["# Generated by sshcnfgr"]
|
|
103
|
+
if include_timestamp:
|
|
104
|
+
header.append(f"# Timestamp: {datetime.now().isoformat(timespec='seconds')}")
|
|
105
|
+
header.append("")
|
|
106
|
+
|
|
107
|
+
# Globale Include-Direktiven gehoeren vor den ersten Host-Block, nicht in Host *.
|
|
108
|
+
include_lines: list[str] = []
|
|
109
|
+
wildcard_options: list[tuple[str, str]] = []
|
|
110
|
+
for key, value in sorted(db.global_options.items(), key=lambda item: item[0].lower()):
|
|
111
|
+
if key.lower() == "include":
|
|
112
|
+
include_lines.append(f"{key} {value}")
|
|
113
|
+
else:
|
|
114
|
+
wildcard_options.append((key, value))
|
|
115
|
+
|
|
116
|
+
blocks: list[str] = []
|
|
117
|
+
if include_lines:
|
|
118
|
+
blocks.append("\n".join(include_lines))
|
|
119
|
+
|
|
120
|
+
# Aktive Hosts sortiert nach ihrer ersten Gruppe (in Nutzer-Gruppenreihenfolge),
|
|
121
|
+
# damit u. a. VS Code Remote-SSH wichtige Gruppen ganz oben anzeigt.
|
|
122
|
+
sections = db.grouped_hosts(enabled_only=True)
|
|
123
|
+
has_named_groups = any(group is not None for group, _ in sections)
|
|
124
|
+
|
|
125
|
+
for group, members in sections:
|
|
126
|
+
section_blocks: list[str] = []
|
|
127
|
+
if has_named_groups:
|
|
128
|
+
if group is not None:
|
|
129
|
+
title = f"# === Gruppe: {group.name} ==="
|
|
130
|
+
if group.description:
|
|
131
|
+
title = f"# === Gruppe: {group.name} ({group.description}) ==="
|
|
132
|
+
section_blocks.append(title)
|
|
133
|
+
else:
|
|
134
|
+
section_blocks.append("# === Ohne Gruppe ===")
|
|
135
|
+
|
|
136
|
+
for host in members:
|
|
137
|
+
section_blocks.append(
|
|
138
|
+
render_host_block(host, db, inline_group_defaults=not use_group_match)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
if has_named_groups:
|
|
142
|
+
# Abschnittskommentar direkt ueber den ersten Host-Block setzen.
|
|
143
|
+
first_host = section_blocks[1]
|
|
144
|
+
blocks.append(f"{section_blocks[0]}\n{first_host}")
|
|
145
|
+
blocks.extend(section_blocks[2:])
|
|
146
|
+
else:
|
|
147
|
+
blocks.extend(section_blocks)
|
|
148
|
+
|
|
149
|
+
# Gruppen-Defaults als 'Match originalhost'-Bloecke nach den Host-Bloecken
|
|
150
|
+
# (Host-Werte gewinnen vor Gruppen-Defaults, Gruppen-Defaults vor Host *).
|
|
151
|
+
if use_group_match:
|
|
152
|
+
enabled_hosts = [host for host in db.hosts if host.enabled]
|
|
153
|
+
match_blocks: list[str] = []
|
|
154
|
+
for group in db.ordered_groups():
|
|
155
|
+
aliases = [host.name for host in enabled_hosts if group.name in host.groups]
|
|
156
|
+
rendered_match = render_group_match_block(group, aliases, db)
|
|
157
|
+
if rendered_match:
|
|
158
|
+
match_blocks.append(rendered_match)
|
|
159
|
+
if match_blocks:
|
|
160
|
+
blocks.append(f"# === Gruppen-Defaults (Match) ===\n{match_blocks[0]}")
|
|
161
|
+
blocks.extend(match_blocks[1:])
|
|
162
|
+
|
|
163
|
+
# OpenSSH verwendet pro Parameter den ersten gefundenen Wert, daher gehoeren
|
|
164
|
+
# globale Defaults (Host *) ans Dateiende, sonst schlagen sie Host-Werte.
|
|
165
|
+
if wildcard_options:
|
|
166
|
+
global_lines = ["Host *"]
|
|
167
|
+
for key, value in wildcard_options:
|
|
168
|
+
global_lines.append(f" {key} {value}")
|
|
169
|
+
blocks.append("\n".join(global_lines))
|
|
170
|
+
|
|
171
|
+
return "\n\n".join(["\n".join(header).rstrip(), *blocks]).strip() + "\n"
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def write_generated_config(
|
|
175
|
+
db: HostDatabase,
|
|
176
|
+
output: Path = GENERATED_CONFIG_PATH,
|
|
177
|
+
*,
|
|
178
|
+
backup: bool = True,
|
|
179
|
+
) -> Path:
|
|
180
|
+
if output.parent == SSH_DIR:
|
|
181
|
+
ensure_private_dir(output.parent)
|
|
182
|
+
else:
|
|
183
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
184
|
+
if backup:
|
|
185
|
+
create_backup(output)
|
|
186
|
+
|
|
187
|
+
content = generate_config(db)
|
|
188
|
+
|
|
189
|
+
tmp_path = output.with_name(output.name + ".tmp")
|
|
190
|
+
tmp_path.write_text(content, encoding="utf-8")
|
|
191
|
+
set_private_permissions(tmp_path)
|
|
192
|
+
tmp_path.replace(output)
|
|
193
|
+
return output
|
|
@@ -3,8 +3,8 @@ from __future__ import annotations
|
|
|
3
3
|
import copy
|
|
4
4
|
from pathlib import Path
|
|
5
5
|
|
|
6
|
-
from PySide6.QtCore import Qt
|
|
7
|
-
from PySide6.QtGui import QAction, QColor, QFont, QKeySequence
|
|
6
|
+
from PySide6.QtCore import Qt, Signal
|
|
7
|
+
from PySide6.QtGui import QAction, QColor, QDropEvent, QFont, QKeySequence
|
|
8
8
|
from PySide6.QtWidgets import (
|
|
9
9
|
QAbstractItemView,
|
|
10
10
|
QCheckBox,
|
|
@@ -74,6 +74,53 @@ def _configure_readonly_table(table: QTableWidget, headers: list[str]) -> None:
|
|
|
74
74
|
table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Interactive)
|
|
75
75
|
|
|
76
76
|
|
|
77
|
+
class ReorderableTableWidget(QTableWidget):
|
|
78
|
+
"""QTableWidget mit sauberem Zeilen-Drag&Drop (ohne Zellen-Ueberschreiben)."""
|
|
79
|
+
|
|
80
|
+
row_moved = Signal(int, int)
|
|
81
|
+
|
|
82
|
+
def __init__(self, parent: QWidget | None = None) -> None:
|
|
83
|
+
super().__init__(parent)
|
|
84
|
+
self.setDragEnabled(True)
|
|
85
|
+
self.viewport().setAcceptDrops(True)
|
|
86
|
+
self.setDropIndicatorShown(True)
|
|
87
|
+
self.setDragDropMode(QAbstractItemView.DragDropMode.InternalMove)
|
|
88
|
+
self.setDragDropOverwriteMode(False)
|
|
89
|
+
self.setDefaultDropAction(Qt.DropAction.MoveAction)
|
|
90
|
+
|
|
91
|
+
def dropEvent(self, event: QDropEvent) -> None:
|
|
92
|
+
if event.source() is not self:
|
|
93
|
+
event.ignore()
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
selected_rows = sorted({idx.row() for idx in self.selectedIndexes()})
|
|
97
|
+
if not selected_rows:
|
|
98
|
+
event.ignore()
|
|
99
|
+
return
|
|
100
|
+
from_row = selected_rows[0]
|
|
101
|
+
|
|
102
|
+
drop_pos = event.position().toPoint()
|
|
103
|
+
target_index = self.indexAt(drop_pos)
|
|
104
|
+
indicator = self.dropIndicatorPosition()
|
|
105
|
+
|
|
106
|
+
if (
|
|
107
|
+
not target_index.isValid()
|
|
108
|
+
or indicator == QAbstractItemView.DropIndicatorPosition.OnViewport
|
|
109
|
+
):
|
|
110
|
+
to_row = self.rowCount() - 1
|
|
111
|
+
else:
|
|
112
|
+
to_row = target_index.row()
|
|
113
|
+
if indicator == QAbstractItemView.DropIndicatorPosition.BelowItem:
|
|
114
|
+
to_row += 1
|
|
115
|
+
if from_row < to_row:
|
|
116
|
+
to_row -= 1
|
|
117
|
+
|
|
118
|
+
to_row = max(0, min(to_row, self.rowCount() - 1))
|
|
119
|
+
event.ignore()
|
|
120
|
+
if from_row != to_row:
|
|
121
|
+
self.row_moved.emit(from_row, to_row)
|
|
122
|
+
|
|
123
|
+
|
|
77
124
|
class SSHCnfgrWindow(QMainWindow):
|
|
78
125
|
"""Qt6 Hauptfenster fuer SSHCnfgr mit Hosts-, Gruppen-, Identity- und Vorschau-Tab."""
|
|
79
126
|
|
|
@@ -239,7 +286,9 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
239
286
|
id_file_row.addWidget(btn_pick_key)
|
|
240
287
|
|
|
241
288
|
self.in_host_groups = QLineEdit(details_group)
|
|
242
|
-
self.in_host_groups.setPlaceholderText(
|
|
289
|
+
self.in_host_groups.setPlaceholderText(
|
|
290
|
+
"Kommagetrennt, z. B. prod, db (erste Gruppe bestimmt die Sortierung)"
|
|
291
|
+
)
|
|
243
292
|
|
|
244
293
|
self.in_host_tags = QLineEdit(details_group)
|
|
245
294
|
self.in_host_tags.setPlaceholderText("Kommagetrennt, z. B. k8s, web")
|
|
@@ -316,7 +365,7 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
316
365
|
return tab
|
|
317
366
|
|
|
318
367
|
# ------------------------------------------------------------------
|
|
319
|
-
# Tab 2: Gruppen
|
|
368
|
+
# Tab 2: Gruppen (mit Drag & Drop Reihenfolge & Match-Defaults)
|
|
320
369
|
# ------------------------------------------------------------------
|
|
321
370
|
def _build_groups_tab(self) -> QWidget:
|
|
322
371
|
tab = QWidget(self)
|
|
@@ -326,27 +375,46 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
326
375
|
left_widget = QWidget(splitter)
|
|
327
376
|
left_layout = QVBoxLayout(left_widget)
|
|
328
377
|
|
|
329
|
-
|
|
378
|
+
hint_lbl = QLabel(
|
|
379
|
+
"Tipp: Gruppen per Drag & Drop (oder ▲ / ▼) anordnen — bestimmt die Reihenfolge "
|
|
380
|
+
"in config.generated und VS Code.",
|
|
381
|
+
left_widget,
|
|
382
|
+
)
|
|
383
|
+
hint_lbl.setWordWrap(True)
|
|
384
|
+
left_layout.addWidget(hint_lbl)
|
|
385
|
+
|
|
386
|
+
self.groups_table = ReorderableTableWidget(left_widget)
|
|
330
387
|
_configure_readonly_table(
|
|
331
388
|
self.groups_table,
|
|
332
|
-
["Gruppe", "Beschreibung", "
|
|
389
|
+
["#", "Gruppe", "Beschreibung", "Identity", "User", "Port", "Extra", "Hosts"],
|
|
333
390
|
)
|
|
391
|
+
self.groups_table.setColumnWidth(0, 36)
|
|
334
392
|
self.groups_table.itemSelectionChanged.connect(self._on_group_selection_changed)
|
|
393
|
+
self.groups_table.row_moved.connect(self._on_group_row_moved)
|
|
335
394
|
left_layout.addWidget(self.groups_table, stretch=1)
|
|
336
395
|
|
|
337
396
|
group_list_btns = QHBoxLayout()
|
|
338
397
|
btn_new_group = QPushButton("Neue Gruppe", left_widget)
|
|
339
398
|
btn_new_group.clicked.connect(self.action_new_group)
|
|
399
|
+
self.btn_group_up = QPushButton("▲ Hoch", left_widget)
|
|
400
|
+
self.btn_group_up.clicked.connect(self.action_move_group_up)
|
|
401
|
+
self.btn_group_down = QPushButton("▼ Runter", left_widget)
|
|
402
|
+
self.btn_group_down.clicked.connect(self.action_move_group_down)
|
|
340
403
|
btn_del_group = QPushButton("Loeschen", left_widget)
|
|
341
404
|
btn_del_group.clicked.connect(self.action_delete_group)
|
|
342
405
|
group_list_btns.addWidget(btn_new_group)
|
|
343
|
-
group_list_btns.addWidget(
|
|
406
|
+
group_list_btns.addWidget(self.btn_group_up)
|
|
407
|
+
group_list_btns.addWidget(self.btn_group_down)
|
|
344
408
|
group_list_btns.addStretch(1)
|
|
409
|
+
group_list_btns.addWidget(btn_del_group)
|
|
345
410
|
left_layout.addLayout(group_list_btns)
|
|
346
411
|
|
|
347
|
-
|
|
412
|
+
right_scroll = QScrollArea(splitter)
|
|
413
|
+
right_scroll.setWidgetResizable(True)
|
|
414
|
+
right_widget = QWidget(right_scroll)
|
|
348
415
|
right_layout = QVBoxLayout(right_widget)
|
|
349
|
-
|
|
416
|
+
|
|
417
|
+
box = QGroupBox("Gruppen-Details & Match-Defaults", right_widget)
|
|
350
418
|
form = QFormLayout(box)
|
|
351
419
|
|
|
352
420
|
self.in_group_name = QLineEdit(box)
|
|
@@ -354,20 +422,54 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
354
422
|
self.in_group_desc = QLineEdit(box)
|
|
355
423
|
self.in_group_desc.setPlaceholderText("optionale Beschreibung")
|
|
356
424
|
self.in_group_identity = QComboBox(box)
|
|
425
|
+
self.in_group_user = QLineEdit(box)
|
|
426
|
+
self.in_group_user.setPlaceholderText("optionaler Default-User fuer Match-Regel")
|
|
427
|
+
self.in_group_port = QLineEdit(box)
|
|
428
|
+
self.in_group_port.setPlaceholderText("optionaler Default-Port (1-65535)")
|
|
357
429
|
|
|
358
430
|
form.addRow("Name:", self.in_group_name)
|
|
359
431
|
form.addRow("Beschreibung:", self.in_group_desc)
|
|
360
432
|
form.addRow("Default-Identity:", self.in_group_identity)
|
|
433
|
+
form.addRow("Default-User:", self.in_group_user)
|
|
434
|
+
form.addRow("Default-Port:", self.in_group_port)
|
|
361
435
|
right_layout.addWidget(box)
|
|
362
436
|
|
|
437
|
+
extra_group = QGroupBox(
|
|
438
|
+
"Gruppen Extra-Optionen (Match originalhost, z. B. IdentitiesOnly, ProxyJump)",
|
|
439
|
+
right_widget,
|
|
440
|
+
)
|
|
441
|
+
extra_layout = QVBoxLayout(extra_group)
|
|
442
|
+
self.group_extra_options_table = QTableWidget(0, 2, extra_group)
|
|
443
|
+
self.group_extra_options_table.setHorizontalHeaderLabels(["Direktive", "Wert"])
|
|
444
|
+
self.group_extra_options_table.verticalHeader().setVisible(False)
|
|
445
|
+
self.group_extra_options_table.horizontalHeader().setStretchLastSection(True)
|
|
446
|
+
self.group_extra_options_table.setSelectionBehavior(
|
|
447
|
+
QAbstractItemView.SelectionBehavior.SelectRows
|
|
448
|
+
)
|
|
449
|
+
self.group_extra_options_table.setMinimumHeight(130)
|
|
450
|
+
extra_layout.addWidget(self.group_extra_options_table)
|
|
451
|
+
|
|
452
|
+
g_extra_btns = QHBoxLayout()
|
|
453
|
+
btn_g_add_extra = QPushButton("+ Option", extra_group)
|
|
454
|
+
btn_g_add_extra.clicked.connect(self._add_group_extra_option_row)
|
|
455
|
+
btn_g_rm_extra = QPushButton("- Entfernen", extra_group)
|
|
456
|
+
btn_g_rm_extra.clicked.connect(self._remove_group_extra_option_row)
|
|
457
|
+
g_extra_btns.addWidget(btn_g_add_extra)
|
|
458
|
+
g_extra_btns.addWidget(btn_g_rm_extra)
|
|
459
|
+
g_extra_btns.addStretch(1)
|
|
460
|
+
extra_layout.addLayout(g_extra_btns)
|
|
461
|
+
right_layout.addWidget(extra_group)
|
|
462
|
+
|
|
363
463
|
btn_save_group = QPushButton("Gruppe speichern", right_widget)
|
|
364
464
|
btn_save_group.clicked.connect(self.action_save_group)
|
|
365
465
|
right_layout.addWidget(btn_save_group)
|
|
366
466
|
right_layout.addStretch(1)
|
|
367
467
|
|
|
468
|
+
right_scroll.setWidget(right_widget)
|
|
469
|
+
|
|
368
470
|
splitter.addWidget(left_widget)
|
|
369
|
-
splitter.addWidget(
|
|
370
|
-
splitter.setSizes([
|
|
471
|
+
splitter.addWidget(right_scroll)
|
|
472
|
+
splitter.setSizes([640, 480])
|
|
371
473
|
root_layout.addWidget(splitter)
|
|
372
474
|
return tab
|
|
373
475
|
|
|
@@ -544,7 +646,9 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
544
646
|
needle = self.host_search_input.text().strip().lower()
|
|
545
647
|
only_active = self.host_only_active_cb.isChecked()
|
|
546
648
|
hosts = [
|
|
547
|
-
h
|
|
649
|
+
h
|
|
650
|
+
for h in self.db.ordered_hosts_by_group()
|
|
651
|
+
if self._host_matches_filter(h, needle, only_active)
|
|
548
652
|
]
|
|
549
653
|
|
|
550
654
|
table = self.hosts_table
|
|
@@ -575,7 +679,7 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
575
679
|
_make_item(
|
|
576
680
|
user_text,
|
|
577
681
|
muted=user_muted,
|
|
578
|
-
tooltip="Geerbt
|
|
682
|
+
tooltip="Geerbt via Gruppe oder Host *" if user_muted else None,
|
|
579
683
|
),
|
|
580
684
|
)
|
|
581
685
|
table.setItem(
|
|
@@ -584,7 +688,7 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
584
688
|
_make_item(
|
|
585
689
|
port_text,
|
|
586
690
|
muted=port_muted,
|
|
587
|
-
tooltip="Effektiver Port (
|
|
691
|
+
tooltip="Effektiver Port (Gruppe/Global/Default)" if port_muted else None,
|
|
588
692
|
),
|
|
589
693
|
)
|
|
590
694
|
table.setItem(
|
|
@@ -603,7 +707,6 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
603
707
|
table.selectRow(selected_row)
|
|
604
708
|
finally:
|
|
605
709
|
table.blockSignals(False)
|
|
606
|
-
table.setSortingEnabled(True)
|
|
607
710
|
|
|
608
711
|
def _describe_identity(self, host: HostEntry) -> tuple[str, bool, str | None]:
|
|
609
712
|
if host.identity_file:
|
|
@@ -619,7 +722,7 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
619
722
|
return "", False, None
|
|
620
723
|
|
|
621
724
|
def _refresh_groups_table(self) -> None:
|
|
622
|
-
groups = self.db.
|
|
725
|
+
groups = self.db.ordered_groups()
|
|
623
726
|
table = self.groups_table
|
|
624
727
|
table.blockSignals(True)
|
|
625
728
|
try:
|
|
@@ -629,10 +732,18 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
629
732
|
if group.name == self.selected_group_name:
|
|
630
733
|
selected_row = row
|
|
631
734
|
host_count = sum(1 for h in self.db.hosts if group.name in h.groups)
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
735
|
+
extra_summary = ", ".join(
|
|
736
|
+
f"{k}={v}"
|
|
737
|
+
for k, v in sorted(group.extra_options.items(), key=lambda p: p[0].lower())
|
|
738
|
+
)
|
|
739
|
+
table.setItem(row, 0, _make_item(str(row + 1), user_data=group.name))
|
|
740
|
+
table.setItem(row, 1, _make_item(group.name, user_data=group.name))
|
|
741
|
+
table.setItem(row, 2, _make_item(group.description))
|
|
742
|
+
table.setItem(row, 3, _make_item(group.identity or ""))
|
|
743
|
+
table.setItem(row, 4, _make_item(group.user or ""))
|
|
744
|
+
table.setItem(row, 5, _make_item(str(group.port) if group.port is not None else ""))
|
|
745
|
+
table.setItem(row, 6, _make_item(extra_summary))
|
|
746
|
+
table.setItem(row, 7, _make_item(str(host_count)))
|
|
636
747
|
if selected_row >= 0:
|
|
637
748
|
table.selectRow(selected_row)
|
|
638
749
|
finally:
|
|
@@ -888,17 +999,74 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
888
999
|
selected_rows = self.groups_table.selectionModel().selectedRows()
|
|
889
1000
|
if not selected_rows:
|
|
890
1001
|
return
|
|
891
|
-
item = self.groups_table.item(selected_rows[0].row(),
|
|
1002
|
+
item = self.groups_table.item(selected_rows[0].row(), 1)
|
|
892
1003
|
if item is None:
|
|
893
1004
|
return
|
|
894
1005
|
group_name = str(item.data(Qt.ItemDataRole.UserRole))
|
|
895
1006
|
group = self.db.get_group_by_name(group_name)
|
|
896
1007
|
if group is None:
|
|
897
1008
|
return
|
|
1009
|
+
self._load_group_into_form(group)
|
|
1010
|
+
|
|
1011
|
+
def _load_group_into_form(self, group: HostGroup) -> None:
|
|
898
1012
|
self.selected_group_name = group.name
|
|
899
1013
|
self.in_group_name.setText(group.name)
|
|
900
1014
|
self.in_group_desc.setText(group.description)
|
|
901
1015
|
self._populate_identity_combo(self.in_group_identity, group.identity)
|
|
1016
|
+
self.in_group_user.setText(group.user or "")
|
|
1017
|
+
self.in_group_port.setText(str(group.port) if group.port is not None else "")
|
|
1018
|
+
|
|
1019
|
+
self.group_extra_options_table.setRowCount(0)
|
|
1020
|
+
for key, value in sorted(group.extra_options.items(), key=lambda pair: pair[0].lower()):
|
|
1021
|
+
row = self.group_extra_options_table.rowCount()
|
|
1022
|
+
self.group_extra_options_table.insertRow(row)
|
|
1023
|
+
self.group_extra_options_table.setItem(row, 0, QTableWidgetItem(key))
|
|
1024
|
+
self.group_extra_options_table.setItem(row, 1, QTableWidgetItem(value))
|
|
1025
|
+
|
|
1026
|
+
def _add_group_extra_option_row(self) -> None:
|
|
1027
|
+
row = self.group_extra_options_table.rowCount()
|
|
1028
|
+
self.group_extra_options_table.insertRow(row)
|
|
1029
|
+
self.group_extra_options_table.setItem(row, 0, QTableWidgetItem(""))
|
|
1030
|
+
self.group_extra_options_table.setItem(row, 1, QTableWidgetItem(""))
|
|
1031
|
+
self.group_extra_options_table.editItem(self.group_extra_options_table.item(row, 0))
|
|
1032
|
+
|
|
1033
|
+
def _remove_group_extra_option_row(self) -> None:
|
|
1034
|
+
rows = sorted(
|
|
1035
|
+
{idx.row() for idx in self.group_extra_options_table.selectionModel().selectedRows()},
|
|
1036
|
+
reverse=True,
|
|
1037
|
+
)
|
|
1038
|
+
for row in rows:
|
|
1039
|
+
self.group_extra_options_table.removeRow(row)
|
|
1040
|
+
|
|
1041
|
+
def _on_group_row_moved(self, from_row: int, to_row: int) -> None:
|
|
1042
|
+
candidate_db = copy.deepcopy(self.db)
|
|
1043
|
+
if not candidate_db.move_group(from_row, to_row):
|
|
1044
|
+
return
|
|
1045
|
+
moved_group = candidate_db.groups[to_row]
|
|
1046
|
+
if not self._commit_db(candidate_db):
|
|
1047
|
+
return
|
|
1048
|
+
self.selected_group_name = moved_group.name
|
|
1049
|
+
self._refresh_all()
|
|
1050
|
+
self._load_group_into_form(moved_group)
|
|
1051
|
+
self._set_status(f"Gruppe '{moved_group.name}' auf Position {to_row + 1} verschoben")
|
|
1052
|
+
|
|
1053
|
+
def _selected_group_row(self) -> int | None:
|
|
1054
|
+
selected_rows = self.groups_table.selectionModel().selectedRows()
|
|
1055
|
+
if not selected_rows:
|
|
1056
|
+
return None
|
|
1057
|
+
return selected_rows[0].row()
|
|
1058
|
+
|
|
1059
|
+
def action_move_group_up(self) -> None:
|
|
1060
|
+
row = self._selected_group_row()
|
|
1061
|
+
if row is None or row <= 0:
|
|
1062
|
+
return
|
|
1063
|
+
self._on_group_row_moved(row, row - 1)
|
|
1064
|
+
|
|
1065
|
+
def action_move_group_down(self) -> None:
|
|
1066
|
+
row = self._selected_group_row()
|
|
1067
|
+
if row is None or row >= len(self.db.groups) - 1:
|
|
1068
|
+
return
|
|
1069
|
+
self._on_group_row_moved(row, row + 1)
|
|
902
1070
|
|
|
903
1071
|
def action_new_group(self) -> None:
|
|
904
1072
|
self.selected_group_name = None
|
|
@@ -906,6 +1074,9 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
906
1074
|
self.in_group_name.clear()
|
|
907
1075
|
self.in_group_desc.clear()
|
|
908
1076
|
self._populate_identity_combo(self.in_group_identity, None)
|
|
1077
|
+
self.in_group_user.clear()
|
|
1078
|
+
self.in_group_port.clear()
|
|
1079
|
+
self.group_extra_options_table.setRowCount(0)
|
|
909
1080
|
self.in_group_name.setFocus()
|
|
910
1081
|
|
|
911
1082
|
def action_save_group(self) -> None:
|
|
@@ -913,6 +1084,36 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
913
1084
|
desc = self.in_group_desc.text().strip()
|
|
914
1085
|
identity_data = self.in_group_identity.currentData()
|
|
915
1086
|
identity = str(identity_data) if identity_data else None
|
|
1087
|
+
user = self.in_group_user.text().strip() or None
|
|
1088
|
+
|
|
1089
|
+
form_errors: list[str] = []
|
|
1090
|
+
port: int | None = None
|
|
1091
|
+
port_raw = self.in_group_port.text().strip()
|
|
1092
|
+
if port_raw:
|
|
1093
|
+
try:
|
|
1094
|
+
port = int(port_raw)
|
|
1095
|
+
except ValueError:
|
|
1096
|
+
form_errors.append(f"Port ist keine Zahl: {port_raw}")
|
|
1097
|
+
|
|
1098
|
+
extra_options: dict[str, str] = {}
|
|
1099
|
+
for row in range(self.group_extra_options_table.rowCount()):
|
|
1100
|
+
key_item = self.group_extra_options_table.item(row, 0)
|
|
1101
|
+
val_item = self.group_extra_options_table.item(row, 1)
|
|
1102
|
+
key = key_item.text().strip() if key_item else ""
|
|
1103
|
+
val = val_item.text().strip() if val_item else ""
|
|
1104
|
+
if not key and not val:
|
|
1105
|
+
continue
|
|
1106
|
+
if not key or not val:
|
|
1107
|
+
form_errors.append(f"Gruppen-Option in Zeile {row + 1} ist unvollstaendig")
|
|
1108
|
+
continue
|
|
1109
|
+
if any(ch.isspace() for ch in key):
|
|
1110
|
+
form_errors.append(f"Gruppen-Option '{key}' darf keine Leerzeichen enthalten")
|
|
1111
|
+
continue
|
|
1112
|
+
extra_options[key] = val
|
|
1113
|
+
|
|
1114
|
+
if form_errors:
|
|
1115
|
+
QMessageBox.warning(self, "Validierungsfehler", "\n".join(form_errors))
|
|
1116
|
+
return
|
|
916
1117
|
|
|
917
1118
|
candidate_db = copy.deepcopy(self.db)
|
|
918
1119
|
existing = (
|
|
@@ -921,12 +1122,24 @@ class SSHCnfgrWindow(QMainWindow):
|
|
|
921
1122
|
else candidate_db.get_group_by_name(name)
|
|
922
1123
|
)
|
|
923
1124
|
if existing is None:
|
|
924
|
-
candidate_db.groups.append(
|
|
1125
|
+
candidate_db.groups.append(
|
|
1126
|
+
HostGroup(
|
|
1127
|
+
name=name,
|
|
1128
|
+
description=desc,
|
|
1129
|
+
identity=identity,
|
|
1130
|
+
user=user,
|
|
1131
|
+
port=port,
|
|
1132
|
+
extra_options=extra_options,
|
|
1133
|
+
)
|
|
1134
|
+
)
|
|
925
1135
|
else:
|
|
926
1136
|
old_name = existing.name
|
|
927
1137
|
existing.name = name
|
|
928
1138
|
existing.description = desc
|
|
929
1139
|
existing.identity = identity
|
|
1140
|
+
existing.user = user
|
|
1141
|
+
existing.port = port
|
|
1142
|
+
existing.extra_options = extra_options
|
|
930
1143
|
if old_name != name:
|
|
931
1144
|
for host in candidate_db.hosts:
|
|
932
1145
|
host.groups = [name if g == old_name else g for g in host.groups]
|
|
@@ -20,17 +20,37 @@ class HostGroup:
|
|
|
20
20
|
name: str
|
|
21
21
|
description: str = ""
|
|
22
22
|
identity: str | None = None
|
|
23
|
+
user: str | None = None
|
|
24
|
+
port: int | None = None
|
|
25
|
+
extra_options: dict[str, str] = field(default_factory=dict)
|
|
23
26
|
|
|
24
27
|
def validate(self) -> list[str]:
|
|
25
28
|
errors: list[str] = []
|
|
26
29
|
if not self.name:
|
|
27
30
|
errors.append("group.name darf nicht leer sein")
|
|
28
|
-
if
|
|
31
|
+
if any(ch.isspace() for ch in self.name):
|
|
29
32
|
errors.append("group.name darf keine Leerzeichen enthalten")
|
|
30
|
-
if self.identity is not None and
|
|
33
|
+
if self.identity is not None and any(ch.isspace() for ch in self.identity):
|
|
31
34
|
errors.append("group.identity darf keine Leerzeichen enthalten")
|
|
35
|
+
if self.port is not None and not (1 <= self.port <= 65535):
|
|
36
|
+
errors.append("group.port muss zwischen 1 und 65535 liegen")
|
|
37
|
+
for key, value in self.extra_options.items():
|
|
38
|
+
if not key or any(ch.isspace() for ch in key):
|
|
39
|
+
errors.append(f"group.extra_options Schluessel ungueltig: '{key}'")
|
|
40
|
+
if not value:
|
|
41
|
+
errors.append(f"group.extra_options Wert fuer '{key}' darf nicht leer sein")
|
|
32
42
|
return errors
|
|
33
43
|
|
|
44
|
+
def has_ssh_defaults(self, db: HostDatabase) -> bool:
|
|
45
|
+
"""Prueft, ob diese Gruppe wirksame SSH-Defaults fuer einen Match-Block hat."""
|
|
46
|
+
if self.user or self.port is not None or self.extra_options:
|
|
47
|
+
return True
|
|
48
|
+
if self.identity:
|
|
49
|
+
identity = db.get_identity_by_name(self.identity)
|
|
50
|
+
if identity and identity.enabled:
|
|
51
|
+
return True
|
|
52
|
+
return False
|
|
53
|
+
|
|
34
54
|
|
|
35
55
|
@dataclass(slots=True)
|
|
36
56
|
class IdentityProfile:
|
|
@@ -155,6 +175,58 @@ class HostDatabase:
|
|
|
155
175
|
def sorted_groups(self) -> list[HostGroup]:
|
|
156
176
|
return sorted(self.groups, key=lambda g: g.name.lower())
|
|
157
177
|
|
|
178
|
+
def ordered_groups(self) -> list[HostGroup]:
|
|
179
|
+
"""Liefert die Gruppen in der vom Nutzer festgelegten Reihenfolge."""
|
|
180
|
+
return list(self.groups)
|
|
181
|
+
|
|
182
|
+
def move_group(self, from_index: int, to_index: int) -> bool:
|
|
183
|
+
"""Verschiebt eine Gruppe innerhalb von self.groups (z. B. per Drag & Drop)."""
|
|
184
|
+
count = len(self.groups)
|
|
185
|
+
if not (0 <= from_index < count) or not (0 <= to_index < count):
|
|
186
|
+
return False
|
|
187
|
+
if from_index == to_index:
|
|
188
|
+
return False
|
|
189
|
+
group = self.groups.pop(from_index)
|
|
190
|
+
self.groups.insert(to_index, group)
|
|
191
|
+
return True
|
|
192
|
+
|
|
193
|
+
def grouped_hosts(
|
|
194
|
+
self, *, enabled_only: bool = False
|
|
195
|
+
) -> list[tuple[HostGroup | None, list[HostEntry]]]:
|
|
196
|
+
"""Gruppiert Hosts nach ihrer ersten Gruppe in der konfigurierten Gruppen-Reihenfolge.
|
|
197
|
+
|
|
198
|
+
Innerhalb jeder Gruppe wird alphabetisch nach Alias sortiert; Hosts ohne Gruppe
|
|
199
|
+
(oder mit unbekannter erster Gruppe) erscheinen am Ende.
|
|
200
|
+
"""
|
|
201
|
+
candidates = [h for h in self.hosts if (not enabled_only or h.enabled)]
|
|
202
|
+
by_group: dict[str, list[HostEntry]] = {group.name: [] for group in self.groups}
|
|
203
|
+
ungrouped: list[HostEntry] = []
|
|
204
|
+
|
|
205
|
+
for host in candidates:
|
|
206
|
+
primary_group = host.groups[0] if host.groups else None
|
|
207
|
+
if primary_group and primary_group in by_group:
|
|
208
|
+
by_group[primary_group].append(host)
|
|
209
|
+
else:
|
|
210
|
+
ungrouped.append(host)
|
|
211
|
+
|
|
212
|
+
sections: list[tuple[HostGroup | None, list[HostEntry]]] = []
|
|
213
|
+
for group in self.groups:
|
|
214
|
+
members = sorted(by_group[group.name], key=lambda h: h.name.lower())
|
|
215
|
+
if members:
|
|
216
|
+
sections.append((group, members))
|
|
217
|
+
|
|
218
|
+
if ungrouped:
|
|
219
|
+
sections.append((None, sorted(ungrouped, key=lambda h: h.name.lower())))
|
|
220
|
+
|
|
221
|
+
return sections
|
|
222
|
+
|
|
223
|
+
def ordered_hosts_by_group(self, *, enabled_only: bool = False) -> list[HostEntry]:
|
|
224
|
+
"""Flache Hostliste in Gruppen-Reihenfolge (nach erster Gruppe, dann Alias)."""
|
|
225
|
+
result: list[HostEntry] = []
|
|
226
|
+
for _, members in self.grouped_hosts(enabled_only=enabled_only):
|
|
227
|
+
result.extend(members)
|
|
228
|
+
return result
|
|
229
|
+
|
|
158
230
|
def sorted_identities(self) -> list[IdentityProfile]:
|
|
159
231
|
return sorted(self.identities, key=lambda i: i.name.lower())
|
|
160
232
|
|
|
@@ -198,11 +270,21 @@ class HostDatabase:
|
|
|
198
270
|
host_agent = get_option_ci(host.extra_options, "IdentityAgent")
|
|
199
271
|
if host_agent is not None:
|
|
200
272
|
return host_agent
|
|
273
|
+
for group_name in host.groups:
|
|
274
|
+
group = self.get_group_by_name(group_name)
|
|
275
|
+
if group:
|
|
276
|
+
group_agent = get_option_ci(group.extra_options, "IdentityAgent")
|
|
277
|
+
if group_agent is not None:
|
|
278
|
+
return group_agent
|
|
201
279
|
return get_option_ci(self.global_options, "IdentityAgent")
|
|
202
280
|
|
|
203
281
|
def resolve_user(self, host: HostEntry) -> str | None:
|
|
204
282
|
if host.user:
|
|
205
283
|
return host.user
|
|
284
|
+
for group_name in host.groups:
|
|
285
|
+
group = self.get_group_by_name(group_name)
|
|
286
|
+
if group and group.user:
|
|
287
|
+
return group.user
|
|
206
288
|
global_user = get_option_ci(self.global_options, "User")
|
|
207
289
|
if global_user:
|
|
208
290
|
return global_user
|
|
@@ -211,6 +293,10 @@ class HostDatabase:
|
|
|
211
293
|
def resolve_port(self, host: HostEntry) -> int:
|
|
212
294
|
if host.port is not None:
|
|
213
295
|
return host.port
|
|
296
|
+
for group_name in host.groups:
|
|
297
|
+
group = self.get_group_by_name(group_name)
|
|
298
|
+
if group and group.port is not None:
|
|
299
|
+
return group.port
|
|
214
300
|
global_port = get_option_ci(self.global_options, "Port")
|
|
215
301
|
if global_port:
|
|
216
302
|
try:
|
|
@@ -64,6 +64,12 @@ def _group_to_dict(group: HostGroup) -> dict[str, object]:
|
|
|
64
64
|
payload["description"] = group.description
|
|
65
65
|
if group.identity:
|
|
66
66
|
payload["identity"] = group.identity
|
|
67
|
+
if group.user is not None:
|
|
68
|
+
payload["user"] = group.user
|
|
69
|
+
if group.port is not None:
|
|
70
|
+
payload["port"] = group.port
|
|
71
|
+
if group.extra_options:
|
|
72
|
+
payload["extra_options"] = group.extra_options
|
|
67
73
|
return payload
|
|
68
74
|
|
|
69
75
|
|
|
@@ -100,10 +106,17 @@ def _host_from_dict(data: dict[str, object]) -> HostEntry:
|
|
|
100
106
|
|
|
101
107
|
|
|
102
108
|
def _group_from_dict(data: dict[str, object]) -> HostGroup:
|
|
109
|
+
extra_raw = data.get("extra_options")
|
|
110
|
+
extra_options: dict[str, str] = {}
|
|
111
|
+
if isinstance(extra_raw, dict):
|
|
112
|
+
extra_options = {str(k): str(v) for k, v in extra_raw.items()}
|
|
103
113
|
return HostGroup(
|
|
104
114
|
name=str(data.get("name", "")),
|
|
105
115
|
description=str(data.get("description", "")) if data.get("description") else "",
|
|
106
116
|
identity=str(data.get("identity", "")) if data.get("identity") else None,
|
|
117
|
+
user=str(data["user"]) if data.get("user") is not None else None,
|
|
118
|
+
port=int(data["port"]) if data.get("port") is not None else None,
|
|
119
|
+
extra_options=extra_options,
|
|
107
120
|
)
|
|
108
121
|
|
|
109
122
|
|
|
@@ -208,7 +221,8 @@ def save_database(db: HostDatabase, path: Path = DB_PATH, *, backup: bool = True
|
|
|
208
221
|
create_backup(path)
|
|
209
222
|
|
|
210
223
|
payload: dict[str, object] = {
|
|
211
|
-
|
|
224
|
+
# Gruppen-Reihenfolge explizit beibehalten (bestimmt Sortierung in config.generated/VS Code)
|
|
225
|
+
"groups": [_group_to_dict(group) for group in db.ordered_groups()],
|
|
212
226
|
"identities": [_identity_to_dict(identity) for identity in db.sorted_identities()],
|
|
213
227
|
"global_options": db.global_options,
|
|
214
228
|
"hosts": [_host_to_dict(host) for host in db.sorted_hosts()],
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from datetime import datetime
|
|
4
|
-
from pathlib import Path
|
|
5
|
-
|
|
6
|
-
from .model import HostDatabase, HostEntry, get_option_ci
|
|
7
|
-
from .paths import GENERATED_CONFIG_PATH, SSH_DIR, ensure_private_dir, set_private_permissions
|
|
8
|
-
from .store import create_backup
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def render_host_block(host: HostEntry, db: HostDatabase) -> str:
|
|
12
|
-
lines: list[str] = [f"Host {host.name}"]
|
|
13
|
-
lines.append(f" HostName {host.host_name}")
|
|
14
|
-
|
|
15
|
-
if host.user:
|
|
16
|
-
lines.append(f" User {host.user}")
|
|
17
|
-
if host.port is not None:
|
|
18
|
-
lines.append(f" Port {host.port}")
|
|
19
|
-
resolved_identity_file = db.resolve_identity_file(host)
|
|
20
|
-
global_identity_file = get_option_ci(db.global_options, "IdentityFile")
|
|
21
|
-
# Der globale IdentityFile-Default wird schon im Host-*-Block ausgegeben
|
|
22
|
-
# und muss nicht in jeden Host-Block wiederholt werden.
|
|
23
|
-
if resolved_identity_file and resolved_identity_file != global_identity_file:
|
|
24
|
-
lines.append(f" IdentityFile {resolved_identity_file}")
|
|
25
|
-
|
|
26
|
-
for key, value in sorted(host.extra_options.items(), key=lambda item: item[0].lower()):
|
|
27
|
-
lines.append(f" {key} {value}")
|
|
28
|
-
|
|
29
|
-
return "\n".join(lines)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def generate_config(db: HostDatabase, *, include_timestamp: bool = True) -> str:
|
|
33
|
-
header = ["# Generated by sshcnfgr"]
|
|
34
|
-
if include_timestamp:
|
|
35
|
-
header.append(f"# Timestamp: {datetime.now().isoformat(timespec='seconds')}")
|
|
36
|
-
header.append("")
|
|
37
|
-
|
|
38
|
-
# Globale Include-Direktiven gehoeren vor den ersten Host-Block, nicht in Host *.
|
|
39
|
-
include_lines: list[str] = []
|
|
40
|
-
wildcard_options: list[tuple[str, str]] = []
|
|
41
|
-
for key, value in sorted(db.global_options.items(), key=lambda item: item[0].lower()):
|
|
42
|
-
if key.lower() == "include":
|
|
43
|
-
include_lines.append(f"{key} {value}")
|
|
44
|
-
else:
|
|
45
|
-
wildcard_options.append((key, value))
|
|
46
|
-
|
|
47
|
-
blocks: list[str] = []
|
|
48
|
-
if include_lines:
|
|
49
|
-
blocks.append("\n".join(include_lines))
|
|
50
|
-
|
|
51
|
-
blocks.extend(render_host_block(host, db) for host in db.sorted_hosts() if host.enabled)
|
|
52
|
-
|
|
53
|
-
# OpenSSH verwendet pro Parameter den ersten gefundenen Wert, daher gehoeren
|
|
54
|
-
# globale Defaults (Host *) ans Dateiende, sonst schlagen sie Host-Werte.
|
|
55
|
-
if wildcard_options:
|
|
56
|
-
global_lines = ["Host *"]
|
|
57
|
-
for key, value in wildcard_options:
|
|
58
|
-
global_lines.append(f" {key} {value}")
|
|
59
|
-
blocks.append("\n".join(global_lines))
|
|
60
|
-
|
|
61
|
-
return "\n\n".join(["\n".join(header).rstrip(), *blocks]).strip() + "\n"
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def write_generated_config(
|
|
65
|
-
db: HostDatabase,
|
|
66
|
-
output: Path = GENERATED_CONFIG_PATH,
|
|
67
|
-
*,
|
|
68
|
-
backup: bool = True,
|
|
69
|
-
) -> Path:
|
|
70
|
-
if output.parent == SSH_DIR:
|
|
71
|
-
ensure_private_dir(output.parent)
|
|
72
|
-
else:
|
|
73
|
-
output.parent.mkdir(parents=True, exist_ok=True)
|
|
74
|
-
if backup:
|
|
75
|
-
create_backup(output)
|
|
76
|
-
|
|
77
|
-
content = generate_config(db)
|
|
78
|
-
|
|
79
|
-
tmp_path = output.with_name(output.name + ".tmp")
|
|
80
|
-
tmp_path.write_text(content, encoding="utf-8")
|
|
81
|
-
set_private_permissions(tmp_path)
|
|
82
|
-
tmp_path.replace(output)
|
|
83
|
-
return output
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|