Search in sources :

Example 16 with Kernel

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

the class DockerImageArtifactDownloadTest method before.

@BeforeEach
void before() throws Exception {
    Instant credentialsExpiry = Instant.now().plusSeconds(10);
    AuthorizationData authorizationData = AuthorizationData.builder().authorizationToken(Base64.getEncoder().encodeToString("username:password".getBytes(StandardCharsets.UTF_8))).expiresAt(credentialsExpiry).build();
    GetAuthorizationTokenResponse response = GetAuthorizationTokenResponse.builder().authorizationData(authorizationData).build();
    lenient().when(ecrClient.getAuthorizationToken(any(GetAuthorizationTokenRequest.class))).thenReturn(response);
    lenient().when(dockerClient.dockerInstalled()).thenReturn(true);
    AtomicBoolean mqttOnline = new AtomicBoolean(true);
    lenient().when(mqttClient.getMqttOnline()).thenReturn(mqttOnline);
    kernel = new Kernel();
    NucleusPaths nucleusPaths = kernel.getNucleusPaths();
    nucleusPaths.setComponentStorePath(tempRootDir);
    ComponentStore store = new ComponentStore(nucleusPaths, platformResolver, recipeLoader);
    EcrAccessor ecrAccessor = new EcrAccessor(ecrClient);
    kernel.getContext().put(ComponentStore.class, store);
    kernel.getContext().put(EcrAccessor.class, ecrAccessor);
    kernel.getContext().put(DefaultDockerClient.class, dockerClient);
    kernel.getContext().put(MqttClient.class, mqttClient);
    preloadLocalStoreContent();
    componentManager = kernel.getContext().get(ComponentManager.class);
}
Also used : GetAuthorizationTokenResponse(software.amazon.awssdk.services.ecr.model.GetAuthorizationTokenResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GetAuthorizationTokenRequest(software.amazon.awssdk.services.ecr.model.GetAuthorizationTokenRequest) AuthorizationData(software.amazon.awssdk.services.ecr.model.AuthorizationData) EcrAccessor(com.aws.greengrass.componentmanager.plugins.docker.EcrAccessor) NucleusPaths(com.aws.greengrass.util.NucleusPaths) Instant(java.time.Instant) ComponentManager(com.aws.greengrass.componentmanager.ComponentManager) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 17 with Kernel

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

the class KernelUpdateDeploymentTask method call.

@SuppressWarnings({ "PMD.AvoidDuplicateLiterals" })
@Override
public DeploymentResult call() {
    Deployment.DeploymentStage stage = deployment.getDeploymentStage();
    try {
        List<GreengrassService> servicesToTrack = kernel.orderedDependencies().stream().filter(GreengrassService::shouldAutoStart).filter(o -> !kernel.getMain().equals(o)).collect(Collectors.toList());
        long mergeTimestamp = kernel.getConfig().lookup("system", "rootpath").getModtime();
        logger.atDebug().kv("serviceToTrack", servicesToTrack).kv("mergeTime", mergeTimestamp).log("Nucleus update workflow waiting for services to complete update");
        DeploymentConfigMerger.waitForServicesToStart(servicesToTrack, mergeTimestamp);
        DeploymentResult result = null;
        if (KERNEL_ACTIVATION.equals(stage)) {
            result = new DeploymentResult(DeploymentResult.DeploymentStatus.SUCCESSFUL, null);
        } else if (KERNEL_ROLLBACK.equals(stage)) {
            result = new DeploymentResult(DeploymentResult.DeploymentStatus.FAILED_ROLLBACK_COMPLETE, getDeploymentStatusDetails());
        }
        componentManager.cleanupStaleVersions();
        return result;
    } catch (InterruptedException e) {
        logger.atError("deployment-interrupted", e).log();
        try {
            saveDeploymentStatusDetails(e.getMessage());
        } catch (IOException ioException) {
            logger.atError().log("Failed to persist deployment error information", ioException);
        }
        // Interrupted workflow. Shutdown kernel and retry this stage.
        kernel.shutdown(30, REQUEST_RESTART);
        return null;
    } catch (ServiceUpdateException e) {
        logger.atError("deployment-errored", e).log();
        if (KERNEL_ACTIVATION.equals(stage)) {
            try {
                deployment.setDeploymentStage(KERNEL_ROLLBACK);
                saveDeploymentStatusDetails(e.getMessage());
                // Rollback workflow. Flip symlinks and restart kernel
                kernel.getContext().get(KernelAlternatives.class).prepareRollback();
                kernel.shutdown(30, REQUEST_RESTART);
            } catch (IOException ioException) {
                logger.atError().log("Failed to set up Nucleus rollback directory", ioException);
                return new DeploymentResult(DeploymentResult.DeploymentStatus.FAILED_UNABLE_TO_ROLLBACK, e);
            }
            return null;
        } else if (KERNEL_ROLLBACK.equals(stage)) {
            logger.atError().log("Nucleus update workflow failed on rollback", e);
            return new DeploymentResult(DeploymentResult.DeploymentStatus.FAILED_UNABLE_TO_ROLLBACK, getDeploymentStatusDetails());
        }
        return null;
    }
}
Also used : KERNEL_ROLLBACK(com.aws.greengrass.deployment.model.Deployment.DeploymentStage.KERNEL_ROLLBACK) Deployment(com.aws.greengrass.deployment.model.Deployment) IOException(java.io.IOException) DeploymentTask(com.aws.greengrass.deployment.model.DeploymentTask) Collectors(java.util.stream.Collectors) ComponentManager(com.aws.greengrass.componentmanager.ComponentManager) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) List(java.util.List) Utils(com.aws.greengrass.util.Utils) DEPLOYMENT_ID_LOG_KEY(com.aws.greengrass.deployment.DeploymentConfigMerger.DEPLOYMENT_ID_LOG_KEY) ServiceUpdateException(com.aws.greengrass.deployment.exceptions.ServiceUpdateException) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) REQUEST_RESTART(com.aws.greengrass.deployment.bootstrap.BootstrapSuccessCode.REQUEST_RESTART) KERNEL_ACTIVATION(com.aws.greengrass.deployment.model.Deployment.DeploymentStage.KERNEL_ACTIVATION) Logger(com.aws.greengrass.logging.api.Logger) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) Deployment(com.aws.greengrass.deployment.model.Deployment) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) IOException(java.io.IOException) ServiceUpdateException(com.aws.greengrass.deployment.exceptions.ServiceUpdateException)

