-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
181 lines (169 loc) · 3.87 KB
/
config.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"gnd.la/config"
"gnd.la/log"
"github.com/fiam/stringutil"
)
type Config struct {
File string
Command string
Name string
Dir string
Env map[string]string
Start bool `default:"true"`
User string
Group string
Priority int `default:"1000"`
Watchdog *Watchdog
WatchdogInterval int `default:"300"`
MaxOpenFiles int
Log *Logger
Err error
}
func (c *Config) Cmd() (*exec.Cmd, error) {
if c.Err != nil {
return nil, c.Err
}
if c.Command == "" {
return nil, fmt.Errorf("no command")
}
fields, err := stringutil.SplitFields(c.Command, " ")
if err != nil {
return nil, err
}
if !filepath.IsAbs(fields[0]) {
p, err := exec.LookPath(fields[0])
if err != nil {
return nil, err
}
fields[0] = p
}
dir := c.Dir
if dir == "" {
dir = filepath.Dir(fields[0])
}
cmd := &exec.Cmd{Path: fields[0], Args: fields, Dir: dir}
for k, v := range c.Env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
if _, ok := c.Env["GOMAXPROCS"]; !ok {
cmd.Env = append(cmd.Env, fmt.Sprintf("GOMAXPROCS=%d", runtime.NumCPU()))
}
for _, v := range os.Environ() {
if p := strings.IndexByte(v, '='); p >= 0 {
k := v[:p]
if _, ok := c.Env[k]; !ok {
cmd.Env = append(cmd.Env, v)
}
}
}
info, err := os.Stat(fields[0])
if err != nil {
return nil, err
}
stat := info.Sys().(*syscall.Stat_t)
uid := stat.Uid
gid := stat.Gid
if c.Group != "" {
if g := getGroupId(c.Group); g > 0 {
gid = uint32(g)
} else {
return nil, fmt.Errorf("invalid group %q", c.Group)
}
}
if c.User != "" {
u, err := user.Lookup(c.User)
if err != nil {
return nil, err
}
ui, _ := strconv.Atoi(u.Uid)
uid = uint32(ui)
if gid == 0 {
gi, _ := strconv.Atoi(u.Gid)
gid = uint32(gi)
}
}
var cred *syscall.Credential
if uid != 0 || gid != 0 {
cred = &syscall.Credential{Uid: uid, Gid: gid}
}
attr := &syscall.SysProcAttr{
Credential: cred,
}
prepareSysProcAttr(attr)
cmd.SysProcAttr = attr
log.Debugf("%s wd: %s, env: %s, cred: %+v", c.ServiceName(), dir, cmd.Env, cred)
return cmd, nil
}
func (c *Config) ServiceName() string {
if c.Name != "" {
return c.Name
}
return c.File
}
func (g *Governator) servicesDir() string {
return filepath.Join(g.configDir, "services")
}
func (g *Governator) servicePath(filename string) string {
return filepath.Join(g.servicesDir(), filename)
}
func (g *Governator) configDirIsDefault() bool {
return g.configDir == defaultConfigDir
}
func (g *Governator) parseConfig(filename string) *Config {
cfg := &Config{File: filename}
err := config.ParseFile(g.servicePath(filename), cfg)
cfg.Err = err
if cfg.Log == nil {
cfg.Log = new(Logger)
cfg.Log.Parse("")
}
cfg.Name = cfg.ServiceName()
cfg.Log.Name = cfg.Name
return cfg
}
func (g *Governator) parseConfigs() ([]*Config, error) {
dir := g.servicesDir()
if g.configDirIsDefault() {
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("error creating services directory %s: %s", dir, err)
}
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("error reading config directory %s: %s", dir, err)
}
var configs []*Config
for _, v := range files {
name := v.Name()
if g.shouldIgnoreFile(name, false) {
continue
}
cfg := g.parseConfig(name)
log.Debugf("Parsed config %s: %+v", name, cfg)
configs = append(configs, cfg)
}
return configs, nil
}
func (g *Governator) shouldIgnoreFile(name string, deleted bool) bool {
if name == "" || name[0] == '.' || strings.HasSuffix(name, "~") {
return true
}
if !deleted {
info, err := os.Stat(g.servicePath(name))
if err != nil || info.Size() == 0 || info.IsDir() {
return true
}
}
return false
}