Search in sources :

Example 1 with UpdateAction

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

the class DeploymentConfigMerger method mergeInNewConfig.

/**
 * Merge in new configuration values and new services.
 *
 * @param deployment deployment object
 * @param newConfig  the map of new configuration
 * @return future which completes only once the config is merged and all the services in the config are running
 */
public Future<DeploymentResult> mergeInNewConfig(Deployment deployment, Map<String, Object> newConfig) {
    CompletableFuture<DeploymentResult> totallyCompleteFuture = new CompletableFuture<>();
    DeploymentActivator activator;
    try {
        activator = kernel.getContext().get(DeploymentActivatorFactory.class).getDeploymentActivator(newConfig);
    } catch (ServiceUpdateException | ComponentConfigurationValidationException e) {
        // Failed to pre-process new config, no rollback needed
        logger.atError().setEventType(MERGE_ERROR_LOG_EVENT_KEY).setCause(e).log("Failed to process new configuration for activation");
        totallyCompleteFuture.complete(new DeploymentResult(DeploymentResult.DeploymentStatus.FAILED_NO_STATE_CHANGE, e));
        return totallyCompleteFuture;
    }
    boolean ggcRestart = false;
    if (activator instanceof KernelUpdateActivator) {
        ggcRestart = true;
    }
    DeploymentDocument deploymentDocument = deployment.getDeploymentDocumentObj();
    if (DeploymentComponentUpdatePolicyAction.NOTIFY_COMPONENTS.equals(deploymentDocument.getComponentUpdatePolicy().getComponentUpdatePolicyAction())) {
        kernel.getContext().get(UpdateSystemPolicyService.class).addUpdateAction(deploymentDocument.getDeploymentId(), new UpdateAction(deploymentDocument.getDeploymentId(), ggcRestart, deploymentDocument.getComponentUpdatePolicy().getTimeout(), () -> updateActionForDeployment(newConfig, deployment, activator, totallyCompleteFuture)));
    } else {
        logger.atInfo().log("Deployment is configured to skip update policy check," + " not waiting for disruptable time to update");
        updateActionForDeployment(newConfig, deployment, activator, totallyCompleteFuture);
    }
    return totallyCompleteFuture;
}
Also used : UpdateSystemPolicyService(com.aws.greengrass.lifecyclemanager.UpdateSystemPolicyService) CompletableFuture(java.util.concurrent.CompletableFuture) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) UpdateAction(com.aws.greengrass.lifecyclemanager.UpdateAction) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) ComponentConfigurationValidationException(com.aws.greengrass.deployment.exceptions.ComponentConfigurationValidationException) KernelUpdateActivator(com.aws.greengrass.deployment.activator.KernelUpdateActivator) ServiceUpdateException(com.aws.greengrass.deployment.exceptions.ServiceUpdateException) DeploymentActivator(com.aws.greengrass.deployment.activator.DeploymentActivator)

Example 2 with UpdateAction

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

the class DeploymentConfigMergerTest method GIVEN_deployment_WHEN_task_cancelled_THEN_update_is_cancelled.

