Search in sources :

Example 1 with KernelAlternatives

use of com.aws.greengrass.lifecyclemanager.KernelAlternatives in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceConfiguration method persistInitialLaunchParams.

/**
 * Persist initial launch parameters of JVM options.
 *
 * @param kernelAlts KernelAlternatives instance
 */
void persistInitialLaunchParams(KernelAlternatives kernelAlts) {
    if (Files.exists(kernelAlts.getLaunchParamsPath())) {
        logger.atDebug().log("Nucleus launch parameters has already been set up");
        return;
    }
    // Persist initial Nucleus launch parameters
    try {
        String jvmOptions = ManagementFactory.getRuntimeMXBean().getInputArguments().stream().sorted().filter(s -> !s.startsWith(JVM_OPTION_ROOT_PATH)).collect(Collectors.joining(" "));
        kernel.getConfig().lookup(SERVICES_NAMESPACE_TOPIC, getNucleusComponentName(), CONFIGURATION_CONFIG_KEY, DEVICE_PARAM_JVM_OPTIONS).withNewerValue(DEFAULT_VALUE_TIMESTAMP + 1, jvmOptions);
        kernelAlts.writeLaunchParamsToFile(jvmOptions);
        logger.atInfo().log("Successfully setup Nucleus launch parameters");
    } catch (IOException e) {
        logger.atError().log("Unable to setup Nucleus launch parameters", e);
    }
}
Also used : PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) SerializerFactory.getRecipeSerializer(com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializer) Arrays(java.util.Arrays) URISyntaxException(java.net.URISyntaxException) LogStore(com.aws.greengrass.logging.impl.config.LogStore) Validator(com.aws.greengrass.config.Validator) ComponentConfigurationValidationException(com.aws.greengrass.deployment.exceptions.ComponentConfigurationValidationException) SERVICE_LIFECYCLE_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_LIFECYCLE_NAMESPACE_TOPIC) LogFormat(com.aws.greengrass.logging.impl.config.LogFormat) Level(org.slf4j.event.Level) Map(java.util.Map) SETENV_CONFIG_NAMESPACE(com.aws.greengrass.lifecyclemanager.GreengrassService.SETENV_CONFIG_NAMESPACE) LogManager(com.aws.greengrass.logging.impl.LogManager) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Node(com.aws.greengrass.config.Node) SERVICE_TYPE_TOPIC_KEY(com.aws.greengrass.lifecyclemanager.Kernel.SERVICE_TYPE_TOPIC_KEY) TLSAuthException(com.aws.greengrass.util.exceptions.TLSAuthException) Collectors(java.util.stream.Collectors) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) StandardCharsets(java.nio.charset.StandardCharsets) SERVICES_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC) ChildChanged(com.aws.greengrass.config.ChildChanged) Objects(java.util.Objects) Topics(com.aws.greengrass.config.Topics) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) NucleusPaths(com.aws.greengrass.util.NucleusPaths) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) Optional(java.util.Optional) SecurityService(com.aws.greengrass.security.SecurityService) DefaultAwsRegionProviderChain(software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) VERSION_CONFIG_KEY(com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY) COPY_ATTRIBUTES(java.nio.file.StandardCopyOption.COPY_ATTRIBUTES) CONFIGURATION_CONFIG_KEY(com.aws.greengrass.componentmanager.KernelConfigResolver.CONFIGURATION_CONFIG_KEY) Coerce(com.aws.greengrass.util.Coerce) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogConfigUpdate(com.aws.greengrass.logging.impl.config.model.LogConfigUpdate) ArrayList(java.util.ArrayList) Platform(com.aws.greengrass.util.platforms.Platform) Inject(javax.inject.Inject) MAIN_SERVICE_NAME(com.aws.greengrass.lifecyclemanager.KernelCommandLine.MAIN_SERVICE_NAME) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) NOFOLLOW_LINKS(java.nio.file.LinkOption.NOFOLLOW_LINKS) Semver(com.vdurmont.semver4j.Semver) ManagementFactory(java.lang.management.ManagementFactory) Region(software.amazon.awssdk.regions.Region) DependencyProperties(com.amazon.aws.iot.greengrass.component.common.DependencyProperties) SERVICE_DEPENDENCIES_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_DEPENDENCIES_NAMESPACE_TOPIC) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) FileSystemPermission(com.aws.greengrass.util.FileSystemPermission) Files(java.nio.file.Files) Permissions(com.aws.greengrass.util.Permissions) IOException(java.io.IOException) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) KeyManager(javax.net.ssl.KeyManager) Topic(com.aws.greengrass.config.Topic) ComponentType(com.amazon.aws.iot.greengrass.component.common.ComponentType) Utils(com.aws.greengrass.util.Utils) RecipeLoader(com.aws.greengrass.componentmanager.converter.RecipeLoader) WhatHappened(com.aws.greengrass.config.WhatHappened) Paths(java.nio.file.Paths) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) KernelAlternatives.locateCurrentKernelUnpackDir(com.aws.greengrass.lifecyclemanager.KernelAlternatives.locateCurrentKernelUnpackDir) DeviceConfigurationException(com.aws.greengrass.deployment.exceptions.DeviceConfigurationException) KernelConfigResolver(com.aws.greengrass.componentmanager.KernelConfigResolver) Collections(java.util.Collections) Logger(com.aws.greengrass.logging.api.Logger) DEFAULT_VALUE_TIMESTAMP(com.aws.greengrass.config.Topic.DEFAULT_VALUE_TIMESTAMP) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) IOException(java.io.IOException)

