forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap_state.go
52 lines (40 loc) · 1.06 KB
/
bootstrap_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package platform
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type BootstrapState struct {
Linux LinuxState
path string
fs boshsys.FileSystem
}
type LinuxState struct {
HostsConfigured bool `json:"hosts_configured"`
}
func NewBootstrapState(fs boshsys.FileSystem, path string) (*BootstrapState, error) {
state := BootstrapState{fs: fs, path: path}
if !fs.FileExists(path) {
return &state, nil
}
bytes, err := fs.ReadFile(path)
if err != nil {
return nil, bosherr.WrapError(err, "Reading bootstrap state file")
}
err = json.Unmarshal(bytes, &state)
if err != nil {
return nil, bosherr.WrapError(err, "Unmarshalling bootstrap state")
}
return &state, nil
}
func (s *BootstrapState) SaveState() (err error) {
jsonState, err := json.Marshal(*s)
if err != nil {
return bosherr.WrapError(err, "Marshalling bootstrap state")
}
err = s.fs.WriteFile(s.path, jsonState)
if err != nil {
return bosherr.WrapError(err, "Writing bootstrap state to file")
}
return
}