forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
162 lines (129 loc) · 4.57 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
package agent
import (
"errors"
"path"
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
logger boshlog.Logger
}
func NewBootstrap(
platform boshplatform.Platform,
dirProvider boshdir.Provider,
settingsService boshsettings.Service,
logger boshlog.Logger,
) Bootstrap {
return bootstrap{
fs: platform.GetFs(),
platform: platform,
dirProvider: dirProvider,
settingsService: settingsService,
logger: logger,
}
}
func (boot bootstrap) Run() (err error) {
if err = boot.platform.SetupRuntimeConfiguration(); err != nil {
return bosherr.WrapError(err, "Setting up runtime configuration")
}
publicKey, err := boot.settingsService.PublicSSHKeyForUsername(boshsettings.VCAPUsername)
if err != nil {
return bosherr.WrapError(err, "Setting up ssh: Getting public key")
}
if len(publicKey) > 0 {
if err = boot.platform.SetupSSH(publicKey, boshsettings.VCAPUsername); err != nil {
return bosherr.WrapError(err, "Setting up ssh")
}
}
if err = boot.settingsService.LoadSettings(); err != nil {
return bosherr.WrapError(err, "Fetching settings")
}
settings := boot.settingsService.GetSettings()
if err = boot.setUserPasswords(settings.Env); err != nil {
return bosherr.WrapError(err, "Settings user password")
}
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.Ntp); 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())
if err = boot.platform.SetupEphemeralDiskWithPath(ephemeralDiskPath); 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.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 len(settings.Disks.Persistent) > 1 {
return errors.New("Error mounting persistent disk, there is more than one persistent disk")
}
for diskID := range settings.Disks.Persistent {
diskSettings, _ := settings.PersistentDiskSettings(diskID)
isPartitioned, err := boot.platform.IsPersistentDiskMountable(diskSettings)
if err != nil {
return bosherr.WrapError(err, "Checking if persistent disk is partitioned")
}
if isPartitioned {
if err = boot.platform.MountPersistentDisk(diskSettings, boot.dirProvider.StoreDir()); err != nil {
return bosherr.WrapError(err, "Mounting persistent disk")
}
}
}
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")
}
}
return nil
}
func (boot bootstrap) setUserPasswords(env boshsettings.Env) error {
password := env.GetPassword()
if password == "" {
return nil
}
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
}