-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemd-unit.go
91 lines (86 loc) · 2.95 KB
/
systemd-unit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package daemon
import (
"fmt"
"path/filepath"
"strings"
"github.com/coreos/go-systemd/v22/unit"
)
func SetUnitConfig(section, name, value string) {
if _, ok := unitConfig[section]; !ok {
unitConfig[section] = make(map[string]string)
}
unitConfig[section][name] = value
}
var unitConfig = map[string]map[string]string{
"Unit": {
"Wants": "network.target",
},
"Install": {
// "DefaultInstance": "default",
"WantedBy": "multi-user.target",
},
"Service": {
"Type": "exec",
"ExecReload": "/bin/kill -s HUP $MAINPID", // 发送HUP信号重载服务
"Restart": "always", // 只要不是通过systemctl stop来停止服务,任何情况下都必须要重启服务
"RestartSec": "0", // 重启间隔
"StartLimitInterval": "30", // 启动尝试间隔
"StartLimitBurst": "10", // 最大启动尝试次数
"RestartPreventExitStatus": "SIGKILL", // kill -9 不重启
},
}
func CreateUnit(multi bool, binName, desc, path string, args ...string) ([]byte, error) {
if unitConfig == nil {
return nil, fmt.Errorf("unitConfig is nil")
}
if _, ok := unitConfig["Install"]; !ok {
unitConfig["Install"] = make(map[string]string)
}
if multi {
unitConfig["Install"]["DefaultInstance"] = "default"
binName += "@%i"
} else {
delete(unitConfig["Install"], "DefaultInstance")
}
if _, ok := unitConfig["Unit"]; !ok {
unitConfig["Unit"] = make(map[string]string)
}
if _, ok := unitConfig["Unit"]["Description"]; !ok {
unitConfig["Unit"]["Description"] = strings.ToUpper(binName[:1]) + binName[1:] + " " + desc
}
if _, ok := unitConfig["Service"]; !ok {
unitConfig["Service"] = make(map[string]string)
}
if _, ok := unitConfig["Service"]["WorkingDirectory"]; !ok {
unitConfig["Service"]["WorkingDirectory"] = filepath.Dir(path)
}
if _, ok := unitConfig["Service"]["PIDFile"]; !ok {
unitConfig["Service"]["PIDFile"] = "/run/" + binName + ".pid"
}
if _, ok := unitConfig["Service"]["ExecStartPre"]; !ok {
unitConfig["Service"]["ExecStartPre"] = "/bin/rm -f /run/" + binName + ".pid"
}
if _, ok := unitConfig["Service"]["ExecStart"]; !ok {
if multi {
unitConfig["Service"]["ExecStart"] = path + " --instance %i " + strings.Join(args, " ")
} else {
unitConfig["Service"]["ExecStart"] = path + " " + strings.Join(args, " ")
}
}
if _, ok := unitConfig["Service"]["ExecStartPost"]; !ok {
unitConfig["Service"]["ExecStartPost"] = "/bin/bash -c '/bin/systemctl show -p MainPID --value " + binName + " > /run/" + binName + ".pid'"
}
data := make([]*unit.UnitOption, 0, 10)
for sec, v := range unitConfig {
for name, value := range v {
data = append(data, &unit.UnitOption{Section: sec, Name: name, Value: value})
}
}
reader := unit.Serialize(data)
buf := make([]byte, 1024)
n, err := reader.Read(buf)
if err != nil {
return nil, err
}
return buf[:n], nil
}