@Test
void GIVEN_deployment_WHEN_task_cancelled_THEN_update_is_cancelled() throws Throwable {
    ArgumentCaptor<UpdateAction> cancelledTaskCaptor = ArgumentCaptor.forClass(UpdateAction.class);
    UpdateSystemPolicyService updateSystemPolicyService = mock(UpdateSystemPolicyService.class);
    DeploymentActivatorFactory factory = mock(DeploymentActivatorFactory.class);
    when(factory.getDeploymentActivator(anyMap())).thenReturn(mock(KernelUpdateActivator.class));
    when(context.get(any())).thenAnswer(invocationOnMock -> {
        Object argument = invocationOnMock.getArgument(0);
        if (UpdateSystemPolicyService.class.equals(argument)) {
            return updateSystemPolicyService;
        } else if (DeploymentActivatorFactory.class.equals(argument)) {
            return factory;
        }
        throw new InvalidUseOfMatchersException(String.format("Argument %s does not match", argument));
    });
    // GIVEN
    DeploymentConfigMerger merger = new DeploymentConfigMerger(kernel, deviceConfiguration, validator);
    DeploymentDocument doc = mock(DeploymentDocument.class);
    when(doc.getDeploymentId()).thenReturn("DeploymentId");
    when(doc.getComponentUpdatePolicy()).thenReturn(new ComponentUpdatePolicy(0, NOTIFY_COMPONENTS));
    Future<DeploymentResult> fut = merger.mergeInNewConfig(createMockDeployment(doc), new HashMap<>());
    verify(updateSystemPolicyService).addUpdateAction(any(), cancelledTaskCaptor.capture());
    assertEquals(0, cancelledTaskCaptor.getValue().getTimeout());
    assertEquals("DeploymentId", cancelledTaskCaptor.getValue().getDeploymentId());
    assertTrue(cancelledTaskCaptor.getValue().isGgcRestart());
    // WHEN
    fut.cancel(true);
    cancelledTaskCaptor.getValue().getAction().run();
    // THEN
    verify(doc, times(0)).getFailureHandlingPolicy();
}
Also used : DeploymentActivatorFactory(com.aws.greengrass.deployment.activator.DeploymentActivatorFactory) ComponentUpdatePolicy(com.aws.greengrass.deployment.model.ComponentUpdatePolicy) UpdateAction(com.aws.greengrass.lifecyclemanager.UpdateAction) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) KernelUpdateActivator(com.aws.greengrass.deployment.activator.KernelUpdateActivator) UpdateSystemPolicyService(com.aws.greengrass.lifecyclemanager.UpdateSystemPolicyService) InvalidUseOfMatchersException(org.mockito.exceptions.misusing.InvalidUseOfMatchersException) Test(org.junit.jupiter.api.Test)

Example 3 with UpdateAction

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

the class DeploymentConfigMergerTest method GIVEN_deployment_activate_WHEN_deployment_has_some_new_config_THEN_old_config_is_validated.

