-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdefinition_store.go
696 lines (608 loc) · 23 KB
/
definition_store.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// 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 deployments
import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/ystia/yorc/v4/deployments/store"
"github.com/ystia/yorc/v4/events"
"github.com/ystia/yorc/v4/helper/consulutil"
"github.com/ystia/yorc/v4/log"
"github.com/ystia/yorc/v4/storage"
storageTypes "github.com/ystia/yorc/v4/storage/types"
"github.com/ystia/yorc/v4/tosca"
)
const blockingOperationOnDeploymentFlagName = ".blockingOp"
// AddBlockingOperationOnDeploymentFlag set a flag on a given deployment to specify that an operation is ongoing and no other tasks should be run on this deployment
func AddBlockingOperationOnDeploymentFlag(ctx context.Context, deploymentID string) error {
return consulutil.StoreConsulKey(path.Join(consulutil.DeploymentKVPrefix, deploymentID, blockingOperationOnDeploymentFlagName), nil)
}
// RemoveBlockingOperationOnDeploymentFlag removes a flag on a given deployment to specify that an operation is ongoing and no other tasks should be run on this deployment
func RemoveBlockingOperationOnDeploymentFlag(ctx context.Context, deploymentID string) error {
_, err := consulutil.GetKV().Delete(path.Join(consulutil.DeploymentKVPrefix, deploymentID, blockingOperationOnDeploymentFlagName), nil)
return errors.Wrap(err, consulutil.ConsulGenericErrMsg)
}
// HasBlockingOperationOnDeploymentFlag checks if there is a flag on a given deployment to specify that an operation is ongoing and no other tasks should be run on this deployment
func HasBlockingOperationOnDeploymentFlag(ctx context.Context, deploymentID string) (bool, error) {
kvp, _, err := consulutil.GetKV().Get(path.Join(consulutil.DeploymentKVPrefix, deploymentID, blockingOperationOnDeploymentFlagName), nil)
return kvp != nil, errors.Wrap(err, consulutil.ConsulGenericErrMsg)
}
// StoreDeploymentDefinition takes a defPath and parse it as a tosca.Topology then it store it in consul under
// consulutil.DeploymentKVPrefix/deploymentID
func StoreDeploymentDefinition(ctx context.Context, deploymentID string, defPath string) error {
if err := SetDeploymentStatus(ctx, deploymentID, INITIAL); err != nil {
return handleDeploymentStatus(ctx, deploymentID, err)
}
topology := tosca.Topology{}
definition, err := os.Open(defPath)
if err != nil {
return handleDeploymentStatus(ctx, deploymentID, errors.Wrapf(err, "Failed to open definition file %q", defPath))
}
defBytes, err := ioutil.ReadAll(definition)
if err != nil {
return handleDeploymentStatus(ctx, deploymentID, errors.Wrapf(err, "Failed to open definition file %q", defPath))
}
err = yaml.Unmarshal(defBytes, &topology)
if err != nil {
return handleDeploymentStatus(ctx, deploymentID, errors.Wrapf(err, "Failed to unmarshal yaml definition for file %q", defPath))
}
consulutil.StoreConsulKeyAsString(path.Join(consulutil.DeploymentKVPrefix, deploymentID, "status"), fmt.Sprint(INITIAL))
err = store.Deployment(ctx, topology, deploymentID, filepath.Dir(defPath))
if err != nil {
return handleDeploymentStatus(ctx, deploymentID, errors.Wrapf(err, "Failed to store TOSCA Definition for deployment with id %q, (file path %q)", deploymentID, defPath))
}
// Post storage process
nodes, err := GetNodes(ctx, deploymentID)
if err != nil {
return err
}
err = PostDeploymentDefinitionStorageProcess(ctx, deploymentID, nodes)
if err != nil {
return handleDeploymentStatus(ctx, deploymentID, err)
}
return handleDeploymentStatus(ctx, deploymentID, enhanceTopology(ctx, deploymentID, nodes))
}
// PostDeploymentDefinitionStorageProcess allows to execute Post deployment storage process
// It concerns deployment store only (not instances) and can be run separately (upgrade)
func PostDeploymentDefinitionStorageProcess(ctx context.Context, deploymentID string, nodes []string) error {
// Workflow enhancement
err := enhanceWorkflows(ctx, deploymentID)
if err != nil {
return err
}
// Implementation types registration
err = registerImplementationTypes(ctx, deploymentID)
if err != nil {
return handleDeploymentStatus(ctx, deploymentID, err)
}
// Topology enhancement
for _, nodeName := range nodes {
err = fixGetOperationOutput(ctx, deploymentID, nodeName)
if err != nil {
return err
}
substitutable, err := isSubstitutableNode(ctx, deploymentID, nodeName)
if err != nil {
return err
}
if !substitutable {
err = fixAlienBlockStorages(ctx, deploymentID, nodeName)
if err != nil {
return err
}
}
}
return nil
}
func handleDeploymentStatus(ctx context.Context, deploymentID string, err error) error {
if err != nil {
SetDeploymentStatus(ctx, deploymentID, DEPLOYMENT_FAILED)
}
return err
}
// createInstancesForNode checks if the given node is hosted on a Scalable node, stores the number of required instances and sets the instance's status to INITIAL
func createInstancesForNode(ctx context.Context, consulStore consulutil.ConsulStore, deploymentID, nodeName string) error {
nbInstances, err := GetDefaultNbInstancesForNode(ctx, deploymentID, nodeName)
if err != nil {
return err
}
createNodeInstances(consulStore, nbInstances, deploymentID, nodeName)
// Check for FIPConnectivity capabilities
is, capabilityNodeName, err := HasAnyRequirementCapability(ctx, deploymentID, nodeName, "network", "yorc.capabilities.openstack.FIPConnectivity")
if err != nil {
return err
}
if is {
createNodeInstances(consulStore, nbInstances, deploymentID, capabilityNodeName)
}
// Check for Assignable capabilities
is, capabilityNodeName, err = HasAnyRequirementCapability(ctx, deploymentID, nodeName, "assignment", "yorc.capabilities.Assignable")
if err != nil {
return err
}
if is {
createNodeInstances(consulStore, nbInstances, deploymentID, capabilityNodeName)
}
bs, bsNames, err := checkBlockStorage(ctx, deploymentID, nodeName)
if err != nil {
return err
}
if bs {
for _, name := range bsNames {
createNodeInstances(consulStore, nbInstances, deploymentID, name)
}
}
return nil
}
func registerImplementationTypes(ctx context.Context, deploymentID string) error {
// We use synchronous communication with consul here to allow to check for duplicates
types, err := GetTypesNames(ctx, deploymentID)
if err != nil {
return err
}
extensionsMap := make(map[string]string)
for _, t := range types {
isImpl, err := IsTypeDerivedFrom(ctx, deploymentID, t, "tosca.artifacts.Implementation")
if err != nil {
if IsTypeMissingError(err) {
// Bypassing this error it may happen in case of an used type let's trust Alien
events.SimpleLogEntry(ctx, events.LogLevelWARN, deploymentID).RegisterAsString(fmt.Sprintf("[WARNING] %s", err))
continue
}
return err
}
if isImpl {
extensions, err := GetArtifactTypeExtensions(ctx, deploymentID, t)
if err != nil {
return err
}
for _, ext := range extensions {
ext = strings.ToLower(ext)
extensionsMap[ext] = t
}
}
}
if len(extensionsMap) > 0 {
return storage.GetStore(storageTypes.StoreTypeDeployment).Set(ctx, path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", implementationArtifactsExtensionsPath), extensionsMap)
}
return nil
}
// enhanceTopology walk through the provided nodes an for each of them if needed it creates the instances
func enhanceTopology(ctx context.Context, deploymentID string, nodes []string) error {
ctxStore, errGroup, consulStore := consulutil.WithContext(ctx)
computes := make([]string, 0)
for _, nodeName := range nodes {
isCompute, err := checkForInstancesCreation(ctxStore, consulStore, deploymentID, nodeName)
if err != nil {
return err
}
if isCompute {
computes = append(computes, nodeName)
}
}
err := createMissingBlockStorageForNodes(ctx, consulStore, deploymentID, computes)
if err != nil {
return err
}
err = errGroup.Wait()
if err != nil {
return err
}
_, errGroup, consulStore = consulutil.WithContext(ctx)
for _, nodeName := range nodes {
err = createRelationshipInstances(ctx, consulStore, deploymentID, nodeName)
if err != nil {
return err
}
}
err = enhanceAttributes(ctx, deploymentID, nodes)
if err != nil {
return err
}
return errGroup.Wait()
}
func checkForInstancesCreation(ctx context.Context, consulStore consulutil.ConsulStore, deploymentID string, nodeName string) (bool, error) {
var isCompute bool
substitutable, err := isSubstitutableNode(ctx, deploymentID, nodeName)
if err != nil {
return isCompute, err
}
if !substitutable {
err = createInstancesForNode(ctx, consulStore, deploymentID, nodeName)
if err != nil {
return isCompute, err
}
isCompute, err = IsNodeDerivedFrom(ctx, deploymentID, nodeName, "tosca.nodes.Compute")
}
return isCompute, err
}
func fixGetOperationOutput(ctx context.Context, deploymentID, nodeName string) error {
// Check operation outputs on node template
node, err := getNodeTemplate(ctx, deploymentID, nodeName)
if err != nil {
return err
}
nodeTypeName, err := GetNodeType(ctx, deploymentID, nodeName)
if err != nil {
return err
}
// Check input value assignments
for _, interfaceDef := range node.Interfaces {
for _, operationDef := range interfaceDef.Operations {
for _, inputDef := range operationDef.Inputs {
err := lookForOperationOutputInVA(ctx, deploymentID, nodeName, nodeTypeName, "", inputDef.ValueAssign)
if err != nil {
return err
}
}
}
}
// Check requirements
for reqIndex, reqAssMap := range node.Requirements {
for _, reqAss := range reqAssMap {
err = fixGetOperationOutputForRelationship(ctx, deploymentID, nodeName, reqAss.Relationship, reqIndex)
if err != nil {
return err
}
}
}
return fixGetOperationOutputOnType(ctx, deploymentID, nodeName, nodeTypeName)
}
func fixGetOperationOutputOnType(ctx context.Context, deploymentID, nodeName, nodeTypeName string) error {
nodeType := new(tosca.NodeType)
err := getExpectedTypeFromName(ctx, deploymentID, nodeTypeName, nodeType)
if err != nil {
return err
}
// Check attributes definitions
for _, attributeDef := range nodeType.Attributes {
err := lookForOperationOutputInVA(ctx, deploymentID, nodeName, nodeTypeName, "", attributeDef.Default)
if err != nil {
return err
}
}
// Check input value assignments
for _, interfaceDef := range nodeType.Interfaces {
for _, operationDef := range interfaceDef.Operations {
for _, inputDef := range operationDef.Inputs {
err := lookForOperationOutputInVA(ctx, deploymentID, nodeName, nodeTypeName, "", inputDef.ValueAssign)
if err != nil {
return err
}
}
}
}
// Check requirements
for reqIndex, reqDefMap := range nodeType.Requirements {
for _, reqDef := range reqDefMap {
err = fixGetOperationOutputForRelationship(ctx, deploymentID, nodeName, reqDef.Relationship, reqIndex)
if err != nil {
return err
}
}
}
// descend all type hierarchy to add operation outputs
if nodeType.DerivedFrom != "" {
return fixGetOperationOutputOnType(ctx, deploymentID, nodeName, nodeType.DerivedFrom)
}
return nil
}
func fixGetOperationOutputForRelationship(ctx context.Context, deploymentID, nodeName, relationshipTypeName string, reqIndex int) error {
ind := strconv.Itoa(reqIndex)
// descend all relationship type hierarchy to add operation outputs
for relationshipTypeName != "" {
rType := new(tosca.RelationshipType)
err := getExpectedTypeFromName(ctx, deploymentID, relationshipTypeName, rType)
if err != nil {
return err
}
// Check attributes definitions
for _, attributeDef := range rType.Attributes {
err := lookForOperationOutputInVA(ctx, deploymentID, nodeName, relationshipTypeName, ind, attributeDef.Default)
if err != nil {
return err
}
}
// Check input value assignments
for _, interfaceDef := range rType.Interfaces {
// Check global inputs
for _, inputDef := range interfaceDef.Inputs {
err := lookForOperationOutputInVA(ctx, deploymentID, nodeName, relationshipTypeName, ind, inputDef.ValueAssign)
if err != nil {
return err
}
}
for _, operationDef := range interfaceDef.Operations {
for _, inputDef := range operationDef.Inputs {
err := lookForOperationOutputInVA(ctx, deploymentID, nodeName, relationshipTypeName, ind, inputDef.ValueAssign)
if err != nil {
return err
}
}
}
}
relationshipTypeName = rType.DerivedFrom
}
return nil
}
func lookForOperationOutputInVA(ctx context.Context, deploymentID, nodeName, typeName, reqIndex string, va *tosca.ValueAssignment) error {
if va == nil || va.Type != tosca.ValueAssignmentFunction || va.GetFunction() == nil {
return nil
}
opOutputFuncs := va.GetFunction().GetFunctionsByOperator(tosca.GetOperationOutputOperator)
for _, oof := range opOutputFuncs {
if len(oof.Operands) != 4 {
return errors.Errorf("Invalid %q TOSCA function: %v", tosca.GetOperationOutputOperator, oof)
}
entityName := oof.Operands[0].String()
interfaceName := oof.Operands[1].String()
operationName := oof.Operands[2].String()
outputName := oof.Operands[3].String()
outputVA := &tosca.ValueAssignment{
Type: tosca.ValueAssignmentFunction,
Value: oof.String()}
switch entityName {
case "SELF":
return storeOperationOutputVA(ctx, deploymentID, nodeName, typeName, interfaceName, operationName, outputName, outputVA)
case "HOST":
hostedOn, err := GetHostedOnNode(ctx, deploymentID, nodeName)
if err != nil || hostedOn == "" {
return errors.Wrapf(err, "Fail to get the hostedOn to fix the output for deploymentID:%q, node name:%q", deploymentID, nodeName)
}
hostedNodeType, err := GetNodeType(ctx, deploymentID, hostedOn)
if err != nil {
return err
}
return storeOperationOutputVA(ctx, deploymentID, nodeName, hostedNodeType, interfaceName, operationName, outputName, outputVA)
case "SOURCE":
nodeType, err := GetNodeType(ctx, deploymentID, nodeName)
if err != nil {
return err
}
return storeOperationOutputVA(ctx, deploymentID, nodeName, nodeType, interfaceName, operationName, outputName, outputVA)
case "TARGET":
if reqIndex == "" {
return errors.Errorf("missing requirement index for adding operation output on type:%q, deployment:%q", typeName, deploymentID)
}
targetNodeName, err := GetTargetNodeForRequirement(ctx, deploymentID, nodeName, reqIndex)
if err != nil || targetNodeName == "" {
return errors.Wrapf(err, "failed to retrieve target node for deployment:%q, node:%q, requirement index:%q", deploymentID, nodeName, reqIndex)
}
targetNodeTypeName, err := GetNodeType(ctx, deploymentID, targetNodeName)
if err != nil {
return err
}
return storeOperationOutputVA(ctx, deploymentID, nodeName, targetNodeTypeName, interfaceName, operationName, outputName, outputVA)
default:
log.Printf("[WARNING] The entity name:%q for operation output on node/relationship is not handled for type:%q", entityName, typeName)
}
}
return nil
}
func checkIfOperationExists(interfaces map[string]tosca.InterfaceDefinition, interfaceName, operationName string) bool {
if interfaces == nil {
return false
}
i, exist := interfaces[interfaceName]
if !exist {
return false
}
if i.Operations == nil {
return false
}
_, exist = i.Operations[operationName]
if !exist {
return false
}
return true
}
func storeOperationOutputVA(ctx context.Context, deploymentID, nodeName, typeName, interfaceName, operationName, outputName string, outputVA *tosca.ValueAssignment) error {
// Check first if the operation is implemented in the node template
isNodeImplOpe, err := IsNodeTemplateImplementingOperation(ctx, deploymentID, nodeName, operationName)
if err != nil {
return nil
}
if isNodeImplOpe {
nodeTemplate, err := getNodeTemplate(ctx, deploymentID, nodeName)
if err != nil {
return err
}
err = addOperationOutputVAToInterfaces(interfaceName, operationName, outputName, nodeName, nodeTemplate.Interfaces, outputVA)
if err != nil {
return err
}
nodePath := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology/nodes", nodeName)
return storage.GetStore(storageTypes.StoreTypeDeployment).Set(ctx, nodePath, nodeTemplate)
}
return storeOperationOutputVAOnType(ctx, deploymentID, typeName, interfaceName, operationName, outputName, outputVA)
}
func addOperationOutputVAToInterfaces(interfaceName, operationName, outputName, nodeOrTypeName string, interfaces map[string]tosca.InterfaceDefinition, outputVA *tosca.ValueAssignment) error {
if !checkIfOperationExists(interfaces, interfaceName, operationName) {
log.Printf("{WARNING] interface (%s) - operation (%s) not found for node/type:%q", interfaceName, operationName, nodeOrTypeName)
return nil
}
op := interfaces[interfaceName].Operations[operationName]
if op.Outputs == nil {
op.Outputs = make(map[string]tosca.Output)
}
output, is := op.Outputs[outputName]
if !is {
output = tosca.Output{}
}
output.ValueAssign = outputVA
op.Outputs[outputName] = output
interfaces[interfaceName].Operations[operationName] = op
return nil
}
func storeOperationOutputVAOnType(ctx context.Context, deploymentID, typeName, interfaceName, operationName, outputName string, outputVA *tosca.ValueAssignment) error {
// Retrieve the type in hierarchy which implements the operation
typeNameImpl, err := GetTypeImplementingAnOperation(ctx, deploymentID, typeName, fmt.Sprintf("%s.%s", interfaceName, operationName))
if err != nil {
if IsOperationNotImplemented(err) {
log.Printf("[WARNING] failed to store output with name:%q for operation:%q, interface:%q, type:%q, deploymentID:%q, due to error:%v", outputName, operationName, interfaceName, typeName, deploymentID, err)
return nil
}
return err
}
tType, typePath, err := getTypeFromName(ctx, deploymentID, typeName)
if err != nil {
return err
}
var tTypeInterfaces map[string]tosca.InterfaceDefinition
switch t := tType.(type) {
case *tosca.NodeType:
tTypeInterfaces = t.Interfaces
case *tosca.RelationshipType:
tTypeInterfaces = t.Interfaces
}
err = addOperationOutputVAToInterfaces(interfaceName, operationName, outputName, typeNameImpl, tTypeInterfaces, outputVA)
if err != nil {
return err
}
return storage.GetStore(storageTypes.StoreTypeDeployment).Set(ctx, typePath, tType)
}
// fixAlienBlockStorages rewrites the relationship between a BlockStorage and a Compute to match the TOSCA specification
func fixAlienBlockStorages(ctx context.Context, deploymentID, nodeName string) error {
isBS, err := IsNodeDerivedFrom(ctx, deploymentID, nodeName, "tosca.nodes.BlockStorage")
if err != nil {
return err
}
if isBS {
attachReqs, err := GetRequirementsByTypeForNode(ctx, deploymentID, nodeName, "attachment")
if err != nil {
return err
}
for _, attachReq := range attachReqs {
req := attachReq.RequirementAssignment
// Reverse the target node
computeNodeName := req.Node
req.Node = nodeName
device, err := GetNodePropertyValue(ctx, deploymentID, nodeName, "device")
if err != nil {
return errors.Wrapf(err, "Failed to fix Alien-specific BlockStorage %q", nodeName)
}
if device != nil {
va := &tosca.ValueAssignment{}
if device.RawString() != "" {
err = yaml.Unmarshal([]byte(device.RawString()), &va)
if err != nil {
return errors.Wrapf(err, "Failed to fix Alien-specific BlockStorage %q, failed to parse device property", nodeName)
}
}
// Add device requirement property
if req.RelationshipProps == nil {
req.RelationshipProps = make(map[string]*tosca.ValueAssignment)
}
req.RelationshipProps["device"] = va
}
// Update the compute node with new requirement
node, err := getNodeTemplate(ctx, deploymentID, computeNodeName)
if err != nil {
return err
}
reqMap := make(map[string]tosca.RequirementAssignment)
reqMap["local_storage"] = req
node.Requirements = append(node.Requirements, reqMap)
nodePrefix := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "nodes", computeNodeName)
return storage.GetStore(storageTypes.StoreTypeDeployment).Set(ctx, nodePrefix, node)
}
}
return nil
}
/*
*
This function create a given number of floating IP instances
*/
func createNodeInstances(consulStore consulutil.ConsulStore, numberInstances uint32, deploymentID, nodeName string) {
for i := uint32(0); i < numberInstances; i++ {
instanceName := strconv.FormatUint(uint64(i), 10)
createNodeInstance(consulStore, deploymentID, nodeName, instanceName)
}
}
// createInstancesForNodes checks if the given nodes are hosted on a Scalable node,
// stores the number of required instances and sets the instance's status to INITIAL
func createMissingBlockStorageForNodes(ctx context.Context, consulStore consulutil.ConsulStore, deploymentID string, nodeNames []string) error {
for _, nodeName := range nodeNames {
requirements, err := GetRequirementsByTypeForNode(ctx, deploymentID, nodeName, "local_storage")
if err != nil {
return err
}
nbInstances, err := GetNbInstancesForNode(ctx, deploymentID, nodeName)
if err != nil {
return err
}
var bsName []string
for _, requirement := range requirements {
if requirement.Capability != "" {
bsName = append(bsName, requirement.Node)
}
}
for _, name := range bsName {
createNodeInstances(consulStore, nbInstances, deploymentID, name)
}
}
return nil
}
/*
*
This function check if a nodes need a block storage, and return the name of BlockStorage node.
*/
func checkBlockStorage(ctx context.Context, deploymentID, nodeName string) (bool, []string, error) {
requirements, err := GetRequirementsByTypeForNode(ctx, deploymentID, nodeName, "local_storage")
if err != nil {
return false, nil, err
}
var bsName []string
for _, requirement := range requirements {
if requirement.Capability != "" {
bsName = append(bsName, requirement.Node)
}
}
return true, bsName, nil
}
// enhanceAttributes walk through the topology nodes an for each of them if needed it creates instances attributes notifications
// to allow resolving any attribute when one is updated
func enhanceAttributes(ctx context.Context, deploymentID string, nodes []string) error {
for _, nodeName := range nodes {
// retrieve all node attributes
attributes, err := GetNodeAttributesNames(ctx, deploymentID, nodeName)
if err != nil {
return err
}
// retrieve all node instances
instances, err := GetNodeInstancesIds(ctx, deploymentID, nodeName)
if err != nil {
return err
}
// 1. Add attribute notifications
// 2. Resolve attributes and publish default values when not nil or empty
for _, instanceName := range instances {
for _, attribute := range attributes {
err := addAttributeNotifications(ctx, deploymentID, nodeName, instanceName, attribute)
if err != nil {
return err
}
}
}
}
return nil
}