Example 2 with KernelAlternatives

use of com.aws.greengrass.lifecyclemanager.KernelAlternatives in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceConfigurationTest method GIVEN_existing_launch_param_file_WHEN_persistInitialLaunchParams_THEN_skip.

@Test
void GIVEN_existing_launch_param_file_WHEN_persistInitialLaunchParams_THEN_skip(@TempDir Path tempDir) throws Exception {
    deviceConfiguration = new DeviceConfiguration(mockKernel);
    KernelAlternatives kernelAlternatives = mock(KernelAlternatives.class);
    Path tempFile = tempDir.resolve("testFile");
    Files.createFile(tempFile);
    doReturn(tempFile).when(kernelAlternatives).getLaunchParamsPath();
    deviceConfiguration.persistInitialLaunchParams(kernelAlternatives);
    verify(kernelAlternatives, times(0)).writeLaunchParamsToFile(anyString());
}
Also used : Path(java.nio.file.Path) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) Test(org.junit.jupiter.api.Test)

Example 3 with KernelAlternatives

use of com.aws.greengrass.lifecyclemanager.KernelAlternatives in project aws-greengrass-nucleus by aws-greengrass.

the class BaseITCase method setup.

@BeforeAll
static void setup() throws IOException, InterruptedException {
    if (!PlatformResolver.isWindows) {
        return;
    }
    // To test runWith on Windows, need to prepare user and credential
    createWindowsTestUser(WINDOWS_TEST_UESRNAME, WINDOWS_TEST_PASSWORD);
    createWindowsTestUser(WINDOWS_TEST_UESRNAME_2, WINDOWS_TEST_PASSWORD);
    WindowsCredUtils.add(WINDOWS_TEST_UESRNAME, WINDOWS_TEST_PASSWORD.getBytes(WindowsCredUtils.getCharsetForSystem()));
    WindowsCredUtils.add(WINDOWS_TEST_UESRNAME_2, WINDOWS_TEST_PASSWORD.getBytes(WindowsCredUtils.getCharsetForSystem()));
    // mock runas path
    KernelAlternatives mockKernelAlts = mock(KernelAlternatives.class);
    when(mockKernelAlts.getBinDir()).thenReturn(Paths.get("scripts").toAbsolutePath());
    testContext = new Context();
    testContext.put(KernelAlternatives.class, mockKernelAlts);
}
Also used : Context(com.aws.greengrass.dependency.Context) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 4 with KernelAlternatives

use of com.aws.greengrass.lifecyclemanager.KernelAlternatives in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceConfigurationTest method GIVEN_no_launch_param_file_WHEN_persistInitialLaunchParams_THEN_write_jvm_args_to_file.

@Test
void GIVEN_no_launch_param_file_WHEN_persistInitialLaunchParams_THEN_write_jvm_args_to_file(@TempDir Path tempDir) throws Exception {
    deviceConfiguration = new DeviceConfiguration(mockKernel);
    KernelAlternatives kernelAlternatives = mock(KernelAlternatives.class);
    Path tempFile = tempDir.resolve("testFile");
    doReturn(tempFile).when(kernelAlternatives).getLaunchParamsPath();
    deviceConfiguration.persistInitialLaunchParams(kernelAlternatives);
    verify(kernelAlternatives).writeLaunchParamsToFile(anyString());
}
Also used : Path(java.nio.file.Path) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) Test(org.junit.jupiter.api.Test)

Example 5 with KernelAlternatives

use of com.aws.greengrass.lifecyclemanager.KernelAlternatives in project aws-greengrass-nucleus by aws-greengrass.

the class PluginComponentTest method GIVEN_kernel_WHEN_deploy_updated_plugin_THEN_request_kernel_restart.

