Search in sources :

Example 1 with Configuration

use of com.amazon.aws.iot.greengrass.configuration.common.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentServiceIntegrationTest method submitSampleJobDocument.

private void submitSampleJobDocument(URI uri, String arn, DeploymentType type) throws Exception {
    Configuration deploymentConfiguration = OBJECT_MAPPER.readValue(new File(uri), Configuration.class);
    deploymentConfiguration.setCreationTimestamp(System.currentTimeMillis());
    deploymentConfiguration.setConfigurationArn(arn);
    Deployment deployment = new Deployment(OBJECT_MAPPER.writeValueAsString(deploymentConfiguration), type, deploymentConfiguration.getConfigurationArn());
    deploymentQueue.offer(deployment);
}
Also used : DeploymentDocumentConverter.convertFromDeploymentConfiguration(com.aws.greengrass.deployment.converter.DeploymentDocumentConverter.convertFromDeploymentConfiguration) Configuration(com.amazon.aws.iot.greengrass.configuration.common.Configuration) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Deployment(com.aws.greengrass.deployment.model.Deployment) File(java.io.File)

Example 2 with Configuration

use of com.amazon.aws.iot.greengrass.configuration.common.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class ShadowDeploymentListener method shadowUpdated.

protected void shadowUpdated(Map<String, Object> desired, Map<String, Object> reported, Integer version) {
    if (lastVersion.get() > version) {
        logger.atDebug().kv("SHADOW_VERSION", version).log("Received an older version of shadow. Ignoring...");
        return;
    }
    lastVersion.set(version);
    // the reported section of the shadow was updated
    if (reported != null && !reported.isEmpty()) {
        syncShadowDeploymentStatus(reported);
    }
    if (desired == null || desired.isEmpty()) {
        logger.debug("Empty desired state, no update to desired section or no device deployments created yet");
        return;
    }
    String fleetConfigStr = (String) desired.get(FLEET_CONFIG_KEY);
    Configuration configuration;
    try {
        configuration = SerializerFactory.getFailSafeJsonObjectMapper().readValue(fleetConfigStr, Configuration.class);
    } catch (JsonProcessingException e) {
        logger.atError().log("failed to process shadow update", e);
        return;
    }
    String configurationArn = configuration.getConfigurationArn();
    if (configurationArn == null) {
        logger.atError().log("Desired state has null configuration ARN. Ignoring shadow update");
        return;
    }
    String desiredStatus = (String) desired.get(DESIRED_STATUS_KEY);
    if (desiredStatus == null) {
        logger.atError().log("Desired status is null. Ignoring shadow update");
        return;
    }
    boolean cancelDeployment = DESIRED_STATUS_CANCELED.equals(desiredStatus);
    synchronized (ShadowDeploymentListener.class) {
        // If lastConfigurationArn is null, this is the first shadow update since startup
        if (lastConfigurationArn == null) {
            lastConfigurationArn = configurationArn;
            // Ignore if the latest deployment was canceled
            if (cancelDeployment) {
                logger.atInfo().kv(CONFIGURATION_ARN_LOG_KEY_NAME, configurationArn).log("Deployment was canceled. Ignoring shadow update at startup");
                return;
            }
            // the reported status is terminal (i.e. not in_progress) because it's already fully processed
            if (reported != null && configurationArn.equals(reported.get(ARN_FOR_STATUS_KEY)) && !JobStatus.IN_PROGRESS.toString().equals(reported.get(STATUS_KEY))) {
                logger.atInfo().kv(CONFIGURATION_ARN_LOG_KEY_NAME, configurationArn).log("Deployment result already reported. Ignoring shadow update at startup");
                return;
            }
            // Ignore if it's the ongoing deployment. This can happen if the last shadow deployment caused restart
            try {
                // Using locate instead of injection here because DeploymentService lacks usable injection
                // constructor. Same as in IotJobsHelper.evaluateCancellationAndCancelDeploymentIfNeeded
                GreengrassService deploymentServiceLocateResult = kernel.locate(DeploymentService.DEPLOYMENT_SERVICE_TOPICS);
                if (deploymentServiceLocateResult instanceof DeploymentService) {
                    DeploymentTaskMetadata currentDeployment = ((DeploymentService) deploymentServiceLocateResult).getCurrentDeploymentTaskMetadata();
                    if (currentDeployment != null && configurationArn.equals(currentDeployment.getDeploymentId())) {
                        logger.atInfo().kv(CONFIGURATION_ARN_LOG_KEY_NAME, configurationArn).log("Ongoing deployment. Ignoring shadow update at startup");
                        return;
                    }
                }
            } catch (ServiceLoadException e) {
                logger.atError().setCause(e).log("Failed to find deployment service");
            }
        } else {
            if (lastConfigurationArn.equals(configurationArn) && !cancelDeployment) {
                logger.atInfo().kv(CONFIGURATION_ARN_LOG_KEY_NAME, configurationArn).log("Duplicate deployment notification. Ignoring shadow update");
                return;
            }
            lastConfigurationArn = configurationArn;
        }
    }
    Deployment deployment;
    if (cancelDeployment) {
        deployment = new Deployment(DeploymentType.SHADOW, UUID.randomUUID().toString(), true);
    } else {
        deployment = new Deployment(fleetConfigStr, DeploymentType.SHADOW, configurationArn);
    }
    if (deploymentQueue.offer(deployment)) {
        logger.atInfo().kv("ID", deployment.getId()).log("Added shadow deployment job");
    }
}
Also used : Configuration(com.amazon.aws.iot.greengrass.configuration.common.Configuration) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) Deployment(com.aws.greengrass.deployment.model.Deployment) DeploymentTaskMetadata(com.aws.greengrass.deployment.model.DeploymentTaskMetadata) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException)

