-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
188 lines (160 loc) · 4.55 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
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
// go-api - Client for the Cacophony API server.
// Copyright (C) 2018, The Cacophony Project
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package api
import (
"fmt"
"regexp"
"strings"
goconfig "github.com/TheCacophonyProject/go-config"
"github.com/TheCacophonyProject/go-utils/logging"
"github.com/TheCacophonyProject/go-utils/saltutil"
"github.com/spf13/afero"
)
var log = logging.NewLogger("info")
const (
hostnameFile = "/etc/hostname"
hostsFile = "/etc/hosts"
hostsFileFormat = "127.0.0.1\t%s"
)
type Config struct {
ServerURL string
Group string
DeviceName string
DevicePassword string
DeviceID int
configRW *goconfig.Config
}
func NewConfig(configFolder string) (*Config, error) {
configRW, err := goconfig.New(configFolder)
if err != nil {
return nil, err
}
return &Config{
configRW: configRW,
}, nil
}
func (c *Config) Registered() bool {
return c.DeviceID != 0
}
func (c *Config) read() error {
var deviceConf goconfig.Device
if err := c.configRW.Unmarshal(goconfig.DeviceKey, &deviceConf); err != nil {
return err
}
var secretsConf goconfig.Secrets
if err := c.configRW.Unmarshal(goconfig.SecretsKey, &secretsConf); err != nil {
return err
}
c.ServerURL = deviceConf.Server
c.Group = deviceConf.Group
c.DeviceName = deviceConf.Name
c.DevicePassword = secretsConf.DevicePassword
c.DeviceID = deviceConf.ID
if err := c.validate(); err != nil {
return err
}
return nil
}
func (c *Config) write() error {
if err := c.validate(); err != nil {
return err
}
var d goconfig.Device
if err := c.configRW.Unmarshal(goconfig.DeviceKey, &d); err != nil {
return err
}
d.Group = c.Group
d.ID = c.DeviceID
d.Name = c.DeviceName
d.Server = c.ServerURL
if err := c.configRW.Set(goconfig.DeviceKey, &d); err != nil {
return err
}
var s goconfig.Secrets
if err := c.configRW.Unmarshal(goconfig.SecretsKey, &s); err != nil {
return err
}
s.DevicePassword = c.DevicePassword
return c.configRW.Set(goconfig.SecretsKey, &s)
}
func (c *Config) validate() error {
// Not registere is a valid state
valsNotSet := []string{}
if c.ServerURL == "" {
valsNotSet = append(valsNotSet, "server url is not set")
}
if c.DeviceID == 0 {
valsNotSet = append(valsNotSet, "device id is not set")
}
if c.DevicePassword == "" {
valsNotSet = append(valsNotSet, "device password is not set")
}
if c.Group == "" {
valsNotSet = append(valsNotSet, "device group is not set")
}
if c.DeviceName == "" {
valsNotSet = append(valsNotSet, "device name is not set")
}
if len(valsNotSet) == 0 || len(valsNotSet) == 5 {
return nil // Not registered (nothing set in config) is a valid state
}
return fmt.Errorf("error with config. %s", strings.Join(valsNotSet, ", "))
}
func safeName(name string) string {
name = strings.ToLower(name)
reg := regexp.MustCompile("[^a-z0-9]+")
return reg.ReplaceAllString(name, "")
}
func updateHostnameAndSaltGrains(device *CacophonyDevice) error {
// Write the new hostname.
if err := afero.WriteFile(Fs, hostnameFile, []byte(device.hostname()), 0644); err != nil {
return err
}
input, err := afero.ReadFile(Fs, hostsFile)
if err != nil {
return err
}
lines := strings.Split(string(input), "\n")
for i, line := range lines {
fields := strings.Fields(line)
if len(fields) >= 1 && fields[0] == "127.0.0.1" {
lines[i] = fmt.Sprintf(hostsFileFormat, device.hostname())
}
}
output := strings.Join(lines, "\n")
err = afero.WriteFile(Fs, hostsFile, []byte(output), 0644)
if err != nil {
return err
}
// Write the new salt grains.
grains := saltutil.Grains{
DeviceName: device.name,
Group: device.group,
}
// Run the salt-call grains.setvals command in the
// background as it can take about 20 seconds to run.
go func() {
err := setSaltGrains(grains)
if err != nil {
log.Errorf("Failed to set grains: %v", err)
}
}()
return nil
}
// setSaltGrains is a wrapper around the saltutil.SetGrains command. This is done for testing purposes.
var setSaltGrains = func(grains saltutil.Grains) error {
return saltutil.SetGrains(grains, log)
}
var Fs = afero.NewOsFs()