forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installation.go
97 lines (86 loc) · 2.47 KB
/
installation.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
package installation
import (
biinstallmanifest "github.com/cloudfoundry/bosh-cli/installation/manifest"
biregistry "github.com/cloudfoundry/bosh-cli/registry"
biui "github.com/cloudfoundry/bosh-cli/ui"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type Installation interface {
Target() Target
Job() InstalledJob
WithRunningRegistry(boshlog.Logger, biui.Stage, func() error) error
StartRegistry() error
StopRegistry() error
}
type installation struct {
target Target
job InstalledJob
manifest biinstallmanifest.Manifest
registryServerManager biregistry.ServerManager
registryServer biregistry.Server
}
func NewInstallation(
target Target,
job InstalledJob,
manifest biinstallmanifest.Manifest,
registryServerManager biregistry.ServerManager,
) Installation {
return &installation{
target: target,
job: job,
manifest: manifest,
registryServerManager: registryServerManager,
}
}
func (i *installation) Target() Target {
return i.target
}
func (i *installation) Job() InstalledJob {
return i.job
}
func (i *installation) WithRunningRegistry(logger boshlog.Logger, stage biui.Stage, fn func() error) error {
err := stage.Perform("Starting registry", func() error {
return i.StartRegistry()
})
if err != nil {
return err
}
defer i.stopRegistryNice(logger, stage)
return fn()
}
func (i *installation) stopRegistryNice(logger boshlog.Logger, stage biui.Stage) {
err := stage.Perform("Stopping registry", func() error {
return i.StopRegistry()
})
if err != nil {
logger.Warn("installation", "Registry failed to stop: %s", err)
}
}
func (i *installation) StartRegistry() error {
if !i.manifest.Registry.IsEmpty() {
if i.registryServer != nil {
return bosherr.Error("Registry already started")
}
config := i.manifest.Registry
registryServer, err := i.registryServerManager.Start(config.Username, config.Password, config.Host, config.Port)
if err != nil {
return bosherr.WrapError(err, "Starting registry")
}
i.registryServer = registryServer
}
return nil
}
func (i *installation) StopRegistry() error {
if !i.manifest.Registry.IsEmpty() {
if i.registryServer == nil {
return bosherr.Error("Registry must be started before it can be stopped")
}
err := i.registryServer.Stop()
if err != nil {
return bosherr.WrapError(err, "Stopping registry")
}
i.registryServer = nil
}
return nil
}