This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
dp_builder.go
115 lines (101 loc) · 3.41 KB
/
dp_builder.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
package dpbuilder
import (
"errors"
"fmt"
artifacts "github.com/Microsoft/kunlun/artifacts"
deployments "github.com/Microsoft/kunlun/artifacts/deployments"
yaml "gopkg.in/yaml.v2"
)
type deploymentItem struct {
hostGroup deployments.HostGroup
deployment deployments.Deployment
}
type DeploymentBuilder struct{}
func (dp DeploymentBuilder) Produce(
manifest artifacts.Manifest,
) ([]deployments.HostGroup, []deployments.Deployment, error) {
// generate the deployments
deploymentItems := []deploymentItem{}
for _, item := range manifest.VMGroups {
hostGroup, err := dp.produceHostGroup(item)
if err != nil {
return nil, nil, err
}
deployment, err := dp.generateDeployment(item)
if err != nil {
return nil, nil, err
}
deploymentItems = append(deploymentItems, deploymentItem{
hostGroup: hostGroup,
deployment: deployment,
})
}
// generate the ansible scripts based on the deployments.
hostGroups := []deployments.HostGroup{}
deployments := []deployments.Deployment{}
for _, di := range deploymentItems {
hostGroups = append(hostGroups, di.hostGroup)
deployments = append(deployments, di.deployment)
}
return hostGroups, deployments, nil
}
func (dp DeploymentBuilder) produceHostGroup(vmGroup artifacts.VMGroup) (deployments.HostGroup, error) {
hostGroup := deployments.HostGroup{}
hostGroup.Name = dp.generateHostGroupName(vmGroup)
networkInfos := vmGroup.NetworkInfos
if len(networkInfos) == 0 || len(networkInfos[0].Outputs) == 0 {
return deployments.HostGroup{},
fmt.Errorf("no network info or outputs found in group %s, %d, %d", vmGroup.Name, len(networkInfos), len(networkInfos[0].Outputs))
}
host := deployments.Host{}
host.User = vmGroup.OSProfile.AdminName
if vmGroup.Jumpbox() {
hostGroup.GroupType = artifacts.JumpboxHostGroupType
if vmGroup.Count != 1 {
return deployments.HostGroup{}, errors.New("jumpbox count should be only one")
}
host.Alias = hostGroup.Name
if networkInfos[0].Outputs[0].Host == "" {
host.Host = networkInfos[0].Outputs[0].PublicIP
} else {
host.Host = networkInfos[0].Outputs[0].Host
}
hostGroup.Hosts = append(hostGroup.Hosts, host)
} else {
if vmGroup.Count != len(networkInfos[0].Outputs) {
return deployments.HostGroup{}, errors.New("the outputs number does not match the vm group")
}
firstNetworkInfoOutputs := networkInfos[0].Outputs
for i := 0; i < vmGroup.Count; i++ {
// TODO(andliu) think about a better way to generate the alias.
host.Alias = firstNetworkInfoOutputs[i].IP
host.Host = firstNetworkInfoOutputs[i].IP
hostGroup.Hosts = append(hostGroup.Hosts, host)
}
}
return hostGroup, nil
}
func (dp DeploymentBuilder) generateHostGroupName(vmGroup artifacts.VMGroup) string {
return vmGroup.Name
}
func (dp DeploymentBuilder) generateDeployment(vmGroup artifacts.VMGroup) (deployments.Deployment, error) {
deployment := deployments.Deployment{}
deployment.HostGroupName = dp.generateHostGroupName(vmGroup)
for _, role := range vmGroup.Roles {
deployment.Roles = append(deployment.Roles, artifacts.Role{
Name: role.Name,
BecomeUser: role.BecomeUser,
})
// append the vars
if deployment.Vars == nil {
deployment.Vars = yaml.MapSlice{}
} else {
// TODO merge these together now, but we should think about to separate them because
// the names may conflict.
for _, item := range role.Vars {
deployment.Vars = append(deployment.Vars, item)
}
}
}
return deployment, nil
}