Example 18 with Kernel

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

the class GreengrassSetupTest method GIVEN_invalid_user_WHEN_script_is_used_THEN_error.

@ParameterizedTest
@MethodSource("invalidUsers")
@DisabledOnOs(OS.WINDOWS)
void GIVEN_invalid_user_WHEN_script_is_used_THEN_error(String user, ExtensionContext context) throws Exception {
    ignoreExceptionUltimateCauseOfType(context, IOException.class);
    Kernel realKernel = new Kernel();
    greengrassSetup = new GreengrassSetup(System.out, System.err, deviceProvisioningHelper, platform, realKernel, "--component-default-user", user, "--aws-region", "us-east-1", "-ss", "false");
    Exception e = assertThrows(RuntimeException.class, () -> {
        greengrassSetup.parseArgs();
        greengrassSetup.performSetup();
    });
    realKernel.shutdown();
    assertThat(e.getMessage(), containsString(String.format("Error while looking up primary group for %s. No " + "group specified for the user", user)));
}
Also used : Kernel(com.aws.greengrass.lifecyclemanager.Kernel) IOException(java.io.IOException) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 19 with Kernel

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

the class FleetStatusServiceSetupTest method setupKernel.

@BeforeEach
void setupKernel() throws Exception {
    fleetStatusDetails = new AtomicReference<>();
    kernel = new Kernel();
    ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, FleetStatusServiceSetupTest.class.getResource("onlyMain.yaml"));
    kernel.getContext().put(MqttClient.class, mqttClient);
    when(mqttClient.publish(any(PublishRequest.class))).thenAnswer(i -> {
        Object argument = i.getArgument(0);
        PublishRequest publishRequest = (PublishRequest) argument;
        try {
            fleetStatusDetails.set(OBJECT_MAPPER.readValue(publishRequest.getPayload(), FleetStatusDetails.class));
        } catch (JsonMappingException ignored) {
        }
        return CompletableFuture.completedFuture(0);
    });
}
Also used : FleetStatusDetails(com.aws.greengrass.status.FleetStatusDetails) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) PublishRequest(com.aws.greengrass.mqttclient.PublishRequest) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 20 with Kernel

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

the class IotJobsFleetStatusServiceTest method setupKernel.

