-
Notifications
You must be signed in to change notification settings - Fork 114
/
apply.go
135 lines (112 loc) · 3.02 KB
/
apply.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
124
125
126
127
128
129
130
131
132
133
134
135
package action
import (
"errors"
"os"
"path"
boshappl "github.com/cloudfoundry/bosh-agent/agent/applier"
boshas "github.com/cloudfoundry/bosh-agent/agent/applier/applyspec"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
const (
userBaseDirPermissions = os.FileMode(0755)
userInstanceFilePermissions = os.FileMode(0644)
)
type ApplyAction struct {
applier boshappl.Applier
specService boshas.V1Service
settingsService boshsettings.Service
instanceDir string
fs boshsys.FileSystem
}
func NewApply(
applier boshappl.Applier,
specService boshas.V1Service,
settingsService boshsettings.Service,
instanceDir string,
fs boshsys.FileSystem,
) (action ApplyAction) {
action.applier = applier
action.specService = specService
action.settingsService = settingsService
action.instanceDir = instanceDir
action.fs = fs
return
}
func (a ApplyAction) IsAsynchronous(_ ProtocolVersion) bool {
return true
}
func (a ApplyAction) IsPersistent() bool {
return false
}
func (a ApplyAction) IsLoggable() bool {
return true
}
func (a ApplyAction) Run(desiredSpec boshas.V1ApplySpec) (string, error) {
settings := a.settingsService.GetSettings()
resolvedDesiredSpec, err := a.specService.PopulateDHCPNetworks(desiredSpec, settings)
if err != nil {
return "", bosherr.WrapError(err, "Resolving dynamic networks")
}
if desiredSpec.ConfigurationHash != "" {
currentSpec, err := a.specService.Get()
if err != nil {
return "", bosherr.WrapError(err, "Getting current spec")
}
err = a.applier.Apply(currentSpec, resolvedDesiredSpec)
if err != nil {
return "", bosherr.WrapError(err, "Applying")
}
}
err = a.specService.Set(resolvedDesiredSpec)
if err != nil {
return "", bosherr.WrapError(err, "Persisting apply spec")
}
err = a.writeInstanceData(resolvedDesiredSpec)
if err != nil {
return "", err
}
return "applied", nil
}
func (a ApplyAction) writeInstanceData(spec boshas.V1ApplySpec) error {
err := a.writeInstanceField("id", spec.NodeID)
if err != nil {
return err
}
err = a.writeInstanceField("az", spec.AvailabilityZone)
if err != nil {
return err
}
err = a.writeInstanceField("name", spec.Name)
if err != nil {
return err
}
err = a.writeInstanceField("deployment", spec.Deployment)
if err != nil {
return err
}
err = a.fs.Chmod(a.instanceDir, userBaseDirPermissions)
if err != nil {
return err
}
return nil
}
func (a ApplyAction) writeInstanceField(filename string, instanceField string) error {
instanceFieldFilePath := path.Join(a.instanceDir, filename)
err := a.fs.WriteFileString(instanceFieldFilePath, instanceField)
if err != nil {
return err
}
err = a.fs.Chmod(instanceFieldFilePath, userInstanceFilePermissions)
if err != nil {
return err
}
return nil
}
func (a ApplyAction) Resume() (interface{}, error) {
return nil, errors.New("not supported")
}
func (a ApplyAction) Cancel() error {
return errors.New("not supported")
}