Example 3 with Configuration

use of com.amazon.aws.iot.greengrass.configuration.common.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentDocumentConverterTest method GIVEN_FCS_Deployment_Config_Missing_Fields_When_convert_Then_all_fields_are_converted_with_defaults.

@Test
void GIVEN_FCS_Deployment_Config_Missing_Fields_When_convert_Then_all_fields_are_converted_with_defaults() throws Exception {
    // GIVEN
    String filename = "FcsDeploymentConfig_Missing_Fields.json";
    String json = new String(Files.readAllBytes(Paths.get(getClass().getResource(filename).toURI())));
    Configuration resultConfig = mapper.readValue(json, Configuration.class);
    // WHEN
    DeploymentDocument deploymentDocument = DeploymentDocumentConverter.convertFromDeploymentConfiguration(resultConfig);
    // THEN
    // The following values are from FcsDeploymentConfig_Missing_Fields.json
    assertThat(deploymentDocument.getTimestamp(), is(1604067741583L));
    assertThat(deploymentDocument.getConfigurationArn(), is("arn:aws:greengrass:us-east-1:698947471564:configuration:thinggroup/SampleGroup:2"));
    assertThat(deploymentDocument.getGroupName(), is("thinggroup/SampleGroup"));
    assertThat(deploymentDocument.getRequiredCapabilities(), is(empty()));
    assertThat(deploymentDocument.getDeploymentPackageConfigurationList(), hasSize(1));
    DeploymentPackageConfiguration componentConfiguration = deploymentDocument.getDeploymentPackageConfigurationList().get(0);
    assertThat(componentConfiguration.getPackageName(), equalTo("CustomerApp"));
    assertThat(componentConfiguration.getResolvedVersion(), equalTo("1.0.0"));
    assertNull(componentConfiguration.getConfigurationUpdateOperation());
    // The following fields are not provided in the json so default values should be used.
    // Default for FailureHandlingPolicy should be ROLLBACK
    assertThat(deploymentDocument.getFailureHandlingPolicy(), is(FailureHandlingPolicy.ROLLBACK));
    // Default for ComponentUpdatePolicy is NOTIFY_COMPONENTS with 60 sec as timeout
    assertThat(deploymentDocument.getComponentUpdatePolicy().getComponentUpdatePolicyAction(), is(NOTIFY_COMPONENTS));
    assertThat(deploymentDocument.getComponentUpdatePolicy().getTimeout(), is(60));
}
Also used : DeploymentPackageConfiguration(com.aws.greengrass.deployment.model.DeploymentPackageConfiguration) DeploymentPackageConfiguration(com.aws.greengrass.deployment.model.DeploymentPackageConfiguration) Configuration(com.amazon.aws.iot.greengrass.configuration.common.Configuration) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) Test(org.junit.jupiter.api.Test)

Example 4 with Configuration

use of com.amazon.aws.iot.greengrass.configuration.common.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentDocumentConverterTest method GIVEN_FCS_Deployment_Config_Missing_Components_When_convert_is_empty_list_is_returned.