@Test
void GIVEN_deployment_activate_WHEN_deployment_has_some_new_config_THEN_old_config_is_validated(ExtensionContext extensionContext) throws Throwable {
    ArgumentCaptor<UpdateAction> taskCaptor = ArgumentCaptor.forClass(UpdateAction.class);
    UpdateSystemPolicyService updateSystemPolicyService = mock(UpdateSystemPolicyService.class);
    when(context.get(UpdateSystemPolicyService.class)).thenReturn(updateSystemPolicyService);
    DeploymentActivatorFactory deploymentActivatorFactory = new DeploymentActivatorFactory(kernel);
    when(context.get(DeploymentActivatorFactory.class)).thenReturn(deploymentActivatorFactory);
    BootstrapManager bootstrapManager = mock(BootstrapManager.class);
    when(bootstrapManager.isBootstrapRequired(any())).thenReturn(false);
    when(context.get(BootstrapManager.class)).thenReturn(bootstrapManager);
    DefaultActivator defaultActivator = mock(DefaultActivator.class);
    when(context.get(DefaultActivator.class)).thenReturn(defaultActivator);
    Topic regionTopic = Topic.of(context, DEVICE_PARAM_AWS_REGION, "us-west-2");
    when(deviceConfiguration.getAWSRegion()).thenReturn(regionTopic);
    Topic credEndpointTopic = Topic.of(context, DEVICE_PARAM_IOT_CRED_ENDPOINT, "xxxxxx.credentials.iot.us-west-2.amazonaws.com");
    when(deviceConfiguration.getIotCredentialEndpoint()).thenReturn(credEndpointTopic);
    Topic dataEndpointTopic = Topic.of(context, DEVICE_PARAM_IOT_DATA_ENDPOINT, "xxxxxx-ats.iot.us-west-2.amazonaws.com");
    when(deviceConfiguration.getIotDataEndpoint()).thenReturn(dataEndpointTopic);
    when(deviceConfiguration.getNucleusComponentName()).thenReturn(DEFAULT_NUCLEUS_COMPONENT_NAME);
    ArgumentCaptor<String> regionCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> credEndpointCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> dataEndpointCaptor = ArgumentCaptor.forClass(String.class);
    ignoreExceptionOfType(extensionContext, IOException.class);
    Map<String, Object> newConfig = new HashMap<>();
    Map<String, Object> newConfig2 = new HashMap<>();
    Map<String, Object> newConfig3 = new HashMap<>();
    Map<String, Object> newConfig4 = new HashMap<>();
    newConfig4.put(DEVICE_PARAM_AWS_REGION, "us-east-1");
    newConfig3.put(CONFIGURATION_CONFIG_KEY, newConfig4);
    newConfig2.put(DEFAULT_NUCLEUS_COMPONENT_NAME, newConfig3);
    newConfig.put(SERVICES_NAMESPACE_TOPIC, newConfig2);
    // GIVEN
    DeploymentConfigMerger merger = new DeploymentConfigMerger(kernel, deviceConfiguration, validator);
    DeploymentDocument doc = mock(DeploymentDocument.class);
    when(doc.getDeploymentId()).thenReturn("DeploymentId");
    when(doc.getComponentUpdatePolicy()).thenReturn(new ComponentUpdatePolicy(0, NOTIFY_COMPONENTS));
    merger.mergeInNewConfig(createMockDeployment(doc), newConfig);
    verify(updateSystemPolicyService).addUpdateAction(any(), taskCaptor.capture());
    assertEquals(0, taskCaptor.getValue().getTimeout());
    assertEquals("DeploymentId", taskCaptor.getValue().getDeploymentId());
    assertFalse(taskCaptor.getValue().isGgcRestart());
    // WHEN
    taskCaptor.getValue().getAction().run();
    // THEN
    verify(defaultActivator, times(1)).activate(any(), any(), any());
    verify(deviceConfiguration, times(1)).validateEndpoints(regionCaptor.capture(), credEndpointCaptor.capture(), dataEndpointCaptor.capture());
    assertNotNull(regionCaptor.getValue());
    assertEquals("us-east-1", regionCaptor.getValue());
    assertNotNull(credEndpointCaptor.getValue());
    assertEquals("xxxxxx.credentials.iot.us-west-2.amazonaws.com", credEndpointCaptor.getValue());
    assertNotNull(dataEndpointCaptor.getValue());
    assertEquals("xxxxxx-ats.iot.us-west-2.amazonaws.com", dataEndpointCaptor.getValue());
}
Also used : DeploymentActivatorFactory(com.aws.greengrass.deployment.activator.DeploymentActivatorFactory) BootstrapManager(com.aws.greengrass.deployment.bootstrap.BootstrapManager) ComponentUpdatePolicy(com.aws.greengrass.deployment.model.ComponentUpdatePolicy) UpdateAction(com.aws.greengrass.lifecyclemanager.UpdateAction) HashMap(java.util.HashMap) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) UpdateSystemPolicyService(com.aws.greengrass.lifecyclemanager.UpdateSystemPolicyService) DefaultActivator(com.aws.greengrass.deployment.activator.DefaultActivator) Topic(com.aws.greengrass.config.Topic) Test(org.junit.jupiter.api.Test)

Example 4 with UpdateAction

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

the class DeploymentConfigMergerTest method GIVEN_deployment_WHEN_task_not_cancelled_THEN_update_is_continued.

