Skip to content

Commit e6789b5

Browse files
authored
Revert "RSDK-12900: use viam-defaults.json only when cloud config is … (#219)
1 parent 400235e commit e6789b5

File tree

4 files changed

+12
-177
lines changed

4 files changed

+12
-177
lines changed

manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ func (m *Manager) GetConfig(ctx context.Context) (time.Duration, error) {
752752
m.logger.Error(errw.Wrapf(err, "processing update data for %s", viamserver.SubsysName))
753753
}
754754

755-
cfgFromCloud, err := utils.StackProtoConfig(resp)
755+
cfgFromCloud, err := utils.StackConfigs(resp)
756756
if err != nil {
757757
m.logger.Warn(errw.Wrap(err, "processing config"))
758758
}

subsystems/networking/networkmanager_linux.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package networking
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"errors"
87
"io"
98
"net/http"
@@ -260,25 +259,10 @@ func (n *Subsystem) startProvisioning(ctx context.Context, inputChan chan<- user
260259
}
261260
n.internalOpMu.Lock()
262261
defer n.internalOpMu.Unlock()
263-
264262
if ctx.Err() != nil {
265263
return ctx.Err()
266264
}
267265

268-
// try to rebase the current config onto the default. since we no longer merge configs once a cloud config is available,
269-
// it may not include provisioning settings that were in the viam-defaults.json.
270-
if provisioningCfg, err := rebaseNetworkConfiguration(n.cfg); err != nil {
271-
n.logger.Infof("merging current networking config over viam-defaults.json failed with err. Continuing with current config.",
272-
"err", err, "current_cfg", n.cfg)
273-
} else if provisioningCfg != n.cfg {
274-
// only log if merge produced diffs: current cfg should be either base+cloud or base+defaults; rebased cfg should be base+defaults+cloud.
275-
// diffs should be entirely the fields set in viam-defaults that are not present in cloud.
276-
n.logger.Infof("starting provisioning. temporarily merging current networking config with 'viam-defaults' (if available)",
277-
"defaults_path", utils.DefaultsFilePath)
278-
// this has either 1) no change if we've never been online 2) will be restored to options from cloud-only once we're online & refetch.
279-
n.cfg = provisioningCfg
280-
}
281-
282266
n.portalData.resetInputData(inputChan)
283267
hotspotErr := n.startProvisioningHotspot(ctx)
284268
if hotspotErr != nil {
@@ -297,28 +281,6 @@ func (n *Subsystem) startProvisioning(ctx context.Context, inputChan chan<- user
297281
return errors.Join(hotspotErr, bluetoothErr)
298282
}
299283

300-
// rebaseNetworkConfiguration reapplies a NetworkConfiguration over the default configuration.
301-
func rebaseNetworkConfiguration(nCfg utils.NetworkConfiguration) (utils.NetworkConfiguration, error) {
302-
asJSON, err := json.Marshal(nCfg)
303-
if err != nil {
304-
return nCfg, err
305-
}
306-
307-
// get default cfg. Hardcoded values + viam_defaults.json (if available - does not err if file does not exist).
308-
newCfg, err := utils.StackOfflineConfig()
309-
if err != nil {
310-
return nCfg, err
311-
}
312-
313-
// merge current cfg on over
314-
err = json.Unmarshal(asJSON, &newCfg.NetworkConfiguration)
315-
if err != nil {
316-
return nCfg, err
317-
}
318-
319-
return newCfg.NetworkConfiguration, err
320-
}
321-
322284
// startProvisioningHotspot should only be called by 'StartProvisioning' (to
323285
// ensure opMutex is acquired).
324286
func (n *Subsystem) startProvisioningHotspot(ctx context.Context) error {

utils/config.go

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,15 @@ func LoadConfigFromCache() (AgentConfig, error) {
299299
cacheBytes, err := os.ReadFile(cachePath)
300300
if err != nil {
301301
if errors.Is(err, fs.ErrNotExist) {
302-
return StackOfflineConfig()
302+
return StackConfigs(&pb.DeviceAgentConfigResponse{})
303303
} else {
304-
cfg, newErr := StackOfflineConfig()
304+
cfg, newErr := StackConfigs(&pb.DeviceAgentConfigResponse{})
305305
return cfg, errors.Join(errw.Wrap(err, "reading config cache"), newErr)
306306
}
307307
} else {
308308
err = json.Unmarshal(cacheBytes, &cfg)
309309
if err != nil {
310-
cfg, newErr := StackOfflineConfig()
310+
cfg, newErr := StackConfigs(&pb.DeviceAgentConfigResponse{})
311311
return cfg, errors.Join(errw.Wrap(err, "parsing config cache"), newErr)
312312
}
313313
}
@@ -328,29 +328,21 @@ func ApplyCLIArgs(cfg AgentConfig) AgentConfig {
328328
return newCfg
329329
}
330330

331-
// StackOldProvisioningConfig reads viam-provisioning.json if available and merges it over startCfg.
332-
func stackOldProvisioningConfig(startCfg AgentConfig) (AgentConfig, error) {
331+
func StackConfigs(proto *pb.DeviceAgentConfigResponse) (AgentConfig, error) {
332+
cfg := DefaultConfig()
333333
var errOut error
334334

335-
// parse/apply deprecated /etc/viam-provisioning.json (NetworkConfiguration only)
335+
// parse/apply deprecated /etc/viam-provisioning.json
336336
oldCfg, err := LoadOldProvisioningConfig()
337337
if err != nil {
338338
if !errors.Is(err, fs.ErrNotExist) {
339339
errOut = errors.Join(errOut, errw.Wrap(err, "reading deprecated /etc/viam-provisioning.json"))
340340
}
341341
} else {
342-
startCfg.NetworkConfiguration = *oldCfg
342+
cfg.NetworkConfiguration = *oldCfg
343343
}
344-
return startCfg, errOut
345-
}
346-
347-
// StackOldProvisioningConfig reads viam-defaults.json if available and merges it over startCfg.
348-
func stackViamDefaultsConfig(startCfg AgentConfig) (AgentConfig, error) {
349-
cfg := startCfg
350-
var errOut error
351344

352345
// manufacturer config from local disk (/etc/viam-defaults.json)
353-
// use only if cloud read wasn't provided or unmarshall failed (don't merge the two).
354346
jsonBytes, err := os.ReadFile(DefaultsFilePath)
355347
if err != nil {
356348
if !errors.Is(err, fs.ErrNotExist) {
@@ -362,70 +354,21 @@ func stackViamDefaultsConfig(startCfg AgentConfig) (AgentConfig, error) {
362354
}
363355
}
364356

365-
return cfg, errOut
366-
}
367-
368-
// StackConfigs merges nextCfg over startCfg.
369-
func StackConfigs(startCfg, nextCfg AgentConfig) (AgentConfig, error) {
370-
cfg := startCfg
371-
var errOut error
372-
373-
jsonBytes, err := json.Marshal(nextCfg)
374-
if err != nil {
375-
errOut = errors.Join(errOut, err)
376-
} else {
377-
if err := json.Unmarshal(jsonBytes, &cfg); err != nil {
378-
errOut = errors.Join(errOut, err)
379-
}
380-
}
381-
return cfg, errOut
382-
}
383-
384-
// StackOfflineConfig returns a merged config resulting from applying in order:
385-
// DefaultConfig -> deprecated viam-provisioning.json -> viam-defaults.json.
386-
func StackOfflineConfig() (AgentConfig, error) {
387-
return StackProtoConfig(nil)
388-
}
389-
390-
// StackProtoConfig returns a merged config resulting from applying in order:
391-
// DefaultConfig -> deprecated viam-provisioning.json -> cloud config if available & valid / otherwise viam-defaults.json.
392-
func StackProtoConfig(fromCloudProto *pb.DeviceAgentConfigResponse) (AgentConfig, error) {
393-
cfg := DefaultConfig()
394-
var errOut error
395-
396-
cfgTmp, err := stackOldProvisioningConfig(cfg)
357+
// cloud-provided config
358+
cloudCfg, err := ProtoToConfig(proto)
397359
if err != nil {
398360
errOut = errors.Join(errOut, err)
399361
} else {
400-
cfg = cfgTmp
401-
}
402-
403-
var cloudCfgSuccess bool
404-
if fromCloudProto != nil {
405-
cloudCfg, err := ProtoToConfig(fromCloudProto)
362+
jsonBytes, err = json.Marshal(cloudCfg)
406363
if err != nil {
407364
errOut = errors.Join(errOut, err)
408365
} else {
409-
cfgTmp, err := StackConfigs(cfg, cloudCfg)
410-
if err != nil {
366+
if err := json.Unmarshal(jsonBytes, &cfg); err != nil {
411367
errOut = errors.Join(errOut, err)
412-
} else {
413-
cfg = cfgTmp
414-
cloudCfgSuccess = true
415368
}
416369
}
417370
}
418371

419-
// use viam-defaults (stack on top of base) only if cloud config is invalid
420-
if !cloudCfgSuccess {
421-
cfgTmp, err := stackViamDefaultsConfig(cfg)
422-
if err != nil {
423-
errOut = errors.Join(errOut, err)
424-
} else {
425-
cfg = cfgTmp
426-
}
427-
}
428-
429372
// validate/enforce/limit values
430373
validatedCfg, err := validateConfig(cfg)
431374
errOut = errors.Join(errOut, err)

utils/config_test.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ package utils
22

33
import (
44
"encoding/json"
5-
"os"
6-
"path/filepath"
75
"testing"
86

9-
agentpb "go.viam.com/api/app/agent/v1"
107
"go.viam.com/test"
11-
"google.golang.org/protobuf/types/known/structpb"
128
)
139

1410
// basic test for the config structure names.
@@ -90,69 +86,3 @@ func TestConvertJson(t *testing.T) {
9086
test.That(t, err, test.ShouldBeNil)
9187
test.That(t, *newConfig, test.ShouldResemble, testConfig)
9288
}
93-
94-
func TestStackConfigs(t *testing.T) {
95-
tempDir := t.TempDir()
96-
97-
DefaultsFilePath = filepath.Join(tempDir, "viam-defaults.json")
98-
99-
jsonBytes := `
100-
{
101-
"advanced_settings": {
102-
"debug": false,
103-
"viam_server_start_timeout_minutes": 12
104-
},
105-
"network_configuration": {
106-
"manufacturer": "viam",
107-
"model": "custom",
108-
"fragment_id": "",
109-
"hotspot_prefix": "viam-setup",
110-
"hotspot_ssid": "viamsetuptestssid123",
111-
"hotspot_password": "viamsetup",
112-
"disable_captive_portal_redirect": false,
113-
"offline_before_starting_hotspot_minutes": 2,
114-
"user_idle_minutes": 6,
115-
"retry_connection_timeout_minutes": 10,
116-
"wifi_power_save": null,
117-
"bluetooth_trust_all": false
118-
},
119-
"system_configuration": {
120-
"logging_journald_system_max_use_megabytes": 513,
121-
"logging_journald_runtime_max_use_megabytes": 512,
122-
"os_auto_upgrade_type": "",
123-
"forward_system_logs": ""
124-
}
125-
}
126-
`
127-
viamDefaultsCfg := DefaultConfig()
128-
err := json.Unmarshal([]byte(jsonBytes), &viamDefaultsCfg)
129-
test.That(t, err, test.ShouldBeNil)
130-
131-
err = os.WriteFile(DefaultsFilePath, []byte(jsonBytes), 0o644)
132-
test.That(t, err, test.ShouldBeNil)
133-
134-
agentConfigNoCloud, err := StackOfflineConfig()
135-
test.That(t, err, test.ShouldBeNil)
136-
test.That(t, agentConfigNoCloud, test.ShouldResemble, viamDefaultsCfg)
137-
138-
// if a readable cloud proto is provided, only its values should be used.
139-
// if a value is not provided, it should be the hardcoded default from DefaultConfig. viam-defaults.json should not be used at all
140-
adv, err := structpb.NewStruct(map[string]any{
141-
"debug": true,
142-
})
143-
test.That(t, err, test.ShouldBeNil)
144-
fromCloudProto := &agentpb.DeviceAgentConfigResponse{
145-
AdvancedSettings: adv,
146-
}
147-
agentCfgFromCloud, err := StackProtoConfig(fromCloudProto)
148-
test.That(t, err, test.ShouldBeNil)
149-
test.That(t, agentCfgFromCloud.AdvancedSettings.Debug, test.ShouldNotEqual, viamDefaultsCfg.AdvancedSettings.Debug)
150-
//nolint:lll
151-
test.That(t, agentCfgFromCloud.AdvancedSettings.ViamServerStartTimeoutMinutes, test.ShouldNotEqual, viamDefaultsCfg.AdvancedSettings.ViamServerStartTimeoutMinutes)
152-
// defaults to hostname. we don't use field from defaults file.
153-
test.That(t, agentCfgFromCloud.NetworkConfiguration.HotspotSSID, test.ShouldNotEqual, viamDefaultsCfg.NetworkConfiguration.HotspotSSID)
154-
//nolint:lll
155-
test.That(t, agentCfgFromCloud.NetworkConfiguration.UserIdleMinutes, test.ShouldNotEqual, viamDefaultsCfg.NetworkConfiguration.UserIdleMinutes)
156-
//nolint:lll
157-
test.That(t, agentCfgFromCloud.SystemConfiguration.LoggingJournaldSystemMaxUseMegabytes, test.ShouldNotEqual, viamDefaultsCfg.SystemConfiguration.LoggingJournaldSystemMaxUseMegabytes)
158-
}

0 commit comments

Comments
 (0)