Skip to content

Commit 05eaf0c

Browse files
committed
rebase Networking subsystem's cfg over default before starting provisioning
1 parent 5e59e35 commit 05eaf0c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

subsystems/networking/networkmanager_linux.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package networking
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
67
"errors"
78
"io"
89
"net/http"
@@ -259,10 +260,20 @@ func (n *Networking) startProvisioning(ctx context.Context, inputChan chan<- use
259260
}
260261
n.internalOpMu.Lock()
261262
defer n.internalOpMu.Unlock()
263+
262264
if ctx.Err() != nil {
263265
return ctx.Err()
264266
}
265267

268+
// rebase the current config onto the default. since we no longer merge configs once a cloud config is available,
269+
// we 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("rebase existing networking config over viam-defaults.json failed with err. Continuing with existing config.", "err", err)
272+
} else {
273+
// 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.
274+
n.cfg = provisioningCfg
275+
}
276+
266277
n.portalData.resetInputData(inputChan)
267278
hotspotErr := n.startProvisioningHotspot(ctx)
268279
if hotspotErr != nil {
@@ -281,6 +292,28 @@ func (n *Networking) startProvisioning(ctx context.Context, inputChan chan<- use
281292
return errors.Join(hotspotErr, bluetoothErr)
282293
}
283294

295+
// rebaseNetworkConfiguration reapplies a NetworkConfiguration over the default configuration.
296+
func rebaseNetworkConfiguration(nCfg utils.NetworkConfiguration) (utils.NetworkConfiguration, error) {
297+
asJson, err := json.Marshal(nCfg)
298+
if err != nil {
299+
return utils.NetworkConfiguration{}, err
300+
}
301+
302+
// get default cfg. Hardcoded values + viam_defaults.json (if available - does not err if file does not exist).
303+
newCfg, err := utils.StackConfigs(nil)
304+
if err != nil {
305+
return utils.NetworkConfiguration{}, err
306+
}
307+
308+
// merge current cfg on over
309+
err = json.Unmarshal(asJson, &newCfg.NetworkConfiguration)
310+
if err != nil {
311+
return utils.NetworkConfiguration{}, err
312+
}
313+
314+
return newCfg.NetworkConfiguration, err
315+
}
316+
284317
// startProvisioningHotspot should only be called by 'StartProvisioning' (to
285318
// ensure opMutex is acquired).
286319
func (n *Networking) startProvisioningHotspot(ctx context.Context) error {

0 commit comments

Comments
 (0)