-
Notifications
You must be signed in to change notification settings - Fork 17
/
common_config.go
304 lines (273 loc) · 7.95 KB
/
common_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
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
package subcommands
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"regexp"
"strings"
"time"
"github.com/fatih/color"
toml "github.com/pelletier/go-toml"
"github.com/sirupsen/logrus"
"github.com/foundriesio/fioctl/client"
)
// Aktualizr puts all config files into a single lexographically sorted map.
// We have to make sure this file is parsed *after* sota.toml.
const (
FIO_TOML_NAME = "z-50-fioctl.toml"
FIO_COMPOSE_APPS_DIR = "/var/sota/compose-apps"
FIO_TOML_ONCHANGED = "/usr/share/fioconfig/handlers/aktualizr-toml-update"
)
type SetConfigOptions struct {
Reason string
FileArgs []string
IsRawFile bool
SetFunc func(client.ConfigCreateRequest) error
EncryptFunc func(string) string
}
func SetConfig(opts *SetConfigOptions) {
cfg := client.ConfigCreateRequest{Reason: opts.Reason}
if opts.IsRawFile {
if len(opts.FileArgs) != 1 {
DieNotNil(fmt.Errorf("Raw file only accepts one file argument"))
}
ReadConfig(opts.FileArgs[0], &cfg)
} else {
for _, keyval := range opts.FileArgs {
parts := strings.SplitN(keyval, "=", 2)
if len(parts) != 2 {
DieNotNil(fmt.Errorf("Invalid file=content argument: %s", keyval))
}
// support for filename=filecontent format
content := parts[1]
if len(content) > 0 && content[0] == '=' {
// support for filename==/file/path.ext format
data, err := os.ReadFile(content[1:])
DieNotNil(err, "Unable to read config file:")
content = string(data)
}
cfg.Files = append(cfg.Files, client.ConfigFile{Name: parts[0], Value: content})
}
}
if opts.EncryptFunc != nil {
for i := range cfg.Files {
file := &cfg.Files[i]
if !file.Unencrypted {
file.Value = opts.EncryptFunc(file.Value)
}
}
}
DieNotNil(opts.SetFunc(cfg))
}
type LogConfigsOptions struct {
Limit int
ShowAppliedAt bool
ListFunc func() (*client.DeviceConfigList, error)
ListContFunc func(string) (*client.DeviceConfigList, error)
}
func LogConfigs(opts *LogConfigsOptions) {
var dcl *client.DeviceConfigList
listLimit := opts.Limit
for {
var err error
if dcl == nil {
dcl, err = opts.ListFunc()
} else {
if dcl.Next != nil {
dcl, err = opts.ListContFunc(*dcl.Next)
} else {
return
}
}
DieNotNil(err)
for _, cfg := range dcl.Configs {
PrintConfig(&cfg, opts.ShowAppliedAt, true, "")
if listLimit -= 1; listLimit == 0 {
return
} else {
fmt.Println("")
}
}
}
}
func ReadConfig(configFile string, cfg *client.ConfigCreateRequest) {
var content []byte
var err error
if configFile == "-" {
logrus.Debug("Reading config from STDIN")
content, err = io.ReadAll(os.Stdin)
} else {
content, err = os.ReadFile(configFile)
}
DieNotNil(err, "Unable to read config file:")
DieNotNil(json.Unmarshal(content, cfg), "Unable to parse config file:")
}
func PrintConfig(cfg *client.DeviceConfig, showAppliedAt, highlightFirstLine bool, indent string) {
printf := func(format string, a ...interface{}) {
fmt.Printf(indent+format, a...)
}
if highlightFirstLine {
firstLine := color.New(color.FgYellow)
firstLine.Printf(indent+"Created At: %s\n", cfg.CreatedAt)
} else {
printf("Created At: %s\n", cfg.CreatedAt)
}
if showAppliedAt {
printf("Applied At: %s\n", cfg.AppliedAt)
}
printf("Change Reason: %s\n", cfg.Reason)
printf("Files:\n")
for _, f := range cfg.Files {
if len(f.OnChanged) == 0 {
printf("\t%s\n", f.Name)
} else {
printf("\t%s - %v\n", f.Name, f.OnChanged)
}
if f.Unencrypted {
for _, line := range strings.Split(f.Value, "\n") {
printf("\t | %s\n", line)
}
}
}
}
type RotateCertOptions struct {
Reason string
EstServer string
PkeyIds []string
CertIds []string
}
func (o RotateCertOptions) AsConfig() client.ConfigCreateRequest {
b := new(bytes.Buffer)
fmt.Fprintf(b, "ESTSERVER=%s\n", o.EstServer)
fmt.Fprintf(b, "PKEYIDS=%s\n", strings.Join(o.PkeyIds, ","))
fmt.Fprintf(b, "CERTIDS=%s\n", strings.Join(o.CertIds, ","))
fmt.Fprintf(b, "ROTATIONID=certs-%d\n", time.Now().Unix())
return client.ConfigCreateRequest{
Reason: o.Reason,
Files: []client.ConfigFile{
{
Name: "fio-rotate-certs",
Value: b.String(),
Unencrypted: true,
OnChanged: []string{"/usr/share/fioconfig/handlers/renew-client-cert"},
},
},
}
}
type SetUpdatesConfigOptions struct {
UpdateTag string
UpdateApps string
IsDryRun bool
IsForced bool
Device *client.Device
ListFunc func() (*client.DeviceConfigList, error)
SetFunc func(client.ConfigCreateRequest, bool) error
}
func SetUpdatesConfig(opts *SetUpdatesConfigOptions, reportedTag string, reportedApps []string) {
DieNotNil(validateUpdateArgs(opts))
dcl, err := opts.ListFunc()
if !opts.IsForced {
DieNotNil(err, "Failed to fetch existing config changelog (override with --force):")
}
sota, err := loadSotaConfig(dcl)
if !opts.IsForced {
DieNotNil(err, "Invalid FIO toml file (override with --force):")
}
if opts.UpdateApps == "" && opts.UpdateTag == "" {
if opts.Device != nil {
fmt.Println("= Reporting to server with")
fmt.Println(" Tag: ", opts.Device.Tag)
fmt.Println(" Apps: ", strings.Join(opts.Device.DockerApps, ","))
fmt.Println("")
}
fmt.Println("= Configured overrides")
fmt.Println(sota)
return
}
configuredApps := sota.GetDefault("pacman.docker_apps", "").(string)
configuredTag := sota.GetDefault("pacman.tags", "").(string)
if len(configuredTag) == 0 && len(reportedTag) > 0 {
configuredTag = reportedTag
}
if len(configuredApps) == 0 && reportedApps != nil {
configuredApps = strings.Join(reportedApps, ",")
}
changed := false
if opts.UpdateApps != "" && configuredApps != opts.UpdateApps {
if strings.TrimSpace(opts.UpdateApps) == "," {
opts.UpdateApps = ""
}
fmt.Printf("Changing apps from: [%s] -> [%s]\n", configuredApps, opts.UpdateApps)
sota.Set("pacman.docker_apps", opts.UpdateApps)
sota.Set("pacman.compose_apps", opts.UpdateApps)
changed = true
}
if opts.UpdateTag != "" && configuredTag != opts.UpdateTag {
if strings.TrimSpace(opts.UpdateTag) == "," {
opts.UpdateTag = ""
}
fmt.Printf("Changing tag from: %s -> %s\n", configuredTag, opts.UpdateTag)
sota.Set("pacman.tags", opts.UpdateTag)
changed = true
}
if !changed {
fmt.Println("No changes found. Device is already configured with the specified options.")
os.Exit(0)
}
newToml, err := sota.ToTomlString()
DieNotNil(err, "Unable to encode toml:")
cfg := client.ConfigCreateRequest{
Reason: "Override aktualizr-lite update configuration ",
Files: []client.ConfigFile{
{
Name: FIO_TOML_NAME,
Unencrypted: true,
OnChanged: []string{"/usr/share/fioconfig/handlers/aktualizr-toml-update"},
Value: newToml,
},
},
}
if opts.IsDryRun {
fmt.Println(newToml)
} else {
DieNotNil(opts.SetFunc(cfg, opts.IsForced))
}
}
func loadSotaConfig(dcl *client.DeviceConfigList) (sota *toml.Tree, err error) {
found := false
if dcl != nil && len(dcl.Configs) > 0 {
for _, cfgFile := range dcl.Configs[0].Files {
if cfgFile.Name == FIO_TOML_NAME {
sota, err = toml.Load(cfgFile.Value)
if err != nil {
err = fmt.Errorf("Unable to decode toml: %w\n- TOML is: %s", err, cfgFile.Value)
}
found = true
break
}
}
}
if !found {
logrus.Debugf("Not found a FIO toml in the latest config")
}
// In case if FIO TOML file is missing or an error - return an empty one.
// Let a caller decide what to do in case of an error.
if !found || err != nil {
sota, _ = toml.Load("[pacman]")
}
return
}
func validateUpdateArgs(opts *SetUpdatesConfigOptions) error {
// Validate the inputs: Must be alphanumeric, a dash, underscore, or comma
pattern := `^[a-zA-Z0-9-_,]+$`
re := regexp.MustCompile(pattern)
if len(opts.UpdateApps) > 0 && !re.MatchString(opts.UpdateApps) {
return fmt.Errorf("Invalid value for apps: %s\nMust be %s", opts.UpdateApps, pattern)
}
if len(opts.UpdateTag) > 0 && !re.MatchString(opts.UpdateTag) {
return fmt.Errorf("Invalid value for tag: %s\nMust be %s", opts.UpdateTag, pattern)
}
return nil
}