@Test
void GIVEN_kernel_WHEN_deploy_updated_plugin_THEN_request_kernel_restart(ExtensionContext context) throws Exception {
    ignoreExceptionOfType(context, PackageDownloadException.class);
    ignoreExceptionOfType(context, IOException.class);
    ignoreExceptionOfType(context, SdkClientException.class);
    Kernel kernelSpy = spy(kernel.parseArgs());
    setupPackageStoreAndConfigWithDigest();
    String deploymentId = "deployment1";
    KernelAlternatives kernelAltsSpy = spy(kernelSpy.getContext().get(KernelAlternatives.class));
    kernelSpy.getContext().put(KernelAlternatives.class, kernelAltsSpy);
    // In actual workflow, DeploymentService will setup deployment artifacts directory per deployment before
    // submitting task. Here in test, it's called explicitly because the directory is required by snapshot file.
    kernelSpy.getContext().get(DeploymentDirectoryManager.class).createNewDeploymentDirectory(deploymentId);
    kernelSpy.getContext().put(KernelUpdateActivator.class, new KernelUpdateActivator(kernelSpy, kernelSpy.getContext().get(BootstrapManager.class)));
    // launch Nucleus
    kernelSpy.launch();
    // Ensure that the dependency isn't somehow in our class loader already
    assertThrows(ClassNotFoundException.class, () -> Class.forName("com.aws.greengrass.integrationtests.lifecyclemanager.resource.PluginDependency"));
    // First deployment to add plugin-1.0.0 to kernel
    submitSampleJobDocument(getPluginDeploymentDocument(System.currentTimeMillis(), "1.0.0", deploymentId, FailureHandlingPolicy.DO_NOTHING, componentName), kernelSpy).get(30, TimeUnit.SECONDS);
    GreengrassService eg = kernelSpy.locate(componentName);
    assertEquals("com.aws.greengrass.integrationtests.lifecyclemanager.resource.APluginService", eg.getClass().getName());
    assertEquals(componentId.getVersion().toString(), Coerce.toString(eg.getServiceConfig().findLeafChild(VERSION_CONFIG_KEY)));
    kernelSpy.getContext().get(EZPlugins.class).forName("com.aws.greengrass.integrationtests.lifecyclemanager.resource.PluginDependency");
    // setup again because local files removed by cleanup in the previous deployment
    setupPackageStoreAndConfigWithDigest();
    String deploymentId2 = "deployment2";
    // No need to actually verify directory setup or make directory changes here.
    doReturn(true).when(kernelAltsSpy).isLaunchDirSetup();
    doNothing().when(kernelAltsSpy).prepareBootstrap(eq(deploymentId2));
    doNothing().when(kernelSpy).shutdown(anyInt(), eq(REQUEST_RESTART));
    // Second deployment to add plugin-1.1.0 to kernel which should enter kernel restart workflow
    assertThrows(TimeoutException.class, () -> submitSampleJobDocument(getPluginDeploymentDocument(System.currentTimeMillis(), "1.1.0", deploymentId2, FailureHandlingPolicy.DO_NOTHING, componentName), kernelSpy).get(10, TimeUnit.SECONDS));
    verify(kernelSpy).shutdown(eq(30), eq(REQUEST_RESTART));
}
Also used : EZPlugins(com.aws.greengrass.dependency.EZPlugins) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) DeploymentDirectoryManager(com.aws.greengrass.deployment.DeploymentDirectoryManager) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) KernelUpdateActivator(com.aws.greengrass.deployment.activator.KernelUpdateActivator) Test(org.junit.jupiter.api.Test)

Aggregations

KernelAlternatives (com.aws.greengrass.lifecyclemanager.KernelAlternatives)5 Path (java.nio.file.Path)3 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)2 Kernel (com.aws.greengrass.lifecyclemanager.Kernel)2 Test (org.junit.jupiter.api.Test)2 ComponentType (com.amazon.aws.iot.greengrass.component.common.ComponentType)1 DependencyProperties (com.amazon.aws.iot.greengrass.component.common.DependencyProperties)1 SerializerFactory.getRecipeSerializer (com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializer)1 ComponentStore (com.aws.greengrass.componentmanager.ComponentStore)1 KernelConfigResolver (com.aws.greengrass.componentmanager.KernelConfigResolver)1 CONFIGURATION_CONFIG_KEY (com.aws.greengrass.componentmanager.KernelConfigResolver.CONFIGURATION_CONFIG_KEY)1 VERSION_CONFIG_KEY (com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY)1 RecipeLoader (com.aws.greengrass.componentmanager.converter.RecipeLoader)1 PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)1 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)1 ComponentRecipe (com.aws.greengrass.componentmanager.models.ComponentRecipe)1 CaseInsensitiveString (com.aws.greengrass.config.CaseInsensitiveString)1 ChildChanged (com.aws.greengrass.config.ChildChanged)1 Node (com.aws.greengrass.config.Node)1 Topic (com.aws.greengrass.config.Topic)1