@SuppressWarnings("PMD.CloseResource")
@BeforeEach
void setupKernel(ExtensionContext context) throws Exception {
    ignoreExceptionOfType(context, TLSAuthException.class);
    ignoreExceptionOfType(context, PackageDownloadException.class);
    ignoreExceptionUltimateCauseOfType(context, EOFException.class);
    ignoreExceptionUltimateCauseOfType(context, ResourceNotFoundException.class);
    CountDownLatch fssRunning = new CountDownLatch(1);
    CountDownLatch deploymentServiceRunning = new CountDownLatch(1);
    CompletableFuture<Void> cf = new CompletableFuture<>();
    cf.complete(null);
    lenient().when(mockIotJobsClientWrapper.PublishUpdateJobExecution(any(UpdateJobExecutionRequest.class), any(QualityOfService.class))).thenAnswer(invocationOnMock -> {
        verify(mockIotJobsClientWrapper, atLeastOnce()).SubscribeToUpdateJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), jobsAcceptedHandlerCaptor.capture());
        Consumer<UpdateJobExecutionResponse> jobResponseConsumer = jobsAcceptedHandlerCaptor.getValue();
        UpdateJobExecutionResponse mockJobExecutionResponse = mock(UpdateJobExecutionResponse.class);
        jobResponseConsumer.accept(mockJobExecutionResponse);
        return cf;
    });
    lenient().when(mqttClient.publish(any())).thenReturn(CompletableFuture.completedFuture(0));
    kernel = new Kernel();
    NoOpPathOwnershipHandler.register(kernel);
    ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, IotJobsFleetStatusServiceTest.class.getResource("onlyMain.yaml"));
    kernel.getContext().put(MqttClient.class, mqttClient);
    kernel.getContext().put(ThingGroupHelper.class, thingGroupHelper);
    // Mock out cloud communication
    GreengrassServiceClientFactory mgscf = mock(GreengrassServiceClientFactory.class);
    GreengrassV2DataClient mcf = mock(GreengrassV2DataClient.class);
    lenient().when(mcf.resolveComponentCandidates(any(ResolveComponentCandidatesRequest.class))).thenThrow(ResourceNotFoundException.class);
    lenient().when(mgscf.getGreengrassV2DataClient()).thenReturn(mcf);
    kernel.getContext().put(GreengrassServiceClientFactory.class, mgscf);
    componentNamesToCheck.clear();
    kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
        if (service.getName().equals(FleetStatusService.FLEET_STATUS_SERVICE_TOPICS) && newState.equals(State.RUNNING)) {
            fssRunning.countDown();
        }
        if (service.getName().equals(DeploymentService.DEPLOYMENT_SERVICE_TOPICS) && newState.equals(State.RUNNING)) {
            deploymentServiceRunning.countDown();
            deploymentService = (DeploymentService) service;
            IotJobsHelper iotJobsHelper = deploymentService.getContext().get(IotJobsHelper.class);
            iotJobsHelper.setIotJobsClientWrapper(mockIotJobsClientWrapper);
        }
        componentNamesToCheck.add(service.getName());
    });
    // set required instances from context
    deviceConfiguration = new DeviceConfiguration(kernel, "ThingName", "xxxxxx-ats.iot.us-east-1.amazonaws.com", "xxxxxx.credentials.iot.us-east-1.amazonaws.com", "privKeyFilePath", "certFilePath", "caFilePath", "us-east-1", "roleAliasName");
    kernel.getContext().put(DeviceConfiguration.class, deviceConfiguration);
    // pre-load contents to package store
    Path localStoreContentPath = Paths.get(IotJobsFleetStatusServiceTest.class.getResource("local_store_content").toURI());
    PreloadComponentStoreHelper.preloadRecipesFromTestResourceDir(localStoreContentPath.resolve("recipes"), kernel.getNucleusPaths().recipePath());
    copyFolderRecursively(localStoreContentPath.resolve("artifacts"), kernel.getNucleusPaths().artifactPath(), REPLACE_EXISTING);
    kernel.launch();
    assertTrue(fssRunning.await(10, TimeUnit.SECONDS));
    assertTrue(deploymentServiceRunning.await(10, TimeUnit.SECONDS));
}
Also used : Path(java.nio.file.Path) UpdateJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionResponse) GreengrassServiceClientFactory(com.aws.greengrass.util.GreengrassServiceClientFactory) CountDownLatch(java.util.concurrent.CountDownLatch) GreengrassV2DataClient(software.amazon.awssdk.services.greengrassv2data.GreengrassV2DataClient) CompletableFuture(java.util.concurrent.CompletableFuture) IotJobsHelper(com.aws.greengrass.deployment.IotJobsHelper) UpdateJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) ResolveComponentCandidatesRequest(software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Kernel (com.aws.greengrass.lifecyclemanager.Kernel)49 CountDownLatch (java.util.concurrent.CountDownLatch)22 BeforeEach (org.junit.jupiter.api.BeforeEach)22 Test (org.junit.jupiter.api.Test)22 HashMap (java.util.HashMap)13 IOException (java.io.IOException)12 DeploymentConfigMerger (com.aws.greengrass.deployment.DeploymentConfigMerger)11 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)11 Map (java.util.Map)11 List (java.util.List)10 TimeUnit (java.util.concurrent.TimeUnit)10 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)10 State (com.aws.greengrass.dependency.State)9 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)9 BaseITCase (com.aws.greengrass.integrationtests.BaseITCase)9 ConfigPlatformResolver (com.aws.greengrass.integrationtests.util.ConfigPlatformResolver)9 TestUtils (com.aws.greengrass.testcommons.testutilities.TestUtils)9 Coerce (com.aws.greengrass.util.Coerce)9 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)9 LogManager (com.aws.greengrass.logging.impl.LogManager)8