/
LocalAppDeployer.java
526 lines (459 loc) · 18.2 KB
/
LocalAppDeployer.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 2016-2022 the original author or authors.
*
* 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.springframework.cloud.deployer.spi.local;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.deployer.spi.app.AppDeployer;
import org.springframework.cloud.deployer.spi.app.AppInstanceStatus;
import org.springframework.cloud.deployer.spi.app.AppScaleRequest;
import org.springframework.cloud.deployer.spi.app.AppStatus;
import org.springframework.cloud.deployer.spi.app.DeploymentState;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.cloud.deployer.spi.core.RuntimeEnvironmentInfo;
import org.springframework.cloud.deployer.spi.local.LocalDeployerProperties.HttpProbe;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
/**
* An {@link AppDeployer} implementation that spins off a new JVM process per app
* instance.
*
* @author Eric Bottard
* @author Marius Bogoevici
* @author Mark Fisher
* @author Ilayaperumal Gopinathan
* @author Janne Valkealahti
* @author Patrick Peralta
* @author Thomas Risberg
* @author Oleg Zhurakousky
* @author Michael Minella
* @author Glenn Renfro
* @author Christian Tzolov
* @author David Turanski
*/
public class LocalAppDeployer extends AbstractLocalDeployerSupport implements AppDeployer {
private static final Logger logger = LoggerFactory.getLogger(LocalAppDeployer.class);
private static final String JMX_DEFAULT_DOMAIN_KEY = "spring.jmx.default-domain";
private static final String ENDPOINTS_SHUTDOWN_ENABLED_KEY = "endpoints.shutdown.enabled";
private final Map<String, AppInstancesHolder> running = new ConcurrentHashMap<>();
/**
* Instantiates a new local app deployer.
*
* @param properties the properties
*/
public LocalAppDeployer(LocalDeployerProperties properties) {
super(properties);
}
/**
* Returns the process exit value. We explicitly use Integer instead of int to indicate
* that if {@code NULL} is returned, the process is still running.
* @param process the process
* @return the process exit value or {@code NULL} if process is still alive
*/
private static Integer getProcessExitValue(Process process) {
try {
return process.exitValue();
}
catch (IllegalThreadStateException e) {
// process is still alive
return null;
}
}
/**
* Gets the local process pid if available. This should be a safe workaround for unix
* systems where reflection can be used to get pid. More reliable way should land with
* jdk9.
*
* @param p the process
* @return the local process pid
*/
private static synchronized int getLocalProcessPid(Process p) {
int pid = 0;
try {
if (p.getClass().getName().equals("java.lang.UNIXProcess")) {
Field f = p.getClass().getDeclaredField("pid");
f.setAccessible(true);
pid = f.getInt(p);
f.setAccessible(false);
}
}
catch (Exception e) {
pid = 0;
}
return pid;
}
@Override
public String deploy(AppDeploymentRequest request) {
String group = request.getDeploymentProperties().get(GROUP_PROPERTY_KEY);
String deploymentId = String.format("%s.%s", group, request.getDefinition().getName());
validateStatus(deploymentId, DeploymentState.unknown);
List<AppInstance> processes = new ArrayList<>();
running.put(deploymentId, new AppInstancesHolder(processes, request));
try {
Path workDir = createWorkingDir(request.getDeploymentProperties(), deploymentId);
String countProperty = request.getDeploymentProperties().get(COUNT_PROPERTY_KEY);
int count = (StringUtils.hasText(countProperty)) ? Integer.parseInt(countProperty) : 1;
for (int index = 0; index < count; index++) {
processes.add(deployApp(request, workDir, group, deploymentId, index, request.getDeploymentProperties()));
}
}
catch (IOException e) {
throw new RuntimeException("Exception trying to deploy " + request, e);
}
return deploymentId;
}
@Override
public void scale(AppScaleRequest appScaleRequest) {
validateStatus(appScaleRequest.getDeploymentId(), DeploymentState.deployed);
AppInstancesHolder holder = running.get(appScaleRequest.getDeploymentId());
List<AppInstance> instances = holder != null ? holder.instances : null;
if (instances == null) {
throw new IllegalStateException(
"Can't find existing instances for deploymentId " + appScaleRequest.getDeploymentId());
}
AppDeploymentRequest request = holder.request;
String group = request.getDeploymentProperties().get(GROUP_PROPERTY_KEY);
String deploymentId = String.format("%s.%s", group, request.getDefinition().getName());
try {
Path workDir = createWorkingDir(request.getDeploymentProperties(), deploymentId);
int deltaCount = appScaleRequest.getCount() - instances.size();
int targetCount = instances.size() + deltaCount;
if (deltaCount > 0) {
for (int index = instances.size(); index < targetCount; index++) {
instances.add(deployApp(request, workDir, group, deploymentId, index, request.getDeploymentProperties()));
}
}
else if (deltaCount < 0) {
List<AppInstance> processes = new ArrayList<>();
for (int index = instances.size() - 1; index >= targetCount; index--) {
processes.add(instances.remove(index));
}
for (AppInstance instance : processes) {
if (isAlive(instance.getProcess())) {
logger.info("Un-deploying app with deploymentId {} instance {}.", deploymentId, instance.getInstanceNumber());
shutdownAndWait(instance);
}
}
}
}
catch (IOException e) {
throw new RuntimeException("Exception trying to deploy " + request, e);
}
}
@Override
public void undeploy(String id) {
AppInstancesHolder holder = running.get(id);
List<AppInstance> processes = holder != null ? holder.instances : null;
if (processes != null) {
for (AppInstance instance : processes) {
if (isAlive(instance.getProcess())) {
logger.info("Un-deploying app with deploymentId {} instance {}.", id, instance.getInstanceNumber());
shutdownAndWait(instance);
}
}
running.remove(id);
}
else {
throw new IllegalStateException(String.format("App with deploymentId %s is not in a deployed state.", id));
}
}
@Override
public AppStatus status(String id) {
AppInstancesHolder holder = running.get(id);
List<AppInstance> instances = holder != null ? holder.instances : null;
AppStatus.Builder builder = AppStatus.of(id);
if (instances != null) {
for (AppInstance instance : instances) {
builder.with(instance);
}
}
return builder.build();
}
@Override
public String getLog(String id) {
AppInstancesHolder holder = running.get(id);
List<AppInstance> instances = holder != null ? holder.instances : null;
StringBuilder stringBuilder = new StringBuilder();
if (instances != null) {
for (AppInstance instance : instances) {
String stderr = instance.getStdErr();
if (StringUtils.hasText(stderr)) {
stringBuilder.append("stderr:\n");
stringBuilder.append(stderr);
}
String stdout = instance.getStdOut();
if (StringUtils.hasText(stdout)) {
stringBuilder.append("stdout:\n");
stringBuilder.append(stdout);
}
}
}
return stringBuilder.toString();
}
@Override
public RuntimeEnvironmentInfo environmentInfo() {
return super.createRuntimeEnvironmentInfo(AppDeployer.class, this.getClass());
}
@PreDestroy
public void shutdown() {
for (String deploymentId : running.keySet()) {
undeploy(deploymentId);
}
}
private AppInstance deployApp(AppDeploymentRequest request, Path workDir, String group, String deploymentId,
int index, Map<String, String> deploymentProperties) throws IOException {
LocalDeployerProperties localDeployerPropertiesToUse = bindDeploymentProperties(deploymentProperties);
// consolidatedAppProperties is a Map of all application properties to be used by
// the app being launched. These values should end up as environment variables
// either explicitly or as a SPRING_APPLICATION_JSON value.
HashMap<String, String> consolidatedAppProperties = new HashMap<>(request.getDefinition().getProperties());
consolidatedAppProperties.put(JMX_DEFAULT_DOMAIN_KEY, deploymentId);
if (!request.getDefinition().getProperties().containsKey(ENDPOINTS_SHUTDOWN_ENABLED_KEY)) {
consolidatedAppProperties.put(ENDPOINTS_SHUTDOWN_ENABLED_KEY, "true");
}
consolidatedAppProperties.put("endpoints.jmx.unique-names", "true");
if (group != null) {
consolidatedAppProperties.put("spring.cloud.application.group", group);
}
// This Map is the consolidated application properties *for the instance*
// to be deployed in this iteration
Map<String, String> appInstanceEnv = new HashMap<>(consolidatedAppProperties);
// we only set 'normal' style props reflecting what we set for env format
// for cross reference to work inside SAJ.
// looks like for now we can't remove these env style formats as i.e.
// DeployerIntegrationTestProperties in tests really assume 'INSTANCE_INDEX' and
// this might be indication that we can't yet fully remove those.
String guid = toGuid(deploymentId, index);
if (useSpringApplicationJson(request)) {
appInstanceEnv.put("instance.index", Integer.toString(index));
appInstanceEnv.put("spring.cloud.stream.instanceIndex", Integer.toString(index));
appInstanceEnv.put("spring.application.index", Integer.toString(index));
appInstanceEnv.put("spring.cloud.application.guid", guid);
}
else {
appInstanceEnv.put("INSTANCE_INDEX", Integer.toString(index));
appInstanceEnv.put("SPRING_APPLICATION_INDEX", Integer.toString(index));
appInstanceEnv.put("SPRING_CLOUD_APPLICATION_GUID", guid);
}
this.getLocalDeployerProperties().getAppAdmin().addCredentialsToAppEnvironmentAsProperties(appInstanceEnv);
boolean useDynamicPort = !request.getDefinition().getProperties().containsKey(SERVER_PORT_KEY);
// WATCH OUT: The calcServerPort sets the computed port in the appInstanceEnv#SERVER_PORT_KEY.
// Later is implicitly passed to and used inside the command builder. Therefore the calcServerPort() method
// must always be called before the buildProcessBuilder(..)!
int port = calcServerPort(request, useDynamicPort, appInstanceEnv);
ProcessBuilder builder = buildProcessBuilder(request, appInstanceEnv, Optional.of(index), deploymentId)
.inheritIO();
builder.directory(workDir.toFile());
URL baseUrl = (StringUtils.hasText(localDeployerPropertiesToUse.getHostname())) ?
new URL("http", localDeployerPropertiesToUse.getHostname(), port, "")
: getCommandBuilder(request).getBaseUrl(deploymentId, index, port);
AppInstance instance = new AppInstance(deploymentId, index, port, baseUrl,
localDeployerPropertiesToUse.getStartupProbe(), localDeployerPropertiesToUse.getHealthProbe());
if (this.shouldInheritLogging(request)) {
instance.start(builder, workDir);
logger.info("Deploying app with deploymentId {} instance {}.\n Logs will be inherited.",
deploymentId, index);
}
else {
instance.start(builder, workDir, getLocalDeployerProperties().isDeleteFilesOnExit());
logger.info("Deploying app with deploymentId {} instance {}.\n Logs will be in {}", deploymentId,
index, workDir);
}
return instance;
}
private Path createWorkingDir(Map<String, String> deploymentProperties, String deploymentId) throws IOException {
LocalDeployerProperties localDeployerPropertiesToUse = bindDeploymentProperties(deploymentProperties);
Path workingDirectoryRoot = Files.createDirectories(localDeployerPropertiesToUse.getWorkingDirectoriesRoot());
Path workDir = Files.createDirectories(workingDirectoryRoot.resolve(Long.toString(System.currentTimeMillis())).resolve(deploymentId));
if (getLocalDeployerProperties().isDeleteFilesOnExit()) {
workDir.toFile().deleteOnExit();
}
return workDir;
}
private void validateStatus(String deploymentId, DeploymentState expectedState) {
DeploymentState state = status(deploymentId).getState();
Assert.state(state == expectedState,
String.format("App with deploymentId [%s] with state [%s] doesn't match expected state [%s]",
deploymentId, state, expectedState));
}
private static String toGuid(String deploymentId, int appIndex) {
return String.format("%s-%s", deploymentId, appIndex);
}
private static class AppInstance implements Instance, AppInstanceStatus {
private final String deploymentId;
private final int instanceNumber;
private final URL baseUrl;
private final Map<String, String> attributes = new TreeMap<>();
private int pid;
private Process process;
private File workFile;
private File stdout;
private File stderr;
private int port;
private HttpProbeExecutor startupProbeExecutor;
private HttpProbeExecutor healthProbeExecutor;
private boolean startupProbeOk;
private AppInstance(String deploymentId, int instanceNumber, int port, URL baseUrl, HttpProbe startupProbe,
HttpProbe healthProbe) {
this.deploymentId = deploymentId;
this.instanceNumber = instanceNumber;
this.port = port;
this.baseUrl = baseUrl;
this.attributes.put("port", Integer.toString(port));
this.attributes.put("guid", toGuid(deploymentId, instanceNumber));
this.attributes.put("url", baseUrl.toString());
this.startupProbeExecutor = HttpProbeExecutor.from(baseUrl, startupProbe);
this.healthProbeExecutor = HttpProbeExecutor.from(baseUrl, healthProbe);
}
@Override
public String getId() {
return deploymentId + "-" + instanceNumber;
}
@Override
public URL getBaseUrl() {
return this.baseUrl;
}
@Override
public Process getProcess() {
return this.process;
}
@Override
public String toString() {
return String.format("%s [%s]", getId(), getState());
}
@Override
public DeploymentState getState() {
Integer exit = getProcessExitValue(process);
// TODO: consider using exit code mapper concept from batch
if (exit != null) {
return DeploymentState.failed;
}
if (port < 1) {
// Support case where user passed in zero or negative port indicating fully random
// chosen by OS. In this case we simply return deployed if process is up.
// Also we can't even try http check as we would not know port to connect to.
return DeploymentState.deployed;
}
// do startup probe first and only until we're deployed
if (startupProbeExecutor != null && !startupProbeOk) {
boolean ok = startupProbeExecutor.probe();
if (ok) {
startupProbeOk = true;
return DeploymentState.deployed;
}
else {
return DeploymentState.deploying;
}
}
// now deployed, checking health probe
if (healthProbeExecutor != null) {
return healthProbeExecutor.probe() ? DeploymentState.deployed : DeploymentState.failed;
}
try {
HttpURLConnection urlConnection = (HttpURLConnection) baseUrl.openConnection();
urlConnection.setConnectTimeout(100);
urlConnection.connect();
urlConnection.disconnect();
return DeploymentState.deployed;
}
catch (IOException e) {
return DeploymentState.deploying;
}
}
public String getStdOut() {
try {
return FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream(this.stdout)));
}
catch (IOException e) {
return "Log retrieval returned " + e.getMessage();
}
}
public String getStdErr() {
try {
return FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream(this.stderr)));
}
catch (IOException e) {
return "Log retrieval returned " + e.getMessage();
}
}
public int getInstanceNumber() {
return instanceNumber;
}
@Override
public Map<String, String> getAttributes() {
return this.attributes;
}
/**
* Will start the process while redirecting 'out' and 'err' streams to the 'out' and 'err'
* streams of this process.
*/
private void start(ProcessBuilder builder, Path workDir) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("Local App Deployer Commands: " + String.join(",", builder.command())
+ ", Environment: " + builder.environment());
}
this.workFile = workDir.toFile();
this.attributes.put("working.dir", this.workFile.getAbsolutePath());
this.process = builder.start();
this.pid = getLocalProcessPid(this.process);
if (pid > 0) {
// add pid if we got it
attributes.put("pid", Integer.toString(pid));
}
}
private void start(ProcessBuilder builder, Path workDir, boolean deleteOnExit) throws IOException {
String workDirPath = workDir.toFile().getAbsolutePath();
this.stdout = Files.createFile(Paths.get(workDirPath, "stdout_" + instanceNumber + ".log")).toFile();
this.attributes.put("stdout", stdout.getAbsolutePath());
this.stderr = Files.createFile(Paths.get(workDirPath, "stderr_" + instanceNumber + ".log")).toFile();
this.attributes.put("stderr", stderr.getAbsolutePath());
if (deleteOnExit) {
this.stdout.deleteOnExit();
this.stderr.deleteOnExit();
}
builder.redirectOutput(Redirect.to(this.stdout));
builder.redirectError(Redirect.to(this.stderr));
this.start(builder, workDir);
}
}
private static class AppInstancesHolder {
final List<AppInstance> instances;
final AppDeploymentRequest request;
public AppInstancesHolder(List<AppInstance> instances, AppDeploymentRequest request) {
this.instances = instances;
this.request = request;
}
}
}