Skip to content

Commit

Permalink
Reduce cyclomatic complexity of update module code.
Browse files Browse the repository at this point in the history
Just moving code around, a couple of extra variables needed, but no
functionality change.

Changelog: None

Signed-off-by: Kristian Amlie <[email protected]>
  • Loading branch information
Kristian Amlie committed Mar 1, 2019
1 parent 319b448 commit 762ae61
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 117 deletions.
50 changes: 28 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,28 +448,7 @@ func handleCLIOptions(runOptions runOptionsType, env *uBootEnv, dualRootfsDevice
*runOptions.commit,
*runOptions.rollback:

menderPieces, err := commonInit(config, &runOptions)
if err != nil {
return err
}
stateExec := newStateScriptExecutor(config)
deviceManager := NewDeviceManager(dualRootfsDevice, config, menderPieces.store)

switch {
case *runOptions.showArtifact:
return PrintArtifactName(deviceManager)

case *runOptions.imageFile != "":
vKey := config.GetVerificationKey()
return doStandaloneInstall(deviceManager, runOptions, vKey, stateExec)

case *runOptions.commit:
return doStandaloneCommit(deviceManager, stateExec)

case *runOptions.rollback:
return doStandaloneRollback(deviceManager, stateExec)

}
return handleArtifactOperations(runOptions, dualRootfsDevice, config)

case *runOptions.bootstrap:
return doBootstrapAuthorize(config, &runOptions)
Expand All @@ -489,6 +468,33 @@ func handleCLIOptions(runOptions runOptionsType, env *uBootEnv, dualRootfsDevice
return nil
}

func handleArtifactOperations(runOptions runOptionsType, dualRootfsDevice dualRootfsDevice, config *menderConfig) error {
menderPieces, err := commonInit(config, &runOptions)
if err != nil {
return err
}
stateExec := newStateScriptExecutor(config)
deviceManager := NewDeviceManager(dualRootfsDevice, config, menderPieces.store)

switch {
case *runOptions.showArtifact:
return PrintArtifactName(deviceManager)

case *runOptions.imageFile != "":
vKey := config.GetVerificationKey()
return doStandaloneInstall(deviceManager, runOptions, vKey, stateExec)

case *runOptions.commit:
return doStandaloneCommit(deviceManager, stateExec)

case *runOptions.rollback:
return doStandaloneRollback(deviceManager, stateExec)

default:
return errors.New("handleArtifactOperations: Should never get here")
}
}

func getMenderDaemonPID(cmd *exec.Cmd) (string, error) {
buf := bytes.NewBuffer(nil)
cmd.Stdout = buf
Expand Down
2 changes: 1 addition & 1 deletion modules_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

const (
successfulInstallUnknown = iota
_ = iota // We don't want it to be zero, to avoid it being unintentionally defined.
successfulInstall
successfulRollback
successfulUncommitted
Expand Down
120 changes: 76 additions & 44 deletions standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ func doStandaloneInstall(device *deviceManager, args runOptionsType,
return doStandaloneInstallStates(ioutil.NopCloser(tr), vKey, device, stateExec)
}

func doStandaloneInstallStates(art io.ReadCloser, key []byte,
device *deviceManager, stateExec statescript.Executor) error {
func doStandaloneInstallStatesDownload(art io.ReadCloser, key []byte,
device *deviceManager, stateExec statescript.Executor) (*standaloneData, error) {

dt, err := device.GetDeviceType()
if err != nil {
log.Errorf("Could not determine device type: %s", err.Error())
return err
return nil, err
}

// Download state
Expand All @@ -106,7 +106,7 @@ func doStandaloneInstallStates(art io.ReadCloser, key []byte,
log.Errorf("Download_Enter script failed: %s", err.Error())
callErrorScript("Download", stateExec)
// No doStandaloneFailureStates here, since we have not done anything yet.
return err
return nil, err
}
installer, installers, err := installer.ReadHeaders(art, dt, key,
device.stateScriptPath, &device.installerFactories)
Expand All @@ -117,7 +117,7 @@ func doStandaloneInstallStates(art io.ReadCloser, key []byte,
log.Errorf("Reading headers failed: %s", err.Error())
callErrorScript("Download", stateExec)
doStandaloneFailureStates(device, standaloneData, stateExec, false, false, true)
return err
return nil, err
}

standaloneData.artifactName = installer.GetArtifactName()
Expand All @@ -127,16 +127,29 @@ func doStandaloneInstallStates(art io.ReadCloser, key []byte,
log.Errorf("Download failed: %s", err.Error())
callErrorScript("Download", stateExec)
doStandaloneFailureStates(device, standaloneData, stateExec, false, false, true)
return err
return nil, err
}
err = stateExec.ExecuteAll("Download", "Leave", false, nil)
if err != nil {
log.Errorf("Download_Leave script failed: %s", err.Error())
callErrorScript("Download", stateExec)
doStandaloneFailureStates(device, standaloneData, stateExec, false, false, true)
return nil, err
}

return standaloneData, nil
}

func doStandaloneInstallStates(art io.ReadCloser, key []byte,
device *deviceManager, stateExec statescript.Executor) error {

standaloneData, err := doStandaloneInstallStatesDownload(art, key, device, stateExec)
if err != nil {
return err
}

installers := standaloneData.installers

rollbackSupport, err := determineRollbackSupport(installers)
if err != nil {
log.Error(err.Error())
Expand Down Expand Up @@ -175,7 +188,7 @@ func doStandaloneInstallStates(art io.ReadCloser, key []byte,
return err
}

err = standaloneStoreArtifactState(device.store, installer.GetArtifactName(), installers)
err = standaloneStoreArtifactState(device.store, standaloneData.artifactName, installers)
if err != nil {
log.Errorf("Could not update database: %s", err.Error())
return err
Expand Down Expand Up @@ -322,56 +335,75 @@ func doStandaloneRollbackState(standaloneData *standaloneData, stateExec statesc
return firstErr
}

func doStandaloneFailureStates(device *deviceManager, standaloneData *standaloneData,
stateExec statescript.Executor, rollback, failure, cleanup bool) error {
func doStandaloneFailureStatesRollback(standaloneData *standaloneData,
stateExec statescript.Executor) (bool, error) {

var firstErr error
var rollbackSupport bool
var err error
var rollbackSupport bool

if rollback {
rollbackSupport, err = determineRollbackSupport(standaloneData.installers)
rollbackSupport, err = determineRollbackSupport(standaloneData.installers)
if err != nil {
log.Error(err.Error())
// Continue with failure scripts anyway.
} else if rollbackSupport {
err = doStandaloneRollbackState(standaloneData, stateExec)
if err != nil {
if firstErr == nil {
firstErr = err
}
log.Error(err.Error())
// Continue with failure scripts anyway.
} else if rollbackSupport {
err = doStandaloneRollbackState(standaloneData, stateExec)
if err != nil {
if firstErr == nil {
firstErr = err
}
log.Errorf("Error rolling back: %s", err.Error())
}
// Continue with failure scripts anyway.
log.Errorf("Error rolling back: %s", err.Error())
}
// Continue with failure scripts anyway.
}

if failure {
err = stateExec.ExecuteAll("ArtifactFailure", "Enter", true, nil)
return rollbackSupport, err
}

func doStandaloneFailureStatesFailure(standaloneData *standaloneData,
stateExec statescript.Executor) error {

var err error
var firstErr error

err = stateExec.ExecuteAll("ArtifactFailure", "Enter", true, nil)
if err != nil {
if firstErr == nil {
firstErr = err
}
log.Errorf("Error when executing ArtifactFailure_Enter scripts: %s", err.Error())
}
for _, inst := range standaloneData.installers {
err = inst.Failure()
if err != nil {
if firstErr == nil {
firstErr = err
}
log.Errorf("Error when executing ArtifactFailure_Enter scripts: %s", err.Error())
log.Errorf("Error when executing ArtifactFailure state: %s", err.Error())
}
for _, inst := range standaloneData.installers {
err = inst.Failure()
if err != nil {
if firstErr == nil {
firstErr = err
}
log.Errorf("Error when executing ArtifactFailure state: %s", err.Error())
}
}
err = stateExec.ExecuteAll("ArtifactFailure", "Leave", true, nil)
if err != nil {
if firstErr == nil {
firstErr = err
}
err = stateExec.ExecuteAll("ArtifactFailure", "Leave", true, nil)
if err != nil {
if firstErr == nil {
firstErr = err
}
log.Errorf("Error when executing ArtifactFailure_Leave scripts: %s", err.Error())
log.Errorf("Error when executing ArtifactFailure_Leave scripts: %s", err.Error())
}

return firstErr
}

func doStandaloneFailureStates(device *deviceManager, standaloneData *standaloneData,
stateExec statescript.Executor, rollback, failure, cleanup bool) error {

var firstErr error
var rollbackSupport bool
var err error

if rollback {
rollbackSupport, firstErr = doStandaloneFailureStatesRollback(standaloneData, stateExec)
}

if failure {
err = doStandaloneFailureStatesFailure(standaloneData, stateExec)
if err != nil && firstErr == nil {
firstErr = err
}
}

Expand Down
Loading

0 comments on commit 762ae61

Please sign in to comment.