仰望星辰工作室

miaopanel

miaopanel/ internal/system/service.go 3.8 KB · 142 行 原始文件
1package system
2
3import (
4 "context"
5 "fmt"
6 "strings"
7 "time"
8
9 "github.com/miaopanel/miaopanel/internal/fsutil"
10)
11
12// UnitDir is the systemd unit directory.
13const UnitDir = "/etc/systemd/system"
14
15// Service manages one systemd unit.
16type Service struct {
17 Runner *Runner
18 Name string // full unit name, e.g. miaopanel-site-blog.service
19 Distro Distro
20}
21
22// WriteUnit installs (or replaces) a unit file and reloads systemd.
23func (s *Service) WriteUnit(ctx context.Context, unit string) error {
24 if s.Distro.ServiceManager() != "systemd" {
25 return fmt.Errorf("%w: 需要 systemd", ErrUnsupported)
26 }
27 if err := fsutil.WriteFileAtomic(UnitDir+"/"+s.Name, []byte(unit), 0o644); err != nil {
28 return fmt.Errorf("写入 unit 失败: %w", err)
29 }
30 _, err := s.Runner.Run(ctx, "systemctl", "daemon-reload")
31 return err
32}
33
34// Enable starts a unit and enables it on boot.
35func (s *Service) Enable(ctx context.Context) error {
36 if s.Distro.ServiceManager() != "systemd" {
37 return fmt.Errorf("%w: 需要 systemd", ErrUnsupported)
38 }
39 if _, err := s.Runner.Run(ctx, "systemctl", "enable", "--now", s.Name); err != nil {
40 return fmt.Errorf("启动 %s 失败: %w", s.Name, err)
41 }
42 return nil
43}
44
45// Start starts a unit.
46func (s *Service) Start(ctx context.Context) error {
47 return s.act(ctx, "start")
48}
49
50// Stop stops a unit.
51func (s *Service) Stop(ctx context.Context) error {
52 return s.act(ctx, "stop")
53}
54
55// Restart restarts a unit.
56func (s *Service) Restart(ctx context.Context) error {
57 return s.act(ctx, "restart")
58}
59
60// Disable stops a unit and removes it from boot.
61func (s *Service) Disable(ctx context.Context) error {
62 if s.Distro.ServiceManager() != "systemd" {
63 return fmt.Errorf("%w: 需要 systemd", ErrUnsupported)
64 }
65 if _, err := s.Runner.Run(ctx, "systemctl", "disable", "--now", s.Name); err != nil {
66 return err
67 }
68 return nil
69}
70
71// Remove stops the unit and deletes the file.
72func (s *Service) Remove(ctx context.Context) error {
73 _ = s.Disable(ctx)
74 _ = fsutil.RemoveFile(UnitDir + "/" + s.Name)
75 _, err := s.Runner.Run(ctx, "systemctl", "daemon-reload")
76 return err
77}
78
79// Active reports the unit state ("active", "inactive", "failed", "unknown").
80func (s *Service) Active(ctx context.Context) string {
81 if s.Distro.ServiceManager() != "systemd" {
82 return "unsupported"
83 }
84 res, err := s.Runner.Run(ctx, "systemctl", "is-active", s.Name)
85 state := strings.TrimSpace(res.Stdout)
86 if err != nil && state == "" {
87 return "unknown"
88 }
89 if state == "" {
90 state = strings.TrimSpace(res.Stderr)
91 }
92 if state == "" {
93 return "unknown"
94 }
95 return state
96}
97
98// Logs returns the journal tail of the unit.
99func (s *Service) Logs(ctx context.Context, lines int) (string, error) {
100 if lines <= 0 {
101 lines = 200
102 }
103 res, err := s.Runner.Run(ctx, "journalctl", "-u", s.Name, "-n",
104 fmt.Sprint(lines), "--no-pager", "-o", "short-iso")
105 if err != nil && res.Stdout == "" {
106 return "", err
107 }
108 return res.Stdout, nil
109}
110
111func (s *Service) act(ctx context.Context, verb string) error {
112 if s.Distro.ServiceManager() != "systemd" {
113 return fmt.Errorf("%w: 需要 systemd", ErrUnsupported)
114 }
115 if _, err := s.Runner.Run(ctx, "systemctl", verb, s.Name); err != nil {
116 return fmt.Errorf("systemctl %s %s: %w", verb, s.Name, err)
117 }
118 return nil
119}
120
121// ReloadNginx reloads the web server through systemd or the binary itself.
122func ReloadNginx(ctx context.Context, runner *Runner, binary string) error {
123 if binary != "" {
124 if _, err := runner.Run(ctx, binary, "-s", "reload"); err == nil {
125 return nil
126 }
127 }
128 res, err := runner.RunLong(ctx, 20*time.Second, "systemctl", "reload", "nginx")
129 if err != nil {
130 return fmt.Errorf("重载 nginx 失败: %s", firstNonEmpty(res.Stderr, res.Stdout))
131 }
132 return nil
133}
134
135func firstNonEmpty(vals ...string) string {
136 for _, v := range vals {
137 if strings.TrimSpace(v) != "" {
138 return strings.TrimSpace(v)
139 }
140 }
141 return ""
142}