@Test
void GIVEN_deployment_WHEN_task_not_cancelled_THEN_update_is_continued() throws Throwable {
    ArgumentCaptor<UpdateAction> taskCaptor = ArgumentCaptor.forClass(UpdateAction.class);
    UpdateSystemPolicyService updateSystemPolicyService = mock(UpdateSystemPolicyService.class);
    when(context.get(UpdateSystemPolicyService.class)).thenReturn(updateSystemPolicyService);
    DeploymentActivatorFactory deploymentActivatorFactory = new DeploymentActivatorFactory(kernel);
    when(context.get(DeploymentActivatorFactory.class)).thenReturn(deploymentActivatorFactory);
    BootstrapManager bootstrapManager = mock(BootstrapManager.class);
    when(bootstrapManager.isBootstrapRequired(any())).thenReturn(false);
    when(context.get(BootstrapManager.class)).thenReturn(bootstrapManager);
    DefaultActivator defaultActivator = mock(DefaultActivator.class);
    when(context.get(DefaultActivator.class)).thenReturn(defaultActivator);
    // GIVEN
    DeploymentConfigMerger merger = new DeploymentConfigMerger(kernel, deviceConfiguration, validator);
    DeploymentDocument doc = mock(DeploymentDocument.class);
    when(doc.getDeploymentId()).thenReturn("DeploymentId");
    when(doc.getComponentUpdatePolicy()).thenReturn(new ComponentUpdatePolicy(0, NOTIFY_COMPONENTS));
    merger.mergeInNewConfig(createMockDeployment(doc), new HashMap<>());
    verify(updateSystemPolicyService).addUpdateAction(any(), taskCaptor.capture());
    assertEquals(0, taskCaptor.getValue().getTimeout());
    assertEquals("DeploymentId", taskCaptor.getValue().getDeploymentId());
    assertFalse(taskCaptor.getValue().isGgcRestart());
    // WHEN
    taskCaptor.getValue().getAction().run();
    // THEN
    verify(defaultActivator, times(1)).activate(any(), any(), any());
}
Also used : UpdateSystemPolicyService(com.aws.greengrass.lifecyclemanager.UpdateSystemPolicyService) DeploymentActivatorFactory(com.aws.greengrass.deployment.activator.DeploymentActivatorFactory) BootstrapManager(com.aws.greengrass.deployment.bootstrap.BootstrapManager) DefaultActivator(com.aws.greengrass.deployment.activator.DefaultActivator) ComponentUpdatePolicy(com.aws.greengrass.deployment.model.ComponentUpdatePolicy) UpdateAction(com.aws.greengrass.lifecyclemanager.UpdateAction) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) Test(org.junit.jupiter.api.Test)

Example 5 with UpdateAction

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

the class DeploymentConfigMergerTest method GIVEN_deployment_activate_WHEN_deployment_has_new_config_THEN_new_config_is_validated.