@Test
void GIVEN_FCS_Deployment_Config_Missing_Components_When_convert_is_empty_list_is_returned() throws Exception {
    // GIVEN
    String filename = "FcsDeploymentConfig_Missing_Components.json";
    String json = new String(Files.readAllBytes(Paths.get(getClass().getResource(filename).toURI())));
    Configuration resultConfig = mapper.readValue(json, Configuration.class);
    DeploymentDocumentConverter.convertFromDeploymentConfiguration(resultConfig);
    // WHEN
    DeploymentDocument deploymentDocument = DeploymentDocumentConverter.convertFromDeploymentConfiguration(resultConfig);
    // THEN
    // The following values are from FcsDeploymentConfig_Missing_Components.json
    assertThat(deploymentDocument.getDeploymentPackageConfigurationList(), empty());
    assertThat(deploymentDocument.getTimestamp(), is(1604067741583L));
    assertThat(deploymentDocument.getConfigurationArn(), is("arn:aws:greengrass:us-east-1:698947471564:configuration:thinggroup/SampleGroup:2"));
    assertThat(deploymentDocument.getGroupName(), is("thinggroup/SampleGroup"));
    assertThat(deploymentDocument.getRequiredCapabilities(), is(empty()));
    // The following fields are not provided in the json so default values should be used.
    // Default for FailureHandlingPolicy should be ROLLBACK
    assertThat(deploymentDocument.getFailureHandlingPolicy(), is(FailureHandlingPolicy.DO_NOTHING));
    // Default for ComponentUpdatePolicy is NOTIFY_COMPONENTS with 60 sec as timeout
    assertThat(deploymentDocument.getComponentUpdatePolicy().getComponentUpdatePolicyAction(), is(NOTIFY_COMPONENTS));
    assertThat(deploymentDocument.getComponentUpdatePolicy().getTimeout(), is(120));
}
Also used : DeploymentPackageConfiguration(com.aws.greengrass.deployment.model.DeploymentPackageConfiguration) Configuration(com.amazon.aws.iot.greengrass.configuration.common.Configuration) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) Test(org.junit.jupiter.api.Test)

Example 5 with Configuration

use of com.amazon.aws.iot.greengrass.configuration.common.Configuration in project aws-greengrass-nucleus by aws-greengrass.

the class IotJobsFleetStatusServiceTest method offerSampleIoTJobsDeployment.

private void offerSampleIoTJobsDeployment(String fileName, String deploymentId) throws Exception {
    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);
    DeploymentQueue deploymentQueue = (DeploymentQueue) kernel.getContext().getvIfExists(DeploymentQueue.class).get();
    Configuration deploymentConfiguration = OBJECT_MAPPER.readValue(new File(getClass().getResource(fileName).toURI()), Configuration.class);
    deploymentQueue.offer(new Deployment(OBJECT_MAPPER.writeValueAsString(deploymentConfiguration), DeploymentType.IOT_JOBS, deploymentId));
}
Also used : Path(java.nio.file.Path) Configuration(com.amazon.aws.iot.greengrass.configuration.common.Configuration) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Deployment(com.aws.greengrass.deployment.model.Deployment) File(java.io.File) DeploymentQueue(com.aws.greengrass.deployment.DeploymentQueue)

Aggregations

Configuration (com.amazon.aws.iot.greengrass.configuration.common.Configuration)9 Deployment (com.aws.greengrass.deployment.model.Deployment)5 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)5 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)4 File (java.io.File)4 Test (org.junit.jupiter.api.Test)4 DeploymentPackageConfiguration (com.aws.greengrass.deployment.model.DeploymentPackageConfiguration)3 DeploymentDocumentConverter.convertFromDeploymentConfiguration (com.aws.greengrass.deployment.converter.DeploymentDocumentConverter.convertFromDeploymentConfiguration)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Topics (com.aws.greengrass.config.Topics)1 DeploymentQueue (com.aws.greengrass.deployment.DeploymentQueue)1 DeploymentStatusKeeper (com.aws.greengrass.deployment.DeploymentStatusKeeper)1 InvalidRequestException (com.aws.greengrass.deployment.exceptions.InvalidRequestException)1 DeploymentTaskMetadata (com.aws.greengrass.deployment.model.DeploymentTaskMetadata)1 LocalOverrideRequest (com.aws.greengrass.deployment.model.LocalOverrideRequest)1 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)1 ServiceLoadException (com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1