完善 Scert 前后端与部署支持
This commit is contained in:
1 parent
afdb947bea
commit
7f0af703ed
85 files changed
+7530
-800
No files matched your search
@@ -0,0 +1,81 @@
|
||||
//go:build linux
|
||||
|
||||
package servicehost
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func Install(ctx context.Context, options Options) error {
|
||||
o, err := options.normalized()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runSystemctl(ctx, "enable", o.Name); err != nil {
|
||||
return commandError("注册", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Uninstall(ctx context.Context, name string) error {
|
||||
if err := validateName(name); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runSystemctl(ctx, "disable", name); err != nil {
|
||||
return commandError("取消注册", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Start(ctx context.Context, name string) error {
|
||||
if err := validateName(name); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runSystemctl(ctx, "start", name); err != nil {
|
||||
return commandError("启动", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Stop(ctx context.Context, name string) error {
|
||||
if err := validateName(name); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runSystemctl(ctx, "stop", name); err != nil {
|
||||
return commandError("停止", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runSystemctl(ctx context.Context, args ...string) error {
|
||||
command := exec.CommandContext(ctx, "systemctl", args...)
|
||||
output, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
return &commandFailure{output: string(output), err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type commandFailure struct {
|
||||
output string
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *commandFailure) Error() string {
|
||||
if e.err != nil {
|
||||
return e.output + ": " + e.err.Error()
|
||||
}
|
||||
return e.output
|
||||
}
|
||||
|
||||
func (e *commandFailure) Unwrap() error { return e.err }
|
||||
|
||||
func validateName(name string) error {
|
||||
if name == "" || name == "." || name == ".." {
|
||||
return ErrInvalidName
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var ErrInvalidName = &commandFailure{output: "服务名称无效"}
|
||||
@@ -0,0 +1,65 @@
|
||||
package servicehost
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Name string
|
||||
DisplayName string
|
||||
Description string
|
||||
Executable string
|
||||
ConfigPath string
|
||||
WorkDir string
|
||||
}
|
||||
|
||||
func (o Options) Validate() error {
|
||||
if strings.TrimSpace(o.Name) == "" {
|
||||
return errors.New("服务名称不能为空")
|
||||
}
|
||||
if strings.ContainsAny(o.Name, `/\\`) {
|
||||
return errors.New("服务名称不能包含路径分隔符")
|
||||
}
|
||||
if strings.TrimSpace(o.Executable) == "" {
|
||||
return errors.New("服务程序路径不能为空")
|
||||
}
|
||||
if filepath.Clean(o.Executable) == "." {
|
||||
return errors.New("服务程序路径无效")
|
||||
}
|
||||
if strings.TrimSpace(o.ConfigPath) == "" {
|
||||
return errors.New("配置文件路径不能为空")
|
||||
}
|
||||
if filepath.Clean(o.ConfigPath) == "." {
|
||||
return errors.New("配置文件路径无效")
|
||||
}
|
||||
if strings.TrimSpace(o.WorkDir) == "" {
|
||||
return errors.New("服务工作目录不能为空")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o Options) normalized() (Options, error) {
|
||||
if err := o.Validate(); err != nil {
|
||||
return Options{}, err
|
||||
}
|
||||
o.Name = strings.TrimSpace(o.Name)
|
||||
o.DisplayName = strings.TrimSpace(o.DisplayName)
|
||||
if o.DisplayName == "" {
|
||||
o.DisplayName = o.Name
|
||||
}
|
||||
o.Description = strings.TrimSpace(o.Description)
|
||||
if o.Description == "" {
|
||||
o.Description = o.DisplayName
|
||||
}
|
||||
o.Executable = filepath.Clean(strings.TrimSpace(o.Executable))
|
||||
o.ConfigPath = filepath.Clean(strings.TrimSpace(o.ConfigPath))
|
||||
o.WorkDir = filepath.Clean(strings.TrimSpace(o.WorkDir))
|
||||
return o, nil
|
||||
}
|
||||
|
||||
func commandError(action string, err error) error {
|
||||
return fmt.Errorf("%s服务失败:%w", action, err)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package servicehost
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOptionsValidate(t *testing.T) {
|
||||
base := Options{
|
||||
Name: "Scert",
|
||||
Executable: filepath.Join("C:", "scert.exe"),
|
||||
ConfigPath: filepath.Join("C:", "scert", "scert.json"),
|
||||
WorkDir: filepath.Join("C:", "scert"),
|
||||
}
|
||||
if err := base.Validate(); err != nil {
|
||||
t.Fatalf("有效服务配置不应失败:%v", err)
|
||||
}
|
||||
for name, mutate := range map[string]func(*Options){
|
||||
"名称为空": func(o *Options) { o.Name = "" },
|
||||
"名称含路径": func(o *Options) { o.Name = "a/b" },
|
||||
"程序路径为空": func(o *Options) { o.Executable = "" },
|
||||
"配置路径为空": func(o *Options) { o.ConfigPath = "" },
|
||||
"工作目录为空": func(o *Options) { o.WorkDir = "" },
|
||||
} {
|
||||
item := base
|
||||
mutate(&item)
|
||||
if err := item.Validate(); err == nil {
|
||||
t.Errorf("%s时应校验失败", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptionsNormalizedDefaultsDisplayText(t *testing.T) {
|
||||
item, err := (Options{
|
||||
Name: " Scert ", Executable: "scert.exe", ConfigPath: "scert.json", WorkDir: filepath.Join("C:", "scert"),
|
||||
}).normalized()
|
||||
if err != nil {
|
||||
t.Fatalf("规范化服务配置失败:%v", err)
|
||||
}
|
||||
if item.DisplayName != "Scert" || item.Description != "Scert" {
|
||||
t.Fatalf("服务显示信息默认值错误:%+v", item)
|
||||
}
|
||||
if strings.TrimSpace(item.Executable) == "" {
|
||||
t.Fatal("规范化后程序路径不能为空")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//go:build !windows && !linux
|
||||
|
||||
package servicehost
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrUnsupported = errors.New("当前操作系统不支持系统服务管理")
|
||||
|
||||
func Install(_ context.Context, _ Options) error { return ErrUnsupported }
|
||||
func Uninstall(_ context.Context, _ string) error { return ErrUnsupported }
|
||||
func Start(_ context.Context, _ string) error { return ErrUnsupported }
|
||||
func Stop(_ context.Context, _ string) error { return ErrUnsupported }
|
||||
@@ -0,0 +1,90 @@
|
||||
//go:build windows
|
||||
|
||||
package servicehost
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Install(ctx context.Context, options Options) error {
|
||||
o, err := options.normalized()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
binPath := strconv.Quote(o.Executable) + " --config " + strconv.Quote(o.ConfigPath) + " serve"
|
||||
if err := runSC(ctx, "create", o.Name, "binPath=", binPath, "start=", "auto", "DisplayName=", o.DisplayName); err != nil {
|
||||
return commandError("安装", err)
|
||||
}
|
||||
if err := runSC(ctx, "description", o.Name, o.Description); err != nil {
|
||||
return commandError("写入描述", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Uninstall(ctx context.Context, name string) error {
|
||||
if name == "" {
|
||||
return commandError("卸载", ErrInvalidName)
|
||||
}
|
||||
if err := runSC(ctx, "stop", name); err != nil {
|
||||
// 服务未运行时允许继续删除。
|
||||
_ = err
|
||||
}
|
||||
if err := runSC(ctx, "delete", name); err != nil {
|
||||
return commandError("卸载", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Start(ctx context.Context, name string) error {
|
||||
if err := validateName(name); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runSC(ctx, "start", name); err != nil {
|
||||
return commandError("启动", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Stop(ctx context.Context, name string) error {
|
||||
if err := validateName(name); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runSC(ctx, "stop", name); err != nil {
|
||||
return commandError("停止", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runSC(ctx context.Context, args ...string) error {
|
||||
command := exec.CommandContext(ctx, "sc.exe", args...)
|
||||
output, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
return &commandFailure{output: string(output), err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var ErrInvalidName = &commandFailure{output: "服务名称无效"}
|
||||
|
||||
type commandFailure struct {
|
||||
output string
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *commandFailure) Error() string {
|
||||
if e.err != nil {
|
||||
return e.output + ": " + e.err.Error()
|
||||
}
|
||||
return e.output
|
||||
}
|
||||
|
||||
func (e *commandFailure) Unwrap() error { return e.err }
|
||||
|
||||
func validateName(name string) error {
|
||||
if name == "" || name == "." || name == ".." {
|
||||
return ErrInvalidName
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user