forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_dns_state.go
83 lines (66 loc) · 1.88 KB
/
sync_dns_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package state
import (
"encoding/json"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
type SyncDNSState struct {
platform boshplatform.Platform
fs boshsys.FileSystem
path string
uuidGenerator boshuuid.Generator
}
func NewSyncDNSState(platform boshplatform.Platform, path string, generator boshuuid.Generator) SyncDNSState {
return SyncDNSState{
platform: platform,
fs: platform.GetFs(),
path: path,
uuidGenerator: generator,
}
}
func (s SyncDNSState) SaveState(localDNSState []byte) error {
uuid, err := s.uuidGenerator.Generate()
if err != nil {
return bosherr.WrapError(err, "generating uuid for temp file")
}
tmpFilePath := s.path + uuid
err = s.fs.WriteFileQuietly(tmpFilePath, localDNSState)
if err != nil {
return bosherr.WrapError(err, "writing the blobstore DNS state")
}
err = s.platform.SetupRecordsJSONPermission(tmpFilePath)
if err != nil {
return bosherr.WrapError(err, "setting permissions of blobstore DNS state")
}
err = s.fs.Rename(tmpFilePath, s.path)
if err != nil {
return bosherr.WrapError(err, "renaming")
}
return nil
}
func (s SyncDNSState) NeedsUpdate(newVersion uint64) bool {
if !s.fs.FileExists(s.path) {
return true
}
version, err := s.loadVersion()
if err != nil {
return true
}
return version < newVersion
}
func (s SyncDNSState) loadVersion() (uint64, error) {
contents, err := s.fs.ReadFile(s.path)
if err != nil {
return 0, bosherr.WrapError(err, "reading state file")
}
var localVersion struct {
Version uint64 `json:"version"`
}
err = json.Unmarshal(contents, &localVersion)
if err != nil {
return 0, bosherr.WrapError(err, "unmarshalling state file")
}
return localVersion.Version, nil
}