/
startp.go
191 lines (170 loc) · 4.24 KB
/
startp.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package dstartcore
import (
"errors"
"log"
"os"
"os/signal"
"os/user"
"strconv"
"strings"
"syscall"
)
func startFromConfig(c *Config) error {
if c.Global.Envs == nil {
c.Global.Envs = make(map[string]string)
}
log.Println("verifying all services")
for s, v := range c.Services {
if err := VerifyService(v); err != nil {
log.Println("verify service:", s, "failed.", err)
return err
}
}
log.Println("starting all services")
for s, v := range c.Services {
if err := StartService(v, c.Global.Envs); err != nil {
log.Println("start service:", s, "failed.", err)
return err
}
log.Println("service:", s, "started.")
}
return nil
}
func VerifyService(service Service) error {
if !service.RedirectErrorOutputToOutput {
if len(service.ErrorOutputFile) <= 0 {
return errors.New("verify config error: need error output file when not setting redirect error output file")
}
}
if len(service.ExecCommand) <= 0 {
return errors.New("need exec command")
}
return nil
}
func StartService(service Service, globalEnv map[string]string) error {
attr := new(os.ProcAttr)
// working directory
if service.WorkingDirectory != "" {
attr.Dir = service.WorkingDirectory
}
// envs
var envs []string
for k, v := range globalEnv {
envs = append(envs, k+"="+v)
}
for k, v := range service.Envs {
envs = append(envs, k+"="+v)
}
if len(service.Envs) > 0 {
attr.Env = append(os.Environ(), envs...)
} else {
attr.Env = append(os.Environ())
}
// input file
in := os.Stdin
if service.InputFile != "" {
f, err := os.Open(service.InputFile)
if err != nil {
log.Println("open input file error: ", err)
return errors.New("open input file error")
}
in = f
}
// output file
out := os.Stdout
if service.OutputFile != "" {
fp := os.O_WRONLY | os.O_CREATE
if service.OutputFileAppend {
fp |= os.O_APPEND
} else {
fp |= os.O_TRUNC
}
f, err := os.OpenFile(service.OutputFile, fp, 0666)
if err != nil {
log.Println("open output file error: ", err)
return errors.New("open output file error")
}
out = f
}
// error output file
erroutput := os.Stderr
if service.RedirectErrorOutputToOutput {
erroutput = out
} else {
if service.ErrorOutputFile != "" {
fp := os.O_WRONLY | os.O_CREATE
if service.ErrorOutputFileAppend {
fp |= os.O_APPEND
} else {
fp |= os.O_TRUNC
}
f, err := os.OpenFile(service.ErrorOutputFile, fp, 0666)
if err != nil {
log.Println("open error output file error: ", err)
return errors.New("open error output file error")
}
erroutput = f
}
}
attr.Files = []*os.File{in, out, erroutput} // input/output/error
// user
if service.Username != "" {
u, err := user.Lookup(service.Username)
if err != nil {
log.Println(err)
return errors.New("lookup user error")
}
if attr.Sys == nil {
attr.Sys = new(syscall.SysProcAttr)
attr.Sys.Credential = new(syscall.Credential)
} else {
if attr.Sys.Credential == nil {
attr.Sys.Credential = new(syscall.Credential)
}
}
uid, err := strconv.Atoi(u.Uid)
if err != nil {
log.Println("strconv uid error: ", err)
return errors.New("convert uid error")
}
gid, err := strconv.Atoi(u.Gid)
if err != nil {
log.Println("strconv gid error: ", err)
return errors.New("convert gid error")
}
attr.Sys.Credential.Uid = uint32(uid)
attr.Sys.Credential.Gid = uint32(gid)
}
// group
if service.GroupName != "" {
g, err := user.LookupGroup(service.GroupName)
if err != nil {
log.Println(err)
return errors.New("lookup group error")
}
if attr.Sys == nil {
attr.Sys = new(syscall.SysProcAttr)
attr.Sys.Credential = new(syscall.Credential)
} else {
if attr.Sys.Credential == nil {
attr.Sys.Credential = new(syscall.Credential)
}
}
gid, err := strconv.Atoi(g.Gid)
if err != nil {
log.Println("strconv gid error: ", err)
return errors.New("convert gid error")
}
attr.Sys.Credential.Gid = uint32(gid)
}
// exec
cmds := strings.Split(strings.TrimSpace(service.ExecCommand), " ")
p, err := os.StartProcess(cmds[0], cmds, attr)
signal.Ignore(syscall.SIGCHLD)
if err != nil {
log.Println("start process:[", cmds[0], "] error: ", err)
return errors.New("start process error")
}
log.Println("start process:[", cmds[0], "] pid: ", p.Pid)
return nil
}