-
Notifications
You must be signed in to change notification settings - Fork 47
/
Kernel.java
699 lines (636 loc) · 32.2 KB
/
Kernel.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
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
697
698
699
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.aws.greengrass.lifecyclemanager;
import com.amazon.aws.iot.greengrass.component.common.DependencyType;
import com.amazon.aws.iot.greengrass.configuration.common.DeploymentCapability;
import com.aws.greengrass.componentmanager.ComponentStore;
import com.aws.greengrass.componentmanager.exceptions.PackageLoadingException;
import com.aws.greengrass.componentmanager.models.ComponentIdentifier;
import com.aws.greengrass.config.Configuration;
import com.aws.greengrass.config.ConfigurationWriter;
import com.aws.greengrass.config.Node;
import com.aws.greengrass.config.Topic;
import com.aws.greengrass.config.Topics;
import com.aws.greengrass.dependency.Context;
import com.aws.greengrass.dependency.EZPlugins;
import com.aws.greengrass.dependency.ImplementsService;
import com.aws.greengrass.deployment.DeploymentDirectoryManager;
import com.aws.greengrass.deployment.DeploymentQueue;
import com.aws.greengrass.deployment.DeviceConfiguration;
import com.aws.greengrass.deployment.activator.DeploymentActivatorFactory;
import com.aws.greengrass.deployment.bootstrap.BootstrapManager;
import com.aws.greengrass.deployment.errorcode.DeploymentErrorCodeUtils;
import com.aws.greengrass.deployment.exceptions.DeviceConfigurationException;
import com.aws.greengrass.deployment.exceptions.ServiceUpdateException;
import com.aws.greengrass.deployment.model.Deployment;
import com.aws.greengrass.deployment.model.Deployment.DeploymentStage;
import com.aws.greengrass.lifecyclemanager.exceptions.CustomPluginNotSupportedException;
import com.aws.greengrass.lifecyclemanager.exceptions.InputValidationException;
import com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException;
import com.aws.greengrass.logging.api.Logger;
import com.aws.greengrass.logging.impl.LogManager;
import com.aws.greengrass.util.Coerce;
import com.aws.greengrass.util.CommitableWriter;
import com.aws.greengrass.util.CrashableFunction;
import com.aws.greengrass.util.DependencyOrder;
import com.aws.greengrass.util.NucleusPaths;
import com.aws.greengrass.util.Pair;
import com.aws.greengrass.util.ProxyUtils;
import com.aws.greengrass.util.Utils;
import com.aws.greengrass.util.platforms.Platform;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Clock;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import javax.inject.Singleton;
import static com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY;
import static com.aws.greengrass.config.Topic.DEFAULT_VALUE_TIMESTAMP;
import static com.aws.greengrass.dependency.EZPlugins.JAR_FILE_EXTENSION;
import static com.aws.greengrass.deployment.bootstrap.BootstrapSuccessCode.REQUEST_REBOOT;
import static com.aws.greengrass.deployment.bootstrap.BootstrapSuccessCode.REQUEST_RESTART;
import static com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC;
import static com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_DEPENDENCIES_NAMESPACE_TOPIC;
import static com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_LIFECYCLE_NAMESPACE_TOPIC;
import static com.aws.greengrass.lifecyclemanager.KernelCommandLine.MAIN_SERVICE_NAME;
/**
* Greengrass-kernel.
*/
@SuppressWarnings("PMD.CouplingBetweenObjects")
public class Kernel {
private static final Logger logger = LogManager.getLogger(Kernel.class);
protected static final String CONTEXT_SERVICE_IMPLEMENTERS = "service-implementers";
public static final String SERVICE_CLASS_TOPIC_KEY = "class";
public static final String SERVICE_TYPE_TOPIC_KEY = "componentType";
public static final String SERVICE_TYPE_TO_CLASS_MAP_KEY = "componentTypeToClassMap";
private static final String PLUGIN_SERVICE_TYPE_NAME = "plugin";
static final String DEFAULT_CONFIG_YAML_FILE_READ = "config.yaml";
static final String DEFAULT_CONFIG_YAML_FILE_WRITE = "effectiveConfig.yaml";
static final String DEFAULT_CONFIG_TLOG_FILE = "config.tlog";
public static final String DEFAULT_BOOTSTRAP_CONFIG_TLOG_FILE = "bootstrap.tlog";
public static final String SERVICE_DIGEST_TOPIC_KEY = "service-digest";
private static final String DEPLOYMENT_STAGE_LOG_KEY = "stage";
protected static final ObjectMapper CONFIG_YAML_WRITER =
YAMLMapper.builder().disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET).build();
private static final List<String> SUPPORTED_CAPABILITIES =
Arrays.asList(DeploymentCapability.LARGE_CONFIGURATION.toString(),
DeploymentCapability.LINUX_RESOURCE_LIMITS.toString(),
DeploymentCapability.SUB_DEPLOYMENTS.toString());
@Getter
private final Context context;
@Getter
@Setter(AccessLevel.PACKAGE)
private Configuration config;
@Setter(AccessLevel.PACKAGE)
private KernelCommandLine kernelCommandLine;
@Setter(AccessLevel.PACKAGE)
private KernelLifecycle kernelLifecycle;
@Getter
private final NucleusPaths nucleusPaths;
private Collection<GreengrassService> cachedOD = null;
private DeploymentStage deploymentStageAtLaunch = DeploymentStage.DEFAULT;
/**
* Construct the Kernel and global Context.
*/
public Kernel() {
context = new Context();
config = new Configuration(context);
context.put(Configuration.class, config);
context.put(Kernel.class, this);
ScheduledThreadPoolExecutor ses = new ScheduledThreadPoolExecutor(4);
ExecutorService executorService = Executors.newCachedThreadPool();
context.put(ScheduledThreadPoolExecutor.class, ses);
context.put(ScheduledExecutorService.class, ses);
context.put(Executor.class, executorService);
context.put(ExecutorService.class, executorService);
context.put(ThreadPoolExecutor.class, ses);
Thread.setDefaultUncaughtExceptionHandler(new KernelExceptionHandler());
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
logger.atWarn().log("Shutting down Nucleus due to external signal");
this.shutdown(-1);
}));
nucleusPaths = new NucleusPaths();
context.put(NucleusPaths.class, nucleusPaths);
kernelCommandLine = new KernelCommandLine(this);
kernelLifecycle = new KernelLifecycle(this, kernelCommandLine, nucleusPaths);
context.put(KernelCommandLine.class, kernelCommandLine);
context.put(KernelLifecycle.class, kernelLifecycle);
context.put(DeploymentActivatorFactory.class, new DeploymentActivatorFactory(this));
context.put(Clock.class, Clock.systemUTC());
Map<String, String> typeToClassMap = new ConcurrentHashMap<>();
typeToClassMap.put("lambda", "com.aws.greengrass.lambdamanager.UserLambdaService");
context.put(SERVICE_TYPE_TO_CLASS_MAP_KEY, typeToClassMap);
}
/**
* Find the service that a Node belongs to (or null if it is not under a service).
*
* @param node node to identify the service it belongs to
* @return service name or null
*/
public static String findServiceForNode(Node node) {
String[] p = node.path();
for (int i = 0; i < p.length - 1; i++) {
if (SERVICES_NAMESPACE_TOPIC.equals(p[i])) {
return p[i + 1];
}
}
return null;
}
/**
* Startup the Kernel and all services.
*/
@SuppressWarnings("PMD.MissingBreakInSwitch")
public Kernel launch() {
try {
Platform.getInstance().getRunWithGenerator()
.validateDefaultConfiguration(context.get(DeviceConfiguration.class));
} catch (DeviceConfigurationException e) {
RuntimeException rte = new RuntimeException(e);
logger.atError().setEventType("parse-args-error").setCause(rte).log();
throw rte;
}
BootstrapManager bootstrapManager = kernelCommandLine.getBootstrapManager();
DeploymentDirectoryManager deploymentDirectoryManager = kernelCommandLine.getDeploymentDirectoryManager();
KernelAlternatives kernelAlts = context.get(KernelAlternatives.class);
switch (deploymentStageAtLaunch) {
case BOOTSTRAP:
logger.atInfo().kv("deploymentStage", deploymentStageAtLaunch).log("Resume deployment");
int exitCode;
try {
exitCode = bootstrapManager.executeAllBootstrapTasksSequentially(
deploymentDirectoryManager.getBootstrapTaskFilePath());
if (!bootstrapManager.hasNext()) {
logger.atInfo().log("Completed all bootstrap tasks. Continue to activate deployment changes");
}
// If exitCode is 0, which happens when all bootstrap tasks are completed, restart in new launch
// directories and verify handover is complete. As a result, exit code 0 is treated as 100 here.
logger.atInfo().log((exitCode == REQUEST_REBOOT ? "device reboot" : "Nucleus restart")
+ " requested to complete bootstrap task");
shutdown(30, exitCode == REQUEST_REBOOT ? REQUEST_REBOOT : REQUEST_RESTART);
} catch (ServiceUpdateException | IOException e) {
logger.atError().log("Deployment bootstrap failed", e);
try {
Deployment deployment = deploymentDirectoryManager.readDeploymentMetadata();
deployment.setDeploymentStage(DeploymentStage.KERNEL_ROLLBACK);
Pair<List<String>, List<String>> errorReport =
DeploymentErrorCodeUtils.generateErrorReportFromExceptionStack(e);
deployment.setErrorStack(errorReport.getLeft());
deployment.setErrorTypes(errorReport.getRight());
deployment.setStageDetails(Utils.generateFailureMessage(e));
deploymentDirectoryManager.writeDeploymentMetadata(deployment);
} catch (IOException ioException) {
logger.atError().setCause(ioException).log("Could not read deployment metadata, "
+ "file is either missing or corrupted");
}
try {
kernelAlts.prepareRollback();
shutdown(30, REQUEST_RESTART);
} catch (IOException ioException) {
logger.atError().setCause(ioException).log("Could not prepare rollback");
kernelLifecycle.launch();
}
}
break;
case KERNEL_ACTIVATION:
case KERNEL_ROLLBACK:
logger.atInfo().kv("deploymentStage", deploymentStageAtLaunch).log("Resume deployment");
DeploymentQueue deploymentQueue = new DeploymentQueue();
context.put(DeploymentQueue.class, deploymentQueue);
try {
Deployment deployment = deploymentDirectoryManager.readDeploymentMetadata();
deployment.setDeploymentStage(deploymentStageAtLaunch);
deploymentQueue.offer(deployment);
} catch (IOException e) {
logger.atError().setCause(e)
.log("Failed to load information for the ongoing deployment. Proceed as default");
}
// fall through to launch kernel
default:
kernelLifecycle.launch();
break;
}
return this;
}
/**
* Shutdown Kernel but not exit the process.
*/
public void shutdown() {
shutdown(30);
}
/**
* Shutdown Kernel within the timeout but not exit the process.
*
* @param timeoutSeconds Timeout in seconds
*/
public void shutdown(int timeoutSeconds) {
kernelLifecycle.shutdown(timeoutSeconds);
}
/**
* Shutdown Kernel within the timeout and exit the process with the given code.
*
* @param timeoutSeconds Timeout in seconds
* @param exitCode exit code
*/
public void shutdown(int timeoutSeconds, int exitCode) {
kernelLifecycle.shutdown(timeoutSeconds, exitCode);
}
/**
* Get a reference to the main service.
*/
public GreengrassService getMain() {
return kernelLifecycle.getMain();
}
@SuppressWarnings("PMD.NullAssignment")
public synchronized void clearODcache() {
cachedOD = null;
}
/**
* Get a list of all dependencies in order (with the main service as the last).
*
* @return collection of services in dependency order
*/
public synchronized Collection<GreengrassService> orderedDependencies() {
if (cachedOD != null) {
return cachedOD;
}
if (getMain() == null) {
return Collections.emptyList();
}
final HashSet<GreengrassService> pendingDependencyServices = new LinkedHashSet<>();
getMain().putDependenciesIntoSet(pendingDependencyServices);
final LinkedHashSet<GreengrassService> dependencyFoundServices = new DependencyOrder<GreengrassService>()
.computeOrderedDependencies(pendingDependencyServices, s -> s.getDependencies().keySet());
return cachedOD = dependencyFoundServices;
}
/**
* When a config file gets read, it gets woven together from fragments from multiple sources. This writes a fresh
* copy of the config file, as it is, after the weaving-together process.
*/
public void writeEffectiveConfig() {
Path p = context.get(NucleusPaths.class).configPath();
if (p != null) {
writeEffectiveConfig(p.resolve(DEFAULT_CONFIG_YAML_FILE_WRITE));
}
}
/**
* When a config file gets read, it gets woven together from fragments from multiple sources. This writes a fresh
* copy of the config file, as it is, after the weaving-together process.
*
* @param p Path to write the effective config into
*/
public void writeEffectiveConfig(Path p) {
try (CommitableWriter out = CommitableWriter.abandonOnClose(p)) {
writeConfig(out);
out.commit();
logger.atInfo().setEventType("effective-config-dump-complete").addKeyValue("file", p).log();
} catch (IOException t) {
logger.atInfo().setEventType("effective-config-dump-error").setCause(t).addKeyValue("file", p).log();
}
}
/**
* Write the effective config in the transaction log format.
*
* @param transactionLogPath path to write the file into
* @throws IOException if writing fails
*/
public void writeEffectiveConfigAsTransactionLog(Path transactionLogPath) throws IOException {
ConfigurationWriter.dump(config, transactionLogPath);
}
/**
* Write the effective config into a {@link Writer}.
*
* @param w Writer to write config into
*/
public void writeConfig(Writer w) {
Map<String, Object> configMap = new HashMap<>();
configMap.put(SERVICES_NAMESPACE_TOPIC, config.lookupTopics(SERVICES_NAMESPACE_TOPIC).toPOJO());
configMap.put(DeviceConfiguration.SYSTEM_NAMESPACE_KEY,
config.lookupTopics(DeviceConfiguration.SYSTEM_NAMESPACE_KEY).toPOJO());
try {
CONFIG_YAML_WRITER.writeValue(w, configMap);
} catch (IOException ex) {
logger.atError().setEventType("write-config-error").setCause(ex).log();
}
}
@Nullable
public Topics findServiceTopic(String serviceName) {
return config.findTopics(SERVICES_NAMESPACE_TOPIC, serviceName);
}
/**
* Locate a GreengrassService by name in the kernel context.
*
* @param name name of the service to find
* @return found service or null
* @throws ServiceLoadException if service cannot load
*/
public GreengrassService locate(String name) throws ServiceLoadException {
return context.getValue(GreengrassService.class, name).computeObjectIfEmpty(v ->
locateGreengrassService(v, name, this::locate));
}
/**
* Locate a GreengrassService by name in the kernel context, or return BrokenService if service cannot load.
*
* @param name name of the service to find
* @return found service
*/
public GreengrassService locateIgnoreError(String name) {
return context.getValue(GreengrassService.class, name).computeObjectIfEmpty(v -> {
try {
return locateGreengrassService(v, name, this::locateIgnoreError);
} catch (ServiceLoadException e) {
logger.atError().log("Cannot load service", e);
return new UnloadableService(
config.lookupTopics(DEFAULT_VALUE_TIMESTAMP, SERVICES_NAMESPACE_TOPIC, name), e);
}
});
}
@SuppressWarnings(
{"UseSpecificCatch", "PMD.AvoidCatchingThrowable", "PMD.AvoidDeeplyNestedIfStmts", "PMD.ConfusingTernary"})
private GreengrassService locateGreengrassService(Context.Value v, String name, CrashableFunction<String,
GreengrassService, ServiceLoadException> locateFunction) throws ServiceLoadException {
Topics serviceRootTopics = findServiceTopic(name);
Class<?> clazz = null;
if (serviceRootTopics != null) {
// Try locating all the dependencies first so that they'll all exist prior to their dependant.
// This is to fix an ordering problem with plugins such as lambda manager. The plugin needs to be
// located *before* the dependant is located so that the plugin has its jar loaded into the classloader.
Topic dependenciesTopic = serviceRootTopics.findLeafChild(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC);
if (dependenciesTopic != null && dependenciesTopic.getOnce() instanceof Collection) {
try {
for (Pair<String, DependencyType> p : GreengrassService
.parseDependencies((Collection<String>) dependenciesTopic.getOnce())) {
locateFunction.apply(p.getLeft());
}
} catch (ServiceLoadException | InputValidationException e) {
throw new ServiceLoadException("Unable to load service " + name, e);
}
}
Topic classTopic = serviceRootTopics.findLeafChild(SERVICE_CLASS_TOPIC_KEY);
String className = null;
// If a "class" is specified in the recipe, then use that
if (classTopic != null) {
className = Coerce.toString(classTopic);
} else {
Topic componentTypeTopic = serviceRootTopics.findLeafChild(SERVICE_TYPE_TOPIC_KEY);
// If a "componentType" is specified, then map that to a class
if (componentTypeTopic != null) {
className = ((Map<String, String>) context.getvIfExists(SERVICE_TYPE_TO_CLASS_MAP_KEY).get())
.get(Coerce.toString(componentTypeTopic).toLowerCase());
// If the mapping didn't exist and the component type is "plugin", then load the service from a
// plugin
if (className == null && Coerce.toString(componentTypeTopic)
.equalsIgnoreCase(PLUGIN_SERVICE_TYPE_NAME)) {
clazz = locateExternalPlugin(name, serviceRootTopics);
}
}
}
if (className != null) {
try {
clazz = context.get(EZPlugins.class).forName(className);
} catch (Throwable ex) {
throw new ServiceLoadException("Can't load service class from " + className, ex);
}
}
}
// try to find service implementation class from plugins.
if (clazz == null) {
Map<String, Class<?>> si = context.getIfExists(Map.class, CONTEXT_SERVICE_IMPLEMENTERS);
if (si != null) {
logger.atInfo().kv(GreengrassService.SERVICE_NAME_KEY, name)
.log("Attempt to load service from plugins");
clazz = si.get(name);
}
}
GreengrassService ret;
// If found class, try to load service class from plugins.
if (clazz != null) {
try {
// Lookup the service topics here because the Topics passed into the GreengrassService
// constructor must not be null
Topics topics = config.lookupTopics(SERVICES_NAMESPACE_TOPIC, name);
try {
Constructor<?> ctor = clazz.getConstructor(Topics.class);
ret = (GreengrassService) ctor.newInstance(topics);
} catch (NoSuchMethodException e) {
// If the basic constructor doesn't exist, then try injecting from the context
ret = (GreengrassService) context.newInstance(clazz);
}
// Force plugins and built-in services to be singletons
if (clazz.getAnnotation(Singleton.class) != null || PluginService.class.isAssignableFrom(clazz)
|| clazz.getAnnotation(ImplementsService.class) != null) {
context.put(ret.getClass(), v);
}
if (clazz.getAnnotation(ImplementsService.class) != null) {
topics.createLeafChild(VERSION_CONFIG_KEY)
.withNewerValue(0L, clazz.getAnnotation(ImplementsService.class).version());
}
logger.atDebug("service-loaded").kv(GreengrassService.SERVICE_NAME_KEY, ret.getName())
.log();
return ret;
} catch (Throwable ex) {
throw new ServiceLoadException("Can't create Greengrass Service instance " + clazz.getSimpleName(),
ex);
}
}
if (serviceRootTopics == null || serviceRootTopics.isEmpty()) {
throw new ServiceLoadException("No matching definition in system model for: " + name);
}
// if not found, initialize GenericExternalService
try {
ret = new GenericExternalService(serviceRootTopics);
logger.atDebug("generic-service-loaded").kv(GreengrassService.SERVICE_NAME_KEY, ret.getName()).log();
} catch (Throwable ex) {
throw new ServiceLoadException("Can't create generic service instance " + name, ex);
}
return ret;
}
@SuppressWarnings({"PMD.AvoidCatchingThrowable", "PMD.CloseResource"})
private Class<?> locateExternalPlugin(String name, Topics serviceRootTopics) throws ServiceLoadException {
ComponentIdentifier componentId = ComponentIdentifier.fromServiceTopics(serviceRootTopics);
Path pluginJar;
try {
pluginJar = nucleusPaths.artifactPath(componentId)
.resolve(componentId.getName() + JAR_FILE_EXTENSION);
} catch (IOException e) {
throw new ServiceLoadException(e);
}
if (!pluginJar.toFile().exists() || !pluginJar.toFile().isFile()) {
throw new ServiceLoadException(
String.format("Unable to find %s because %s does not exist", name, pluginJar));
}
Topic storedDigest = config.find(SERVICES_NAMESPACE_TOPIC, MAIN_SERVICE_NAME,
GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC, SERVICE_DIGEST_TOPIC_KEY, componentId.toString());
if (storedDigest == null || storedDigest.getOnce() == null) {
logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name)
.log("Local external plugin is not supported by this greengrass version");
throw new CustomPluginNotSupportedException("Locally deployed plugin components are not supported. "
+ "Plugins must be deployed via a cloud-based deployment.");
}
ComponentStore componentStore = context.get(ComponentStore.class);
try {
if (!componentStore.validateComponentRecipeDigest(componentId, Coerce.toString(storedDigest))) {
logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name)
.log("Plugin recipe was modified after it was downloaded from cloud");
throw new ServiceLoadException("Plugin recipe has been modified after it was downloaded");
}
} catch (PackageLoadingException e) {
logger.atError("plugin-load-error").setCause(e).kv(GreengrassService.SERVICE_NAME_KEY, name)
.log("Unable to calculate local plugin recipe digest");
throw new ServiceLoadException("Unable to calculate local plugin recipe digest", e);
}
Class<?> clazz;
try {
AtomicReference<Class<?>> classReference = new AtomicReference<>();
EZPlugins ezPlugins = context.get(EZPlugins.class);
ezPlugins.loadPluginAnnotatedWith(pluginJar, ImplementsService.class, (c) -> {
// Only use the class whose name matches what we want
ImplementsService serviceImplementation = c.getAnnotation(ImplementsService.class);
if (serviceImplementation.name().equals(name)) {
if (classReference.get() != null) {
logger.atWarn().log("Multiple classes implementing service found in {} "
+ "for component {}. Using the first one found: {}", pluginJar, name,
classReference.get());
return;
}
classReference.set(c);
}
});
clazz = classReference.get();
} catch (Throwable e) {
throw new ServiceLoadException(String.format("Unable to load %s as a plugin", name), e);
}
if (clazz == null) {
throw new ServiceLoadException(String.format(
"Unable to find %s. Could not find any ImplementsService annotation with the same name.", name));
}
return clazz;
}
/**
* Get running custom root components, excluding the kernel's built-in services.
*
* @return returns name and version as a map
*/
public Map<String, String> getRunningCustomRootComponents() {
Map<String, String> rootPackageNameAndVersionMap = new HashMap<>();
for (GreengrassService service : getMain().getDependencies().keySet()) {
Topic version = service.getConfig().find(VERSION_CONFIG_KEY);
// If the service is an autostart service then ignore it.
if (service.isBuiltin()) {
continue;
}
rootPackageNameAndVersionMap.put(service.getName(), Coerce.toString(version));
}
return rootPackageNameAndVersionMap;
}
/**
* Parse kernel arguments and initialized configuration.
*
* @param args CLI args
* @return Kernel instance
*/
@SuppressWarnings("PMD.MissingBreakInSwitch")
public Kernel parseArgs(String... args) {
kernelCommandLine.parseArgs(args);
config.lookupTopics(SERVICES_NAMESPACE_TOPIC, MAIN_SERVICE_NAME, SERVICE_LIFECYCLE_NAMESPACE_TOPIC);
BootstrapManager bootstrapManager = kernelCommandLine.getBootstrapManager();
DeploymentDirectoryManager deploymentDirectoryManager = kernelCommandLine.getDeploymentDirectoryManager();
KernelAlternatives kernelAlts = context.get(KernelAlternatives.class);
DeploymentStage stage = kernelAlts.determineDeploymentStage(bootstrapManager, deploymentDirectoryManager);
String configFileName = "";
switch (stage) {
case KERNEL_ACTIVATION:
case BOOTSTRAP:
try {
Path configPath = deploymentDirectoryManager.getTargetConfigFilePath();
if (!Files.exists(configPath)) {
logger.atError().kv(DEPLOYMENT_STAGE_LOG_KEY, stage).kv("targetConfigFile", configPath)
.log("Detected ongoing deployment, but target configuration file not found");
break;
}
configFileName = configPath.toString();
deploymentStageAtLaunch = stage;
} catch (IOException e) {
logger.atError().kv(DEPLOYMENT_STAGE_LOG_KEY, stage)
.log("Detected ongoing deployment, but failed to load target configuration file", e);
}
break;
case KERNEL_ROLLBACK:
try {
Path configPath = deploymentDirectoryManager.getSnapshotFilePath();
if (!Files.exists(configPath)) {
logger.atError().kv(DEPLOYMENT_STAGE_LOG_KEY, stage).kv("rollbackConfigFile", configPath)
.log("Detected ongoing deployment, but rollback configuration not found");
break;
}
configFileName = configPath.toString();
deploymentStageAtLaunch = stage;
} catch (IOException e) {
logger.atError().kv(DEPLOYMENT_STAGE_LOG_KEY, stage)
.log("Detected ongoing deployment, but failed to load rollback configuration file", e);
}
break;
default:
logger.atInfo().log("No ongoing deployment detected. Proceed as default");
}
if (Utils.isEmpty(configFileName)) {
kernelLifecycle.initConfigAndTlog();
} else {
kernelLifecycle.initConfigAndTlog(configFileName);
}
// Update device configuration from commandline arguments after loading config files
DeviceConfiguration deviceConfiguration = getContext().get(DeviceConfiguration.class);
kernelCommandLine.updateDeviceConfiguration(deviceConfiguration);
// After configuration is fully loaded, initialize Nucleus service config
deviceConfiguration.initializeNucleusFromRecipe(kernelAlts);
setupProxy();
return this;
}
private void setupProxy() {
ProxyUtils.setDeviceConfiguration(context.get(DeviceConfiguration.class));
}
/*
* I added this method because it's really handy for any external service that's
* accessing files. But it's just a trampoline method, which is like a chalkboard
* squeak. They really bug me. But then I noticed that Kernel.java is
* filled with trampolines. And two objects that are the target of the trampolines,
* and which are otherwise unused. It all gets much cleaner if kernelCommandline
* and kernelLifecycle are just accessed through dependency injection where they're
* needed. I did this edit, and it got rid of a pile of code. But it blew the
* the unit tests out of the water. mock(x) is actively injection-hostile. I
* started down the road of fixing the tests, but it got *way* out of hand.
* So I went back to the trampoline form. Someday this should be cleaned up.
* - jag
*/
public String deTilde(String filename) {
return kernelCommandLine.deTilde(filename);
}
public List<String> getSupportedCapabilities() {
return SUPPORTED_CAPABILITIES;
}
}