仰望星辰工作室

miaopanel

miaopanel/ internal/store/plugins.go 8.8 KB · 275 行 原始文件
1package store
2
3import (
4 "context"
5 "database/sql"
6 "errors"
7 "time"
8)
9
10// Plugin is one installed third-party plugin. The signed manifest is kept so
11// the panel can re-verify it after a restart or an upgrade.
12type Plugin struct {
13 Slug string `json:"slug"`
14 Name string `json:"name"`
15 Version string `json:"version"`
16 Status string `json:"status"` // installed|disabled|failed|uninstalling
17 Runtime string `json:"runtime"`
18 Manifest string `json:"manifest"`
19 PlatformSig string `json:"platform_sig"`
20 Pubkey string `json:"pubkey"`
21 SHA256 string `json:"sha256"`
22 AutoUpdate bool `json:"auto_update"`
23 LastError string `json:"last_error"`
24 InstalledAt int64 `json:"installed_at"`
25 UpdatedAt int64 `json:"updated_at"`
26}
27
28const pluginColumns = `slug, name, version, status, runtime, manifest, platform_sig,
29 pubkey, sha256, auto_update, last_error, installed_at, updated_at`
30
31func scanPlugin(row interface{ Scan(...any) error }) (*Plugin, error) {
32 var p Plugin
33 var auto int
34 err := row.Scan(&p.Slug, &p.Name, &p.Version, &p.Status, &p.Runtime, &p.Manifest,
35 &p.PlatformSig, &p.Pubkey, &p.SHA256, &auto, &p.LastError, &p.InstalledAt, &p.UpdatedAt)
36 if errors.Is(err, sql.ErrNoRows) {
37 return nil, ErrNotFound
38 }
39 if err != nil {
40 return nil, err
41 }
42 p.AutoUpdate = auto == 1
43 return &p, nil
44}
45
46// Plugins lists installed plugins ordered by installation time.
47func (s *Store) Plugins(ctx context.Context) ([]*Plugin, error) {
48 rows, err := s.db.QueryContext(ctx, `SELECT `+pluginColumns+` FROM plugins ORDER BY installed_at DESC`)
49 if err != nil {
50 return nil, err
51 }
52 defer rows.Close()
53 out := []*Plugin{}
54 for rows.Next() {
55 p, err := scanPlugin(rows)
56 if err != nil {
57 return nil, err
58 }
59 out = append(out, p)
60 }
61 return out, rows.Err()
62}
63
64// PluginBySlug loads one installed plugin.
65func (s *Store) PluginBySlug(ctx context.Context, slug string) (*Plugin, error) {
66 return scanPlugin(s.db.QueryRowContext(ctx,
67 `SELECT `+pluginColumns+` FROM plugins WHERE slug = ?`, slug))
68}
69
70// SavePlugin inserts or replaces the install record.
71func (s *Store) SavePlugin(ctx context.Context, p *Plugin) error {
72 now := time.Now().Unix()
73 if p.InstalledAt == 0 {
74 p.InstalledAt = now
75 }
76 p.UpdatedAt = now
77 auto := 0
78 if p.AutoUpdate {
79 auto = 1
80 }
81 _, err := s.db.ExecContext(ctx,
82 `INSERT INTO plugins(slug, name, version, status, runtime, manifest, platform_sig,
83 pubkey, sha256, auto_update, last_error, installed_at, updated_at)
84 VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)
85 ON CONFLICT(slug) DO UPDATE SET
86 name=excluded.name, version=excluded.version, status=excluded.status,
87 runtime=excluded.runtime, manifest=excluded.manifest, platform_sig=excluded.platform_sig,
88 pubkey=excluded.pubkey, sha256=excluded.sha256, auto_update=excluded.auto_update,
89 last_error=excluded.last_error, updated_at=excluded.updated_at`,
90 p.Slug, p.Name, p.Version, p.Status, p.Runtime, p.Manifest, p.PlatformSig,
91 p.Pubkey, p.SHA256, auto, p.LastError, p.InstalledAt, p.UpdatedAt)
92 return err
93}
94
95// SetPluginStatus updates the lifecycle status and error text.
96func (s *Store) SetPluginStatus(ctx context.Context, slug, status, lastError string) error {
97 _, err := s.db.ExecContext(ctx,
98 `UPDATE plugins SET status = ?, last_error = ?, updated_at = ? WHERE slug = ?`,
99 status, lastError, time.Now().Unix(), slug)
100 return err
101}
102
103// SetPluginAutoUpdate toggles background upgrades for one plugin.
104func (s *Store) SetPluginAutoUpdate(ctx context.Context, slug string, on bool) error {
105 v := 0
106 if on {
107 v = 1
108 }
109 _, err := s.db.ExecContext(ctx,
110 `UPDATE plugins SET auto_update = ?, updated_at = ? WHERE slug = ?`, v, time.Now().Unix(), slug)
111 return err
112}
113
114// DeletePlugin removes the install record.
115func (s *Store) DeletePlugin(ctx context.Context, slug string) error {
116 _, err := s.db.ExecContext(ctx, `DELETE FROM plugins WHERE slug = ?`, slug)
117 return err
118}
119
120// PluginSetting reads one value for a plugin.
121func (s *Store) PluginSetting(ctx context.Context, slug, key string) string {
122 var v string
123 err := s.db.QueryRowContext(ctx,
124 `SELECT value FROM plugin_settings WHERE slug = ? AND key = ?`, slug, key).Scan(&v)
125 if err != nil {
126 return ""
127 }
128 return v
129}
130
131// SetPluginSetting writes one value for a plugin.
132func (s *Store) SetPluginSetting(ctx context.Context, slug, key, value string) error {
133 _, err := s.db.ExecContext(ctx,
134 `INSERT INTO plugin_settings(slug, key, value, updated_at) VALUES(?,?,?,?)
135 ON CONFLICT(slug, key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`,
136 slug, key, value, time.Now().Unix())
137 return err
138}
139
140// PluginSettings returns every setting of a plugin.
141func (s *Store) PluginSettings(ctx context.Context, slug string) (map[string]string, error) {
142 rows, err := s.db.QueryContext(ctx,
143 `SELECT key, value FROM plugin_settings WHERE slug = ?`, slug)
144 if err != nil {
145 return nil, err
146 }
147 defer rows.Close()
148 out := map[string]string{}
149 for rows.Next() {
150 var k, v string
151 if err := rows.Scan(&k, &v); err != nil {
152 return nil, err
153 }
154 out[k] = v
155 }
156 return out, rows.Err()
157}
158
159// DeletePluginSettings removes every setting of a plugin.
160func (s *Store) DeletePluginSettings(ctx context.Context, slug string) error {
161 _, err := s.db.ExecContext(ctx, `DELETE FROM plugin_settings WHERE slug = ?`, slug)
162 return err
163}
164
165// PluginEvent is one lifecycle record shown in the UI.
166type PluginEvent struct {
167 ID int64 `json:"id"`
168 Slug string `json:"slug"`
169 Action string `json:"action"`
170 Version string `json:"version"`
171 Status string `json:"status"`
172 Message string `json:"message"`
173 CreatedAt int64 `json:"created_at"`
174}
175
176// AddPluginEvent appends a lifecycle record.
177func (s *Store) AddPluginEvent(ctx context.Context, e PluginEvent) error {
178 _, err := s.db.ExecContext(ctx,
179 `INSERT INTO plugin_events(slug, action, version, status, message, created_at)
180 VALUES(?,?,?,?,?,?)`,
181 e.Slug, e.Action, e.Version, e.Status, e.Message, time.Now().Unix())
182 return err
183}
184
185// PluginEvents returns the newest lifecycle records.
186func (s *Store) PluginEvents(ctx context.Context, slug string, limit int) ([]*PluginEvent, error) {
187 if limit <= 0 || limit > 200 {
188 limit = 50
189 }
190 query := `SELECT id, slug, action, version, status, message, created_at FROM plugin_events`
191 args := []any{}
192 if slug != "" {
193 query += ` WHERE slug = ?`
194 args = append(args, slug)
195 }
196 query += ` ORDER BY id DESC LIMIT ?`
197 args = append(args, limit)
198 rows, err := s.db.QueryContext(ctx, query, args...)
199 if err != nil {
200 return nil, err
201 }
202 defer rows.Close()
203 out := []*PluginEvent{}
204 for rows.Next() {
205 var e PluginEvent
206 if err := rows.Scan(&e.ID, &e.Slug, &e.Action, &e.Version, &e.Status, &e.Message, &e.CreatedAt); err != nil {
207 return nil, err
208 }
209 out = append(out, &e)
210 }
211 return out, rows.Err()
212}
213
214// Revocation caches a withdrawal pushed by the cloud.
215type Revocation struct {
216 Slug string `json:"slug"`
217 Version string `json:"version"`
218 Severity string `json:"severity"`
219 Action string `json:"action"`
220 Reason string `json:"reason"`
221}
222
223// UpsertRevocation records one withdrawal.
224func (s *Store) UpsertRevocation(ctx context.Context, rv Revocation) error {
225 _, err := s.db.ExecContext(ctx,
226 `INSERT INTO plugin_revocations(slug, version, severity, action, reason, fetched_at)
227 VALUES(?,?,?,?,?,?)
228 ON CONFLICT(slug, version) DO UPDATE SET severity=excluded.severity,
229 action=excluded.action, reason=excluded.reason, fetched_at=excluded.fetched_at`,
230 rv.Slug, rv.Version, rv.Severity, rv.Action, rv.Reason, time.Now().Unix())
231 return err
232}
233
234// Revocations returns every cached withdrawal.
235func (s *Store) Revocations(ctx context.Context) ([]*Revocation, error) {
236 rows, err := s.db.QueryContext(ctx,
237 `SELECT slug, version, severity, action, reason FROM plugin_revocations`)
238 if err != nil {
239 return nil, err
240 }
241 defer rows.Close()
242 out := []*Revocation{}
243 for rows.Next() {
244 var rv Revocation
245 if err := rows.Scan(&rv.Slug, &rv.Version, &rv.Severity, &rv.Action, &rv.Reason); err != nil {
246 return nil, err
247 }
248 out = append(out, &rv)
249 }
250 return out, rows.Err()
251}
252
253// RevocationFor reports whether a version is blocked or warned.
254func (s *Store) RevocationFor(ctx context.Context, slug, version string) (*Revocation, error) {
255 var rv Revocation
256 err := s.db.QueryRowContext(ctx,
257 `SELECT slug, version, severity, action, reason FROM plugin_revocations
258 WHERE slug = ? AND (version = ? OR version = '') ORDER BY version DESC LIMIT 1`,
259 slug, version).Scan(&rv.Slug, &rv.Version, &rv.Severity, &rv.Action, &rv.Reason)
260 if errors.Is(err, sql.ErrNoRows) {
261 return nil, ErrNotFound
262 }
263 if err != nil {
264 return nil, err
265 }
266 return &rv, nil
267}
268
269// LastRevocationID returns the highest cached revocation row id, used as the
270// incremental cursor when polling the cloud.
271func (s *Store) LastRevocationID(ctx context.Context) int64 {
272 var id int64
273 _ = s.db.QueryRowContext(ctx, `SELECT COALESCE(MAX(rowid),0) FROM plugin_revocations`).Scan(&id)
274 return id
275}