feat: import Sea vHost single-server testing release

This commit is contained in:
canglan committed 2026-10-30 08:39:10 +08:00
commit 248badd33e
441 files changed
+63221

No files matched your search

+174
View File
@@ -0,0 +1,174 @@
package store
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
)
type Node struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Transport string `json:"transport"`
Endpoint string `json:"endpoint"`
RootDomain string `json:"rootDomain"`
TenantRoot string `json:"tenantRoot"`
FTPCertificateDomain string `json:"ftpCertificateDomain"`
Enabled bool `json:"enabled"`
Credentials []byte `json:"-"`
LastSeenAt *time.Time `json:"lastSeenAt,omitempty"`
LastError string `json:"lastError"`
}
const nodeColumns = "id,name,transport,endpoint,root_domain,tenant_root,ftp_certificate_domain,enabled,credentials,last_seen_at,last_error"
func scanNode(row interface{ Scan(...any) error }) (Node, error) {
var n Node
err := row.Scan(&n.ID, &n.Name, &n.Transport, &n.Endpoint, &n.RootDomain, &n.TenantRoot, &n.FTPCertificateDomain, &n.Enabled, &n.Credentials, &n.LastSeenAt, &n.LastError)
if errors.Is(err, sql.ErrNoRows) {
err = ErrNotFound
}
return n, err
}
func (s *Store) Node(ctx context.Context, id uint64) (Node, error) {
return scanNode(s.db.QueryRowContext(ctx, "SELECT "+nodeColumns+" FROM control_nodes WHERE id=? AND deleted_at IS NULL", id))
}
func (s *Store) Nodes(ctx context.Context) ([]Node, error) {
rows, err := s.db.QueryContext(ctx, "SELECT "+nodeColumns+" FROM control_nodes WHERE deleted_at IS NULL ORDER BY id")
if err != nil {
return nil, err
}
defer rows.Close()
result := []Node{}
for rows.Next() {
n, e := scanNode(rows)
if e != nil {
return nil, e
}
result = append(result, n)
}
return result, rows.Err()
}
func (s *Store) InitializeLocalNode(ctx context.Context, domain, root, ftp string) error {
_, err := s.db.ExecContext(ctx, "UPDATE control_nodes SET root_domain=?,tenant_root=?,ftp_certificate_domain=? WHERE id=1 AND transport='local' AND root_domain=''", domain, root, ftp)
return err
}
func (s *Store) CreateNode(ctx context.Context, n Node) (uint64, error) {
r, err := s.db.ExecContext(ctx, `INSERT INTO control_nodes(name,transport,endpoint,root_domain,tenant_root,ftp_certificate_domain,enabled,registry_json) VALUES(?,'https',?,?,?,?,FALSE,'{"version":1,"sites":{},"ftp":{},"databases":{}}')`, n.Name, n.Endpoint, n.RootDomain, n.TenantRoot, n.FTPCertificateDomain)
if err != nil {
return 0, err
}
id, err := r.LastInsertId()
return uint64(id), err
}
func (s *Store) SetNodeCredentials(ctx context.Context, id uint64, cipher []byte) error {
_, err := s.db.ExecContext(ctx, "UPDATE control_nodes SET credentials=?,enabled=FALSE,last_seen_at=NULL,last_error='' WHERE id=? AND deleted_at IS NULL AND transport='https'", cipher, id)
return err
}
func (s *Store) SetNodeEnabled(ctx context.Context, id uint64, enabled bool) error {
r, err := s.db.ExecContext(ctx, "UPDATE control_nodes SET enabled=? WHERE id=? AND deleted_at IS NULL", enabled, id)
if err != nil {
return err
}
n, _ := r.RowsAffected()
if n == 0 {
_, err = s.Node(ctx, id)
}
return err
}
func (s *Store) RecordNodeProbe(ctx context.Context, id uint64, detail string) error {
if len(detail) > 500 {
detail = detail[:500]
}
if detail == "" {
_, err := s.db.ExecContext(ctx, "UPDATE control_nodes SET last_seen_at=UTC_TIMESTAMP(),last_error='' WHERE id=?", id)
return err
}
_, err := s.db.ExecContext(ctx, "UPDATE control_nodes SET last_error=? WHERE id=?", detail, id)
return err
}
func (s *Store) NodeForHost(ctx context.Context, host string) (uint64, error) {
var id uint64
err := s.db.QueryRowContext(ctx, "SELECT node_id FROM projects WHERE host_name=?", host).Scan(&id)
if errors.Is(err, sql.ErrNoRows) {
err = ErrNotFound
}
return id, err
}
func (s *Store) ProjectPlacement(ctx context.Context, id uint64) (nodeID, planID uint64, err error) {
err = s.db.QueryRowContext(ctx, "SELECT node_id,COALESCE(plan_id,0) FROM projects WHERE id=?", id).Scan(&nodeID, &planID)
if errors.Is(err, sql.ErrNoRows) {
err = ErrNotFound
}
return
}
func (s *Store) NodeForSite(ctx context.Context, id uint64) (uint64, error) {
var node uint64
err := s.db.QueryRowContext(ctx, "SELECT node_id FROM sites WHERE id=?", id).Scan(&node)
if errors.Is(err, sql.ErrNoRows) {
err = ErrNotFound
}
return node, err
}
// The registry lives only in the controller DB. A per-node MySQL named lock
// serializes snapshots across foreground requests and scheduler workers.
type NodeRegistryLease struct {
conn *sql.Conn
id uint64
key string
}
func (s *Store) LockNodeRegistry(ctx context.Context, id uint64) (*NodeRegistryLease, error) {
conn, err := s.db.Conn(ctx)
if err != nil {
return nil, err
}
key := fmt.Sprintf("sea-vhost/node/%d", id)
var acquired sql.NullInt64
if err = conn.QueryRowContext(ctx, "SELECT GET_LOCK(?,10)", key).Scan(&acquired); err != nil || !acquired.Valid || acquired.Int64 != 1 {
conn.Close()
return nil, errors.New("node controller operation is busy")
}
return &NodeRegistryLease{conn: conn, id: id, key: key}, nil
}
func (l *NodeRegistryLease) Read(ctx context.Context) ([]byte, error) {
var raw string
err := l.conn.QueryRowContext(ctx, "SELECT registry_json FROM control_nodes WHERE id=? AND deleted_at IS NULL", l.id).Scan(&raw)
return []byte(raw), err
}
func (l *NodeRegistryLease) Save(ctx context.Context, raw []byte) error {
_, err := l.conn.ExecContext(ctx, "UPDATE control_nodes SET registry_json=?,registry_revision=registry_revision+1 WHERE id=?", string(raw), l.id)
return err
}
func (l *NodeRegistryLease) Close() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, _ = l.conn.ExecContext(ctx, "SELECT RELEASE_LOCK(?)", l.key)
_ = l.conn.Close()
}
func (s *Store) StartControlTask(ctx context.Context, nodeID, projectID uint64, operation, requestID string) (uint64, error) {
var project any
if projectID > 0 {
project = projectID
}
r, err := s.db.ExecContext(ctx, "INSERT INTO control_tasks(node_id,project_id,operation,request_id,started_at) VALUES(?,?,?,?,UTC_TIMESTAMP())", nodeID, project, operation, requestID)
if err != nil {
return 0, err
}
id, err := r.LastInsertId()
return uint64(id), err
}
func (s *Store) FinishControlTask(ctx context.Context, id uint64, state, detail string) error {
if state != "succeeded" && state != "failed" && state != "uncertain" {
return errors.New("invalid controller task state")
}
if len(detail) > 500 {
detail = detail[:500]
}
_, err := s.db.ExecContext(ctx, "UPDATE control_tasks SET state=?,detail=?,finished_at=UTC_TIMESTAMP() WHERE id=?", state, detail, id)
return err
}