package system import ( "context" "fmt" "strings" "time" "github.com/miaopanel/miaopanel/internal/fsutil" ) // UnitDir is the systemd unit directory. const UnitDir = "/etc/systemd/system" // Service manages one systemd unit. type Service struct { Runner *Runner Name string // full unit name, e.g. miaopanel-site-blog.service Distro Distro } // WriteUnit installs (or replaces) a unit file and reloads systemd. func (s *Service) WriteUnit(ctx context.Context, unit string) error { if s.Distro.ServiceManager() != "systemd" { return fmt.Errorf("%w: 需要 systemd", ErrUnsupported) } if err := fsutil.WriteFileAtomic(UnitDir+"/"+s.Name, []byte(unit), 0o644); err != nil { return fmt.Errorf("写入 unit 失败: %w", err) } _, err := s.Runner.Run(ctx, "systemctl", "daemon-reload") return err } // Enable starts a unit and enables it on boot. func (s *Service) Enable(ctx context.Context) error { if s.Distro.ServiceManager() != "systemd" { return fmt.Errorf("%w: 需要 systemd", ErrUnsupported) } if _, err := s.Runner.Run(ctx, "systemctl", "enable", "--now", s.Name); err != nil { return fmt.Errorf("启动 %s 失败: %w", s.Name, err) } return nil } // Start starts a unit. func (s *Service) Start(ctx context.Context) error { return s.act(ctx, "start") } // Stop stops a unit. func (s *Service) Stop(ctx context.Context) error { return s.act(ctx, "stop") } // Restart restarts a unit. func (s *Service) Restart(ctx context.Context) error { return s.act(ctx, "restart") } // Disable stops a unit and removes it from boot. func (s *Service) Disable(ctx context.Context) error { if s.Distro.ServiceManager() != "systemd" { return fmt.Errorf("%w: 需要 systemd", ErrUnsupported) } if _, err := s.Runner.Run(ctx, "systemctl", "disable", "--now", s.Name); err != nil { return err } return nil } // Remove stops the unit and deletes the file. func (s *Service) Remove(ctx context.Context) error { _ = s.Disable(ctx) _ = fsutil.RemoveFile(UnitDir + "/" + s.Name) _, err := s.Runner.Run(ctx, "systemctl", "daemon-reload") return err } // Active reports the unit state ("active", "inactive", "failed", "unknown"). func (s *Service) Active(ctx context.Context) string { if s.Distro.ServiceManager() != "systemd" { return "unsupported" } res, err := s.Runner.Run(ctx, "systemctl", "is-active", s.Name) state := strings.TrimSpace(res.Stdout) if err != nil && state == "" { return "unknown" } if state == "" { state = strings.TrimSpace(res.Stderr) } if state == "" { return "unknown" } return state } // Logs returns the journal tail of the unit. func (s *Service) Logs(ctx context.Context, lines int) (string, error) { if lines <= 0 { lines = 200 } res, err := s.Runner.Run(ctx, "journalctl", "-u", s.Name, "-n", fmt.Sprint(lines), "--no-pager", "-o", "short-iso") if err != nil && res.Stdout == "" { return "", err } return res.Stdout, nil } func (s *Service) act(ctx context.Context, verb string) error { if s.Distro.ServiceManager() != "systemd" { return fmt.Errorf("%w: 需要 systemd", ErrUnsupported) } if _, err := s.Runner.Run(ctx, "systemctl", verb, s.Name); err != nil { return fmt.Errorf("systemctl %s %s: %w", verb, s.Name, err) } return nil } // ReloadNginx reloads the web server through systemd or the binary itself. func ReloadNginx(ctx context.Context, runner *Runner, binary string) error { if binary != "" { if _, err := runner.Run(ctx, binary, "-s", "reload"); err == nil { return nil } } res, err := runner.RunLong(ctx, 20*time.Second, "systemctl", "reload", "nginx") if err != nil { return fmt.Errorf("重载 nginx 失败: %s", firstNonEmpty(res.Stderr, res.Stdout)) } return nil } func firstNonEmpty(vals ...string) string { for _, v := range vals { if strings.TrimSpace(v) != "" { return strings.TrimSpace(v) } } return "" }