-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.go
95 lines (86 loc) · 2.14 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
package main
import (
"bytes"
"context"
"crypto/sha1"
"fmt"
"io/ioutil"
"os"
"syscall"
"time"
"github.com/gliderlabs/ssh"
toml "github.com/pelletier/go-toml/v2"
"go.science.ru.nl/log"
)
// Config holds the gitopper config file. It's is updated every so often to pick up new changes.
type Config struct {
Global `toml:"global"`
Services []*Service
}
type Global struct {
*Service
Keys []*Key
}
type Key struct {
Path string
RO bool `toml:"ro"` // treat key as ro, and disallow "write" commands
ssh.PublicKey `toml:"-"`
}
func parseConfig(doc []byte) (c Config, err error) {
t := toml.NewDecoder(bytes.NewReader(doc))
t.DisallowUnknownFields()
err = t.Decode(&c)
return c, err
}
// Valid checks the config in c and returns nil of all mandatory fields have been set.
func (c Config) Valid() error {
if len(c.Global.Keys) == 0 {
return fmt.Errorf("at least one public key should be specified")
}
for i, serv := range c.Services {
s := serv.merge(c.Global)
if s.Machine == "" {
return fmt.Errorf("machine #%d, has empty machine name", i)
}
if s.Upstream == "" {
return fmt.Errorf("machine #%d %q, has empty upstream", i, s.Machine)
}
if s.Mount == "" {
return fmt.Errorf("machine #%d %q, has empty mount", i, s.Machine)
}
if s.Service == "" {
return fmt.Errorf("machine #%d %q, has empty service", i, s.Service)
}
}
return nil
}
// trackConfig will sha1 sum the contents of file and if it differs from previous runs, will SIGHUP ourselves so we
// exist with status code 2, which in turn will systemd restart us again.
func trackConfig(ctx context.Context, file string, done chan<- os.Signal) {
hash := ""
for {
select {
case <-time.After(30 * time.Second):
case <-ctx.Done():
return
}
doc, err := ioutil.ReadFile(file)
if err != nil {
log.Warningf("Failed to read config %q: %s", file, err)
continue
}
sha := sha1.New()
sha.Write(doc)
hash1 := string(sha.Sum(nil))
if hash == "" {
hash = hash1
continue
}
if hash1 != hash {
log.Info("Config change detected, sending SIGHUP")
// haste our exit (can this block?)
done <- syscall.SIGHUP
return
}
}
}