forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer.go
116 lines (102 loc) · 3.64 KB
/
deployer.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
package deployment
import (
"time"
biblobstore "github.com/cloudfoundry/bosh-cli/blobstore"
bicloud "github.com/cloudfoundry/bosh-cli/cloud"
bidisk "github.com/cloudfoundry/bosh-cli/deployment/disk"
biinstance "github.com/cloudfoundry/bosh-cli/deployment/instance"
bideplmanifest "github.com/cloudfoundry/bosh-cli/deployment/manifest"
bivm "github.com/cloudfoundry/bosh-cli/deployment/vm"
biinstallmanifest "github.com/cloudfoundry/bosh-cli/installation/manifest"
bistemcell "github.com/cloudfoundry/bosh-cli/stemcell"
biui "github.com/cloudfoundry/bosh-cli/ui"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type Deployer interface {
Deploy(
bicloud.Cloud,
bideplmanifest.Manifest,
bistemcell.CloudStemcell,
biinstallmanifest.Registry,
bivm.Manager,
biblobstore.Blobstore,
bool,
biui.Stage,
) (Deployment, error)
}
type deployer struct {
vmManagerFactory bivm.ManagerFactory
instanceManagerFactory biinstance.ManagerFactory
deploymentFactory Factory
logger boshlog.Logger
logTag string
}
func NewDeployer(
vmManagerFactory bivm.ManagerFactory,
instanceManagerFactory biinstance.ManagerFactory,
deploymentFactory Factory,
logger boshlog.Logger,
) Deployer {
return &deployer{
vmManagerFactory: vmManagerFactory,
instanceManagerFactory: instanceManagerFactory,
deploymentFactory: deploymentFactory,
logger: logger,
logTag: "deployer",
}
}
func (d *deployer) Deploy(
cloud bicloud.Cloud,
deploymentManifest bideplmanifest.Manifest,
cloudStemcell bistemcell.CloudStemcell,
registryConfig biinstallmanifest.Registry,
vmManager bivm.Manager,
blobstore biblobstore.Blobstore,
skipDrain bool,
deployStage biui.Stage,
) (Deployment, error) {
instanceManager := d.instanceManagerFactory.NewManager(cloud, vmManager, blobstore)
pingTimeout := 10 * time.Second
pingDelay := 500 * time.Millisecond
if err := instanceManager.DeleteAll(pingTimeout, pingDelay, skipDrain, deployStage); err != nil {
return nil, err
}
instances, disks, err := d.createAllInstances(deploymentManifest, instanceManager, cloudStemcell, registryConfig, deployStage)
if err != nil {
return nil, err
}
stemcells := []bistemcell.CloudStemcell{cloudStemcell}
return d.deploymentFactory.NewDeployment(instances, disks, stemcells), nil
}
func (d *deployer) createAllInstances(
deploymentManifest bideplmanifest.Manifest,
instanceManager biinstance.Manager,
cloudStemcell bistemcell.CloudStemcell,
registryConfig biinstallmanifest.Registry,
deployStage biui.Stage,
) ([]biinstance.Instance, []bidisk.Disk, error) {
instances := []biinstance.Instance{}
disks := []bidisk.Disk{}
if len(deploymentManifest.Jobs) != 1 {
return instances, disks, bosherr.Errorf("There must only be one job, found %d", len(deploymentManifest.Jobs))
}
for _, jobSpec := range deploymentManifest.Jobs {
if jobSpec.Instances != 1 {
return instances, disks, bosherr.Errorf("Job '%s' must have only one instance, found %d", jobSpec.Name, jobSpec.Instances)
}
for instanceID := 0; instanceID < jobSpec.Instances; instanceID++ {
instance, instanceDisks, err := instanceManager.Create(jobSpec.Name, instanceID, deploymentManifest, cloudStemcell, registryConfig, deployStage)
if err != nil {
return instances, disks, bosherr.WrapErrorf(err, "Creating instance '%s/%d'", jobSpec.Name, instanceID)
}
instances = append(instances, instance)
disks = append(disks, instanceDisks...)
err = instance.UpdateJobs(deploymentManifest, deployStage)
if err != nil {
return instances, disks, err
}
}
}
return instances, disks, nil
}