/
device_aggregate.go
333 lines (290 loc) · 9.58 KB
/
device_aggregate.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
Copyright (c) 2022-2023 NTT Communications Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package core
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"path/filepath"
"sort"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/nttcom/kuesta/internal/file"
"github.com/nttcom/kuesta/internal/gogit"
"github.com/nttcom/kuesta/internal/logger"
"github.com/nttcom/kuesta/internal/util"
"github.com/nttcom/kuesta/internal/validator"
"github.com/nttcom/kuesta/pkg/credentials"
"github.com/nttcom/kuesta/pkg/kuesta"
"github.com/pkg/errors"
)
var UpdateCheckDuration = 5 * time.Second // TODO parameterize
type DeviceAggregateCfg struct {
RootCfg
Addr string
NoTLS bool
Insecure bool
TLSCrtPath string
TLSKeyPath string
TLSCACrtPath string
}
func (c *DeviceAggregateCfg) TLSServerConfig() *credentials.TLSServerConfig {
cfg := &credentials.TLSServerConfig{
TLSConfigBase: credentials.TLSConfigBase{
NoTLS: c.NoTLS,
CrtPath: c.TLSCrtPath,
KeyPath: c.TLSKeyPath,
CACrtPath: c.TLSCACrtPath,
},
}
if c.Insecure {
cfg.ClientAuth = tls.VerifyClientCertIfGiven
} else {
cfg.ClientAuth = tls.RequireAndVerifyClientCert
}
return cfg
}
// Validate validates exposed fields according to the `validate` tag.
func (c *DeviceAggregateCfg) Validate() error {
if !c.NoTLS {
if c.TLSKeyPath == "" || c.TLSCrtPath == "" {
return fmt.Errorf("tls-key and tls-crt options must be set to use TLS")
}
}
return validator.Validate(c)
}
// Mask returns the copy whose sensitive data are masked.
func (c *DeviceAggregateCfg) Mask() *DeviceAggregateCfg {
cc := *c
cc.RootCfg = *c.RootCfg.Mask()
return &cc
}
// RunDeviceAggregate runs the main process of the `device aggregate` command.
func RunDeviceAggregate(ctx context.Context, cfg *DeviceAggregateCfg) error {
l := logger.FromContext(ctx)
l.Debugw("device aggregate called", "config", cfg.Mask())
ctx, cancel := context.WithCancel(ctx)
defer cancel()
s := NewDeviceAggregateServer(cfg)
s.Run(ctx)
l.Infof("starting simple api server on %s", cfg.Addr)
http.HandleFunc("/commit", s.HandleFunc)
if cfg.NoTLS {
hs := &http.Server{
Addr: cfg.Addr,
ReadHeaderTimeout: 5 * time.Second,
}
if err := hs.ListenAndServe(); err != nil {
return errors.WithStack(fmt.Errorf("run server: %w", err))
}
return nil
}
credCfg := cfg.TLSServerConfig()
// NOTE server certificate is set inside ListenAndServeTLS
tlsCfg, err := credentials.NewTLSConfig(credCfg.VerifyClient())
if err != nil {
return fmt.Errorf("new tls config: %w", err)
}
hs := &http.Server{
Addr: cfg.Addr,
ReadHeaderTimeout: 5 * time.Second,
TLSConfig: tlsCfg,
}
if err := hs.ListenAndServeTLS(cfg.TLSCrtPath, cfg.TLSKeyPath); err != nil {
return errors.WithStack(fmt.Errorf("run server: %w", err))
}
return nil
}
// DeviceAggregateServer runs saver loop and committer loop along with serving commit API to persist device config to git.
// Device config are written locally and added to git just after commit API call. Updated configs are aggregated
// and git-pushed as batch commit periodically.
type DeviceAggregateServer struct {
ch chan *SaveConfigRequest
cfg *DeviceAggregateCfg
}
// NewDeviceAggregateServer creates new DeviceAggregateServer.
func NewDeviceAggregateServer(cfg *DeviceAggregateCfg) *DeviceAggregateServer {
return &DeviceAggregateServer{
ch: make(chan *SaveConfigRequest),
cfg: cfg,
}
}
// HandleFunc handles API call to persist actual device config.
func (s *DeviceAggregateServer) HandleFunc(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
ctx := r.Context()
if err, code := s.add(ctx, r.Body); err != nil {
http.Error(w, err.Error(), code)
}
defer r.Body.Close()
return
default:
http.Error(w, `{"status": "only POST allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *DeviceAggregateServer) add(ctx context.Context, r io.Reader) (error, int) {
req, err := DecodeSaveConfigRequest(r)
if err != nil {
return err, 400
}
s.ch <- req
return nil, 200
}
func (s *DeviceAggregateServer) Run(ctx context.Context) {
s.runSaver(ctx)
s.runCommitter(ctx)
}
func (s *DeviceAggregateServer) runSaver(ctx context.Context) {
l := logger.FromContext(ctx)
go func() {
for {
select {
case r := <-s.ch:
l.Infof("update received: device=%s", r.Device)
if err := s.SaveConfig(ctx, r); err != nil {
logger.ErrorWithStack(ctx, err, "save actual device config", "device", r.Device)
}
case <-ctx.Done():
return
}
}
}()
l.Info("saver loop started")
}
func (s *DeviceAggregateServer) runCommitter(ctx context.Context) {
util.SetInterval(ctx, func() {
if err := s.GitPushDeviceConfig(ctx); err != nil {
logger.ErrorWithStack(ctx, err, "push sync branch")
}
}, UpdateCheckDuration)
}
// SaveConfig writes device config contained in supplied SaveConfigRequest.
func (s *DeviceAggregateServer) SaveConfig(ctx context.Context, r *SaveConfigRequest) error {
dp := kuesta.DevicePath{RootDir: s.cfg.StatusRootPath, Device: r.Device}
if err := file.WriteFileWithMkdir(dp.DeviceActualConfigPath(kuesta.IncludeRoot), []byte(*r.Config)); err != nil {
return fmt.Errorf("write actual device config: %w", err)
}
return nil
}
// GitPushDeviceConfig runs git-commit all unstaged device config updates as batch commit then git-push to remote origin.
func (s *DeviceAggregateServer) GitPushDeviceConfig(ctx context.Context) error {
l := logger.FromContext(ctx)
g, err := gogit.NewGit(s.cfg.StatusGitOptions().ShouldCloneIfNotExist())
if err != nil {
return fmt.Errorf("init git: %w", err)
}
if err := g.Pull(); err != nil {
return fmt.Errorf("git pull: %w", err)
}
w, err := g.Checkout()
if err != nil {
return fmt.Errorf("git checkout to trunk: %w", err)
}
if err := g.RemoveGoneBranches(); err != nil {
return fmt.Errorf("remove gone branches: %w", err)
}
_, err = w.Add("devices")
if err != nil {
return fmt.Errorf("git add devices: %w", err)
}
stmap, err := w.Status()
if err != nil {
return fmt.Errorf("get status map: %w", err)
}
// TODO check only staged files
if len(stmap) == 0 {
l.Info("skipped: there are no update")
return nil
}
if err := CheckGitIsStagedOrUnmodified(stmap); err != nil {
return fmt.Errorf("check files are either staged or unmodified: %w", err)
}
commitMsg := MakeSyncCommitMessage(stmap)
if _, err := g.Commit(commitMsg); err != nil {
return fmt.Errorf("git commit: %w", err)
}
l.Infof("committed: %s\n", commitMsg)
if err := g.Push(); err != nil {
return fmt.Errorf("git push: %w", err)
}
return nil
}
type SaveConfigRequest struct {
Device string `json:"device" validate:"required"`
Config *string `json:"config" validate:"required"`
}
func (r *SaveConfigRequest) Validate() error {
return validator.Validate(r)
}
// DecodeSaveConfigRequest decodes supplied payload to SaveConfigRequest.
func DecodeSaveConfigRequest(r io.Reader) (*SaveConfigRequest, error) {
var req SaveConfigRequest
if err := json.NewDecoder(r).Decode(&req); err != nil {
return nil, fmt.Errorf("failed to decode request: %w", err)
}
return &req, req.Validate()
}
// MakeSyncCommitMessage returns the commit message that shows the device actual config updates.
func MakeSyncCommitMessage(stmap git.Status) string {
var devicesAdded []string
var devicesModified []string
var devicesDeleted []string
for path, st := range stmap {
dir, file := filepath.Split(path)
dirElem := strings.Split(dir, string(filepath.Separator))
if dirElem[0] == "devices" && file == "actual_config.cue" {
deviceName := dirElem[1]
switch st.Staging {
case git.Added:
devicesAdded = append(devicesAdded, deviceName)
case git.Modified:
devicesModified = append(devicesModified, deviceName)
case git.Deleted:
devicesDeleted = append(devicesDeleted, deviceName)
default:
// noop
}
}
}
for _, v := range [][]string{devicesAdded, devicesModified, devicesDeleted} {
sort.Slice(v, func(i, j int) bool { return v[i] < v[j] })
}
var devices []string
devices = append(devices, devicesAdded...)
devices = append(devices, devicesDeleted...)
devices = append(devices, devicesModified...)
title := fmt.Sprintf("Updated: %s", strings.Join(devices, " "))
var bodylines []string
bodylines = append(bodylines, "", "Devices:")
for _, d := range devicesAdded {
bodylines = append(bodylines, fmt.Sprintf("\tadded: %s", d))
}
for _, d := range devicesDeleted {
bodylines = append(bodylines, fmt.Sprintf("\tdeleted: %s", d))
}
for _, d := range devicesModified {
bodylines = append(bodylines, fmt.Sprintf("\tmodified: %s", d))
}
return title + "\n" + strings.Join(bodylines, "\n")
}