1panel-toolkit 0.3.0__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.
- 1panel_toolkit-0.3.0.dist-info/METADATA +407 -0
- 1panel_toolkit-0.3.0.dist-info/RECORD +47 -0
- 1panel_toolkit-0.3.0.dist-info/WHEEL +4 -0
- 1panel_toolkit-0.3.0.dist-info/entry_points.txt +2 -0
- 1panel_toolkit-0.3.0.dist-info/licenses/LICENSE +21 -0
- p1toolkit/__init__.py +3 -0
- p1toolkit/checks/__init__.py +21 -0
- p1toolkit/checks/compose.py +188 -0
- p1toolkit/checks/forms.py +129 -0
- p1toolkit/checks/hygiene.py +73 -0
- p1toolkit/checks/i18n.py +140 -0
- p1toolkit/checks/lessons.py +111 -0
- p1toolkit/checks/structure.py +140 -0
- p1toolkit/checks/substance.py +55 -0
- p1toolkit/cli.py +890 -0
- p1toolkit/config.py +114 -0
- p1toolkit/dependencies.py +218 -0
- p1toolkit/deploy.py +302 -0
- p1toolkit/depth.py +414 -0
- p1toolkit/errors.py +18 -0
- p1toolkit/evidence.py +523 -0
- p1toolkit/fix.py +394 -0
- p1toolkit/gen.py +728 -0
- p1toolkit/lifecycle.py +143 -0
- p1toolkit/loader.py +110 -0
- p1toolkit/logo.py +174 -0
- p1toolkit/model.py +71 -0
- p1toolkit/net.py +81 -0
- p1toolkit/panel.py +154 -0
- p1toolkit/patterns/__init__.py +250 -0
- p1toolkit/patterns/catalog.py +271 -0
- p1toolkit/patterns/data/patterns.json +724 -0
- p1toolkit/patterns/extract.py +402 -0
- p1toolkit/remote.py +554 -0
- p1toolkit/report.py +96 -0
- p1toolkit/resolve/__init__.py +554 -0
- p1toolkit/resolve/analyze.py +329 -0
- p1toolkit/resolve/sources.py +306 -0
- p1toolkit/rules/__init__.py +99 -0
- p1toolkit/rules/data/decisions.yaml +132 -0
- p1toolkit/rules/data/dependencies.yaml +113 -0
- p1toolkit/rules/data/envkeys.yaml +19 -0
- p1toolkit/rules/data/form-design.yaml +59 -0
- p1toolkit/rules/data/i18n.yaml +354 -0
- p1toolkit/rules/data/lessons.yaml +84 -0
- p1toolkit/rules/data/store.yaml +171 -0
- p1toolkit/selftest.py +211 -0
p1toolkit/panel.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""Minimal 1Panel API client.
|
|
2
|
+
|
|
3
|
+
Only what the packaging loop needs: prove the credentials work, then later
|
|
4
|
+
upload a local app, install it, read its logs and uninstall it.
|
|
5
|
+
|
|
6
|
+
The authentication scheme is easy to get wrong, so it is spelled out here.
|
|
7
|
+
Upstream `backend/middleware/session.go` (dev branch, 1Panel v2) does:
|
|
8
|
+
|
|
9
|
+
.. code-block:: go
|
|
10
|
+
|
|
11
|
+
panelToken := c.GetHeader("1Panel-Token")
|
|
12
|
+
panelTimestamp := c.GetHeader("1Panel-Timestamp")
|
|
13
|
+
...
|
|
14
|
+
GenerateMD5("1panel" + global.CONF.System.ApiKey + panelTimestamp) == panelToken
|
|
15
|
+
|
|
16
|
+
So on v2 the token header is **not** the API key: it is
|
|
17
|
+
``md5("1panel" + api_key + timestamp)``, the header is ``1Panel-Timestamp``
|
|
18
|
+
(not ``1Panel-Time``), and the routes live under ``/api/v2/``. Sending the raw
|
|
19
|
+
key to ``/api/v1/`` produces the same "API 接口密钥错误" as a wrong key, which
|
|
20
|
+
makes the mistake very hard to see from the response alone.
|
|
21
|
+
|
|
22
|
+
The panel also applies an IP whitelist, and an empty ``SecurityEntrance`` is
|
|
23
|
+
normal.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
import json
|
|
29
|
+
import hashlib
|
|
30
|
+
import time
|
|
31
|
+
import urllib.error
|
|
32
|
+
import urllib.request
|
|
33
|
+
from dataclasses import dataclass
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class PanelError(Exception):
|
|
37
|
+
"""The panel answered, but not with success."""
|
|
38
|
+
|
|
39
|
+
def __init__(self, message: str, code: int | None = None) -> None:
|
|
40
|
+
super().__init__(message)
|
|
41
|
+
self.code = code
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class PanelUnreachable(PanelError):
|
|
45
|
+
"""The panel could not be reached at all."""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass
|
|
49
|
+
class PanelClient:
|
|
50
|
+
base_url: str
|
|
51
|
+
api_key: str
|
|
52
|
+
timeout: float = 15.0
|
|
53
|
+
api_version: str = "v2"
|
|
54
|
+
|
|
55
|
+
def __post_init__(self) -> None:
|
|
56
|
+
self.base_url = self.base_url.rstrip("/")
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def sign(api_key: str, timestamp: int) -> str:
|
|
60
|
+
"""v2 token = md5('1panel' + api_key + timestamp)."""
|
|
61
|
+
return hashlib.md5(f"1panel{api_key}{timestamp}".encode()).hexdigest()
|
|
62
|
+
|
|
63
|
+
def _headers(self, timestamp: int) -> dict[str, str]:
|
|
64
|
+
if self.api_version == "v1":
|
|
65
|
+
# v1 sent the key itself, with a differently named time header.
|
|
66
|
+
return {"1Panel-Token": self.api_key, "1Panel-Time": str(timestamp)}
|
|
67
|
+
return {
|
|
68
|
+
"1Panel-Token": self.sign(self.api_key, timestamp),
|
|
69
|
+
"1Panel-Timestamp": str(timestamp),
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
def _url(self, path: str) -> str:
|
|
73
|
+
if path.startswith("/api/"):
|
|
74
|
+
return f"{self.base_url}{path}"
|
|
75
|
+
return f"{self.base_url}/api/{self.api_version}{path}"
|
|
76
|
+
|
|
77
|
+
def request(
|
|
78
|
+
self,
|
|
79
|
+
path: str,
|
|
80
|
+
method: str = "GET",
|
|
81
|
+
payload: dict | None = None,
|
|
82
|
+
timeout: float | None = None,
|
|
83
|
+
) -> dict:
|
|
84
|
+
url = self._url(path)
|
|
85
|
+
body = json.dumps(payload).encode() if payload is not None else None
|
|
86
|
+
request = urllib.request.Request(url, data=body, method=method)
|
|
87
|
+
for key, value in self._headers(int(time.time())).items():
|
|
88
|
+
request.add_header(key, value)
|
|
89
|
+
request.add_header("Accept", "application/json")
|
|
90
|
+
if body is not None:
|
|
91
|
+
request.add_header("Content-Type", "application/json")
|
|
92
|
+
try:
|
|
93
|
+
with urllib.request.urlopen(
|
|
94
|
+
request, timeout=timeout or self.timeout
|
|
95
|
+
) as response:
|
|
96
|
+
raw = response.read().decode("utf-8", errors="replace")
|
|
97
|
+
except urllib.error.HTTPError as exc:
|
|
98
|
+
raw = exc.read().decode("utf-8", errors="replace")
|
|
99
|
+
raise PanelError(_message(raw, exc.code), exc.code) from exc
|
|
100
|
+
except urllib.error.URLError as exc:
|
|
101
|
+
raise PanelUnreachable(f"{url}: {exc.reason}") from exc
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
data = json.loads(raw)
|
|
105
|
+
except json.JSONDecodeError as exc:
|
|
106
|
+
raise PanelUnreachable(
|
|
107
|
+
f"{url} returned non-JSON (is this the panel root, and is the API "
|
|
108
|
+
f"interface enabled for this address?): {raw[:200]}"
|
|
109
|
+
) from exc
|
|
110
|
+
code = data.get("code")
|
|
111
|
+
if code not in (200, None):
|
|
112
|
+
raise PanelError(str(data.get("message") or data), code)
|
|
113
|
+
return data.get("data") or {}
|
|
114
|
+
|
|
115
|
+
def ping(self) -> dict:
|
|
116
|
+
"""Round-trip the cheapest authenticated endpoint."""
|
|
117
|
+
return self.request("/dashboard/base/os")
|
|
118
|
+
|
|
119
|
+
def containers(self, state: str = "running", page_size: int = 200) -> list[dict]:
|
|
120
|
+
# The panel validates these fields: `state` may not be empty and
|
|
121
|
+
# `orderBy` must be one of its enum values (`created_at` is rejected).
|
|
122
|
+
data = self.request(
|
|
123
|
+
"/containers/search",
|
|
124
|
+
"POST",
|
|
125
|
+
{
|
|
126
|
+
"page": 1,
|
|
127
|
+
"pageSize": page_size,
|
|
128
|
+
"state": state,
|
|
129
|
+
"orderBy": "createdAt",
|
|
130
|
+
"order": "descending",
|
|
131
|
+
},
|
|
132
|
+
)
|
|
133
|
+
return list(data.get("items") or [])
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def connect(base_url: str, api_key: str, timeout: float = 15.0) -> PanelClient:
|
|
137
|
+
"""Return a client that works against this panel, v2 first then v1."""
|
|
138
|
+
v2 = PanelClient(base_url, api_key, timeout=timeout, api_version="v2")
|
|
139
|
+
try:
|
|
140
|
+
v2.ping()
|
|
141
|
+
return v2
|
|
142
|
+
except PanelError as exc:
|
|
143
|
+
if exc.code != 404 and "not found" not in str(exc).lower():
|
|
144
|
+
raise
|
|
145
|
+
v1 = PanelClient(base_url, api_key, timeout=timeout, api_version="v1")
|
|
146
|
+
v1.ping()
|
|
147
|
+
return v1
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _message(raw: str, status: int) -> str:
|
|
151
|
+
try:
|
|
152
|
+
return str(json.loads(raw).get("message") or raw)
|
|
153
|
+
except json.JSONDecodeError:
|
|
154
|
+
return f"HTTP {status}: {raw[:200]}"
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"""The pattern library: real, store-accepted examples for every moving part.
|
|
2
|
+
|
|
3
|
+
Build it from a corpus of official packages, ship the result inside the wheel,
|
|
4
|
+
and let both humans and agents pull a working example instead of guessing YAML.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
from dataclasses import asdict, dataclass, field
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any, Iterable
|
|
13
|
+
|
|
14
|
+
from ..loader import find_version_dirs, load_version, LoadError, load_yaml
|
|
15
|
+
from .catalog import CATALOG, PatternDef, by_id, categories
|
|
16
|
+
from .extract import DETECTORS, AppCtx, Hit, collect, missing_detectors
|
|
17
|
+
|
|
18
|
+
SCHEMA_VERSION = 1
|
|
19
|
+
DATA_FILE = Path(__file__).parent / "data" / "patterns.json"
|
|
20
|
+
EXAMPLES_PER_PATTERN = 3
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class Pattern:
|
|
25
|
+
id: str
|
|
26
|
+
title: str
|
|
27
|
+
category: str
|
|
28
|
+
intent: str
|
|
29
|
+
when_to_use: str
|
|
30
|
+
notes: list[str] = field(default_factory=list)
|
|
31
|
+
apps: int = 0
|
|
32
|
+
versions: int = 0
|
|
33
|
+
examples: list[dict[str, str]] = field(default_factory=list)
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def snippet(self) -> str:
|
|
37
|
+
return self.examples[0]["snippet"] if self.examples else ""
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def source(self) -> str:
|
|
41
|
+
if not self.examples:
|
|
42
|
+
return ""
|
|
43
|
+
first = self.examples[0]
|
|
44
|
+
return f"{first['app']}/{first['file']}"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _load_app(root: Path) -> AppCtx | None:
|
|
48
|
+
data_path = root / "data.yml"
|
|
49
|
+
if not data_path.exists():
|
|
50
|
+
return None
|
|
51
|
+
try:
|
|
52
|
+
app_data = load_yaml(data_path)
|
|
53
|
+
except LoadError:
|
|
54
|
+
return None
|
|
55
|
+
key = str((app_data.get("additionalProperties") or {}).get("key") or root.name)
|
|
56
|
+
readme_path = root / "README.md"
|
|
57
|
+
readme = (
|
|
58
|
+
readme_path.read_text(encoding="utf-8-sig", errors="replace")
|
|
59
|
+
if readme_path.exists()
|
|
60
|
+
else ""
|
|
61
|
+
)
|
|
62
|
+
return AppCtx(
|
|
63
|
+
key=key,
|
|
64
|
+
root=root,
|
|
65
|
+
app_data=app_data,
|
|
66
|
+
app_text=data_path.read_text(encoding="utf-8-sig", errors="replace"),
|
|
67
|
+
readme=readme,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def iter_app_roots(corpus: Path) -> Iterable[Path]:
|
|
72
|
+
"""Accept a corpus root, an ``apps/`` dir, or a single app package."""
|
|
73
|
+
if (corpus / "data.yml").exists():
|
|
74
|
+
yield corpus
|
|
75
|
+
return
|
|
76
|
+
candidates = [corpus / "apps", corpus]
|
|
77
|
+
for base in candidates:
|
|
78
|
+
if not base.is_dir():
|
|
79
|
+
continue
|
|
80
|
+
for child in sorted(base.iterdir()):
|
|
81
|
+
if child.is_dir() and (child / "data.yml").exists():
|
|
82
|
+
yield child
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def build(corpus_dirs: list[Path]) -> dict[str, Any]:
|
|
86
|
+
definitions: list[PatternDef] = CATALOG
|
|
87
|
+
hits: dict[str, list[Hit]] = {}
|
|
88
|
+
sources: list[dict[str, Any]] = []
|
|
89
|
+
|
|
90
|
+
for corpus in corpus_dirs:
|
|
91
|
+
apps = versions = 0
|
|
92
|
+
for root in iter_app_roots(corpus):
|
|
93
|
+
ctx = _load_app(root)
|
|
94
|
+
if ctx is None:
|
|
95
|
+
continue
|
|
96
|
+
apps += 1
|
|
97
|
+
for definition in definitions:
|
|
98
|
+
detector = DETECTORS[definition.detector]
|
|
99
|
+
if detector.scope == "app":
|
|
100
|
+
collect(hits, definition, ctx, None)
|
|
101
|
+
for version_dir in find_version_dirs(root):
|
|
102
|
+
try:
|
|
103
|
+
version = load_version(version_dir)
|
|
104
|
+
except LoadError:
|
|
105
|
+
continue
|
|
106
|
+
versions += 1
|
|
107
|
+
for definition in definitions:
|
|
108
|
+
if DETECTORS[definition.detector].scope == "version":
|
|
109
|
+
collect(hits, definition, ctx, version)
|
|
110
|
+
label = f"{corpus.name}/apps" if (corpus / "apps").is_dir() else corpus.name
|
|
111
|
+
# Only the label ships: the absolute path of the author's machine has no
|
|
112
|
+
# business inside a distributed data file.
|
|
113
|
+
sources.append(
|
|
114
|
+
{
|
|
115
|
+
"label": label,
|
|
116
|
+
"apps": apps,
|
|
117
|
+
"versions": versions,
|
|
118
|
+
}
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
patterns: list[dict[str, Any]] = []
|
|
122
|
+
for definition in definitions:
|
|
123
|
+
found = hits.get(definition.id, [])
|
|
124
|
+
# Richest example first (a full 10-language label beats a 5-language one),
|
|
125
|
+
# then the shortest, so the top snippet is complete but not padded.
|
|
126
|
+
found.sort(key=lambda hit: (-hit.score, len(hit.snippet), hit.app))
|
|
127
|
+
examples = [
|
|
128
|
+
{"app": h.app, "version": h.version, "file": h.file, "snippet": h.snippet}
|
|
129
|
+
for h in found[:EXAMPLES_PER_PATTERN]
|
|
130
|
+
]
|
|
131
|
+
patterns.append(
|
|
132
|
+
{
|
|
133
|
+
"id": definition.id,
|
|
134
|
+
"title": definition.title,
|
|
135
|
+
"category": definition.category,
|
|
136
|
+
"intent": definition.intent,
|
|
137
|
+
"when_to_use": definition.when_to_use,
|
|
138
|
+
"notes": list(definition.notes),
|
|
139
|
+
"apps": len({h.app for h in found}),
|
|
140
|
+
"versions": len(found),
|
|
141
|
+
"examples": examples,
|
|
142
|
+
}
|
|
143
|
+
)
|
|
144
|
+
return {
|
|
145
|
+
"schema": SCHEMA_VERSION,
|
|
146
|
+
"generated_from": sources,
|
|
147
|
+
"patterns": patterns,
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def save(data: dict[str, Any], path: Path = DATA_FILE) -> Path:
|
|
152
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
153
|
+
path.write_text(
|
|
154
|
+
json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
|
|
155
|
+
)
|
|
156
|
+
return path
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def load_bundled(path: Path = DATA_FILE) -> dict[str, Any]:
|
|
160
|
+
if not path.exists():
|
|
161
|
+
raise FileNotFoundError(
|
|
162
|
+
f"no pattern library at {path}; run `p1 patterns build --corpus <dir>`"
|
|
163
|
+
)
|
|
164
|
+
return json.loads(path.read_text(encoding="utf-8"))
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def patterns(data: dict[str, Any] | None = None) -> list[Pattern]:
|
|
168
|
+
raw = (data or load_bundled()).get("patterns") or []
|
|
169
|
+
out = []
|
|
170
|
+
for item in raw:
|
|
171
|
+
out.append(Pattern(**{k: v for k, v in item.items() if k in Pattern.__annotations__}))
|
|
172
|
+
return out
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def find(query: str, data: dict[str, Any] | None = None) -> list[Pattern]:
|
|
176
|
+
needle = query.lower()
|
|
177
|
+
out = []
|
|
178
|
+
for pattern in patterns(data):
|
|
179
|
+
haystack = " ".join(
|
|
180
|
+
[pattern.id, pattern.title, pattern.category, pattern.intent, pattern.when_to_use]
|
|
181
|
+
+ pattern.notes
|
|
182
|
+
).lower()
|
|
183
|
+
if needle in haystack:
|
|
184
|
+
out.append(pattern)
|
|
185
|
+
return out
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def get(pattern_id: str, data: dict[str, Any] | None = None) -> Pattern | None:
|
|
189
|
+
for pattern in patterns(data):
|
|
190
|
+
if pattern.id == pattern_id:
|
|
191
|
+
return pattern
|
|
192
|
+
return None
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def render_markdown(data: dict[str, Any] | None = None) -> str:
|
|
196
|
+
data = data or load_bundled()
|
|
197
|
+
items = patterns(data)
|
|
198
|
+
lines = [
|
|
199
|
+
"# 1Panel 应用包写法库",
|
|
200
|
+
"",
|
|
201
|
+
"从官方应用包里抽取的真实写法。每段代码都来自一个已经上架、能装的应用,",
|
|
202
|
+
"不是凭空写的示例。`apps` 列是使用该写法的官方应用数量,可以看出哪些是主流做法。",
|
|
203
|
+
"",
|
|
204
|
+
]
|
|
205
|
+
for source in data.get("generated_from", []):
|
|
206
|
+
lines.append(
|
|
207
|
+
f"- 语料:`{source.get('label') or source.get('corpus')}`"
|
|
208
|
+
f"({source['apps']} 个应用 / {source['versions']} 个版本)"
|
|
209
|
+
)
|
|
210
|
+
lines.append("")
|
|
211
|
+
for category in categories():
|
|
212
|
+
group = [p for p in items if p.category == category]
|
|
213
|
+
if not group:
|
|
214
|
+
continue
|
|
215
|
+
lines.append(f"## {category}")
|
|
216
|
+
lines.append("")
|
|
217
|
+
for pattern in group:
|
|
218
|
+
lines.append(f"### `{pattern.id}` — {pattern.title}")
|
|
219
|
+
lines.append("")
|
|
220
|
+
lines.append(f"- **用途**:{pattern.intent}")
|
|
221
|
+
lines.append(f"- **何时使用**:{pattern.when_to_use}")
|
|
222
|
+
lines.append(f"- **官方应用采用数**:{pattern.apps} 个应用 / {pattern.versions} 个版本")
|
|
223
|
+
if pattern.source:
|
|
224
|
+
lines.append(f"- **示例来源**:`{pattern.source}`")
|
|
225
|
+
for note in pattern.notes:
|
|
226
|
+
lines.append(f"- {note}")
|
|
227
|
+
if pattern.snippet:
|
|
228
|
+
lines.append("")
|
|
229
|
+
lines.append("```yaml")
|
|
230
|
+
lines.append(pattern.snippet)
|
|
231
|
+
lines.append("```")
|
|
232
|
+
lines.append("")
|
|
233
|
+
return "\n".join(lines)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
__all__ = [
|
|
237
|
+
"CATALOG",
|
|
238
|
+
"DETECTORS",
|
|
239
|
+
"Pattern",
|
|
240
|
+
"build",
|
|
241
|
+
"by_id",
|
|
242
|
+
"categories",
|
|
243
|
+
"find",
|
|
244
|
+
"get",
|
|
245
|
+
"load_bundled",
|
|
246
|
+
"missing_detectors",
|
|
247
|
+
"patterns",
|
|
248
|
+
"render_markdown",
|
|
249
|
+
"save",
|
|
250
|
+
]
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"""The recipe catalog: what a 1Panel package is made of, in reusable pieces.
|
|
2
|
+
|
|
3
|
+
Snippets are never invented here. Each pattern names a detector that pulls a
|
|
4
|
+
real, store-accepted example out of an official app package, so what an agent
|
|
5
|
+
reads is something 1Panel already installs successfully.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True)
|
|
14
|
+
class PatternDef:
|
|
15
|
+
id: str
|
|
16
|
+
title: str
|
|
17
|
+
category: str
|
|
18
|
+
intent: str
|
|
19
|
+
when_to_use: str
|
|
20
|
+
detector: str
|
|
21
|
+
notes: list[str] = field(default_factory=list)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
CATALOG: list[PatternDef] = [
|
|
25
|
+
# ---------- 应用声明 ----------
|
|
26
|
+
PatternDef(
|
|
27
|
+
id="data-yml/root-declaration",
|
|
28
|
+
title="应用声明文件 apps/<key>/data.yml",
|
|
29
|
+
category="data-yml",
|
|
30
|
+
intent="声明应用是谁:key、名称、分类、类型、官网/仓库/文档地址、跨版本升级能力。",
|
|
31
|
+
when_to_use="每个新包的第一份文件。key 只用小写英文,它决定 Linux 上创建的目录名。",
|
|
32
|
+
detector="root_declaration",
|
|
33
|
+
notes=[
|
|
34
|
+
"type 官方只认 website / runtime / tool;website 类型可以在「网站」里一键部署。",
|
|
35
|
+
"shortDescZh 不要超过 30 个字。",
|
|
36
|
+
"limit 是安装数量限制,0 表示不限制。",
|
|
37
|
+
],
|
|
38
|
+
),
|
|
39
|
+
PatternDef(
|
|
40
|
+
id="data-yml/version-form",
|
|
41
|
+
title="版本参数文件 <version>/data.yml",
|
|
42
|
+
category="data-yml",
|
|
43
|
+
intent="定义安装表单,表单值即 docker-compose.yml 里 ${VAR} 的来源。",
|
|
44
|
+
when_to_use="任何需要用户输入端口、密码、地址的包。可以没有表单,但文件必须存在且含 formFields。",
|
|
45
|
+
detector="version_form",
|
|
46
|
+
notes=[
|
|
47
|
+
"formFields 必须写在 additionalProperties 下面。",
|
|
48
|
+
"版本目录名不要以 v 开头,用 1.2.3 或 latest。",
|
|
49
|
+
],
|
|
50
|
+
),
|
|
51
|
+
# ---------- 表单字段 ----------
|
|
52
|
+
PatternDef(
|
|
53
|
+
id="form-field/service-db-selector",
|
|
54
|
+
title="数据库依赖选择器(type: service)",
|
|
55
|
+
category="form-field",
|
|
56
|
+
intent="让用户在安装表单里直接选一个面板上已安装的数据库实例,而不是手填主机地址。",
|
|
57
|
+
when_to_use="应用支持外部数据库,且面板上装了对应的数据库应用(mysql / postgresql / redis 等)。",
|
|
58
|
+
detector="ff_service_db",
|
|
59
|
+
notes=[
|
|
60
|
+
"key 是被依赖应用的 key(例如 mysql),不是展示名。",
|
|
61
|
+
"envKey 用 PANEL_DB_HOST,compose 里再映射到应用自己的变量名。",
|
|
62
|
+
"面板会注入 PANEL_DB_HOST / PANEL_DB_PORT,这两项不需要用户填。",
|
|
63
|
+
],
|
|
64
|
+
),
|
|
65
|
+
PatternDef(
|
|
66
|
+
id="form-field/apps-child-service",
|
|
67
|
+
title="两步式依赖选择(type: apps + child.service)",
|
|
68
|
+
category="form-field",
|
|
69
|
+
intent="先选依赖类型(MySQL 还是 PostgreSQL),再选具体实例。",
|
|
70
|
+
when_to_use="同一个应用支持多种数据库引擎时。",
|
|
71
|
+
detector="ff_apps_child",
|
|
72
|
+
notes=["values[].label 必须是纯字符串,写成多语言 map 会让面板反序列化失败。"],
|
|
73
|
+
),
|
|
74
|
+
PatternDef(
|
|
75
|
+
id="form-field/port",
|
|
76
|
+
title="端口字段(PANEL_APP_PORT_*)",
|
|
77
|
+
category="form-field",
|
|
78
|
+
intent="声明对外暴露的端口,1Panel 用它做端口占用检测。",
|
|
79
|
+
when_to_use="任何有对外服务的应用。",
|
|
80
|
+
detector="ff_port",
|
|
81
|
+
notes=[
|
|
82
|
+
"envKey 必须以 PANEL_APP_PORT_ 开头,否则面板不会做占用检测。",
|
|
83
|
+
"rule 固定用 paramPort,type 用 number。",
|
|
84
|
+
"容器侧端口要来自上游 EXPOSE 或官方 compose,不要照抄模板。",
|
|
85
|
+
],
|
|
86
|
+
),
|
|
87
|
+
PatternDef(
|
|
88
|
+
id="form-field/password",
|
|
89
|
+
title="密码字段(type: password)",
|
|
90
|
+
category="form-field",
|
|
91
|
+
intent="收集密码/密钥类输入,表单里以掩码显示。",
|
|
92
|
+
when_to_use="数据库密码、管理员密码、API Key 等。",
|
|
93
|
+
detector="ff_password",
|
|
94
|
+
notes=[
|
|
95
|
+
"配合 random: true 可以自动在默认值后追加随机串。",
|
|
96
|
+
"rule 常用 paramComplexity 做强度校验。",
|
|
97
|
+
],
|
|
98
|
+
),
|
|
99
|
+
PatternDef(
|
|
100
|
+
id="form-field/text-random",
|
|
101
|
+
title="带随机后缀的文本字段(random: true)",
|
|
102
|
+
category="form-field",
|
|
103
|
+
intent="生成不会重复的资源名,例如数据库名、用户名。",
|
|
104
|
+
when_to_use="同一应用可能装多份、名字需要唯一时。",
|
|
105
|
+
detector="ff_text_random",
|
|
106
|
+
),
|
|
107
|
+
PatternDef(
|
|
108
|
+
id="form-field/select",
|
|
109
|
+
title="枚举字段(type: select)",
|
|
110
|
+
category="form-field",
|
|
111
|
+
intent="把有限的取值做成下拉框,避免用户填错。",
|
|
112
|
+
when_to_use="日志级别、运行模式、提供商选择、开关类 true/false。",
|
|
113
|
+
detector="ff_select",
|
|
114
|
+
notes=[
|
|
115
|
+
"values[].label 必须是纯字符串;true/false 也要写成带引号的字符串。",
|
|
116
|
+
],
|
|
117
|
+
),
|
|
118
|
+
PatternDef(
|
|
119
|
+
id="form-field/i18n-label",
|
|
120
|
+
title="表单字段的多语言 label",
|
|
121
|
+
category="form-field",
|
|
122
|
+
intent="让安装表单在 10 种语言下都显示母语。",
|
|
123
|
+
when_to_use="所有面向 appstore 的包。",
|
|
124
|
+
detector="ff_i18n_label",
|
|
125
|
+
notes=[
|
|
126
|
+
"官方同时写 labelZh/labelEn 和 label 多语言 map,两者都保留兼容性最好。",
|
|
127
|
+
"语言集:en / es-es / ja / ms / pt-br / ru / ko / zh-Hant / zh / tr。",
|
|
128
|
+
],
|
|
129
|
+
),
|
|
130
|
+
# ---------- compose ----------
|
|
131
|
+
PatternDef(
|
|
132
|
+
id="compose/single-service",
|
|
133
|
+
title="单服务 compose",
|
|
134
|
+
category="compose",
|
|
135
|
+
intent="最常见形态:一个应用容器 + 一个持久化目录 + 一个对外端口。",
|
|
136
|
+
when_to_use="默认选择。能用单服务就别上多服务。",
|
|
137
|
+
detector="compose_single",
|
|
138
|
+
notes=[
|
|
139
|
+
"不要写顶层 version:,1Panel / Compose 会忽略并告警。",
|
|
140
|
+
"container_name 用 ${CONTAINER_NAME},由面板在安装时注入。",
|
|
141
|
+
],
|
|
142
|
+
),
|
|
143
|
+
PatternDef(
|
|
144
|
+
id="compose/multi-service",
|
|
145
|
+
title="多服务 compose",
|
|
146
|
+
category="compose",
|
|
147
|
+
intent="主服务 + 数据库/缓存/前端控制台等旁挂服务。",
|
|
148
|
+
when_to_use="上游本身就是多容器架构,且不适合拆成面板托管依赖时。",
|
|
149
|
+
detector="compose_multi",
|
|
150
|
+
notes=[
|
|
151
|
+
"次要服务容器名用 ${CONTAINER_NAME}-<suffix>。",
|
|
152
|
+
"主服务同时接入 1panel-network 和内部网络时,不要用 redis/mysql 这类裸名做主机名,会 DNS 撞名。",
|
|
153
|
+
],
|
|
154
|
+
),
|
|
155
|
+
PatternDef(
|
|
156
|
+
id="compose/panel-db-env",
|
|
157
|
+
title="把面板数据库注入到应用变量",
|
|
158
|
+
category="compose",
|
|
159
|
+
intent="把 1Panel 的 PANEL_DB_* 映射成应用自己认识的 DATABASE_* 变量。",
|
|
160
|
+
when_to_use="表单里用了数据库选择器时。",
|
|
161
|
+
detector="compose_db_env",
|
|
162
|
+
),
|
|
163
|
+
PatternDef(
|
|
164
|
+
id="compose/panel-redis-env",
|
|
165
|
+
title="把面板 Redis 注入到应用变量",
|
|
166
|
+
category="compose",
|
|
167
|
+
intent="接入面板托管的 Redis。",
|
|
168
|
+
when_to_use="应用依赖 Redis 且面板上装了 redis 应用。",
|
|
169
|
+
detector="compose_redis_env",
|
|
170
|
+
),
|
|
171
|
+
PatternDef(
|
|
172
|
+
id="compose/healthcheck",
|
|
173
|
+
title="服务健康检查",
|
|
174
|
+
category="compose",
|
|
175
|
+
intent="让依赖方等到真正就绪,而不是只等到容器启动。",
|
|
176
|
+
when_to_use="多服务且存在启动顺序依赖时。",
|
|
177
|
+
detector="compose_healthcheck",
|
|
178
|
+
notes=[
|
|
179
|
+
"只保证启动顺序的 depends_on 不够,首次启动常见「主服务连不上数据库」。",
|
|
180
|
+
"另一种做法是让应用自身带重连逻辑,并在 README 里说明。",
|
|
181
|
+
],
|
|
182
|
+
),
|
|
183
|
+
PatternDef(
|
|
184
|
+
id="compose/named-volume",
|
|
185
|
+
title="命名卷持久化",
|
|
186
|
+
category="compose",
|
|
187
|
+
intent="保留上游的命名卷语义,数据交给 Docker 管理。",
|
|
188
|
+
when_to_use="上游 compose 用的就是命名卷时。",
|
|
189
|
+
detector="compose_named_volume",
|
|
190
|
+
notes=["用命名卷作为主数据目录时,根 data.yml 的 limit 要设为 1。"],
|
|
191
|
+
),
|
|
192
|
+
PatternDef(
|
|
193
|
+
id="compose/data-dir-mount",
|
|
194
|
+
title="相对路径数据目录挂载",
|
|
195
|
+
category="compose",
|
|
196
|
+
intent="把持久化数据放在应用目录下,跟随 1Panel 的应用生命周期。",
|
|
197
|
+
when_to_use="上游用 host path 或没有明确持久化方案时。",
|
|
198
|
+
detector="compose_data_dir",
|
|
199
|
+
),
|
|
200
|
+
PatternDef(
|
|
201
|
+
id="compose/external-network",
|
|
202
|
+
title="接入 1panel-network",
|
|
203
|
+
category="compose",
|
|
204
|
+
intent="让应用能被面板的反向代理和入口访问到。",
|
|
205
|
+
when_to_use="所有 bridge 型、需要被外部访问的应用。",
|
|
206
|
+
detector="compose_external_network",
|
|
207
|
+
notes=["硬要求是「接了至少一个外部网络」,网络名不一定是 1panel-network。"],
|
|
208
|
+
),
|
|
209
|
+
# ---------- 脚本 ----------
|
|
210
|
+
PatternDef(
|
|
211
|
+
id="scripts/init-sh",
|
|
212
|
+
title="init.sh 安装前初始化",
|
|
213
|
+
category="scripts",
|
|
214
|
+
intent="在容器启动前补齐目录、权限、必需配置文件。",
|
|
215
|
+
when_to_use="挂载目录需要非 root 可写,或应用首次启动必须读到某个配置文件时。",
|
|
216
|
+
detector="scripts_init",
|
|
217
|
+
notes=[
|
|
218
|
+
"「静态校验通过、容器一起来就退出」最常见的根因就是这类缺失(见 lesson L001)。",
|
|
219
|
+
"不要假设脚本能拿到所有表单值,路径类变量要有安全默认值。",
|
|
220
|
+
],
|
|
221
|
+
),
|
|
222
|
+
PatternDef(
|
|
223
|
+
id="scripts/upgrade-sh",
|
|
224
|
+
title="upgrade.sh 升级迁移",
|
|
225
|
+
category="scripts",
|
|
226
|
+
intent="升级时补齐新增的环境变量或迁移数据。",
|
|
227
|
+
when_to_use="新版本引入了必填变量、改了变量名或数据布局时。",
|
|
228
|
+
detector="scripts_upgrade",
|
|
229
|
+
notes=["1Panel 升级会重放安装表单的值,被脚本改写过的密钥要持久化并在升级时恢复。"],
|
|
230
|
+
),
|
|
231
|
+
PatternDef(
|
|
232
|
+
id="scripts/uninstall-sh",
|
|
233
|
+
title="uninstall.sh 卸载清理",
|
|
234
|
+
category="scripts",
|
|
235
|
+
intent="卸载时清理数据卷,避免残留。",
|
|
236
|
+
when_to_use="使用命名卷或需要显式回收数据时。",
|
|
237
|
+
detector="scripts_uninstall",
|
|
238
|
+
),
|
|
239
|
+
# ---------- 交付物 ----------
|
|
240
|
+
PatternDef(
|
|
241
|
+
id="readme/store-style",
|
|
242
|
+
title="商店风格 README",
|
|
243
|
+
category="readme",
|
|
244
|
+
intent="用产品介绍 / 主要功能 / 访问说明的结构向用户说明这个应用。",
|
|
245
|
+
when_to_use="每个包都要有。",
|
|
246
|
+
detector="readme_style",
|
|
247
|
+
notes=["不要把打包过程、调试信息写进应用 README。"],
|
|
248
|
+
),
|
|
249
|
+
PatternDef(
|
|
250
|
+
id="env-sample/closure",
|
|
251
|
+
title=".env.sample 变量闭合",
|
|
252
|
+
category="env-sample",
|
|
253
|
+
intent="让用户脱离 1Panel 也能直接 docker compose up。",
|
|
254
|
+
when_to_use="compose 里出现任何 ${VAR} 时。",
|
|
255
|
+
detector="env_sample",
|
|
256
|
+
notes=[".env.sample 只是方便文件,不是 1Panel 安装时的运行时契约。"],
|
|
257
|
+
),
|
|
258
|
+
]
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def by_id() -> dict[str, PatternDef]:
|
|
262
|
+
return {item.id: item for item in CATALOG}
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def categories() -> list[str]:
|
|
266
|
+
seen: list[str] = []
|
|
267
|
+
for item in CATALOG:
|
|
268
|
+
if item.category not in seen:
|
|
269
|
+
seen.append(item.category)
|
|
270
|
+
return seen
|
|
271
|
+
|