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;
}
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();
}
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());
}
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());
}
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());
}
Aggregations