forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_system_deployment_state_service.go
123 lines (99 loc) · 3.51 KB
/
file_system_deployment_state_service.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package config
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
type fileSystemDeploymentStateService struct {
configPath string
fs boshsys.FileSystem
uuidGenerator boshuuid.Generator
logger boshlog.Logger
logTag string
}
func NewFileSystemDeploymentStateService(fs boshsys.FileSystem, uuidGenerator boshuuid.Generator, logger boshlog.Logger, deploymentStatePath string) DeploymentStateService {
return &fileSystemDeploymentStateService{
configPath: deploymentStatePath,
fs: fs,
uuidGenerator: uuidGenerator,
logger: logger,
logTag: "config",
}
}
func DeploymentStatePath(deploymentManifestPath string, deploymentStatePath string) string {
if deploymentStatePath != "" {
return deploymentStatePath
}
baseFileName := filepath.Base(strings.TrimSuffix(deploymentManifestPath, filepath.Ext(deploymentManifestPath)))
return filepath.Join(filepath.Dir(deploymentManifestPath), fmt.Sprintf("%s-state.json", baseFileName))
}
func (s *fileSystemDeploymentStateService) Path() string {
return s.configPath
}
func (s *fileSystemDeploymentStateService) Exists() bool {
return s.fs.FileExists(s.configPath)
}
func (s *fileSystemDeploymentStateService) Load() (DeploymentState, error) {
if s.configPath == "" {
panic("configPath not yet set!")
}
s.logger.Debug(s.logTag, "Loading deployment state: %s", s.configPath)
deploymentState := &DeploymentState{}
if s.fs.FileExists(s.configPath) {
deploymentStateFileContents, err := s.fs.ReadFile(s.configPath)
if err != nil {
return DeploymentState{}, bosherr.WrapErrorf(err, "Reading deployment state file '%s'", s.configPath)
}
s.logger.Debug(s.logTag, "Deployment File Contents %#s", deploymentStateFileContents)
err = json.Unmarshal(deploymentStateFileContents, deploymentState)
if err != nil {
return DeploymentState{}, bosherr.WrapErrorf(err, "Unmarshalling deployment state file '%s'", s.configPath)
}
}
err := s.initDefaults(deploymentState)
if err != nil {
return DeploymentState{}, bosherr.WrapErrorf(err, "Initializing deployment state defaults")
}
return *deploymentState, nil
}
func (s *fileSystemDeploymentStateService) Save(deploymentState DeploymentState) error {
if s.configPath == "" {
panic("configPath not yet set!")
}
s.logger.Debug(s.logTag, "Saving deployment state %#v", deploymentState)
jsonContent, err := json.MarshalIndent(deploymentState, "", " ")
if err != nil {
return bosherr.WrapError(err, "Marshalling deployment state into JSON")
}
err = s.fs.WriteFile(s.configPath, jsonContent)
if err != nil {
return bosherr.WrapErrorf(err, "Writing deployment state file '%s'", s.configPath)
}
return nil
}
func (s *fileSystemDeploymentStateService) initDefaults(deploymentState *DeploymentState) error {
if deploymentState.DirectorID == "" {
uuid, err := s.uuidGenerator.Generate()
if err != nil {
return bosherr.WrapError(err, "Generating DirectorID")
}
deploymentState.DirectorID = uuid
err = s.Save(*deploymentState)
if err != nil {
return bosherr.WrapError(err, "Saving deployment state")
}
}
return nil
}
func (s *fileSystemDeploymentStateService) Cleanup() error {
err := s.fs.RemoveAll(s.configPath)
if err != nil {
return bosherr.WrapErrorf(err, "Could not delete deployment state file %s", s.configPath)
}
return nil
}