Skip to content

Commit

Permalink
Merge pull request #2615 from pedronis/fix-patch4-vs-wip-install
Browse files Browse the repository at this point in the history
make patch4 robust against in-progress install changes

Pending/in-progress install changes could be not far enough to have set a snap state. link-snap does this together with setting the flags that we were trying to upgrade here, so if there's no snap state, there's also nothing on the task to patch.
  • Loading branch information
pedronis committed Jan 11, 2017
2 parents aec795f + f14f9e5 commit 029e047
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
17 changes: 15 additions & 2 deletions overlord/patch/patch4.go
Expand Up @@ -21,6 +21,7 @@ package patch

import (
"encoding/json"
"errors"
"fmt"

"github.com/snapcore/snapd/overlord/state"
Expand Down Expand Up @@ -141,6 +142,8 @@ func (p4 patch4T) taskSnapSetup(task *state.Task) (*patch4SnapSetup, error) {
return &snapsup, nil
}

var errNoSnapState = errors.New("no snap state")

func (p4 patch4T) snapSetupAndState(task *state.Task) (*patch4SnapSetup, *patch4SnapState, error) {
var snapst patch4SnapState

Expand All @@ -152,11 +155,11 @@ func (p4 patch4T) snapSetupAndState(task *state.Task) (*patch4SnapSetup, *patch4
var snaps map[string]*json.RawMessage
err = task.State().Get("snaps", &snaps)
if err != nil {
return nil, nil, fmt.Errorf("cannot get snaps state: %v", err)
return nil, nil, errNoSnapState
}
raw, ok := snaps[snapsup.Name()]
if !ok {
return nil, nil, fmt.Errorf("cannot get snap state for %q: %v", snapsup.Name(), err)
return nil, nil, errNoSnapState
}
err = json.Unmarshal([]byte(*raw), &snapst)
if err != nil {
Expand Down Expand Up @@ -224,6 +227,16 @@ func (p4 patch4T) addCleanup(task *state.Task) error {

func (p4 patch4T) mangle(task *state.Task) error {
snapsup, snapst, err := p4.snapSetupAndState(task)
if err == errNoSnapState {
change := task.Change()
if change.Kind() != "install-snap" {
return fmt.Errorf("cannot get snap state for task %s (%s) of change %s (%s != install-snap)", task.ID(), task.Kind(), change.ID(), change.Kind())
}
// we expect pending/in-progress install changes
// possibly not to have reached link-sanp yet and so
// have no snap state yet, nothing to do
return nil
}
if err != nil {
return err
}
Expand Down
32 changes: 32 additions & 0 deletions overlord/patch/patch4_test.go
Expand Up @@ -72,6 +72,14 @@ var statePatch4JSON = []byte(`
"status": 2,
"data": {"snap-names": ["b"]},
"task-ids": ["10","11","12","13","14","15","16"]
},
"3": {
"id": "3",
"kind": "install-snap",
"summary": "install c snap",
"status": 0,
"data": {"snap-names": ["c"]},
"task-ids": ["17", "18"]
}
},
"tasks": {
Expand Down Expand Up @@ -186,6 +194,30 @@ var statePatch4JSON = []byte(`
"data": {"snap-setup-task": "10", "had-candidate": false},
"wait-tasks": ["15"],
"change": "2"
},
"17": {
"id": "17",
"kind": "prepare-snap",
"summary": "",
"status": 4,
"data": {
"snap-setup": {
"side-info": {"revision": "1", "name": "c"}
}
},
"halt-tasks": ["18"],
"change": "1"
}, "18": {
"id": "18",
"kind": "link-snap",
"summary": "make snap avaiblabla",
"status": 0,
"data": {
"snap-setup-task": "17"
},
"wait-tasks": ["17"],
"change": "3"
}
}
}
Expand Down

0 comments on commit 029e047

Please sign in to comment.