forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
299 lines (237 loc) · 8.67 KB
/
bootstrap.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package agent
import (
"encoding/json"
"errors"
"fmt"
"path"
"path/filepath"
applyspec "github.com/cloudfoundry/bosh-agent/agent/applier/applyspec"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
boshdir "github.com/cloudfoundry/bosh-agent/settings/directories"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type Bootstrap interface {
Run() error
}
type bootstrap struct {
fs boshsys.FileSystem
platform boshplatform.Platform
dirProvider boshdir.Provider
settingsService boshsettings.Service
specService applyspec.V1Service
logger boshlog.Logger
}
func NewBootstrap(
platform boshplatform.Platform,
dirProvider boshdir.Provider,
settingsService boshsettings.Service,
specService applyspec.V1Service,
logger boshlog.Logger,
) Bootstrap {
return bootstrap{
fs: platform.GetFs(),
platform: platform,
dirProvider: dirProvider,
settingsService: settingsService,
specService: specService,
logger: logger,
}
}
func (boot bootstrap) Run() (err error) {
if err = boot.platform.SetupRuntimeConfiguration(); err != nil {
return bosherr.WrapError(err, "Setting up runtime configuration")
}
iaasPublicKey, err := boot.settingsService.PublicSSHKeyForUsername(boshsettings.VCAPUsername)
if err != nil {
return bosherr.WrapError(err, "Setting up ssh: Getting iaas public key")
}
if len(iaasPublicKey) > 0 {
if err = boot.platform.SetupSSH([]string{iaasPublicKey}, boshsettings.VCAPUsername); err != nil {
return bosherr.WrapError(err, "Setting up iaas ssh")
}
}
if err = boot.settingsService.LoadSettings(); err != nil {
return bosherr.WrapError(err, "Fetching settings")
}
settings := boot.settingsService.GetSettings()
envPublicKeys := settings.Env.GetAuthorizedKeys()
if len(envPublicKeys) > 0 {
publicKeys := envPublicKeys
if len(iaasPublicKey) > 0 {
publicKeys = append(publicKeys, iaasPublicKey)
}
if err = boot.platform.SetupSSH(publicKeys, boshsettings.VCAPUsername); err != nil {
return bosherr.WrapError(err, "Adding env-configured ssh keys")
}
}
if err = boot.setUserPasswords(settings.Env); err != nil {
return bosherr.WrapError(err, "Settings user password")
}
if err = boot.platform.SetupIPv6(settings.Env.Bosh.IPv6); err != nil {
return bosherr.WrapError(err, "Setting up IPv6")
}
if err = boot.platform.SetupHostname(settings.AgentID); err != nil {
return bosherr.WrapError(err, "Setting up hostname")
}
if err = boot.platform.SetupNetworking(settings.Networks); err != nil {
return bosherr.WrapError(err, "Setting up networking")
}
if err = boot.platform.SetTimeWithNtpServers(settings.GetNtpServers()); err != nil {
return bosherr.WrapError(err, "Setting up NTP servers")
}
if err = boot.platform.SetupRawEphemeralDisks(settings.RawEphemeralDiskSettings()); err != nil {
return bosherr.WrapError(err, "Setting up raw ephemeral disk")
}
ephemeralDiskPath := boot.platform.GetEphemeralDiskPath(settings.EphemeralDiskSettings())
desiredSwapSizeInBytes := settings.Env.GetSwapSizeInBytes()
if err = boot.platform.SetupEphemeralDiskWithPath(ephemeralDiskPath, desiredSwapSizeInBytes); err != nil {
return bosherr.WrapError(err, "Setting up ephemeral disk")
}
if err = boot.platform.SetupRootDisk(ephemeralDiskPath); err != nil {
return bosherr.WrapError(err, "Setting up root disk")
}
if err = boot.platform.SetupLogDir(); err != nil {
return bosherr.WrapError(err, "Setting up log dir")
}
if err = boot.platform.SetupLoggingAndAuditing(); err != nil {
return bosherr.WrapError(err, "Starting up logging and auditing utilities")
}
if err = boot.platform.SetupDataDir(); err != nil {
return bosherr.WrapError(err, "Setting up data dir")
}
if err = boot.platform.SetupTmpDir(); err != nil {
return bosherr.WrapError(err, "Setting up tmp dir")
}
if err = boot.platform.SetupHomeDir(); err != nil {
return bosherr.WrapError(err, "Setting up home dir")
}
if err = boot.platform.SetupBlobsDir(); err != nil {
return bosherr.WrapError(err, "Setting up blobs dir")
}
if err = boot.comparePersistentDisk(); err != nil {
return bosherr.WrapError(err, "Comparing persistent disks")
}
for diskID := range settings.Disks.Persistent {
var lastDiskID string
diskSettings, _ := settings.PersistentDiskSettings(diskID)
isPartitioned, err := boot.platform.IsPersistentDiskMountable(diskSettings)
if err != nil {
return bosherr.WrapError(err, "Checking if persistent disk is partitioned")
}
lastDiskID, err = boot.lastMountedCid()
if err != nil {
return bosherr.WrapError(err, "Fetching last mounted disk CID")
}
if isPartitioned && diskID == lastDiskID {
if err = boot.platform.MountPersistentDisk(diskSettings, boot.dirProvider.StoreDir()); err != nil {
return bosherr.WrapError(err, "Mounting persistent disk")
}
}
}
v1Spec, err := boot.specService.Get()
if err != nil {
return bosherr.WrapError(err, "Cannot get v1spec from SpecService")
}
for _, job := range v1Spec.Jobs() {
err = job.CreateDirectories(boot.fs, boot.dirProvider)
if err != nil {
return bosherr.WrapError(err, "Cannot create directories for jobs")
}
}
if err = boot.platform.SetupMonitUser(); err != nil {
return bosherr.WrapError(err, "Setting up monit user")
}
if err = boot.platform.StartMonit(); err != nil {
return bosherr.WrapError(err, "Starting monit")
}
if settings.Env.GetRemoveDevTools() {
packageFileListPath := path.Join(boot.dirProvider.EtcDir(), "dev_tools_file_list")
if !boot.fs.FileExists(packageFileListPath) {
return nil
}
if err = boot.platform.RemoveDevTools(packageFileListPath); err != nil {
return bosherr.WrapError(err, "Removing Development Tools Packages")
}
}
if settings.Env.GetRemoveStaticLibraries() {
staticLibrariesListPath := path.Join(boot.dirProvider.EtcDir(), "static_libraries_list")
if !boot.fs.FileExists(staticLibrariesListPath) {
return nil
}
if err = boot.platform.RemoveStaticLibraries(staticLibrariesListPath); err != nil {
return bosherr.WrapError(err, "Removing static libraries")
}
}
return nil
}
func (boot bootstrap) comparePersistentDisk() error {
settings := boot.settingsService.GetSettings()
updateSettingsPath := filepath.Join(boot.platform.GetDirProvider().BoshDir(), "update_settings.json")
if err := boot.checkLastMountedCid(settings); err != nil {
return err
}
var updateSettings boshsettings.UpdateSettings
if boot.platform.GetFs().FileExists(updateSettingsPath) {
contents, err := boot.platform.GetFs().ReadFile(updateSettingsPath)
if err != nil {
return bosherr.WrapError(err, "Reading update_settings.json")
}
if err = json.Unmarshal(contents, &updateSettings); err != nil {
return bosherr.WrapError(err, "Unmarshalling update_settings.json")
}
}
for _, diskAssociation := range updateSettings.DiskAssociations {
if _, ok := settings.PersistentDiskSettings(diskAssociation.DiskCID); !ok {
return fmt.Errorf("Disk %s is not attached", diskAssociation.DiskCID)
}
}
if len(settings.Disks.Persistent) > 1 {
if len(settings.Disks.Persistent) > len(updateSettings.DiskAssociations) {
return errors.New("Unexpected disk attached")
}
}
return nil
}
func (boot bootstrap) setUserPasswords(env boshsettings.Env) error {
password := env.GetPassword()
if !env.GetKeepRootPassword() {
err := boot.platform.SetUserPassword(boshsettings.RootUsername, password)
if err != nil {
return bosherr.WrapError(err, "Setting root password")
}
}
err := boot.platform.SetUserPassword(boshsettings.VCAPUsername, password)
if err != nil {
return bosherr.WrapError(err, "Setting vcap password")
}
return nil
}
func (boot bootstrap) checkLastMountedCid(settings boshsettings.Settings) error {
lastMountedCid, err := boot.lastMountedCid()
if err != nil {
return bosherr.WrapError(err, "Fetching last mounted disk CID")
}
if len(settings.Disks.Persistent) == 0 || lastMountedCid == "" {
return nil
}
if _, ok := settings.PersistentDiskSettings(lastMountedCid); !ok {
return fmt.Errorf("Attached disk disagrees with previous mount")
}
return nil
}
func (boot bootstrap) lastMountedCid() (string, error) {
managedDiskSettingsPath := filepath.Join(boot.platform.GetDirProvider().BoshDir(), "managed_disk_settings.json")
var lastMountedCid string
if boot.platform.GetFs().FileExists(managedDiskSettingsPath) {
contents, err := boot.platform.GetFs().ReadFile(managedDiskSettingsPath)
if err != nil {
return "", bosherr.WrapError(err, "Reading managed_disk_settings.json")
}
lastMountedCid = string(contents)
return lastMountedCid, nil
}
return "", nil
}