MCPcopy Index your code
hub / github.com/GoEdgeLab/EdgeNode / setupSystemd

Method setupSystemd

internal/nodes/system_services.go:65–151  ·  view source on GitHub ↗
(params maps.Map)

Source from the content-addressed store, hash-verified

63}
64
65func (this *SystemServiceManager) setupSystemd(params maps.Map) error {
66 // 只有在Linux下运行
67 if runtime.GOOS != "linux" {
68 return nil
69 }
70
71 if params == nil {
72 params = maps.Map{}
73 }
74 data, err := json.Marshal(params)
75 if err != nil {
76 return err
77 }
78
79 var config = &nodeconfigs.SystemdServiceConfig{}
80 err = json.Unmarshal(data, config)
81 if err != nil {
82 return err
83 }
84
85 // 检查当前的service
86 systemctl, err := executils.LookPath("systemctl")
87 if err != nil {
88 return err
89 }
90 if len(systemctl) == 0 {
91 return errors.New("can not find 'systemctl' on the system")
92 }
93
94 // 记录上次状态
95 var isOnInt int
96 if config.IsOn {
97 isOnInt = 1
98 } else {
99 isOnInt = 0
100 }
101
102 if this.lastIsOn == isOnInt {
103 return nil
104 }
105 defer func() {
106 this.lastIsOn = isOnInt
107 }()
108
109 var shortName = teaconst.SystemdServiceName
110 var cmd = executils.NewTimeoutCmd(10*time.Second, systemctl, "is-enabled", shortName)
111 cmd.WithStdout()
112 err = cmd.Run()
113 var hasInstalled = err == nil
114 if config.IsOn {
115 exe, err := os.Executable()
116 if err != nil {
117 return err
118 }
119
120 // 检查文件路径是否变化
121 if hasInstalled && cmd.Stdout() == "enabled" {
122 data, err := os.ReadFile("/etc/systemd/system/" + teaconst.SystemdServiceName + ".service")

Callers 1

SetupMethod · 0.95

Calls 8

NewServiceManagerFunction · 0.92
NewFunction · 0.92
WithStdoutMethod · 0.80
StdoutMethod · 0.80
StartMethod · 0.65
RunMethod · 0.45
InstallMethod · 0.45
UninstallMethod · 0.45

Tested by

no test coverage detected