-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (152 loc) · 3.59 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/fs"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/ghodss/yaml"
"github.com/sirupsen/logrus"
)
type Service struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Env map[string]string `json:"env,omitempty"`
Cwd string `json:"cwd,omitempty"`
ExecStart []string `json:"execStart,omitempty"`
RestartSec int `json:"restartSec,omitempty"`
Depends []string `json:"depends,omitempty"`
Status bool `json:"status,omitempty"`
}
var (
configDir = flag.String("c", "./config", "set service config file scan dir")
serviceMap = make(map[string]*Service, 5)
)
func main() {
flag.Parse()
err := ScanServcie()
if err != nil {
logrus.WithField("config", *configDir).
WithError(err).Fatalf("failed to scan service")
}
for _, svr := range serviceMap {
go ManageService(svr)
}
// 创建一个信号接收通道
sigCh := make(chan os.Signal, 1)
// 监听指定的系统信号
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
}
func ScanServcie() error {
err := filepath.Walk(*configDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
logrus.WithError(err).Warnf("failed to walk through")
}
if info.IsDir() {
return err
}
if !strings.HasSuffix(info.Name(), "service.yaml") {
return nil
}
fContent, err := os.ReadFile(path)
if err != nil {
logrus.WithField("path", path).
WithError(err).Warnf("failed to open path")
return err
}
svr := Service{}
err = yaml.Unmarshal(fContent, &svr)
if err != nil {
logrus.WithField("path", path).
WithField("content", string(fContent)).
WithError(err).Error("failed to decode config file")
return err
}
svr.Status = false
_, ok := serviceMap[svr.Name]
if ok {
logrus.Fatalf("service [%s] is duplicated", svr.Name)
}
if svr.RestartSec == 0 {
svr.RestartSec = 5
}
serviceMap[svr.Name] = &svr
return nil
})
if err != nil {
logrus.WithError(err).Errorf("failed to scan config file")
return err
}
return nil
}
func ManageService(svr *Service) {
for {
for _, dep := range svr.Depends {
for {
svr, ok := serviceMap[dep]
if !ok {
logrus.WithField("dep", dep).Errorf("depend is not found")
} else {
if svr.Status {
break
}
}
time.Sleep(1 * time.Second)
}
}
cmd := exec.Command(svr.ExecStart[0], svr.ExecStart[1:]...)
cmd.Env = os.Environ()
if svr.Cwd != "" {
cmd.Dir = svr.Cwd
}
cmd.Stdout = NewServiceWrapWriter(svr.Name, os.Stdout)
cmd.Stderr = NewServiceWrapWriter(svr.Name, os.Stdout)
for k, v := range svr.Env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
timer := time.NewTimer(1 * time.Second)
go func() {
_, isOpen := <-timer.C
if isOpen {
svr.Status = true
} else {
svr.Status = false
}
}()
err := cmd.Run()
if err != nil {
logrus.WithField("command", cmd.String()).
WithError(err).
Warnf("failed to run command")
}
timer.Stop()
time.Sleep(time.Duration(svr.RestartSec) * time.Second)
}
}
type ServcieWrapWriter struct {
name string
f *os.File
}
func NewServiceWrapWriter(name string, f *os.File) *ServcieWrapWriter {
return &ServcieWrapWriter{
name: name,
f: f,
}
}
func (w ServcieWrapWriter) Write(p []byte) (n int, err error) {
prefix := []byte(fmt.Sprintf("[%s]", w.name))
p = append(prefix, p...)
n, err = w.f.Write(p)
if n > len(prefix) {
n -= len(prefix)
} else {
n = 0
}
return n, err
}