-
Notifications
You must be signed in to change notification settings - Fork 23
/
compute_instance.go
525 lines (460 loc) · 19.8 KB
/
compute_instance.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// 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 google
import (
"context"
"fmt"
"path"
"strings"
"time"
"github.com/ystia/yorc/log"
"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/helper/pathutil"
"github.com/ystia/yorc/helper/sshutil"
"github.com/ystia/yorc/helper/stringutil"
"github.com/ystia/yorc/prov/terraform/commons"
"golang.org/x/crypto/ssh"
)
func (g *googleGenerator) generateComputeInstance(ctx context.Context, kv *api.KV,
cfg config.Configuration, deploymentID, nodeName, instanceName string, instanceID int,
infrastructure *commons.Infrastructure,
outputs map[string]string, env *[]string, sshAgent *sshutil.SSHAgent) error {
nodeType, err := deployments.GetNodeType(kv, deploymentID, nodeName)
if err != nil {
return err
}
if nodeType != "yorc.nodes.google.Compute" {
return errors.Errorf("Unsupported node type for %q: %s", nodeName, nodeType)
}
instancesPrefix := path.Join(consulutil.DeploymentKVPrefix, deploymentID,
"topology", "instances")
instancesKey := path.Join(instancesPrefix, nodeName)
instance := ComputeInstance{}
// Must be a match of regex '(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)'
instance.Name = strings.ToLower(getResourcesPrefix(cfg, deploymentID) + nodeName + "-" + instanceName)
instance.Name = strings.Replace(instance.Name, "_", "-", -1)
// Getting string parameters
var imageProject, imageFamily, image, serviceAccount string
stringParams := []struct {
pAttr *string
propertyName string
mandatory bool
}{
{&instance.MachineType, "machine_type", true},
{&instance.Zone, "zone", true},
{&imageProject, "image_project", false},
{&imageFamily, "image_family", false},
{&image, "image", false},
{&instance.Description, "description", false},
{&serviceAccount, "service_account", false},
}
for _, stringParam := range stringParams {
if *stringParam.pAttr, err = deployments.GetStringNodeProperty(kv, deploymentID, nodeName,
stringParam.propertyName, stringParam.mandatory); err != nil {
return err
}
}
// Define the boot disk from image settings
var bootImage string
if imageProject != "" {
bootImage = imageProject
if image != "" {
bootImage = bootImage + "/" + image
} else if imageFamily != "" {
bootImage = bootImage + "/" + imageFamily
} else {
// Unexpected image project without a family or image
return errors.Errorf("Exepected an image or family for image project %s on %s", imageProject, nodeName)
}
} else if image != "" {
bootImage = image
} else {
bootImage = imageFamily
}
var bootDisk BootDisk
if bootImage != "" {
bootDisk.InitializeParams = InitializeParams{Image: bootImage}
}
instance.BootDisk = bootDisk
// Network definition
var noAddress bool
if noAddress, err = deployments.GetBooleanNodeProperty(kv, deploymentID, nodeName, "no_address"); err != nil {
return err
}
// Define if a private network access is required
var netInterfaces []NetworkInterface
reqPrivateNetwork, _, err := deployments.HasAnyRequirementFromNodeType(kv, deploymentID, nodeName, "network", "yorc.nodes.google.PrivateNetwork")
if err != nil {
return err
}
// Check for subnet otherwise
if !reqPrivateNetwork {
reqPrivateNetwork, _, err = deployments.HasAnyRequirementFromNodeType(kv, deploymentID, nodeName, "network", "yorc.nodes.google.Subnetwork")
if err != nil {
return err
}
}
if reqPrivateNetwork {
netInterfaces, err = addPrivateNetworkInterfaces(ctx, kv, deploymentID, nodeName)
if err != nil {
return err
}
} else {
// Create a default private network interface
netInterfaces = append(netInterfaces, NetworkInterface{Network: "default"})
}
// Define an external access if there will be an external IP address
if !noAddress {
hasStaticAddressReq, addressNode, err := deployments.HasAnyRequirementCapability(kv, deploymentID, nodeName, "assignment", "yorc.capabilities.Assignable")
if err != nil {
return err
}
var externalAddress string
// External IP address can be static if required
if hasStaticAddressReq {
// Address Lookup
externalAddress, err = attributeLookup(ctx, kv, deploymentID, instanceName, addressNode, "ip_address")
if err != nil {
return err
}
}
// else externalAddress is empty, which means an ephemeral external IP
// address will be assigned to the instance
accessConfig := AccessConfig{NatIP: externalAddress}
netInterfaces[0].AccessConfigs = []AccessConfig{accessConfig}
}
instance.NetworkInterfaces = netInterfaces
// Scheduling definition
var preemptible bool
if preemptible, err = deployments.GetBooleanNodeProperty(kv, deploymentID, nodeName, "preemptible"); err != nil {
return err
}
if preemptible {
instance.Scheduling = Scheduling{Preemptible: true}
}
// Get list of strings parameters
var scopes []string
if scopes, err = deployments.GetStringArrayNodeProperty(kv, deploymentID, nodeName, "scopes"); err != nil {
return err
}
if serviceAccount != "" || len(scopes) > 0 {
// Adding a service account section, where scopes can't be empty
if len(scopes) == 0 {
scopes = []string{"cloud-platform"}
}
configuredAccount := ServiceAccount{serviceAccount, scopes}
instance.ServiceAccounts = []ServiceAccount{configuredAccount}
}
if instance.Tags, err = deployments.GetStringArrayNodeProperty(kv, deploymentID, nodeName, "tags"); err != nil {
return err
}
// Get list of key/value pairs parameters
if instance.Labels, err = deployments.GetKeyValuePairsNodeProperty(kv, deploymentID, nodeName, "labels"); err != nil {
return err
}
if instance.Metadata, err = deployments.GetKeyValuePairsNodeProperty(kv, deploymentID, nodeName, "metadata"); err != nil {
return err
}
// Get connection info (user, private key)
user, privateKey, err := commons.GetConnInfoFromEndpointCredentials(kv, deploymentID, nodeName)
if err != nil {
return err
}
// Add additional Scratch disks
scratchDisks, err := deployments.GetNodePropertyValue(kv, deploymentID, nodeName, "scratch_disks")
if err != nil {
return err
}
if scratchDisks != nil && scratchDisks.RawString() != "" {
list, ok := scratchDisks.Value.([]interface{})
if !ok {
return errors.New("failed to retrieve scratch disk Tosca Value: not expected type")
}
instance.ScratchDisks = make([]ScratchDisk, 0)
for _, n := range list {
v, ok := n.(map[string]interface{})
if !ok {
return errors.New("failed to retrieve scratch disk map: not expected type")
}
for _, val := range v {
i, ok := val.(string)
if !ok {
return errors.New("failed to retrieve scratch disk interface value: not expected type")
}
scratch := ScratchDisk{Interface: i}
instance.ScratchDisks = append(instance.ScratchDisks, scratch)
}
}
}
// Add the compute instance
commons.AddResource(infrastructure, "google_compute_instance", instance.Name, &instance)
// Attach Persistent disks
devices, err := addAttachedDisks(ctx, cfg, kv, deploymentID, nodeName, instanceName, instance.Name, infrastructure, outputs)
if err != nil {
return err
}
// Provide Consul Keys
consulKeys := commons.ConsulKeys{Keys: []commons.ConsulKey{}}
// Define the private IP address using the value exported by Terraform
privateIP := fmt.Sprintf("${google_compute_instance.%s.network_interface.0.address}",
instance.Name)
consulKeyPrivateAddr := commons.ConsulKey{
Path: path.Join(instancesKey, instanceName, "/attributes/private_address"),
Value: privateIP}
consulKeys.Keys = append(consulKeys.Keys, consulKeyPrivateAddr)
// Define the public IP using the value exported by Terraform
// except if it was specified the instance shouldn't have a public address
var accessIP string
if noAddress {
accessIP = privateIP
} else {
accessIP = fmt.Sprintf("${google_compute_instance.%s.network_interface.0.access_config.0.assigned_nat_ip}",
instance.Name)
consulKeyPublicAddr := commons.ConsulKey{
Path: path.Join(instancesKey, instanceName, "/attributes/public_address"),
Value: accessIP}
// For backward compatibility...
consulKeyPublicIPAddr := commons.ConsulKey{
Path: path.Join(instancesKey, instanceName, "/attributes/public_ip_address"),
Value: accessIP}
consulKeys.Keys = append(consulKeys.Keys, consulKeyPublicAddr,
consulKeyPublicIPAddr)
}
// IP ComputeAddress capability
capabilityIPAddr := commons.ConsulKey{
Path: path.Join(instancesKey, instanceName, "/capabilities/endpoint/attributes/ip_address"),
Value: accessIP}
// Default TOSCA Attributes
consulKeyIPAddr := commons.ConsulKey{
Path: path.Join(instancesKey, instanceName, "/attributes/ip_address"),
Value: accessIP}
consulKeys.Keys = append(consulKeys.Keys, consulKeyIPAddr, capabilityIPAddr)
commons.AddResource(infrastructure, "consul_keys", instance.Name, &consulKeys)
// Add Connection check
if err = commons.AddConnectionCheckResource(infrastructure, user, privateKey, accessIP, instance.Name, env); err != nil {
return err
}
// Retrieve devices
if len(devices) > 0 {
// need to use an SSH Agent to make it if allowed by config
if !cfg.DisableSSHAgent && sshAgent == nil {
sshAgent, err = commons.GetSSHAgent(ctx, privateKey)
if err != nil {
return err
}
}
if err = handleDeviceAttributes(cfg, infrastructure, &instance, devices, user, privateKey, accessIP, sshAgent); err != nil {
return err
}
}
return nil
}
func handleDeviceAttributes(cfg config.Configuration, infrastructure *commons.Infrastructure, instance *ComputeInstance, devices []string, user, privateKey, accessIP string, sshAgent *sshutil.SSHAgent) error {
var env map[string]interface{}
// Retrieve devices {
for _, dev := range devices {
devResource := commons.Resource{}
// Remote exec to retrieve the logical device for google device ID and to redirect stdout to file
re := commons.RemoteExec{Inline: []string{fmt.Sprintf("readlink -f /dev/disk/by-id/%s > %s", dev, dev)},
Connection: &commons.Connection{User: user, Host: accessIP, PrivateKey: "${var.private_key}"}}
devResource.Provisioners = make([]map[string]interface{}, 0)
provMap := make(map[string]interface{})
provMap["remote-exec"] = re
devResource.Provisioners = append(devResource.Provisioners, provMap)
devResource.DependsOn = []string{
fmt.Sprintf("null_resource.%s", instance.Name+"-ConnectionCheck"),
fmt.Sprintf("google_compute_attached_disk.%s", dev),
}
commons.AddResource(infrastructure, "null_resource", fmt.Sprintf("%s-GetDevice-%s", instance.Name, dev), &devResource)
// local exec to scp the stdout file locally (use ssh-agent to make it if allowed by config)
var scpCommand string
if !cfg.DisableSSHAgent {
scpCommand = fmt.Sprintf("scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null %s@%s:~/%s %s", user, accessIP, dev, dev)
env = make(map[string]interface{})
env["SSH_AUTH_SOCK"] = sshAgent.Socket
} else {
// check privateKey's a valid path
if is, err := pathutil.IsValidPath(privateKey); err != nil || !is {
// Truncate it if it's a private key
ufo := privateKey
if _, err = ssh.ParsePrivateKey([]byte(privateKey)); err == nil {
ufo = stringutil.Truncate(privateKey, 20)
}
return errors.Errorf("%q is not a valid path", ufo)
}
scpCommand = fmt.Sprintf("scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i %s %s@%s:~/%s %s", privateKey, user, accessIP, dev, dev)
}
loc := commons.LocalExec{
Command: scpCommand,
Environment: env,
}
locMap := make(map[string]interface{})
locMap["local-exec"] = loc
locResource := commons.Resource{}
locResource.Provisioners = append(locResource.Provisioners, locMap)
locResource.DependsOn = []string{fmt.Sprintf("null_resource.%s", fmt.Sprintf("%s-GetDevice-%s", instance.Name, dev))}
commons.AddResource(infrastructure, "null_resource", fmt.Sprintf("%s-CopyOut-%s", instance.Name, dev), &locResource)
// Remote exec to cleanup created file
cleanResource := commons.Resource{}
re = commons.RemoteExec{Inline: []string{fmt.Sprintf("rm -f %s", dev)},
Connection: &commons.Connection{User: user, Host: accessIP, PrivateKey: "${var.private_key}"}}
cleanResource.Provisioners = make([]map[string]interface{}, 0)
m := make(map[string]interface{})
m["remote-exec"] = re
cleanResource.Provisioners = append(devResource.Provisioners, m)
cleanResource.DependsOn = []string{fmt.Sprintf("null_resource.%s", fmt.Sprintf("%s-CopyOut-%s", instance.Name, dev))}
commons.AddResource(infrastructure, "null_resource", fmt.Sprintf("%s-cleanup-%s", instance.Name, dev), &cleanResource)
consulKeys := commons.ConsulKeys{Keys: []commons.ConsulKey{}}
consulKeys.DependsOn = []string{fmt.Sprintf("null_resource.%s", fmt.Sprintf("%s-CopyOut-%s", instance.Name, dev))}
}
return nil
}
func attributeLookup(ctx context.Context, kv *api.KV, deploymentID, instanceName, nodeName, attribute string) (string, error) {
log.Debugf("Attribute:%q lookup for deploymentID:%q, node name:%q, instance:%q", attribute, deploymentID, nodeName, instanceName)
res := make(chan string, 1)
go func() {
for {
if attr, _ := deployments.GetInstanceAttributeValue(kv, deploymentID, nodeName, instanceName, attribute); attr != nil && attr.RawString() != "" {
if attr != nil && attr.RawString() != "" {
res <- attr.RawString()
return
}
}
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
return
}
}
}()
select {
case val := <-res:
return val, nil
case <-ctx.Done():
return "", ctx.Err()
}
}
func addAttachedDisks(ctx context.Context, cfg config.Configuration, kv *api.KV, deploymentID, nodeName, instanceName, computeName string, infrastructure *commons.Infrastructure, outputs map[string]string) ([]string, error) {
devices := make([]string, 0)
storageKeys, err := deployments.GetRequirementsKeysByTypeForNode(kv, deploymentID, nodeName, "local_storage")
if err != nil {
return nil, err
}
for _, storagePrefix := range storageKeys {
requirementIndex := deployments.GetRequirementIndexFromRequirementKey(storagePrefix)
volumeNodeName, err := deployments.GetTargetNodeForRequirement(kv, deploymentID, nodeName, requirementIndex)
if err != nil {
return nil, err
}
log.Debugf("Volume attachment required form Volume named %s", volumeNodeName)
zone, err := deployments.GetStringNodeProperty(kv, deploymentID, volumeNodeName, "zone", true)
if err != nil {
return nil, err
}
modeValue, err := deployments.GetRelationshipPropertyValueFromRequirement(kv, deploymentID, nodeName, requirementIndex, "mode")
if err != nil {
return nil, err
}
volumeIDValue, err := deployments.GetNodePropertyValue(kv, deploymentID, volumeNodeName, "volume_id")
if err != nil {
return nil, err
}
var volumeID string
if volumeIDValue == nil || volumeIDValue.RawString() == "" {
// Lookup for attribute volume_id
volumeID, err = attributeLookup(ctx, kv, deploymentID, instanceName, volumeNodeName, "volume_id")
if err != nil {
return nil, err
}
} else {
volumeID = volumeIDValue.RawString()
}
attachedDisk := &ComputeAttachedDisk{
Disk: volumeID,
Instance: fmt.Sprintf("${google_compute_instance.%s.name}", computeName),
Zone: zone,
}
if modeValue != nil && modeValue.RawString() != "" {
attachedDisk.Mode = modeValue.RawString()
}
attachName := strings.ToLower(getResourcesPrefix(cfg, deploymentID) + volumeNodeName + "-" + instanceName + "-to-" + nodeName + "-" + instanceName)
attachName = strings.Replace(attachName, "_", "-", -1)
// attachName is used as device name to retrieve device attribute as logical volume name
attachedDisk.DeviceName = attachName
// Provide file outputs for device attributes which can't be resolved with Terraform
device := fmt.Sprintf("google-%s", attachName)
commons.AddResource(infrastructure, "google_compute_attached_disk", device, attachedDisk)
outputDeviceVal := commons.FileOutputPrefix + device
instancesPrefix := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "instances")
outputs[path.Join(instancesPrefix, volumeNodeName, instanceName, "attributes/device")] = outputDeviceVal
outputs[path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "relationship_instances", nodeName, requirementIndex, instanceName, "attributes/device")] = outputDeviceVal
outputs[path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "relationship_instances", volumeNodeName, requirementIndex, instanceName, "attributes/device")] = outputDeviceVal
// Add device
devices = append(devices, device)
}
return devices, nil
}
func addPrivateNetworkInterfaces(ctx context.Context, kv *api.KV, deploymentID, nodeName string) ([]NetworkInterface, error) {
var netInterfaces []NetworkInterface
// Check if subnets have been specified by user into network relationship
storageKeys, err := deployments.GetRequirementsKeysByTypeForNode(kv, deploymentID, nodeName, "network")
if err != nil {
return nil, errors.Wrapf(err, "failed to add network interfaces for deploymentID:%q, nodeName:%q", deploymentID, nodeName)
}
for _, storagePrefix := range storageKeys {
requirementIndex := deployments.GetRequirementIndexFromRequirementKey(storagePrefix)
networkNodeName, err := deployments.GetTargetNodeForRequirement(kv, deploymentID, nodeName, requirementIndex)
if err != nil {
return nil, errors.Wrapf(err, "failed to add network interfaces for deploymentID:%q, nodeName:%q", deploymentID, nodeName)
}
// Check if node is network or subnet
netType, err := deployments.GetNodeType(kv, deploymentID, networkNodeName)
if err != nil {
return nil, errors.Wrapf(err, "failed to add network interfaces for deploymentID:%q, nodeName:%q", deploymentID, nodeName)
}
switch netType {
case "yorc.nodes.google.Subnetwork":
subnet, err := attributeLookup(ctx, kv, deploymentID, "0", networkNodeName, "subnetwork_name")
if err != nil {
return nil, errors.Wrapf(err, "failed to add network interfaces for deploymentID:%q, nodeName:%q, networkName:%q", deploymentID, nodeName, networkNodeName)
}
log.Debugf("add network interface with sub-network property:%s", subnet)
netInterfaces = append(netInterfaces, NetworkInterface{Subnetwork: subnet})
case "yorc.nodes.google.PrivateNetwork":
// We mention subnet if provided by network relationship property
subRaw, err := deployments.GetRelationshipPropertyValueFromRequirement(kv, deploymentID, nodeName, requirementIndex, "subnet")
if err != nil {
return nil, err
}
if subRaw != nil && subRaw.RawString() != "" {
log.Debugf("add network interface with user-specified sub-network property:%s", subRaw.RawString())
netInterfaces = append(netInterfaces, NetworkInterface{Subnetwork: subRaw.RawString()})
} else { // we mention the network
network, err := attributeLookup(ctx, kv, deploymentID, "0", networkNodeName, "network_name")
if err != nil {
return nil, errors.Wrapf(err, "failed to add network interfaces for deploymentID:%q, nodeName:%q, networkName:%q", deploymentID, nodeName, networkNodeName)
}
log.Debugf("add network interface with network property:%s", network)
netInterfaces = append(netInterfaces, NetworkInterface{Network: network})
}
default:
return nil, errors.Errorf("type:%q is not handled for compute network interface addition", netType)
}
}
return netInterfaces, nil
}