-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution.go
279 lines (235 loc) · 8.87 KB
/
execution.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
// Copyright 2020 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 main
import (
"context"
"fmt"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/lexis-project/yorc-heappe-plugin/job"
"github.com/ystia/yorc/v4/config"
"github.com/ystia/yorc/v4/deployments"
"github.com/ystia/yorc/v4/events"
"github.com/ystia/yorc/v4/locations"
"github.com/ystia/yorc/v4/log"
"github.com/ystia/yorc/v4/prov"
)
const (
heappeInfrastructureType = "heappe"
locationJobMonitoringTimeInterval = "job_monitoring_time_interval"
locationDefaultMonitoringTimeInterval = 5 * time.Second
heappeJobType = "org.lexis.common.heappe.nodes.pub.Job"
heappeUrgentComputingMonitorJob = "org.lexis.common.heappe.nodes.UrgentComputingMonitorJob"
heappeSendDatasetType = "org.lexis.common.heappe.nodes.Dataset"
heappeReceiveDatasetType = "org.lexis.common.heappe.nodes.Results"
heappeWaitFileGetContent = "org.lexis.common.heappe.nodes.WaitFileAndGetContentJob"
)
// Execution is the interface holding functions to execute an operation
type Execution interface {
ResolveExecution(ctx context.Context) error
ExecuteAsync(ctx context.Context) (*prov.Action, time.Duration, error)
Execute(ctx context.Context) error
}
func newExecution(ctx context.Context, cfg config.Configuration, taskID, deploymentID, nodeName string,
operation prov.Operation) (Execution, error) {
consulClient, err := cfg.GetConsulClient()
if err != nil {
return nil, err
}
kv := consulClient.KV()
var exec Execution
isJob, err := deployments.IsNodeDerivedFrom(ctx, deploymentID, nodeName, heappeJobType)
if err != nil {
return exec, err
}
locationMgr, err := locations.GetManager(cfg)
if err != nil {
return exec, err
}
isSkipped, _ := job.IsSkippedJob(ctx, deploymentID, nodeName)
var locationProps config.DynamicMap
if isSkipped {
// take any location of heappe type to get location properties
locationProps, err = locationMgr.GetPropertiesForFirstLocationOfType(heappeInfrastructureType)
if err != nil {
return exec, err
}
} else {
locationProps, err = locationMgr.GetLocationPropertiesForNode(ctx,
deploymentID, nodeName, heappeInfrastructureType)
if err != nil {
return exec, err
}
}
monitoringTimeInterval := locationProps.GetDuration(locationJobMonitoringTimeInterval)
if monitoringTimeInterval <= 0 {
// Default value
monitoringTimeInterval = locationDefaultMonitoringTimeInterval
}
ids, err := deployments.GetNodeInstancesIds(ctx, deploymentID, nodeName)
if err != nil {
return exec, err
}
if len(ids) == 0 {
return exec, errors.Errorf("Found no instance for node %s in deployment %s", nodeName, deploymentID)
}
// Getting an AAI client to check token validity
aaiClient := job.GetAAIClient(deploymentID, locationProps)
accessToken, err := aaiClient.GetAccessToken()
if err != nil {
return nil, err
}
if accessToken == "" {
token, err := deployments.GetStringNodePropertyValue(ctx, deploymentID,
nodeName, "token")
if err != nil {
return exec, err
}
if token == "" {
return exec, errors.Errorf("Found no token node %s in deployment %s", nodeName, deploymentID)
}
valid, err := aaiClient.IsAccessTokenValid(ctx, token)
if err != nil {
return exec, errors.Wrapf(err, "Failed to check validity of token")
}
if !valid {
errorMsg := fmt.Sprintf("Token provided in input for Job %s is not anymore valid", nodeName)
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, deploymentID).Registerf(errorMsg)
return exec, errors.Errorf(errorMsg)
}
// Exchange this token for an access and a refresh token for the orchestrator
accessToken, _, err = aaiClient.ExchangeToken(ctx, token)
if err != nil {
return exec, errors.Wrapf(err, "Failed to exchange token for orchestrator")
}
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelINFO, deploymentID).Registerf(
fmt.Sprintf("Token exchanged for an orchestrator client access/refresh token for node %s", nodeName))
}
// Checking the access token validity
valid, err := aaiClient.IsAccessTokenValid(ctx, accessToken)
if err != nil {
return exec, errors.Wrapf(err, "Failed to check validity of access token")
}
if !valid {
log.Printf("HEAppE plugin requests to refresh token for deployment %s\n", deploymentID)
accessToken, _, err = aaiClient.RefreshToken(ctx)
if err != nil {
return exec, errors.Wrapf(err, "Failed to refresh token for orchestrator")
}
}
// Getting user info
userInfo, err := aaiClient.GetUserInfo(ctx, accessToken)
if err != nil {
accessToken, _, err = aaiClient.RefreshToken(ctx)
if err != nil {
return exec, errors.Wrapf(err, "Failed to refresh token for orchestrator")
}
userInfo, err = aaiClient.GetUserInfo(ctx, accessToken)
}
if err != nil {
return exec, errors.Wrapf(err, "Failed to get user info from access token for node %s", nodeName)
}
if isJob {
strVal, err := deployments.GetStringNodePropertyValue(ctx, deploymentID, nodeName, "monitoringTimeInterval")
if err != nil {
return exec, err
}
if len(strVal) > 0 {
monitoringIntervalInSeconds, err := strconv.Atoi(strVal)
if err != nil {
return exec, err
}
monitoringTimeInterval = time.Duration(monitoringIntervalInSeconds) * time.Second
}
isUrgentComputingJob, err := deployments.IsNodeDerivedFrom(ctx, deploymentID, nodeName, heappeUrgentComputingMonitorJob)
if err != nil {
return exec, err
}
if isUrgentComputingJob {
cancelRemainingJobs, err := deployments.GetBooleanNodeProperty(ctx, deploymentID, nodeName, "cancelRemainingJobs")
if err != nil {
return exec, err
}
exec = &job.UrgentComputingExecution{
KV: kv,
Cfg: cfg,
DeploymentID: deploymentID,
TaskID: taskID,
NodeName: nodeName,
User: userInfo.GetName(),
CancelRemainingJobs: cancelRemainingJobs,
Operation: operation,
MonitoringTimeInterval: monitoringTimeInterval,
}
return exec, exec.ResolveExecution(ctx)
}
// Regular HEAppE job
listFiles, err := deployments.GetBooleanNodeProperty(ctx, deploymentID,
nodeName, "listChangedFilesWhileRunning")
if err != nil {
return exec, err
}
if listFiles {
// Setting a specific monitoring interval to not overload the access
// to Cluster file system
monitoringTimeInterval = 5 * time.Minute
}
exec = &job.Execution{
KV: kv,
Cfg: cfg,
DeploymentID: deploymentID,
TaskID: taskID,
NodeName: nodeName,
User: userInfo.GetName(),
ListFilesWhileRunning: listFiles,
Operation: operation,
MonitoringTimeInterval: monitoringTimeInterval,
}
return exec, exec.ResolveExecution(ctx)
}
isReceiveDataset := false
isWaitFileGetContent := false
isSendDataset, err := deployments.IsNodeDerivedFrom(ctx, deploymentID, nodeName, heappeSendDatasetType)
if err != nil {
return exec, errors.Wrapf(err, "Could not get type for deployment %s node %s", deploymentID, nodeName)
}
if !isSendDataset {
isReceiveDataset, err = deployments.IsNodeDerivedFrom(ctx, deploymentID, nodeName, heappeReceiveDatasetType)
if err != nil {
return exec, errors.Wrapf(err, "Could not get type for deployment %s node %s", deploymentID, nodeName)
}
if !isReceiveDataset {
isWaitFileGetContent, err = deployments.IsNodeDerivedFrom(ctx, deploymentID, nodeName, heappeWaitFileGetContent)
if err != nil {
return exec, errors.Wrapf(err, "Could not get type for deployment %s node %s", deploymentID, nodeName)
}
}
}
if !isSendDataset && !isReceiveDataset && !isWaitFileGetContent {
return exec, errors.Errorf("operation %q supported only for nodes derived from %q, %q, %q or %q",
operation, heappeJobType, heappeSendDatasetType, heappeReceiveDatasetType, heappeWaitFileGetContent)
}
exec = &job.DatasetTransferExecution{
KV: kv,
Cfg: cfg,
DeploymentID: deploymentID,
TaskID: taskID,
NodeName: nodeName,
User: userInfo.GetName(),
Operation: operation,
MonitoringTimeInterval: monitoringTimeInterval,
}
return exec, exec.ResolveExecution(ctx)
}