Skip to content

Commit

Permalink
Allow getting and setting config at different levels
Browse files Browse the repository at this point in the history
Also add arguments to `Config.Set` RPC:
 * `level` (int, default: 9 [user]) - make changes at the given level.
 * `save` (bool, default: true) - save the changes
 * `try_once` (bool, default: false) - when saving, make the changes only apply on next boot.
 * `reboot` (bool, default: false) - reboot after saving.

`Config.Save` is now optional and obsolete.

Make `mos license` save to level 1 by default to avoid blowing away license on user config reset.

CL: Allow getting and setting config at different levels

PUBLISHED_FROM=16dd8c6059bf4e347e9df71ace4e955100510e6e
  • Loading branch information
Deomid Ryabkov authored and cesantabot committed Feb 20, 2019
1 parent 576f881 commit 92193c1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
47 changes: 36 additions & 11 deletions mos/config/config.go
Expand Up @@ -36,7 +36,7 @@ func Get(ctx context.Context, devConn dev.DevConn) error {
}

// Get all config from the attached device
devConf, err := dev.GetConfig(ctx, devConn)
devConf, err := dev.GetConfigLevel(ctx, devConn, *flags.Level)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func SetWithArgs(

// Get all config from the attached device
ourutil.Reportf("Getting configuration...")
devConf, err := dev.GetConfig(ctx, devConn)
devConf, err := dev.GetConfigLevel(ctx, devConn, *flags.Level)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -86,26 +86,47 @@ func SetWithArgs(
return SetAndSave(ctx, devConn, devConf)
}

func SetAndSave(ctx context.Context, devConn dev.DevConn, devConf *dev.DevConf) error {
func SetAndSaveLevel(ctx context.Context, devConn dev.DevConn, devConf *dev.DevConf, level int) error {
// save changed conf
ourutil.Reportf("Setting new configuration...")
err := dev.SetConfig(ctx, devConn, devConf)
arg := &dev.ConfigSetArg{
Save: !*flags.NoSave,
Reboot: !*flags.NoReboot,
TryOnce: *flags.TryOnce,
}
if level > 0 {
arg.Level = level
ourutil.Reportf("Setting new configuration (level %d)...", arg.Level)
} else {
ourutil.Reportf("Setting new configuration...")
}
saved, err := dev.SetConfig(ctx, devConn, devConf, arg)
if err != nil {
return errors.Trace(err)
}

// Newer firmware (2.12+) doesn't need explicit save.
if arg.Save && saved {
if arg.TryOnce {
ourutil.Reportf("Note: --try-once is set, config is valid for one reboot only")
}
if arg.Reboot {
time.Sleep(200 * time.Millisecond)
}
return nil
}

attempts := saveAttempts
for !*flags.NoSave {
if *flags.NoReboot {
for arg.Save {
if !arg.Reboot {
ourutil.Reportf("Saving...")
} else {
ourutil.Reportf("Saving and rebooting...")
}
ctx2, cancel := context.WithTimeout(ctx, saveTimeout)
defer cancel()
if err = devConn.Call(ctx2, "Config.Save", map[string]interface{}{
"reboot": !*flags.NoReboot,
"try_once": *flags.TryOnce,
"reboot": arg.Reboot,
"try_once": arg.TryOnce,
}, nil); err != nil {
attempts -= 1
if attempts > 0 {
Expand All @@ -114,11 +135,11 @@ func SetAndSave(ctx context.Context, devConn dev.DevConn, devConf *dev.DevConf)
}
return errors.Trace(err)
}
if *flags.TryOnce {
if arg.TryOnce {
ourutil.Reportf("Note: --try-once is set, config is valid for one reboot only")
}

if !*flags.NoReboot {
if arg.Reboot {
time.Sleep(200 * time.Millisecond)
}
break
Expand All @@ -127,6 +148,10 @@ func SetAndSave(ctx context.Context, devConn dev.DevConn, devConf *dev.DevConf)
return nil
}

func SetAndSave(ctx context.Context, devConn dev.DevConn, devConf *dev.DevConf) error {
return SetAndSaveLevel(ctx, devConn, devConf, *flags.Level)
}

func parseParamValues(args []string) (map[string]string, error) {
ret := map[string]string{}
for _, a := range args {
Expand Down
42 changes: 34 additions & 8 deletions mos/dev/dev_conn.go
Expand Up @@ -10,6 +10,15 @@ import (

type ConfigSetArg struct {
Config map[string]interface{} `json:"config,omitempty"`
// Since 2.12
Level int `json:"level,omitempty"`
Save bool `json:"save,omitempty"`
Reboot bool `json:"reboot,omitempty"`
TryOnce bool `json:"try_once,omitempty"`
}

type ConfigSetResp struct {
Saved bool `json:"saved,omitempty"`
}

type GetInfoResultWifi struct {
Expand Down Expand Up @@ -43,34 +52,47 @@ type DevConn interface {

const confOpAttempts = 3

func SetConfig(ctx context.Context, dc DevConn, devConf *DevConf) error {
func SetConfig(ctx context.Context, dc DevConn, devConf *DevConf, setArgTmpl *ConfigSetArg) (bool, error) {
var resp ConfigSetResp
if setArgTmpl == nil {
setArgTmpl = &ConfigSetArg{}
}
setArg := *setArgTmpl
setArg.Config = devConf.diff
attempts := confOpAttempts
for {
ctx2, cancel := context.WithTimeout(ctx, dc.GetTimeout())
defer cancel()
if err := dc.Call(ctx2, "Config.Set", &ConfigSetArg{
Config: devConf.diff,
}, nil); err != nil {
if err := dc.Call(ctx2, "Config.Set", setArg, &resp); err != nil {
attempts -= 1
if attempts > 0 {
glog.Warningf("Error: %s", err)
continue
}
return errors.Trace(err)
return false, errors.Trace(err)
}
glog.Infof("resp: %#v", resp)
break
}

return nil
return resp.Saved, nil
}

func GetConfig(ctx context.Context, dc DevConn) (*DevConf, error) {
func GetConfigLevel(ctx context.Context, dc DevConn, level int) (*DevConf, error) {
var devConf DevConf
attempts := confOpAttempts
for {
ctx2, cancel := context.WithTimeout(ctx, dc.GetTimeout())
defer cancel()
if err := dc.Call(ctx2, "Config.Get", nil, &devConf.data); err != nil {
var err error
if level >= 0 {
err = dc.Call(ctx2, "Config.Get", struct {
Level int `json:"level"`
}{Level: level}, &devConf.data)
} else {
err = dc.Call(ctx2, "Config.Get", nil, &devConf.data)
}
if err != nil {
attempts -= 1
if attempts > 0 {
glog.Warningf("Error: %s", err)
Expand All @@ -83,6 +105,10 @@ func GetConfig(ctx context.Context, dc DevConn) (*DevConf, error) {
return &devConf, nil
}

func GetConfig(ctx context.Context, dc DevConn) (*DevConf, error) {
return GetConfigLevel(ctx, dc, -1)
}

func GetInfo(ctx context.Context, dc DevConn) (*GetInfoResult, error) {
var r GetInfoResult
attempts := confOpAttempts
Expand Down
1 change: 1 addition & 0 deletions mos/flags/flags.go
Expand Up @@ -56,6 +56,7 @@ var (
GCPKeyFile = flag.String("gcp-key-file", "", "Private key file")
GCPRPCCreateTopic = flag.Bool("gcp-rpc-create-topic", false, "Create RPC topic plumbing if needed")

Level = flag.Int("level", -1, "Config level; default - runtime")
NoReboot = flag.Bool("no-reboot", false, "Save config but don't reboot the device.")
NoSave = flag.Bool("no-save", false, "Don't save config and don't reboot the device")
TryOnce = flag.Bool("try-once", false, "When saving the config, do it in such a way that it's only applied on the next boot")
Expand Down
7 changes: 6 additions & 1 deletion mos/license_cmd/license.go
Expand Up @@ -105,5 +105,10 @@ func License(ctx context.Context, devConn dev.DevConn) error {
if err := config.ApplyDiff(devConf, settings); err != nil {
return errors.Trace(err)
}
return config.SetAndSave(ctx, devConn, devConf)
// Save to level 1 by default.
level := *flags.Level
if level < 0 {
level = 1
}
return config.SetAndSaveLevel(ctx, devConn, devConf, level)
}

0 comments on commit 92193c1

Please sign in to comment.