From ec2fd846cc2dd005ab4a00fa0117ce174b14ac1d Mon Sep 17 00:00:00 2001 From: William Petit Date: Tue, 16 Oct 2018 12:29:46 +0200 Subject: [PATCH] Add SkipUpdate() Updater method Allow to skip the update step and reboot to ReachView --- cmd/server/rpc/server.go | 5 +++ emlid/updater/update.go | 6 +++ example/updater/main.go | 70 ++++++++++++++++++++++--------- openwrt/uci_wireless_interface.go | 1 - 4 files changed, 61 insertions(+), 21 deletions(-) diff --git a/cmd/server/rpc/server.go b/cmd/server/rpc/server.go index 6c9a88e..8b3a2a9 100644 --- a/cmd/server/rpc/server.go +++ b/cmd/server/rpc/server.go @@ -3,6 +3,7 @@ package rpc import ( "context" "fmt" + "log" "net/http" "time" @@ -331,6 +332,10 @@ func (o *OrionService) updateAndReboot(rqContext context.Context, box *OrionBox, } reply.Status = status + if err := boxCli.SkipUpdate(); err != nil { + log.Fatalf("error while skipping update: %s", err) + } + ctx, rebootCancel := context.WithTimeout(rqContext, 55*time.Second) defer rebootCancel() return boxCli.RebootNow(ctx, true) diff --git a/emlid/updater/update.go b/emlid/updater/update.go index 4faa983..1d32ab5 100644 --- a/emlid/updater/update.go +++ b/emlid/updater/update.go @@ -5,6 +5,7 @@ import "context" const ( eventUpdate = "update" eventOPKGUpdateResult = "opkg update result" + eventSkipUpdate = "skip update" ) // UpdateStatus embeds informations about update status @@ -26,3 +27,8 @@ func (c *Client) Update(ctx context.Context) (*UpdateStatus, error) { ) return res, nil } + +// SkipUpdate asks the ReachRS module to skip the update +func (c *Client) SkipUpdate() error { + return c.Emit(eventSkipUpdate, nil) +} diff --git a/example/updater/main.go b/example/updater/main.go index 6b4a14c..d9b8156 100644 --- a/example/updater/main.go +++ b/example/updater/main.go @@ -12,6 +12,8 @@ import ( "forge.cadoles.com/Pyxis/orion/emlid/updater" ) +const stepSleep = 5 + const ( phaseConfigureWifi = "configure-wifi" phaseUpdateThenReboot = "update-then-reboot" @@ -97,6 +99,8 @@ func configureWifi() { log.Printf("stc activated ? %v", results.STC) log.Printf("ublox activated ? %v", results.UBlox) + sleep(stepSleep) + log.Printf("adding wifi network '%s'", ssid) ctx, addWifiCancel := context.WithTimeout(ctx, 5*time.Second) @@ -110,6 +114,8 @@ func configureWifi() { log.Fatal("couldnt add wifi network") } + sleep(stepSleep) + log.Println("connecting module to wifi network") ctx, joinWifiCancel := context.WithTimeout(ctx, 5*time.Second) defer joinWifiCancel() @@ -125,47 +131,71 @@ func updateThenReboot() { c := connect() defer c.Close() - ctx := context.Background() + log.Println("checking module status") + ctx, testResultsCancel := context.WithTimeout(context.Background(), 20*time.Second) + defer testResultsCancel() + results, err := c.TestResults(ctx) + if err != nil { + log.Fatalf("error while checking device state: %s", err) + } + + log.Printf("device: '%s'", results.Device) + log.Printf("lora activated ? %v", results.Lora) + log.Printf("mpu activated ? %v", results.MPU) + log.Printf("stc activated ? %v", results.STC) + log.Printf("ublox activated ? %v", results.UBlox) + + sleep(stepSleep) log.Println("checking time sync") - ctx, timeSyncedCancel := context.WithTimeout(ctx, 5*time.Second) + ctx, timeSyncedCancel := context.WithTimeout(context.Background(), 20*time.Second) defer timeSyncedCancel() synced, err := c.TimeSynced(ctx) if err != nil { - log.Fatal(err) + log.Fatalf("error while checking time sync status: %s", err) } log.Printf("time synced ? %v", synced) + sleep(stepSleep) + + log.Println("checking for upgrade") + ctx, upgradeAvailableCancel := context.WithTimeout(context.Background(), 20*time.Second) + defer upgradeAvailableCancel() + _, _, err = c.ReceiverUpgradeAvailable(ctx) + if err != nil { + log.Fatalf("error while checking for upgrade: %s", err) + } + + sleep(stepSleep) + log.Println("checking reachview version") - ctx, reachviewVersionCancel := context.WithTimeout(ctx, 5*time.Second) + ctx, reachviewVersionCancel := context.WithTimeout(context.Background(), 20*time.Second) defer reachviewVersionCancel() version, err := c.ReachViewVersion(ctx) if err != nil { - log.Fatal(err) + log.Fatalf("error while checking version: %s", err) } log.Printf("reachview version ? '%s'", version) - log.Println("checking for update") - ctx, updateCancel := context.WithTimeout(ctx, 5*time.Second) - defer updateCancel() - status, err := c.Update(ctx) - if err != nil { - log.Fatal(err) + sleep(stepSleep) + + log.Println("skipping update") + if err := c.SkipUpdate(); err != nil { + log.Fatalf("error while skipping update: %s", err) } - log.Printf("is update running ? %v", status.Active) - log.Printf("is update locked ? %v", status.Locked) - log.Printf("last update state ? '%s'", status.State) - - if status.Active { - log.Fatal("cannot reboot while an update is active") - } + sleep(stepSleep) log.Println("rebooting device") - ctx, rebootCancel := context.WithTimeout(ctx, 5*time.Second) + ctx, rebootCancel := context.WithTimeout(context.Background(), 20*time.Second) defer rebootCancel() if err := c.RebootNow(ctx, true); err != nil { - log.Fatal(err) + log.Fatalf("error while rebooting: %s", err) } } + +func sleep(seconds int) { + log.Printf("sleeping for %d seconds...", seconds) + time.Sleep(time.Duration(seconds) * time.Second) +} diff --git a/openwrt/uci_wireless_interface.go b/openwrt/uci_wireless_interface.go index 42a5c58..830c81e 100644 --- a/openwrt/uci_wireless_interface.go +++ b/openwrt/uci_wireless_interface.go @@ -169,7 +169,6 @@ func (wi *UCIWirelessInterface) Up(uci *UCI) *Action { // Delete remove wifi interface from UCI Configuration func (wi *UCIWirelessInterface) Delete(uci *UCI) *Action { toDelete := fmt.Sprintf("wireless.@wifi-iface[%d]", wi.Index) - fmt.Printf("DEBUG\n%s\nDEBUG", toDelete) del := uci.Delete(toDelete) if del.ReturnCode != 0 { return del