~themue/juju-core/053-env-more-script-friendly

« back to all changes in this revision

Viewing changes to environs/maas/state.go

Merge trunk and resolve conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package maas
 
2
 
 
3
import (
 
4
        "bytes"
 
5
        "fmt"
 
6
        "io/ioutil"
 
7
        "launchpad.net/goyaml"
 
8
        "launchpad.net/juju-core/state"
 
9
)
 
10
 
 
11
const stateFile = "provider-state"
 
12
 
 
13
// Persistent environment state.  An environment needs to know what instances
 
14
// it manages.
 
15
type bootstrapState struct {
 
16
        StateInstances []state.InstanceId `yaml:"state-instances"`
 
17
}
 
18
 
 
19
// saveState writes the environment's state to the provider-state file stored
 
20
// in the environment's storage.
 
21
func (env *maasEnviron) saveState(state *bootstrapState) error {
 
22
        data, err := goyaml.Marshal(state)
 
23
        if err != nil {
 
24
                return err
 
25
        }
 
26
        buf := bytes.NewBuffer(data)
 
27
        return env.Storage().Put(stateFile, buf, int64(len(data)))
 
28
}
 
29
 
 
30
// loadState reads the environment's state from storage.
 
31
func (env *maasEnviron) loadState() (*bootstrapState, error) {
 
32
        r, err := env.Storage().Get(stateFile)
 
33
        if err != nil {
 
34
                return nil, err
 
35
        }
 
36
        defer r.Close()
 
37
        data, err := ioutil.ReadAll(r)
 
38
        if err != nil {
 
39
                return nil, fmt.Errorf("error reading %q: %v", stateFile, err)
 
40
        }
 
41
        var state bootstrapState
 
42
        err = goyaml.Unmarshal(data, &state)
 
43
        if err != nil {
 
44
                return nil, fmt.Errorf("error unmarshalling %q: %v", stateFile, err)
 
45
        }
 
46
        return &state, nil
 
47
}