@Test
void GIVEN_deployment_activate_WHEN_deployment_has_new_config_THEN_new_config_is_validated(ExtensionContext extensionContext) throws Throwable {
    ArgumentCaptor<UpdateAction> taskCaptor = ArgumentCaptor.forClass(UpdateAction.class);
    UpdateSystemPolicyService updateSystemPolicyService = mock(UpdateSystemPolicyService.class);
    when(context.get(UpdateSystemPolicyService.class)).thenReturn(updateSystemPolicyService);
    DeploymentActivatorFactory deploymentActivatorFactory = new DeploymentActivatorFactory(kernel);
    when(context.get(DeploymentActivatorFactory.class)).thenReturn(deploymentActivatorFactory);
    BootstrapManager bootstrapManager = mock(BootstrapManager.class);
    when(bootstrapManager.isBootstrapRequired(any())).thenReturn(false);
    when(context.get(BootstrapManager.class)).thenReturn(bootstrapManager);
    DefaultActivator defaultActivator = mock(DefaultActivator.class);
    when(context.get(DefaultActivator.class)).thenReturn(defaultActivator);
    Topic regionTopic = Topic.of(context, DEVICE_PARAM_AWS_REGION, "us-west-2");
    when(deviceConfiguration.getAWSRegion()).thenReturn(regionTopic);
    Topic credEndpointTopic = Topic.of(context, DEVICE_PARAM_IOT_CRED_ENDPOINT, "xxxxxx.credentials.iot.us-west-2.amazonaws.com");
    when(deviceConfiguration.getIotCredentialEndpoint()).thenReturn(credEndpointTopic);
    Topic dataEndpointTopic = Topic.of(context, DEVICE_PARAM_IOT_DATA_ENDPOINT, "xxxxxx-ats.iot.us-west-2.amazonaws.com");
    when(deviceConfiguration.getIotDataEndpoint()).thenReturn(dataEndpointTopic);
    when(deviceConfiguration.getNucleusComponentName()).thenReturn(DEFAULT_NUCLEUS_COMPONENT_NAME);
    ArgumentCaptor<String> regionCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> credEndpointCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> dataEndpointCaptor = ArgumentCaptor.forClass(String.class);
    ignoreExceptionOfType(extensionContext, IOException.class);
    Map<String, Object> newConfig = new HashMap<>();
    Map<String, Object> newConfig2 = new HashMap<>();
    Map<String, Object> newConfig3 = new HashMap<>();
    Map<String, Object> newConfig4 = new HashMap<>();
    newConfig4.put(DEVICE_PARAM_AWS_REGION, "us-east-1");
    newConfig4.put(DEVICE_PARAM_IOT_CRED_ENDPOINT, "xxxxxx.credentials.iot.us-east-1.amazonaws.com");
    newConfig4.put(DEVICE_PARAM_IOT_DATA_ENDPOINT, "xxxxxx-ats.iot.us-east-1.amazonaws.com");
    newConfig3.put(CONFIGURATION_CONFIG_KEY, newConfig4);
    newConfig2.put(DEFAULT_NUCLEUS_COMPONENT_NAME, newConfig3);
    newConfig.put(SERVICES_NAMESPACE_TOPIC, newConfig2);
    // GIVEN
    DeploymentConfigMerger merger = new DeploymentConfigMerger(kernel, deviceConfiguration, validator);
    DeploymentDocument doc = mock(DeploymentDocument.class);
    when(doc.getDeploymentId()).thenReturn("DeploymentId");
    when(doc.getComponentUpdatePolicy()).thenReturn(new ComponentUpdatePolicy(0, NOTIFY_COMPONENTS));
    merger.mergeInNewConfig(createMockDeployment(doc), newConfig);
    verify(updateSystemPolicyService).addUpdateAction(any(), taskCaptor.capture());
    assertEquals(0, taskCaptor.getValue().getTimeout());
    assertEquals("DeploymentId", taskCaptor.getValue().getDeploymentId());
    assertFalse(taskCaptor.getValue().isGgcRestart());
    // WHEN
    taskCaptor.getValue().getAction().run();
    // THEN
    verify(defaultActivator, times(1)).activate(any(), any(), any());
    verify(deviceConfiguration, times(1)).validateEndpoints(regionCaptor.capture(), credEndpointCaptor.capture(), dataEndpointCaptor.capture());
    assertNotNull(regionCaptor.getValue());
    assertEquals("us-east-1", regionCaptor.getValue());
    assertNotNull(credEndpointCaptor.getValue());
    assertEquals("xxxxxx.credentials.iot.us-east-1.amazonaws.com", credEndpointCaptor.getValue());
    assertNotNull(dataEndpointCaptor.getValue());
    assertEquals("xxxxxx-ats.iot.us-east-1.amazonaws.com", dataEndpointCaptor.getValue());
}
Also used : DeploymentActivatorFactory(com.aws.greengrass.deployment.activator.DeploymentActivatorFactory) BootstrapManager(com.aws.greengrass.deployment.bootstrap.BootstrapManager) ComponentUpdatePolicy(com.aws.greengrass.deployment.model.ComponentUpdatePolicy) UpdateAction(com.aws.greengrass.lifecyclemanager.UpdateAction) HashMap(java.util.HashMap) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) UpdateSystemPolicyService(com.aws.greengrass.lifecyclemanager.UpdateSystemPolicyService) DefaultActivator(com.aws.greengrass.deployment.activator.DefaultActivator) Topic(com.aws.greengrass.config.Topic) Test(org.junit.jupiter.api.Test)

Aggregations

DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)5 UpdateAction (com.aws.greengrass.lifecyclemanager.UpdateAction)5 UpdateSystemPolicyService (com.aws.greengrass.lifecyclemanager.UpdateSystemPolicyService)5 DeploymentActivatorFactory (com.aws.greengrass.deployment.activator.DeploymentActivatorFactory)4 ComponentUpdatePolicy (com.aws.greengrass.deployment.model.ComponentUpdatePolicy)4 Test (org.junit.jupiter.api.Test)4 DefaultActivator (com.aws.greengrass.deployment.activator.DefaultActivator)3 BootstrapManager (com.aws.greengrass.deployment.bootstrap.BootstrapManager)3 Topic (com.aws.greengrass.config.Topic)2 KernelUpdateActivator (com.aws.greengrass.deployment.activator.KernelUpdateActivator)2 DeploymentResult (com.aws.greengrass.deployment.model.DeploymentResult)2 HashMap (java.util.HashMap)2 DeploymentActivator (com.aws.greengrass.deployment.activator.DeploymentActivator)1 ComponentConfigurationValidationException (com.aws.greengrass.deployment.exceptions.ComponentConfigurationValidationException)1 ServiceUpdateException (com.aws.greengrass.deployment.exceptions.ServiceUpdateException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 InvalidUseOfMatchersException (org.mockito.exceptions.misusing.InvalidUseOfMatchersException)1