/
generator.go
241 lines (209 loc) · 9.92 KB
/
generator.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
// Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openstack
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/hashicorp/consul/api"
"github.com/pkg/errors"
"github.com/ystia/yorc/config"
"github.com/ystia/yorc/deployments"
"github.com/ystia/yorc/helper/consulutil"
"github.com/ystia/yorc/log"
"github.com/ystia/yorc/prov/terraform/commons"
"github.com/ystia/yorc/tosca"
)
type osGenerator struct {
}
func (g *osGenerator) getStringFormConsul(kv *api.KV, baseURL, property string) (string, error) {
getResult, _, err := kv.Get(baseURL+"/"+property, nil)
if err != nil {
return "", errors.Errorf("Can't get property %s for node %s: %v", property, baseURL, err)
}
if getResult == nil {
log.Debugf("Can't get property %s for node %s (not found)", property, baseURL)
return "", nil
}
return string(getResult.Value), nil
}
func (g *osGenerator) GenerateTerraformInfraForNode(ctx context.Context, cfg config.Configuration, deploymentID, nodeName, infrastructurePath string) (bool, map[string]string, []string, commons.PostApplyCallback, error) {
log.Debugf("Generating infrastructure for deployment with id %s", deploymentID)
cClient, err := cfg.GetConsulClient()
if err != nil {
return false, nil, nil, nil, err
}
kv := cClient.KV()
nodeKey := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "nodes", nodeName)
instancesKey := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "instances", nodeName)
terraformStateKey := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "terraform-state", nodeName)
infrastructure := commons.Infrastructure{}
log.Debugf("Generating infrastructure for deployment with node %s", nodeName)
// Remote Configuration for Terraform State to store it in the Consul KV store
infrastructure.Terraform = commons.GetBackendConfiguration(terraformStateKey, cfg)
cmdEnv := []string{
fmt.Sprintf("OS_USERNAME=%s", cfg.Infrastructures[infrastructureName].GetString("user_name")),
fmt.Sprintf("OS_PASSWORD=%s", cfg.Infrastructures[infrastructureName].GetString("password")),
fmt.Sprintf("OS_AUTH_URL=%s", cfg.Infrastructures[infrastructureName].GetString("auth_url")),
}
// Management of variables for Terraform
infrastructure.Provider = map[string]interface{}{
"openstack": map[string]interface{}{
"version": cfg.Terraform.OpenStackPluginVersionConstraint,
"tenant_name": cfg.Infrastructures[infrastructureName].GetString("tenant_name"),
"insecure": cfg.Infrastructures[infrastructureName].GetString("insecure"),
"cacert_file": cfg.Infrastructures[infrastructureName].GetString("cacert_file"),
"cert": cfg.Infrastructures[infrastructureName].GetString("cert"),
"key": cfg.Infrastructures[infrastructureName].GetString("key"),
},
"consul": commons.GetConsulProviderfiguration(cfg),
"null": map[string]interface{}{
"version": commons.NullPluginVersionConstraint,
},
}
log.Debugf("inspecting node %s", nodeKey)
nodeType, err := deployments.GetNodeType(kv, deploymentID, nodeName)
if err != nil {
return false, nil, nil, nil, err
}
outputs := make(map[string]string)
instances, err := deployments.GetNodeInstancesIds(kv, deploymentID, nodeName)
if err != nil {
return false, nil, nil, nil, err
}
for instNb, instanceName := range instances {
instanceState, err := deployments.GetInstanceState(kv, deploymentID, nodeName, instanceName)
if err != nil {
return false, nil, nil, nil, err
}
if instanceState == tosca.NodeStateDeleting || instanceState == tosca.NodeStateDeleted {
// Do not generate something for this node instance (will be deleted if exists)
continue
}
switch nodeType {
case "yorc.nodes.openstack.Compute":
err = g.generateOSInstance(ctx, kv, cfg, deploymentID, nodeName, instanceName, &infrastructure, outputs, &cmdEnv)
if err != nil {
return false, nil, nil, nil, err
}
case "yorc.nodes.openstack.BlockStorage":
var bsIds []string
var volumeID string
if volumeID, err = g.getStringFormConsul(kv, nodeKey, "properties/volume_id"); err != nil {
return false, nil, nil, nil, err
} else if volumeID != "" {
log.Debugf("Reusing existing volume with id %q for node %q", volumeID, nodeName)
bsIds = strings.Split(volumeID, ",")
}
var bsVolume BlockStorageVolume
bsVolume, err = g.generateOSBSVolume(kv, cfg, nodeKey, instanceName)
if err != nil {
return false, nil, nil, nil, err
}
if len(bsIds)-1 < instNb {
commons.AddResource(&infrastructure, "openstack_blockstorage_volume_v1", bsVolume.Name, &bsVolume)
consulKey := commons.ConsulKey{Path: path.Join(instancesKey, instanceName, "/attributes/volume_id"), Value: fmt.Sprintf("${openstack_blockstorage_volume_v1.%s.id}", bsVolume.Name)}
consulKeys := commons.ConsulKeys{Keys: []commons.ConsulKey{consulKey}}
commons.AddResource(&infrastructure, "consul_keys", bsVolume.Name, &consulKeys)
} else {
name := cfg.ResourcesPrefix + nodeName + "-" + instanceName
consulKey := commons.ConsulKey{Path: path.Join(instancesKey, instanceName, "/properties/volume_id"), Value: strings.TrimSpace(bsIds[instNb])}
consulKeys := commons.ConsulKeys{Keys: []commons.ConsulKey{consulKey}}
commons.AddResource(&infrastructure, "consul_keys", name, &consulKeys)
}
case "yorc.nodes.openstack.FloatingIP":
var ip IP
ip, err = g.generateFloatingIP(kv, nodeKey, instanceName)
if err != nil {
return false, nil, nil, nil, err
}
var consulKey commons.ConsulKey
if !ip.IsIP {
floatingIP := FloatingIP{Pool: ip.Pool}
commons.AddResource(&infrastructure, "openstack_compute_floatingip_v2", ip.Name, &floatingIP)
consulKey = commons.ConsulKey{Path: path.Join(instancesKey, instanceName, "/capabilities/endpoint/attributes/floating_ip_address"), Value: fmt.Sprintf("${openstack_compute_floatingip_v2.%s.address}", ip.Name)}
} else {
ips := strings.Split(ip.Pool, ",")
// TODO we should change this. instance name should not be considered as an int
var instName int
instName, err = strconv.Atoi(instanceName)
if err != nil {
return false, nil, nil, nil, err
}
if (len(ips) - 1) < instName {
var networkName string
networkName, err = g.getStringFormConsul(kv, nodeKey, "properties/floating_network_name")
if err != nil {
return false, nil, nil, nil, err
} else if networkName == "" {
return false, nil, nil, nil, errors.Errorf("You need to provide enough IP address or a Pool to generate missing IP address")
}
floatingIP := FloatingIP{Pool: networkName}
commons.AddResource(&infrastructure, "openstack_compute_floatingip_v2", ip.Name, &floatingIP)
consulKey = commons.ConsulKey{Path: path.Join(instancesKey, instanceName, "/capabilities/endpoint/attributes/floating_ip_address"), Value: fmt.Sprintf("${openstack_compute_floatingip_v2.%s.address}", ip.Name)}
} else {
// TODO we should change this. instance name should not be considered as an int
instName, err = strconv.Atoi(instanceName)
if err != nil {
return false, nil, nil, nil, err
}
consulKey = commons.ConsulKey{Path: path.Join(instancesKey, instanceName, "/capabilities/endpoint/attributes/floating_ip_address"), Value: ips[instName]}
}
}
consulKeys := commons.ConsulKeys{Keys: []commons.ConsulKey{consulKey}}
commons.AddResource(&infrastructure, "consul_keys", ip.Name, &consulKeys)
case "yorc.nodes.openstack.Network":
var networkID string
networkID, err = g.getStringFormConsul(kv, nodeKey, "properties/network_id")
if err != nil {
return false, nil, nil, nil, err
} else if networkID != "" {
log.Debugf("Reusing existing volume with id %q for node %q", networkID, nodeName)
return false, nil, cmdEnv, nil, nil
}
var network Network
network, err = g.generateNetwork(kv, cfg, nodeKey, deploymentID)
if err != nil {
return false, nil, nil, nil, err
}
var subnet Subnet
subnet, err = g.generateSubnet(kv, cfg, nodeKey, deploymentID, nodeName)
if err != nil {
return false, nil, nil, nil, err
}
commons.AddResource(&infrastructure, "openstack_networking_network_v2", nodeName, &network)
commons.AddResource(&infrastructure, "openstack_networking_subnet_v2", nodeName+"_subnet", &subnet)
consulKey := commons.ConsulKey{Path: nodeKey + "/attributes/network_id", Value: fmt.Sprintf("${openstack_networking_network_v2.%s.id}", nodeName)}
consulKeys := commons.ConsulKeys{Keys: []commons.ConsulKey{consulKey}}
consulKeys.DependsOn = []string{fmt.Sprintf("openstack_networking_subnet_v2.%s_subnet", nodeName)}
commons.AddResource(&infrastructure, "consul_keys", nodeName, &consulKeys)
default:
return false, nil, nil, nil, errors.Errorf("Unsupported node type '%s' for node '%s' in deployment '%s'", nodeType, nodeName, deploymentID)
}
}
jsonInfra, err := json.MarshalIndent(infrastructure, "", " ")
if err != nil {
return false, nil, nil, nil, errors.Wrap(err, "Failed to generate JSON of terraform Infrastructure description")
}
if err = ioutil.WriteFile(filepath.Join(infrastructurePath, "infra.tf.json"), jsonInfra, 0664); err != nil {
return false, nil, nil, nil, errors.Wrapf(err, "Failed to write file %q", filepath.Join(infrastructurePath, "infra.tf.json"))
}
log.Debugf("Infrastructure generated for deployment with id %s", deploymentID)
return true, outputs, cmdEnv, nil, nil
}