This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ensure.go
218 lines (181 loc) · 5.59 KB
/
ensure.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
package nodeagent
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/caos/orbos/internal/operator/common"
"github.com/caos/orbos/mntr"
)
type FirewallEnsurer interface {
Query(desired common.Firewall) (current []*common.Allowed, ensure func() error, err error)
}
type FirewallEnsurerFunc func(desired common.Firewall) (current []*common.Allowed, ensure func() error, err error)
func (f FirewallEnsurerFunc) Query(desired common.Firewall) (current []*common.Allowed, ensure func() error, err error) {
return f(desired)
}
type Dependency struct {
Installer Installer
Desired common.Package
Current common.Package
}
type Converter interface {
ToDependencies(common.Software) []*Dependency
ToSoftware([]*Dependency, func(Dependency) common.Package) common.Software
}
type Installer interface {
Current() (common.Package, error)
Ensure(uninstall common.Package, install common.Package) error
Equals(other Installer) bool
Is(other Installer) bool
fmt.Stringer
}
func prepareQuery(monitor mntr.Monitor, commit string, firewallEnsurer FirewallEnsurer, conv Converter) func(common.NodeAgentSpec, *common.NodeAgentCurrent) (func() error, error) {
if err := os.MkdirAll("/var/orbiter", 0700); err != nil {
panic(err)
}
return func(desired common.NodeAgentSpec, curr *common.NodeAgentCurrent) (func() error, error) {
curr.Commit = commit
curr.NodeIsReady = isReady()
defer persistReadyness(curr.NodeIsReady)
who, err := exec.Command("who", "-b").CombinedOutput()
if err != nil {
return noop, err
}
dateTime := strings.Fields(string(who))[2:]
str := strings.Join(dateTime, " ") + ":00"
t, err := time.Parse("2006-01-02 15:04:05", str)
if err != nil {
return noop, err
}
curr.Booted = t
if desired.RebootRequired.After(curr.Booted) {
curr.NodeIsReady = false
if !desired.ChangesAllowed {
monitor.Info("Not rebooting as changes are not allowed")
return noop, nil
}
return func() error {
monitor.Info("Rebooting")
if err := exec.Command("sudo", "reboot").Run(); err != nil {
return fmt.Errorf("rebooting system failed: %w", err)
}
return nil
}, nil
}
var ensureFirewall func() error
curr.Open, ensureFirewall, err = firewallEnsurer.Query(*desired.Firewall)
if err != nil {
return noop, err
}
installedSw, err := deriveTraverse(queryFunc(monitor), conv.ToDependencies(*desired.Software))
if err != nil {
return noop, err
}
curr.Software = conv.ToSoftware(installedSw, func(dep Dependency) common.Package {
return dep.Current
})
divergentSw := deriveFilter(divergent, append([]*Dependency(nil), installedSw...))
if len(divergentSw) == 0 && ensureFirewall == nil {
curr.NodeIsReady = true
return noop, nil
}
if curr.NodeIsReady {
curr.NodeIsReady = false
monitor.Changed("Marked node as unready")
return noop, nil
}
return func() error {
if !desired.ChangesAllowed {
monitor.Info("Not ensuring anything as changes are not allowed")
return nil
}
if ensureFirewall != nil {
if err := ensureFirewall(); err != nil {
return err
}
curr.Open = desired.Firewall.Ports()
monitor.Changed("firewall changed")
}
if len(divergentSw) > 0 {
monitor.WithField("software", deriveFmap(func(dependency *Dependency) string {
return dependency.Installer.String()
}, divergentSw)).Info("Ensuring software")
}
ensureDep := ensureFunc(monitor, conv, curr)
_, err := deriveTraverse(ensureDep, divergentSw)
return err
}, nil
}
}
func queryFunc(monitor mntr.Monitor) func(dep *Dependency) (*Dependency, error) {
return func(dep *Dependency) (*Dependency, error) {
version, err := dep.Installer.Current()
if err != nil {
return dep, err
}
dep.Current = version
monitor.Debug("Dependency found")
return dep, nil
}
}
func divergent(dep *Dependency) bool {
return !dep.Desired.Equals(common.Package{}) && !dep.Desired.Equals(dep.Current)
}
func ensureFunc(monitor mntr.Monitor, conv Converter, curr *common.NodeAgentCurrent) func(dep *Dependency) (*Dependency, error) {
return func(dep *Dependency) (*Dependency, error) {
monitor.WithFields(map[string]interface{}{
"dependency": dep.Installer,
"from": dep.Current.Version,
"to": dep.Desired.Version,
}).Info("Ensuring dependency")
if err := dep.Installer.Ensure(dep.Current, dep.Desired); err != nil {
return dep, err
}
curr.Software.Merge(conv.ToSoftware([]*Dependency{dep}, func(dep Dependency) common.Package {
return dep.Desired
}))
monitor.WithFields(map[string]interface{}{
"dependency": dep.Installer,
"from": dep.Current.Version,
"to": dep.Desired.Version,
}).Changed("Dependency ensured")
dep.Current = dep.Desired
return dep, nil
}
}
func merge(inferior []*Dependency, prior []*Dependency) []*Dependency {
keep := deriveFilter(func(item *Dependency) bool {
return !contains(prior, item)
}, append([]*Dependency(nil), inferior...))
return append(keep, prior...)
}
func contains(deps []*Dependency, dep *Dependency) bool {
return deriveAny(func(item *Dependency) bool {
return is(item, dep)
}, deps)
}
func is(this *Dependency, that *Dependency) bool {
return this.Installer.Is(that.Installer)
}
func persistReadyness(ready bool) {
if ready {
if err := ioutil.WriteFile("/var/orbiter/ready", nil, 600); err != nil {
panic(err)
}
return
}
if err := os.RemoveAll("/var/orbiter/ready"); err != nil && !os.IsNotExist(err) {
panic(err)
}
}
func isReady() bool {
_, err := os.Stat("/var/orbiter/ready")
if err != nil && !os.IsNotExist(err) {
panic(err)
}
return err == nil
}
func noop() error { return nil }