66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
//go:build linux
|
|
// +build linux
|
|
|
|
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
|
|
teaconst "github.com/TeaOSLab/EdgeHttpDNS/internal/const"
|
|
)
|
|
|
|
var systemdServiceFile = "/etc/systemd/system/" + teaconst.SystemdServiceName + ".service"
|
|
|
|
func (m *ServiceManager) Install(exePath string, args []string) error {
|
|
if os.Getgid() != 0 {
|
|
return errors.New("only root users can install the service")
|
|
}
|
|
|
|
systemd, err := exec.LookPath("systemctl")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
desc := `[Unit]
|
|
Description=GoEdge HTTPDNS Node Service
|
|
Before=shutdown.target
|
|
After=network-online.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
Restart=always
|
|
RestartSec=1s
|
|
ExecStart=` + exePath + ` daemon
|
|
ExecStop=` + exePath + ` stop
|
|
ExecReload=` + exePath + ` restart
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target`
|
|
|
|
err = os.WriteFile(systemdServiceFile, []byte(desc), 0777)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_ = exec.Command(systemd, "stop", teaconst.SystemdServiceName+".service").Run()
|
|
_ = exec.Command(systemd, "daemon-reload").Run()
|
|
return exec.Command(systemd, "enable", teaconst.SystemdServiceName+".service").Run()
|
|
}
|
|
|
|
func (m *ServiceManager) Uninstall() error {
|
|
if os.Getgid() != 0 {
|
|
return errors.New("only root users can uninstall the service")
|
|
}
|
|
|
|
systemd, err := exec.LookPath("systemctl")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_ = exec.Command(systemd, "disable", teaconst.SystemdServiceName+".service").Run()
|
|
_ = exec.Command(systemd, "daemon-reload").Run()
|
|
return os.Remove(systemdServiceFile)
|
|
}
|