-
Notifications
You must be signed in to change notification settings - Fork 19
/
mapping.go
211 lines (182 loc) · 4.18 KB
/
mapping.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package native
import (
"io/ioutil"
"os"
"path"
"sync"
"github.com/fsnotify/fsnotify"
"gopkg.in/yaml.v2"
"github.com/baetyl/baetyl-go/v2/errors"
"github.com/baetyl/baetyl-go/v2/log"
"github.com/baetyl/baetyl-go/v2/utils"
)
const (
ServiceMappingFile = "var/lib/baetyl/run/services.yml"
)
type ServiceMapping struct {
services map[string]*serviceMappingInfo
tomb utils.Tomb
error error
sync.RWMutex
}
type serviceMappingInfo struct {
PortInfo portsInfo `yaml:",inline"`
}
type portsInfo struct {
Ports []int `yaml:"ports,omitempty"`
offset int
}
func (i *portsInfo) Next() (int, error) {
if len(i.Ports) == 0 {
return 0, errors.New("ports of service are empty in services mapping file")
}
port := i.Ports[i.offset]
i.offset++
i.offset = i.offset % len(i.Ports)
return port, nil
}
func NewServiceMapping() (*ServiceMapping, error) {
m := &ServiceMapping{
services: make(map[string]*serviceMappingInfo),
}
if !utils.FileExists(ServiceMappingFile) {
err := m.save()
if err != nil {
return nil, err
}
}
return m, nil
}
func (s *ServiceMapping) load() error {
if !utils.FileExists(ServiceMappingFile) {
return errors.Errorf("services mapping file (%s) doesn't exist", ServiceMappingFile)
}
data, err := ioutil.ReadFile(ServiceMappingFile)
if err != nil {
return errors.Trace(err)
}
err = yaml.Unmarshal(data, &s.services)
if err != nil {
return errors.Trace(err)
}
return nil
}
func (s *ServiceMapping) save() error {
data, err := yaml.Marshal(&s.services)
if err != nil {
return errors.Trace(err)
}
if !utils.PathExists(path.Dir(ServiceMappingFile)) {
err = os.MkdirAll(path.Dir(ServiceMappingFile), 0755)
if err != nil {
return errors.Trace(err)
}
}
return ioutil.WriteFile(ServiceMappingFile, data, 0755)
}
func (s *ServiceMapping) SetServicePorts(serviceName string, ports []int) error {
s.Lock()
defer s.Unlock()
s.services[serviceName] = &serviceMappingInfo{
PortInfo: portsInfo{
Ports: ports,
},
}
err := s.save()
if err != nil {
return errors.Trace(err)
}
return nil
}
func (s *ServiceMapping) DeleteServicePorts(serviceName string) error {
s.Lock()
defer s.Unlock()
if _, ok := s.services[serviceName]; !ok {
return nil
}
delete(s.services, serviceName)
err := s.save()
if err != nil {
return errors.Trace(err)
}
return nil
}
func (s *ServiceMapping) GetServiceNextPort(serviceName string) (int, error) {
s.Lock()
defer s.Unlock()
if s.error != nil {
return 0, s.error
}
serviceInfo, ok := s.services[serviceName]
if !ok {
return 0, errors.New("no such service in services mapping file")
}
if len(serviceInfo.PortInfo.Ports) == 0 {
return 0, errors.New("no ports info in services mapping file")
}
port, err := s.services[serviceName].PortInfo.Next()
if err != nil {
return 0, err
}
return port, nil
}
func (s *ServiceMapping) WatchFile(logger *log.Logger) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return errors.Trace(err)
}
s.tomb.Go(func() error {
defer func() {
watcher.Close()
logger.Info("stop to watch services mapping file", log.Any("file", ServiceMappingFile))
}()
logger.Info("start to watch services mapping file", log.Any("file", ServiceMappingFile))
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return nil
}
logger.Debug("received a file event", log.Any("eventName", event.Name), log.Any("eventOp", event.Op))
if event.Op&fsnotify.Write != fsnotify.Write {
continue
}
logger.Debug("load services mapping file again")
s.Lock()
err := s.load()
if err != nil {
s.error = err
logger.Warn("load services mapping file failed", log.Error(err))
}
s.Unlock()
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
// TODO: check return or continue under this case
logger.Warn(err.Error())
s.Lock()
s.error = err
s.Unlock()
return nil
case <-s.tomb.Dying():
return nil
}
}
})
err = watcher.Add(ServiceMappingFile)
if err != nil {
return errors.Trace(err)
}
s.Lock()
err = s.load()
s.Unlock()
if err != nil {
return errors.Trace(err)
}
return nil
}
func (s *ServiceMapping) Close() {
s.tomb.Kill(nil)
s.tomb.Wait()
}