继续初始化测试前保存启动链路与页面迁移检查点
This commit is contained in:
1 parent
34d1fb88e5
commit
199d727ecc
20 files changed
+891
-67
No files matched your search
@@ -9,7 +9,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
@@ -21,6 +20,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/app"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/bootstrap"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/buildinfo"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
@@ -653,45 +653,45 @@ func commandStatus(cfg config.Config) map[string]any {
|
||||
}
|
||||
|
||||
func newServeCommand() *cobra.Command {
|
||||
return &cobra.Command{Use: "serve", Short: "启动网站服务", RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := loadConfig()
|
||||
var publicIP string
|
||||
command := &cobra.Command{Use: "serve", Short: "启动网站服务", RunE: func(cmd *cobra.Command, args []string) error {
|
||||
path, err := filepath.Abs(resolveConfigPath())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !cfg.SetupReady {
|
||||
cfg.Server.Host = "127.0.0.1"
|
||||
fmt.Fprintln(cmd.OutOrStdout(), "系统尚未初始化,临时服务仅监听本机;请完成初始化后重启服务。")
|
||||
}
|
||||
if cfg.SetupReady && !cfg.Web.Enabled {
|
||||
return fmt.Errorf("管理页面已关闭")
|
||||
reload := func() (config.Config, error) { return loadServeConfig(path, config.Default(baseDir)) }
|
||||
cfg, err := reload()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lock, err := acquireProcessLock(cfg, "Scert 服务已在运行或备份恢复任务正在执行")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Close()
|
||||
service, err := app.NewWithConfigPath(cmd.Context(), cfg, resolveConfigPath())
|
||||
lockedDataPath := cfg.Paths.Data
|
||||
cfg, err = reload()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer service.Close()
|
||||
if filepath.Clean(cfg.Paths.Data) != filepath.Clean(lockedDataPath) {
|
||||
return fmt.Errorf("配置的数据目录已经变更,请重新启动服务")
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
if service.Renewal != nil && cfg.SetupReady {
|
||||
service.Renewal.Start(ctx)
|
||||
}
|
||||
errCh := make(chan error, 1)
|
||||
go func() { errCh <- service.HTTP.ListenAndServe() }()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case err := <-errCh:
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
advertisedIP := publicIP
|
||||
if !cmd.Flags().Changed("bootstrap-public-ip") {
|
||||
advertisedIP = os.Getenv("SCERT_BOOTSTRAP_PUBLIC_IP")
|
||||
}
|
||||
return serveLifecycle(ctx, cfg, reload,
|
||||
func(ctx context.Context, initial config.Config) (bool, error) {
|
||||
return bootstrap.Run(ctx, bootstrap.ServerOptions{Config: initial, ConfigPath: path, PublicIP: advertisedIP, URLPath: bootstrapURLPath(initial), Output: cmd.OutOrStdout()})
|
||||
},
|
||||
func(ctx context.Context, ready config.Config) (bool, error) { return runApplication(ctx, ready, path) },
|
||||
)
|
||||
}}
|
||||
command.Flags().StringVar(&publicIP, "bootstrap-public-ip", "", "首次初始化地址中的公网 IP;也可设置 SCERT_BOOTSTRAP_PUBLIC_IP")
|
||||
return command
|
||||
}
|
||||
|
||||
func newDomainCommand() *cobra.Command {
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/app"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
type serveStage func(context.Context, config.Config) (bool, error)
|
||||
|
||||
// loadServeConfig 只允许真正不存在的文件进入新初始化;损坏、无权限或符号链接均失败关闭。
|
||||
func loadServeConfig(path string, defaults config.Config) (config.Config, error) {
|
||||
info, err := os.Lstat(path)
|
||||
if err == nil {
|
||||
if !info.Mode().IsRegular() {
|
||||
return config.Config{}, errors.New("服务配置必须是普通文件")
|
||||
}
|
||||
return config.LoadWithEnv(path)
|
||||
}
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return config.Config{}, err
|
||||
}
|
||||
defaults.Paths.Config = filepath.Dir(path)
|
||||
if err := defaults.ApplyEnvironment(); err != nil {
|
||||
return config.Config{}, err
|
||||
}
|
||||
if err := defaults.Validate(); err != nil {
|
||||
return config.Config{}, err
|
||||
}
|
||||
return defaults, nil
|
||||
}
|
||||
|
||||
func serveLifecycle(ctx context.Context, cfg config.Config, reload func() (config.Config, error), bootstrapStage, applicationStage serveStage) error {
|
||||
if !cfg.SetupReady {
|
||||
completed, err := bootstrapStage(ctx, cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !completed || ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
next, err := reload()
|
||||
if err != nil {
|
||||
return fmt.Errorf("重新读取正式配置失败:%w", err)
|
||||
}
|
||||
if !next.SetupReady {
|
||||
return errors.New("初始化服务已退出,但正式配置尚未完成;拒绝启动业务服务")
|
||||
}
|
||||
if filepath.Clean(next.Paths.Data) != filepath.Clean(cfg.Paths.Data) {
|
||||
return errors.New("初始化不能更改进程锁所在数据目录")
|
||||
}
|
||||
cfg = next
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
if !cfg.Web.Enabled {
|
||||
return errors.New("管理页面已关闭")
|
||||
}
|
||||
_, err := applicationStage(ctx, cfg)
|
||||
return err
|
||||
}
|
||||
|
||||
func runApplication(ctx context.Context, cfg config.Config, path string) (bool, error) {
|
||||
service, err := app.NewWithConfigPath(ctx, cfg, path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer service.Close()
|
||||
if service.Renewal != nil {
|
||||
service.Renewal.Start(ctx)
|
||||
}
|
||||
errorsChannel := make(chan error, 1)
|
||||
go func() { errorsChannel <- service.HTTP.ListenAndServe() }()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return false, nil
|
||||
case err := <-errorsChannel:
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
func bootstrapURLPath(cfg config.Config) string {
|
||||
if runtime.GOOS == "linux" {
|
||||
return "/run/scert/bootstrap-url"
|
||||
}
|
||||
path, _ := filepath.Abs(filepath.Join(cfg.Paths.Data, "run", "bootstrap-url"))
|
||||
return path
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
func TestLoadServeConfigFailsClosed(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "conf", "scert.json")
|
||||
defaults := config.Default(root)
|
||||
cfg, err := loadServeConfig(path, defaults)
|
||||
if err != nil || cfg.SetupReady || cfg.Paths.Config != filepath.Dir(path) {
|
||||
t.Fatalf("缺失配置应进入初始化:%v", err)
|
||||
}
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Fatal("仅加载不应保存默认配置")
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, data := range []string{"{", "null", "{}", `{"setup_ready":true}`, "[]", ""} {
|
||||
if err := os.WriteFile(path, []byte(data), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := loadServeConfig(path, defaults); err == nil {
|
||||
t.Fatalf("损坏配置被降级成初始化:%q", data)
|
||||
}
|
||||
}
|
||||
if _, err := loadServeConfig(root, defaults); err == nil {
|
||||
t.Fatal("目录不应被视为缺失配置")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeLifecycle(t *testing.T) {
|
||||
for _, name := range []string{"missing", "ready", "cancel", "bootstrap-error", "reload-error", "not-ready", "path-changed", "web-disabled", "application-error"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
cfg.SetupReady = name == "ready" || name == "web-disabled" || name == "application-error"
|
||||
cfg.Web.Enabled = name != "web-disabled"
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
bootstrapCalls, reloadCalls, appCalls := 0, 0, 0
|
||||
err := serveLifecycle(ctx, cfg, func() (config.Config, error) {
|
||||
reloadCalls++
|
||||
if name == "reload-error" {
|
||||
return config.Config{}, errors.New("test")
|
||||
}
|
||||
next := cfg
|
||||
next.SetupReady = name != "not-ready"
|
||||
if name == "path-changed" {
|
||||
next.Paths.Data += "-changed"
|
||||
}
|
||||
return next, nil
|
||||
}, func(context.Context, config.Config) (bool, error) {
|
||||
bootstrapCalls++
|
||||
if name == "bootstrap-error" {
|
||||
return false, errors.New("test")
|
||||
}
|
||||
if name == "cancel" {
|
||||
cancel()
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}, func(_ context.Context, ready config.Config) (bool, error) {
|
||||
appCalls++
|
||||
if !ready.SetupReady {
|
||||
t.Fatal("未初始化时构造业务应用")
|
||||
}
|
||||
if name == "application-error" {
|
||||
return false, errors.New("test")
|
||||
}
|
||||
return false, nil
|
||||
})
|
||||
wantError := name != "missing" && name != "ready" && name != "cancel"
|
||||
if (err != nil) != wantError {
|
||||
t.Fatalf("错误边界不符:%v", err)
|
||||
}
|
||||
wantApp := 0
|
||||
if name == "missing" || name == "ready" || name == "application-error" {
|
||||
wantApp = 1
|
||||
}
|
||||
if appCalls != wantApp {
|
||||
t.Fatalf("业务启动次数:%d", appCalls)
|
||||
}
|
||||
if cfg.SetupReady && (bootstrapCalls != 0 || reloadCalls != 0) {
|
||||
t.Fatal("已完成配置进入初始化")
|
||||
}
|
||||
if !cfg.SetupReady && bootstrapCalls != 1 {
|
||||
t.Fatal("初始化未运行一次")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user