Files
EdgeAdmin/internal/utils/service_linux.go

163 lines
3.8 KiB
Go
Raw Normal View History

2022-08-14 20:03:31 +08:00
//go:build linux
2021-01-12 11:49:37 +08:00
package utils
import (
"errors"
2024-07-27 15:42:58 +08:00
"os"
"os/exec"
"regexp"
2021-01-12 11:49:37 +08:00
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
2023-12-26 09:09:21 +08:00
executils "github.com/TeaOSLab/EdgeAdmin/internal/utils/exec"
2021-01-12 11:49:37 +08:00
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
)
var systemdServiceFile = "/etc/systemd/system/edge-admin.service"
var initServiceFile = "/etc/init.d/" + teaconst.SystemdServiceName
2022-08-14 20:03:31 +08:00
// Install 安装服务
2021-01-12 11:49:37 +08:00
func (this *ServiceManager) Install(exePath string, args []string) error {
if os.Getgid() != 0 {
return errors.New("only root users can install the service")
}
2023-12-26 09:09:21 +08:00
systemd, err := executils.LookPath("systemctl")
2021-01-12 11:49:37 +08:00
if err != nil {
return this.installInitService(exePath, args)
}
return this.installSystemdService(systemd, exePath, args)
}
2022-08-14 20:03:31 +08:00
// Start 启动服务
2021-01-12 11:49:37 +08:00
func (this *ServiceManager) Start() error {
if os.Getgid() != 0 {
return errors.New("only root users can start the service")
}
if files.NewFile(systemdServiceFile).Exists() {
2023-12-26 09:09:21 +08:00
systemd, err := executils.LookPath("systemctl")
2021-01-12 11:49:37 +08:00
if err != nil {
return err
}
return exec.Command(systemd, "start", teaconst.SystemdServiceName+".service").Start()
}
return exec.Command("service", teaconst.ProcessName, "start").Start()
}
2022-08-14 20:03:31 +08:00
// Uninstall 删除服务
2021-01-12 11:49:37 +08:00
func (this *ServiceManager) Uninstall() error {
if os.Getgid() != 0 {
return errors.New("only root users can uninstall the service")
}
if files.NewFile(systemdServiceFile).Exists() {
2023-12-26 09:09:21 +08:00
systemd, err := executils.LookPath("systemctl")
2021-01-12 11:49:37 +08:00
if err != nil {
return err
}
// disable service
_ = exec.Command(systemd, "disable", teaconst.SystemdServiceName+".service").Start()
2021-01-12 11:49:37 +08:00
// reload
_ = exec.Command(systemd, "daemon-reload").Start()
2021-01-12 11:49:37 +08:00
return files.NewFile(systemdServiceFile).Delete()
}
f := files.NewFile(initServiceFile)
if f.Exists() {
return f.Delete()
}
return nil
}
// install init service
func (this *ServiceManager) installInitService(exePath string, args []string) error {
shortName := teaconst.SystemdServiceName
scriptFile := Tea.Root + "/scripts/" + shortName
if !files.NewFile(scriptFile).Exists() {
return errors.New("'scripts/" + shortName + "' file not exists")
}
2022-08-04 11:51:34 +08:00
data, err := os.ReadFile(scriptFile)
2021-01-12 11:49:37 +08:00
if err != nil {
return err
}
data = regexp.MustCompile("INSTALL_DIR=.+").ReplaceAll(data, []byte("INSTALL_DIR="+Tea.Root))
2022-08-04 11:51:34 +08:00
err = os.WriteFile(initServiceFile, data, 0777)
2021-01-12 11:49:37 +08:00
if err != nil {
return err
}
2023-12-26 09:09:21 +08:00
chkCmd, err := executils.LookPath("chkconfig")
2021-01-12 11:49:37 +08:00
if err != nil {
return err
}
err = exec.Command(chkCmd, "--add", teaconst.ProcessName).Start()
if err != nil {
return err
}
return nil
}
// install systemd service
func (this *ServiceManager) installSystemdService(systemd, exePath string, args []string) error {
shortName := teaconst.SystemdServiceName
2021-01-18 09:30:52 +08:00
longName := "GoEdge Admin" // TODO 将来可以修改
2021-01-12 11:49:37 +08:00
2024-03-08 19:00:14 +08:00
var startCmd = exePath + " daemon"
bashPath, _ := executils.LookPath("bash")
if len(bashPath) > 0 {
startCmd = bashPath + " -c \"" + startCmd + "\""
}
2022-09-28 08:17:32 +08:00
desc := `### BEGIN INIT INFO
# Provides: ` + shortName + `
2021-01-12 11:49:37 +08:00
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: ` + longName + ` Service
### END INIT INFO
[Unit]
Description=` + longName + ` Service
Before=shutdown.target
After=network-online.target
[Service]
Type=simple
2023-04-03 10:01:35 +08:00
Restart=always
2023-04-02 18:05:54 +08:00
RestartSec=5s
2024-03-08 19:00:14 +08:00
ExecStart=` + startCmd + `
2021-01-12 11:49:37 +08:00
ExecStop=` + exePath + ` stop
ExecReload=` + exePath + ` reload
[Install]
WantedBy=multi-user.target`
// write file
2022-08-04 11:51:34 +08:00
err := os.WriteFile(systemdServiceFile, []byte(desc), 0777)
2021-01-12 11:49:37 +08:00
if err != nil {
return err
}
// stop current systemd service if running
_ = exec.Command(systemd, "stop", shortName+".service").Start()
2021-01-12 11:49:37 +08:00
// reload
_ = exec.Command(systemd, "daemon-reload").Start()
2021-01-12 11:49:37 +08:00
// enable
cmd := exec.Command(systemd, "enable", shortName+".service")
return cmd.Run()
}