-
Notifications
You must be signed in to change notification settings - Fork 91
/
DeploymentTasks.java
526 lines (467 loc) · 23.3 KB
/
DeploymentTasks.java
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
/*
* Copyright 2022 Red Hat
*
* 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
*
* https://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 org.jboss.hal.client.deployment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Provider;
import org.jboss.hal.ballroom.dialog.Dialog;
import org.jboss.hal.config.Environment;
import org.jboss.hal.core.deployment.Content;
import org.jboss.hal.core.deployment.Deployment;
import org.jboss.hal.core.deployment.ServerGroupDeployment;
import org.jboss.hal.core.finder.FinderColumn;
import org.jboss.hal.core.runtime.TopologyTasks;
import org.jboss.hal.core.runtime.server.Server;
import org.jboss.hal.dmr.Composite;
import org.jboss.hal.dmr.ModelNode;
import org.jboss.hal.dmr.Operation;
import org.jboss.hal.dmr.Property;
import org.jboss.hal.dmr.ResourceAddress;
import org.jboss.hal.dmr.dispatch.Dispatcher;
import org.jboss.hal.flow.FlowContext;
import org.jboss.hal.flow.Progress;
import org.jboss.hal.flow.Task;
import org.jboss.hal.resources.Resources;
import org.jboss.hal.spi.Message;
import org.jboss.hal.spi.MessageEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.web.bindery.event.shared.EventBus;
import elemental2.dom.File;
import elemental2.dom.FileList;
import elemental2.promise.Promise;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
import static org.jboss.elemento.Elements.p;
import static org.jboss.hal.dmr.ModelDescriptionConstants.ADD;
import static org.jboss.hal.dmr.ModelDescriptionConstants.ADDRESS;
import static org.jboss.hal.dmr.ModelDescriptionConstants.CHILD_TYPE;
import static org.jboss.hal.dmr.ModelDescriptionConstants.CONTENT;
import static org.jboss.hal.dmr.ModelDescriptionConstants.DEPLOYMENT;
import static org.jboss.hal.dmr.ModelDescriptionConstants.ENABLED;
import static org.jboss.hal.dmr.ModelDescriptionConstants.FULL_REPLACE_DEPLOYMENT;
import static org.jboss.hal.dmr.ModelDescriptionConstants.INCLUDE_RUNTIME;
import static org.jboss.hal.dmr.ModelDescriptionConstants.INPUT_STREAM_INDEX;
import static org.jboss.hal.dmr.ModelDescriptionConstants.NAME;
import static org.jboss.hal.dmr.ModelDescriptionConstants.READ_CHILDREN_NAMES_OPERATION;
import static org.jboss.hal.dmr.ModelDescriptionConstants.READ_CHILDREN_RESOURCES_OPERATION;
import static org.jboss.hal.dmr.ModelDescriptionConstants.READ_RESOURCE_OPERATION;
import static org.jboss.hal.dmr.ModelDescriptionConstants.RECURSIVE_DEPTH;
import static org.jboss.hal.dmr.ModelDescriptionConstants.RESULT;
import static org.jboss.hal.dmr.ModelDescriptionConstants.RUNTIME_NAME;
import static org.jboss.hal.dmr.ModelDescriptionConstants.SERVER_GROUP;
import static org.jboss.hal.flow.Flow.sequential;
/** Deployment related functions */
class DeploymentTasks {
static final String SERVER_GROUP_DEPLOYMENTS = "deploymentFunctions.serverGroupDeployments";
private static final String UPLOAD_STATISTICS = "deploymentsFunctions.uploadStatistics";
private static final Logger logger = LoggerFactory.getLogger(DeploymentTasks.class);
/**
* Uploads or updates one or multiple deployment in standalone mode resp. content in domain mode. If available deploys it to
* a server group.
*/
static <T> void upload(FinderColumn<T> column, Environment environment, Dispatcher dispatcher,
EventBus eventBus, Provider<Progress> progress, FileList files,
String serverGroup, Resources resources) {
if (files.getLength() > 0) {
boolean hasServerGroup = serverGroup != null;
StringBuilder builder = new StringBuilder();
List<Task<FlowContext>> tasks = new ArrayList<>();
for (int i = 0; i < files.getLength(); i++) {
String filename = files.item(i).name;
builder.append(filename).append(" ");
tasks.add(new CheckDeployment(dispatcher, filename));
tasks.add(new ConfirmReplacement(resources.constants().replaceDeployment(), resources.constants().replace(),
resources.messages().deploymentReplaceConfirmation(filename)));
tasks.add(new UploadOrReplace(environment, dispatcher, filename, filename, files.item(i), !hasServerGroup));
if (hasServerGroup) {
tasks.add(new AddServerGroupDeployment(environment, dispatcher, filename, filename, serverGroup));
}
}
logger.debug("About to upload / update {} file(s): {}", files.getLength(), builder);
sequential(new FlowContext(progress.get()), tasks)
.then(context -> {
UploadStatistics statistics = context.get(UPLOAD_STATISTICS);
if (statistics != null) {
eventBus.fireEvent(new MessageEvent(statistics.getMessage()));
} else {
logger.error("Unable to find upload statistics in the context using key '{}'", UPLOAD_STATISTICS);
}
column.refresh(FinderColumn.RefreshMode.RESTORE_SELECTION);
return null;
})
.catch_(__ -> {
MessageEvent.fire(eventBus,
Message.error(resources.messages().deploymentOpFailed(files.getLength())));
return null;
});
}
}
private DeploymentTasks() {
}
/** Loads the contents form the content repository and pushes a {@code List<Content>} onto the context stack. */
static final class LoadContent implements Task<FlowContext> {
private final Dispatcher dispatcher;
private final String serverGroup;
LoadContent(Dispatcher dispatcher) {
this(dispatcher, "*");
}
/**
* @param dispatcher the dispatcher
* @param serverGroup use "*" to find deployments on any server group
*/
LoadContent(Dispatcher dispatcher, String serverGroup) {
this.dispatcher = dispatcher;
this.serverGroup = serverGroup;
}
@Override
public Promise<FlowContext> apply(final FlowContext context) {
Operation contentOp = new Operation.Builder(ResourceAddress.root(), READ_CHILDREN_RESOURCES_OPERATION)
.param(CHILD_TYPE, DEPLOYMENT)
.build();
ResourceAddress address = new ResourceAddress()
.add(SERVER_GROUP, serverGroup)
.add(DEPLOYMENT, "*");
Operation deploymentsOp = new Operation.Builder(address, READ_RESOURCE_OPERATION)
.param(INCLUDE_RUNTIME, true)
.build();
return dispatcher.execute(new Composite(contentOp, deploymentsOp)).then(result -> {
Map<String, Content> contentByName = new HashMap<>();
List<Property> properties = result.step(0).get(RESULT).asPropertyList();
for (Property property : properties) {
Content content = new Content(property.getValue());
contentByName.put(content.getName(), content);
}
List<ModelNode> nodes = result.step(1).get(RESULT).asList();
for (ModelNode node : nodes) {
ModelNode addressNode = node.get(ADDRESS);
String groupName = addressNode.asList().get(0).get(SERVER_GROUP).asString();
ModelNode deploymentNode = node.get(RESULT);
ServerGroupDeployment serverGroupDeployment = new ServerGroupDeployment(groupName, deploymentNode);
Content content = contentByName.get(serverGroupDeployment.getName());
if (content != null) {
content.addDeployment(serverGroupDeployment);
}
}
return context.resolve(new ArrayList<>(contentByName.values()));
});
}
}
/**
* Reads the deployments of the specified server group. Stores the list in the context under the key
* {@link DeploymentTasks#SERVER_GROUP_DEPLOYMENTS}. Stores an empty list if there are no deployments or if running in
* standalone mode.
*/
static final class ReadServerGroupDeployments implements Task<FlowContext> {
private final Environment environment;
private final Dispatcher dispatcher;
private final String serverGroup;
private final String deployment;
ReadServerGroupDeployments(Environment environment, Dispatcher dispatcher, String serverGroup) {
this(environment, dispatcher, serverGroup, null);
}
ReadServerGroupDeployments(Environment environment, Dispatcher dispatcher, String serverGroup,
String deployment) {
this.environment = environment;
this.dispatcher = dispatcher;
this.serverGroup = serverGroup;
this.deployment = deployment;
}
@Override
public Promise<FlowContext> apply(final FlowContext context) {
if (environment.isStandalone()) {
List<ServerGroupDeployment> serverGroupDeployments = Collections.emptyList();
return context.resolve(SERVER_GROUP_DEPLOYMENTS, serverGroupDeployments);
} else {
if (deployment != null) {
// read a single deployment
ResourceAddress address = new ResourceAddress().add(SERVER_GROUP, serverGroup)
.add(DEPLOYMENT, deployment);
Operation operation = new Operation.Builder(address, READ_RESOURCE_OPERATION)
.param(INCLUDE_RUNTIME, true)
.build();
return dispatcher.execute(operation).then(result -> {
List<ServerGroupDeployment> serverGroupDeployments = Lists
.newArrayList(new ServerGroupDeployment(serverGroup, result));
return context.resolve(SERVER_GROUP_DEPLOYMENTS, serverGroupDeployments);
});
} else {
// read all deployments
ResourceAddress address = new ResourceAddress().add(SERVER_GROUP, serverGroup);
Operation operation = new Operation.Builder(address, READ_CHILDREN_RESOURCES_OPERATION)
.param(CHILD_TYPE, DEPLOYMENT)
.param(INCLUDE_RUNTIME, true)
.build();
return dispatcher.execute(operation).then(result -> {
List<ServerGroupDeployment> serverGroupDeployments = result.asPropertyList().stream()
.map(property -> new ServerGroupDeployment(serverGroup, property.getValue()))
.collect(toList());
return context.resolve(SERVER_GROUP_DEPLOYMENTS, serverGroupDeployments);
});
}
}
}
}
/** Deploys the specified content to the specified server group. The deployment is not enable on the server group. */
static final class AddServerGroupDeployment implements Task<FlowContext> {
private final Environment environment;
private final Dispatcher dispatcher;
private final String name;
private final String runtimeName;
private final String serverGroup;
AddServerGroupDeployment(Environment environment, Dispatcher dispatcher, String name, String runtimeName,
String serverGroup) {
this.environment = environment;
this.dispatcher = dispatcher;
this.name = name;
this.runtimeName = runtimeName;
this.serverGroup = serverGroup;
}
@Override
public Promise<FlowContext> apply(final FlowContext context) {
if (!context.emptyStack() && (Integer) context.pop() != 404) {
return Promise.resolve(context); // deployment was replaced, skip deploy
}
if (environment.isStandalone()) {
List<ServerGroupDeployment> serverGroupDeployments = Collections.emptyList();
context.set(SERVER_GROUP_DEPLOYMENTS, serverGroupDeployments);
return Promise.resolve(context);
} else {
ResourceAddress address = new ResourceAddress()
.add(SERVER_GROUP, serverGroup)
.add(DEPLOYMENT, name);
Operation operation = new Operation.Builder(address, ADD)
.param(RUNTIME_NAME, runtimeName)
.param(ENABLED, true)
.build();
return dispatcher.execute(operation).then(__ -> Promise.resolve(context));
}
}
}
/**
* Loads the deployments of the first running server from the list of running servers in the context under the key
* {@link TopologyTasks#SERVERS}. Expects the list of deployments under the key {@link #SERVER_GROUP_DEPLOYMENTS} in the
* context. Updates all matching deployments with the deployments from the running server.
*/
static final class LoadDeploymentsFromRunningServer implements Task<FlowContext> {
private final Environment environment;
private final Dispatcher dispatcher;
LoadDeploymentsFromRunningServer(Environment environment, Dispatcher dispatcher) {
this.environment = environment;
this.dispatcher = dispatcher;
}
@Override
public Promise<FlowContext> apply(final FlowContext context) {
if (!environment.isStandalone()) {
List<ServerGroupDeployment> serverGroupDeployments = context
.get(DeploymentTasks.SERVER_GROUP_DEPLOYMENTS);
List<Server> runningServers = context.get(TopologyTasks.SERVERS);
if (serverGroupDeployments != null && runningServers != null &&
!serverGroupDeployments.isEmpty() && !runningServers.isEmpty()) {
Server referenceServer = runningServers.get(0);
Operation operation = new Operation.Builder(referenceServer.getServerAddress(),
READ_CHILDREN_RESOURCES_OPERATION)
.param(CHILD_TYPE, DEPLOYMENT)
.param(INCLUDE_RUNTIME, true)
.param(RECURSIVE_DEPTH, 1)
.build();
return dispatcher.execute(operation).then(result -> {
Map<String, Deployment> deploymentsByName = result.asPropertyList().stream()
.map(property -> new Deployment(referenceServer, property.getValue()))
.collect(toMap(Deployment::getName, identity()));
serverGroupDeployments.forEach(sgd -> sgd.setDeployment(deploymentsByName.get(sgd.getName())));
return Promise.resolve(context);
});
} else {
return Promise.resolve(context);
}
} else {
return Promise.resolve(context);
}
}
}
/**
* Checks whether a deployment with the given name exists and pushes {@code 200} to the context stack if it exists,
* {@code 404} otherwise.
*/
static final class CheckDeployment implements Task<FlowContext> {
private final Dispatcher dispatcher;
private final String name;
CheckDeployment(Dispatcher dispatcher, String name) {
this.dispatcher = dispatcher;
this.name = name;
}
@Override
public Promise<FlowContext> apply(final FlowContext context) {
Operation operation = new Operation.Builder(ResourceAddress.root(), READ_CHILDREN_NAMES_OPERATION)
.param(CHILD_TYPE, DEPLOYMENT)
.build();
return dispatcher.execute(operation)
.then(result -> {
Set<String> names = result.asList().stream().map(ModelNode::asString).collect(toSet());
if (names.contains(name)) {
return context.resolve(200);
} else {
return context.resolve(404);
}
});
}
}
/**
* Check if {@code 404} is on the stack, if not asks the user for confirmation, if user confirms {@code 200} is kept on the
* stack otherwise {@code 403} is pushed on the stack.
*/
static final class ConfirmReplacement implements Task<FlowContext> {
private final String title;
private final String okButton;
private final SafeHtml message;
ConfirmReplacement(String title, String okButton, SafeHtml message) {
this.title = title;
this.okButton = okButton;
this.message = message;
}
@Override
public Promise<FlowContext> apply(FlowContext context) {
Integer result = context.peek();
if (result == 404) {
return Promise.resolve(context);
}
return new Promise<>((resolve, reject) -> {
Dialog confirmDialog = new Dialog.Builder(title)
.primary(okButton, () -> {
resolve.onInvoke(context);
return true;
})
.secondary(() -> {
context.pop();
context.push(403);
resolve.onInvoke(context);
return true;
})
.size(Dialog.Size.MEDIUM)
.add(p().innerHtml(message).element())
.build();
confirmDialog.show();
});
}
}
/**
* Creates a new deployment or replaces an existing deployment. The function looks for a status code in the context. If no
* status context or {@code 404} is found, a new deployment is created, if {@code 200} is found the deployment is replaced.
* If {@code 403} is found the task does nothing.
* <p>
* The function puts an {@link UploadStatistics} under the key {@link DeploymentTasks#UPLOAD_STATISTICS} into the context.
*/
static final class UploadOrReplace implements Task<FlowContext> {
private final Environment environment;
private final Dispatcher dispatcher;
private final String name;
private final String runtimeName;
private final File file;
private final boolean enabled;
UploadOrReplace(Environment environment, Dispatcher dispatcher, String name, String runtimeName, File file,
boolean enabled) {
this.environment = environment;
this.dispatcher = dispatcher;
this.name = name;
this.runtimeName = runtimeName;
this.file = file;
this.enabled = enabled;
}
@Override
public Promise<FlowContext> apply(final FlowContext context) {
boolean skip = false;
boolean replace;
Operation.Builder builder;
if (context.emptyStack()) {
replace = false;
} else {
Integer status = context.peek();
replace = status == 200;
skip = status == 403;
}
if (skip) {
UploadStatistics statistics = context.get(UPLOAD_STATISTICS);
if (statistics == null) {
statistics = new UploadStatistics(environment);
context.set(UPLOAD_STATISTICS, statistics);
}
return Promise.resolve(context);
}
if (replace) {
builder = new Operation.Builder(ResourceAddress.root(), FULL_REPLACE_DEPLOYMENT) // NON-NLS
.param(NAME, name)
.param(RUNTIME_NAME, runtimeName);
// leave "enabled" as undefined to indicate that the state of the existing deployment should be retained
} else {
builder = new Operation.Builder(new ResourceAddress().add(DEPLOYMENT, name), ADD)
.param(RUNTIME_NAME, runtimeName)
.param(ENABLED, enabled);
}
Operation operation = builder.build();
operation.get(CONTENT).add().get(INPUT_STREAM_INDEX).set(0); // NON-NLS
return dispatcher.upload(file, operation)
.then(result -> {
UploadStatistics statistics = context.get(UPLOAD_STATISTICS);
if (statistics == null) {
statistics = new UploadStatistics(environment);
context.set(UPLOAD_STATISTICS, statistics);
}
if (ADD.equals(operation.getName())) {
statistics.recordAdded(name);
} else {
statistics.recordReplaced(name);
}
return Promise.resolve(context);
})
.catch_(error -> {
UploadStatistics statistics = context.get(UPLOAD_STATISTICS);
if (statistics == null) {
statistics = new UploadStatistics(environment);
context.set(UPLOAD_STATISTICS, statistics);
}
statistics.recordFailed(name);
return Promise.reject(error);
});
}
}
/** Adds an unmanaged deployment. */
static final class AddUnmanagedDeployment implements Task<FlowContext> {
private final Dispatcher dispatcher;
private final String name;
private final ModelNode payload;
AddUnmanagedDeployment(Dispatcher dispatcher, String name, ModelNode payload) {
this.dispatcher = dispatcher;
this.name = name;
this.payload = payload;
}
@Override
public Promise<FlowContext> apply(final FlowContext context) {
Operation operation = new Operation.Builder(new ResourceAddress().add(DEPLOYMENT, name), ADD)
.payload(payload)
.build();
return dispatcher.execute(operation).then(__ -> Promise.resolve(context));
}
}
}