Files
EdgeNode/internal/utils/service_linux.go

170 lines
4.0 KiB
Go
Raw Normal View History

2022-09-15 11:45:29 +08:00
//go:build linux
2021-01-11 18:16:15 +08:00
// +build linux
package utils
import (
"errors"
2023-08-11 14:51:23 +08:00
"fmt"
2021-01-11 18:16:15 +08:00
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
2022-09-15 11:45:29 +08:00
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
2021-01-11 18:16:15 +08:00
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"os"
"os/exec"
"regexp"
2022-09-15 11:45:29 +08:00
"time"
2021-01-11 18:16:15 +08:00
)
var systemdServiceFile = "/etc/systemd/system/edge-node.service"
var initServiceFile = "/etc/init.d/" + teaconst.SystemdServiceName
2022-09-15 11:45:29 +08:00
// Install 安装服务
2021-01-11 18:16:15 +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")
}
systemd, err := executils.LookPath("systemctl")
2021-01-11 18:16:15 +08:00
if err != nil {
return this.installInitService(exePath, args)
}
return this.installSystemdService(systemd, exePath, args)
}
2022-09-15 11:45:29 +08:00
// Start 启动服务
2021-01-11 18:16:15 +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() {
systemd, err := executils.LookPath("systemctl")
2021-01-11 18:16:15 +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-09-15 11:45:29 +08:00
// Uninstall 删除服务
2021-01-11 18:16:15 +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() {
systemd, err := executils.LookPath("systemctl")
2021-01-11 18:16:15 +08:00
if err != nil {
return err
}
// disable service
2023-08-01 16:10:05 +08:00
_ = executils.NewTimeoutCmd(10*time.Second, systemd, "disable", teaconst.SystemdServiceName+".service").Start()
2021-01-11 18:16:15 +08:00
// reload
2023-08-01 16:10:05 +08:00
_ = executils.NewTimeoutCmd(10*time.Second, systemd, "daemon-reload").Start()
2021-01-11 18:16:15 +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:34:06 +08:00
data, err := os.ReadFile(scriptFile)
2021-01-11 18:16:15 +08:00
if err != nil {
return err
}
data = regexp.MustCompile("INSTALL_DIR=.+").ReplaceAll(data, []byte("INSTALL_DIR="+Tea.Root))
2022-08-04 11:34:06 +08:00
err = os.WriteFile(initServiceFile, data, 0777)
2021-01-11 18:16:15 +08:00
if err != nil {
return err
}
chkCmd, err := executils.LookPath("chkconfig")
2021-01-11 18:16:15 +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 {
2022-09-15 11:45:29 +08:00
var shortName = teaconst.SystemdServiceName
var longName = "GoEdge Node" // TODO 将来可以修改
2021-01-11 18:16:15 +08:00
2024-03-08 18:56:08 +08:00
var startCmd = exePath + " daemon"
bashPath, _ := executils.LookPath("bash")
if len(bashPath) > 0 {
startCmd = bashPath + " -c \"" + startCmd + "\""
}
2022-09-28 08:17:25 +08:00
var desc = `### BEGIN INIT INFO
# Provides: ` + shortName + `
2021-01-11 18:16:15 +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
2021-01-11 18:16:15 +08:00
[Service]
2021-01-12 10:56:30 +08:00
Type=simple
Restart=always
RestartSec=1s
2024-03-08 18:56:08 +08:00
ExecStart=` + startCmd + `
2021-01-11 18:16:15 +08:00
ExecStop=` + exePath + ` stop
ExecReload=` + exePath + ` reload
[Install]
WantedBy=multi-user.target`
// write file
2022-08-04 11:34:06 +08:00
err := os.WriteFile(systemdServiceFile, []byte(desc), 0777)
2021-01-11 18:16:15 +08:00
if err != nil {
return err
}
// stop current systemd service if running
2023-08-01 16:10:05 +08:00
_ = executils.NewTimeoutCmd(10*time.Second, systemd, "stop", shortName+".service").Start()
2021-01-11 18:16:15 +08:00
// reload
2023-08-01 16:10:05 +08:00
_ = executils.NewTimeoutCmd(10*time.Second, systemd, "daemon-reload").Start()
2021-01-11 18:16:15 +08:00
// enable
2023-08-01 16:10:05 +08:00
var cmd = executils.NewTimeoutCmd(10*time.Second, systemd, "enable", shortName+".service")
2022-09-15 11:45:29 +08:00
cmd.WithStderr()
err = cmd.Run()
if err != nil {
2023-08-11 14:51:23 +08:00
return fmt.Errorf("%w: %s", err, cmd.Stderr())
2022-09-15 11:45:29 +08:00
}
return nil
2021-01-11 18:16:15 +08:00
}