forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_system_deployment_config_service.go
80 lines (65 loc) · 2.1 KB
/
file_system_deployment_config_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
package config
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
boshsys "github.com/cloudfoundry/bosh-agent/system"
)
type fileSystemDeploymentConfigService struct {
configPath string
fs boshsys.FileSystem
logger boshlog.Logger
logTag string
}
func NewFileSystemDeploymentConfigService(configPath string, fs boshsys.FileSystem, logger boshlog.Logger) DeploymentConfigService {
return fileSystemDeploymentConfigService{
configPath: configPath,
fs: fs,
logger: logger,
logTag: "config",
}
}
type StemcellRecord struct {
Name string `json:"name"`
Version string `json:"version"`
SHA1 string `json:"sha1"`
CID string `json:"cid"`
}
type DeploymentFile struct {
UUID string `json:"uuid"`
Stemcells []StemcellRecord `json:"stemcells"`
VMCID string `json:"vm_cid"`
}
func (s fileSystemDeploymentConfigService) Load() (DeploymentConfig, error) {
config := DeploymentConfig{}
deploymentFileContents, err := s.fs.ReadFile(s.configPath)
if err != nil {
return config, nil
}
s.logger.Debug(s.logTag, "Deployment File Contents %#s", deploymentFileContents)
deploymentFile := DeploymentFile{}
err = json.Unmarshal(deploymentFileContents, &deploymentFile)
if err != nil {
return config, bosherr.WrapError(err, "Unmarshalling deployment config file `%s'", s.configPath)
}
config.DeploymentUUID = deploymentFile.UUID
config.Stemcells = deploymentFile.Stemcells
config.VMCID = deploymentFile.VMCID
return config, nil
}
func (s fileSystemDeploymentConfigService) Save(config DeploymentConfig) error {
deploymentFile := DeploymentFile{
UUID: config.DeploymentUUID,
Stemcells: config.Stemcells,
VMCID: config.VMCID,
}
jsonContent, err := json.MarshalIndent(deploymentFile, "", " ")
if err != nil {
return bosherr.WrapError(err, "Marshalling deployment config into JSON")
}
err = s.fs.WriteFile(s.configPath, jsonContent)
if err != nil {
return bosherr.WrapError(err, "Writing deployment config file `%s'", s.configPath)
}
return nil
}