feat: restart to apply update, privileged helper, rollback
This commit is contained in:
1 parent
70460e4170
commit
54e8fd5fed
63 files changed
+8635
-164
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package update
|
||||
|
||||
import (
|
||||
domain "RenewCMS/internal/domain/update"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const systemctlCommand = "systemctl"
|
||||
|
||||
type SystemctlControl struct {
|
||||
service string
|
||||
}
|
||||
|
||||
var (
|
||||
_ domain.ServiceControl = &SystemctlControl{}
|
||||
_ domain.UnitManager = &SystemctlControl{}
|
||||
)
|
||||
|
||||
func NewSystemctlControl(service string) *SystemctlControl {
|
||||
return &SystemctlControl{service: service}
|
||||
}
|
||||
|
||||
func (c *SystemctlControl) Restart() error {
|
||||
return runSystemctl("restart", c.service)
|
||||
}
|
||||
|
||||
func (c *SystemctlControl) Status() (bool, error) {
|
||||
err := exec.Command(systemctlCommand, "is-active", "--quiet", c.service).Run()
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
return false, nil
|
||||
}
|
||||
return err == nil, err
|
||||
}
|
||||
|
||||
func (c *SystemctlControl) DaemonReload() error {
|
||||
return runSystemctl("daemon-reload")
|
||||
}
|
||||
|
||||
func (c *SystemctlControl) EnableNow(unit string) error {
|
||||
return runSystemctl("enable", "--now", unit)
|
||||
}
|
||||
|
||||
func (c *SystemctlControl) DisableNow(unit string) error {
|
||||
return runSystemctl("disable", "--now", unit)
|
||||
}
|
||||
|
||||
func runSystemctl(arguments ...string) error {
|
||||
output, err := exec.Command(systemctlCommand, arguments...).CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("systemctl %s: %w: %s", strings.Join(arguments, " "), err, strings.TrimSpace(string(output)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user