This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioning.go
291 lines (247 loc) · 6.97 KB
/
provisioning.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
// Package provisioning holds utility functions and structures used by other modules in this project.
package provisioning
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"sync/atomic"
"time"
errw "github.com/pkg/errors"
pb "go.viam.com/api/provisioning/v1"
)
var (
// versions embedded at build time.
Version = ""
GitRevision = ""
)
var DefaultConf = Config{
Manufacturer: "viam",
Model: "custom",
FragmentID: "",
HotspotPrefix: "viam-setup",
HotspotPassword: "viamsetup",
DisableDNSRedirect: false,
RoamingMode: false,
OfflineTimeout: Timeout(time.Minute * 2),
UserTimeout: Timeout(time.Minute * 5),
FallbackTimeout: Timeout(time.Minute * 10),
Networks: []NetworkConfig{},
}
// GetVersion returns the version embedded at build time.
func GetVersion() string {
if Version == "" {
return "custom"
}
return Version
}
// GetRevision returns the git revision embedded at build time.
func GetRevision() string {
if GitRevision == "" {
return "unknown"
}
return GitRevision
}
type NetworkInfo struct {
Type string
SSID string
Security string
Signal int32
Connected bool
LastError string
}
func NetworkInfoToProto(net *NetworkInfo) *pb.NetworkInfo {
return &pb.NetworkInfo{
Type: net.Type,
Ssid: net.SSID,
Security: net.Security,
Signal: net.Signal,
Connected: net.Connected,
LastError: net.LastError,
}
}
func NetworkInfoFromProto(buf *pb.NetworkInfo) *NetworkInfo {
return &NetworkInfo{
Type: buf.GetType(),
SSID: buf.GetSsid(),
Security: buf.GetSecurity(),
Signal: buf.GetSignal(),
Connected: buf.GetConnected(),
LastError: buf.GetLastError(),
}
}
type NetworkConfig struct {
Type string `json:"type"`
SSID string `json:"ssid"`
PSK string `json:"psk"`
Priority int32 `json:"priority"`
}
// DeviceConfig represents the minimal needed for /etc/viam.json.
type DeviceConfig struct {
Cloud *CloudConfig `json:"cloud"`
}
type CloudConfig struct {
AppAddress string `json:"app_address"`
ID string `json:"id"`
Secret string `json:"secret"`
}
func WriteDeviceConfig(file string, input UserInput) error {
if input.RawConfig != "" {
return os.WriteFile(file, []byte(input.RawConfig), 0o600)
}
cfg := &DeviceConfig{
Cloud: &CloudConfig{
AppAddress: input.AppAddr,
ID: input.PartID,
Secret: input.Secret,
},
}
jsonBytes, err := json.Marshal(cfg)
if err != nil {
return err
}
return os.WriteFile(file, jsonBytes, 0o600)
}
type UserInput struct {
Updated time.Time
// network
SSID string
PSK string
// device credentials
PartID string
Secret string
AppAddr string
// raw /etc/viam.json contents
RawConfig string
}
func LoadConfig(defaultConf Config, path string) (*Config, error) {
minTimeout := Timeout(time.Second * 15)
//nolint:gosec
jsonBytes, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return &defaultConf, nil
}
return &defaultConf, err
}
conf := defaultConf
if err = json.Unmarshal(jsonBytes, &conf); err != nil {
return &defaultConf, err
}
if conf.Manufacturer == "" || conf.Model == "" || conf.HotspotPrefix == "" || conf.HotspotPassword == "" {
return &defaultConf, errw.New("values in configs/attributes should not be empty, please omit empty fields entirely")
}
var haveBadTimeout bool
if conf.OfflineTimeout < minTimeout {
conf.OfflineTimeout = defaultConf.OfflineTimeout
haveBadTimeout = true
}
if conf.UserTimeout < minTimeout {
conf.UserTimeout = defaultConf.UserTimeout
haveBadTimeout = true
}
if conf.FallbackTimeout < minTimeout {
conf.FallbackTimeout = defaultConf.FallbackTimeout
haveBadTimeout = true
}
if haveBadTimeout {
return &conf, errw.Errorf("timeout values cannot be less than %s", time.Duration(minTimeout))
}
return &conf, nil
}
// Config represents the json configurations parsed from either agent-provisioning.json OR passed from the "attributes" in the cloud config.
type Config struct {
// Things typically set in agent-provisioning.json
Manufacturer string `json:"manufacturer"`
Model string `json:"model"`
FragmentID string `json:"fragment_id"`
// The prefix to prepend to the hotspot name.
HotspotPrefix string `json:"hotspot_prefix"`
// Password required to connect to the hotspot.
HotspotPassword string `json:"hotspot_password"`
// If true, mobile (phone) users connecting to the hotspot won't be automatically redirected to the web portal.
DisableDNSRedirect bool `json:"disable_dns_redirect"`
// How long without a connection before starting provisioning (hotspot) mode.
OfflineTimeout Timeout `json:"offline_timeout"`
// How long since the last user interaction (via GRPC/app or web portal) before the state machine can resume.
UserTimeout Timeout `json:"user_timeout"`
// If not "online", always drop out of hotspot mode and retry everything after this time limit.
FallbackTimeout Timeout `json:"fallback_timeout"`
// When true, will try all known networks looking for internet (global) connectivity.
// Otherwise, will only try the primary wifi network and consider that sufficient if connected (regardless of global connectivity.)
RoamingMode bool `json:"roaming_mode"`
// Additional networks to add/configure. Only useful in RoamingMode.
Networks []NetworkConfig `json:"networks"`
}
// Timeout allows parsing golang-style durations (1h20m30s) OR seconds-as-float from/to json.
type Timeout time.Duration
func (t Timeout) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(t).String())
}
func (t *Timeout) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
*t = Timeout(value * float64(time.Second))
return nil
case string:
tmp, err := time.ParseDuration(value)
if err != nil {
return err
}
*t = Timeout(tmp)
return nil
default:
return errw.Errorf("invalid duration: %+v", v)
}
}
type ContextKey string
const HCReqKey = ContextKey("healthcheck")
// HealthySleep allows a process to sleep while stil responding to context cancellation AND healthchecks. Returns false if cancelled.
func HealthySleep(ctx context.Context, timeout time.Duration) bool {
hc, ok := ctx.Value(HCReqKey).(*atomic.Bool)
if !ok {
// this should never happen, so avoiding having to pass a logger by just printing
//nolint:forbidigo
fmt.Println("context passed to HealthySleep without healthcheck value")
}
stop := &atomic.Bool{}
defer stop.Store(true)
go func() {
for {
if hc.Swap(false) {
//nolint:forbidigo
fmt.Println("HEALTHY")
}
if stop.Load() {
return
}
select {
case <-ctx.Done():
return
case <-time.After(time.Second):
}
}
}()
for {
select {
case <-ctx.Done():
return false
case <-time.After(timeout):
return true
}
}
}
func Sleep(ctx context.Context, timeout time.Duration) bool {
select {
case <-ctx.Done():
return false
case <-time.After(timeout):
return true
}
}