Files
EdgeNode/internal/utils/service_linux.go

163 lines
3.8 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"
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
exec.Command(systemd, "disable", teaconst.SystemdServiceName+".service").Start()
// reload
exec.Command(systemd, "daemon-reload")
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
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
2021-01-12 10:56:30 +08:00
ExecStart=` + exePath + ` daemon
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
2022-09-15 11:45:29 +08:00
executils.NewTimeoutCmd(30*time.Second, systemd, "stop", shortName+".service")
2021-01-11 18:16:15 +08:00
// reload
2022-09-15 11:45:29 +08:00
executils.NewTimeoutCmd(30*time.Second, systemd, "daemon-reload")
2021-01-11 18:16:15 +08:00
// enable
2022-09-15 11:45:29 +08:00
var cmd = executils.NewTimeoutCmd(30*time.Second, systemd, "enable", shortName+".service")
cmd.WithStderr()
err = cmd.Run()
if err != nil {
return errors.New(err.Error() + ": " + cmd.Stderr())
}
return nil
2021-01-11 18:16:15 +08:00
}