Search in sources :

Example 1 with RunWithInfo

use of software.amazon.awssdk.aws.greengrass.model.RunWithInfo in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentDocumentConverterTest method GIVEN_Full_Local_Override_Request_config_update_And_Current_Root_WHEN_convert_THEN_Return_expected_Deployment_Document.

// Existing: ROOT_COMPONENT_TO_REMOVE_1-1.0.0, ROOT_COMPONENT_TO_REMOVE_2-2.0.0, EXISTING_ROOT_COMPONENT-2.0.0
// To Remove: ROOT_COMPONENT_TO_REMOVE_1, ROOT_COMPONENT_TO_REMOVE_2
// To Add: NEW_ROOT_COMPONENT-2.0.0
// To Update: EXISTING_ROOT_COMPONENT-1.0.0 -> 2.0.0
// Result roots: NEW_ROOT_COMPONENT-2.0.0, EXISTING_ROOT_COMPONENT-2.0.0
@Test
void GIVEN_Full_Local_Override_Request_config_update_And_Current_Root_WHEN_convert_THEN_Return_expected_Deployment_Document() throws Exception {
    String dependencyUpdateConfigString = "{ \"MERGE\": { \"Company\": { \"Office\": { \"temperature\": 22 } }, \"path1\": { \"Object2\": { \"key2\": \"val2\" } } }, \"RESET\": [ \"/secret/first\" ] }";
    Map<String, ConfigurationUpdateOperation> updateConfig = new HashMap<>();
    updateConfig.put(DEPENDENCY_COMPONENT, mapper.readValue(dependencyUpdateConfigString, ConfigurationUpdateOperation.class));
    String existingUpdateConfigString = "{ \"MERGE\": {\"foo\": \"bar\"}}";
    updateConfig.put(EXISTING_ROOT_COMPONENT, mapper.readValue(existingUpdateConfigString, ConfigurationUpdateOperation.class));
    Map<String, RunWithInfo> componentToRunWithInfo = new HashMap<>();
    RunWithInfo runWithInfo = new RunWithInfo();
    runWithInfo.setPosixUser("foo:bar");
    runWithInfo.setWindowsUser("testWindowsUser");
    SystemResourceLimits limits = new SystemResourceLimits();
    limits.setMemory(102400L);
    limits.setCpus(1.5);
    runWithInfo.setSystemResourceLimits(limits);
    componentToRunWithInfo.put(NEW_ROOT_COMPONENT, runWithInfo);
    runWithInfo = new RunWithInfo();
    runWithInfo.setPosixUser("1234");
    runWithInfo.setWindowsUser("testWindowsUser2");
    componentToRunWithInfo.put(DEPENDENCY_COMPONENT, runWithInfo);
    // Existing: ROOT_COMPONENT_TO_REMOVE_1-1.0.0, ROOT_COMPONENT_TO_REMOVE_2-2.0.0, EXISTING_ROOT_COMPONENT-2.0.0
    // To Remove: ROOT_COMPONENT_TO_REMOVE_1, ROOT_COMPONENT_TO_REMOVE_2
    // To Add: NEW_ROOT_COMPONENT-2.0.0
    // To Update: EXISTING_ROOT_COMPONENT-1.0.0 -> 2.0.0
    // Result roots: NEW_ROOT_COMPONENT-2.0.0, EXISTING_ROOT_COMPONENT-2.0.0
    LocalOverrideRequest testRequest = LocalOverrideRequest.builder().requestId(REQUEST_ID).requestTimestamp(REQUEST_TIMESTAMP).componentsToMerge(ROOT_COMPONENTS_TO_MERGE).componentsToRemove(Arrays.asList(ROOT_COMPONENT_TO_REMOVE_1, ROOT_COMPONENT_TO_REMOVE_2)).configurationUpdate(updateConfig).componentToRunWithInfo(componentToRunWithInfo).build();
    DeploymentDocument deploymentDocument = DeploymentDocumentConverter.convertFromLocalOverrideRequestAndRoot(testRequest, CURRENT_ROOT_COMPONENTS);
    assertThat(deploymentDocument.getFailureHandlingPolicy(), is(FailureHandlingPolicy.DO_NOTHING));
    assertThat(deploymentDocument.getDeploymentId(), is(REQUEST_ID));
    assertThat(deploymentDocument.getTimestamp(), is(REQUEST_TIMESTAMP));
    assertThat(deploymentDocument.getRootPackages(), is(Arrays.asList(EXISTING_ROOT_COMPONENT, NEW_ROOT_COMPONENT)));
    List<DeploymentPackageConfiguration> deploymentPackageConfigurations = deploymentDocument.getDeploymentPackageConfigurationList();
    assertThat(deploymentPackageConfigurations.size(), is(3));
    // verify deploymentConfigs
    DeploymentPackageConfiguration existingRootComponentConfig = deploymentPackageConfigurations.stream().filter(e -> e.getPackageName().equals(EXISTING_ROOT_COMPONENT)).findAny().get();
    assertThat(existingRootComponentConfig.getResolvedVersion(), is("2.0.0"));
    assertThat(existingRootComponentConfig.getConfigurationUpdateOperation(), is(mapper.readValue(existingUpdateConfigString, ConfigurationUpdateOperation.class)));
    DeploymentPackageConfiguration newRootComponentConfig = deploymentPackageConfigurations.stream().filter(e -> e.getPackageName().equals(NEW_ROOT_COMPONENT)).findAny().get();
    assertThat(newRootComponentConfig.getResolvedVersion(), is("2.0.0"));
    assertNull(newRootComponentConfig.getConfigurationUpdateOperation());
    assertEquals("foo:bar", newRootComponentConfig.getRunWith().getPosixUser());
    assertEquals("testWindowsUser", newRootComponentConfig.getRunWith().getWindowsUser());
    assertEquals(1.5, newRootComponentConfig.getRunWith().getSystemResourceLimits().getCpus());
    assertEquals(102400L, newRootComponentConfig.getRunWith().getSystemResourceLimits().getMemory());
    DeploymentPackageConfiguration DependencyComponentConfig = deploymentPackageConfigurations.stream().filter(e -> e.getPackageName().equals(DEPENDENCY_COMPONENT)).findAny().get();
    assertEquals(DependencyComponentConfig.getConfigurationUpdateOperation(), mapper.readValue(dependencyUpdateConfigString, ConfigurationUpdateOperation.class));
    assertThat(DependencyComponentConfig.getResolvedVersion(), is("*"));
}
Also used : ConfigurationUpdateOperation(com.aws.greengrass.deployment.model.ConfigurationUpdateOperation) DeploymentPackageConfiguration(com.aws.greengrass.deployment.model.DeploymentPackageConfiguration) RunWithInfo(software.amazon.awssdk.aws.greengrass.model.RunWithInfo) LocalOverrideRequest(com.aws.greengrass.deployment.model.LocalOverrideRequest) HashMap(java.util.HashMap) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) SystemResourceLimits(software.amazon.awssdk.aws.greengrass.model.SystemResourceLimits) Test(org.junit.jupiter.api.Test)

Example 2 with RunWithInfo

use of software.amazon.awssdk.aws.greengrass.model.RunWithInfo in project aws-greengrass-cli by aws-greengrass.

the class DeploymentCommandTest method GIVEN_WHEN_components_runwith_provided_THEN_request_contains_the_info.

@Test
void GIVEN_WHEN_components_runwith_provided_THEN_request_contains_the_info() throws Exception {
    int exitCode = runCommandLine("deployment", "create", "--runWith", "Component1:windowsUser=foobar", "--runWith", "Component2:windowsUser=foobar", "--runWith", "Component2:posixUser=1234", "--systemLimits", Paths.get(this.getClass().getResource("resource_limits.json").toURI()).toString());
    Map<String, RunWithInfo> componentToRunWithInfo = new HashMap<>();
    RunWithInfo runWithInfo = new RunWithInfo();
    runWithInfo.setWindowsUser("foobar");
    MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, SystemResourceLimits.class);
    Map<String, SystemResourceLimits> systemResourceLimits = mapper.readValue(getClass().getResource("resource_limits.json"), mapType);
    runWithInfo.setSystemResourceLimits(systemResourceLimits.get("Component1"));
    componentToRunWithInfo.put("Component1", runWithInfo);
    runWithInfo = new RunWithInfo();
    runWithInfo.setPosixUser("1234");
    runWithInfo.setWindowsUser("foobar");
    componentToRunWithInfo.put("Component2", runWithInfo);
    CreateLocalDeploymentRequest request = new CreateLocalDeploymentRequest();
    request.setComponentToRunWithInfo(componentToRunWithInfo);
    verify(nucleusAdapteripc).createLocalDeployment(request);
    assertThat(exitCode, is(0));
}
Also used : CreateLocalDeploymentRequest(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest) RunWithInfo(software.amazon.awssdk.aws.greengrass.model.RunWithInfo) HashMap(java.util.HashMap) SystemResourceLimits(software.amazon.awssdk.aws.greengrass.model.SystemResourceLimits) MapType(com.fasterxml.jackson.databind.type.MapType) Test(org.junit.jupiter.api.Test)

Example 3 with RunWithInfo

use of software.amazon.awssdk.aws.greengrass.model.RunWithInfo in project aws-greengrass-cli by aws-greengrass.

the class IPCCliTest method GIVEN_kernel_running_WHEN_change_configuration_and_deployment_THEN_kernel_copies_artifacts_correctly.

@Test
@Order(8)
void GIVEN_kernel_running_WHEN_change_configuration_and_deployment_THEN_kernel_copies_artifacts_correctly(ExtensionContext context) throws Exception {
    ignoreExceptionOfType(context, SdkClientException.class);
    ignoreExceptionOfType(context, PackageDownloadException.class);
    ignoreExceptionOfType(context, ComponentVersionNegotiationException.class);
    // updated recipes
    Path recipesPath = Paths.get(this.getClass().getResource("recipes").toURI());
    Path artifactsPath = Paths.get(this.getClass().getResource("artifacts").toURI());
    Map<String, Map<String, Object>> componentToConfiguration;
    String update = "{\"Component1\":{\"MERGE\":{\"Message\":\"NewWorld\"}}}";
    componentToConfiguration = OBJECT_MAPPER.readValue(update, Map.class);
    Map<String, RunWithInfo> componentToRunWithInfo = new HashMap<>();
    RunWithInfo runWithInfo = new RunWithInfo();
    runWithInfo.setPosixUser("nobody");
    componentToRunWithInfo.put("Component1", runWithInfo);
    CreateLocalDeploymentRequest createLocalDeploymentRequest = new CreateLocalDeploymentRequest();
    createLocalDeploymentRequest.setGroupName("NewGroup");
    createLocalDeploymentRequest.setRootComponentVersionsToAdd(Collections.singletonMap("Component1", "1.0.0"));
    createLocalDeploymentRequest.setComponentToConfiguration(componentToConfiguration);
    createLocalDeploymentRequest.setComponentToRunWithInfo(componentToRunWithInfo);
    createLocalDeploymentRequest.setRecipeDirectoryPath(recipesPath.toAbsolutePath().toString());
    createLocalDeploymentRequest.setArtifactsDirectoryPath(artifactsPath.toAbsolutePath().toString());
    CountDownLatch waitForComponent1ToRun = waitForServiceToComeInState("Component1", State.RUNNING, kernel);
    CreateLocalDeploymentResponse addComponentDeploymentResponse = clientConnection.createLocalDeployment(createLocalDeploymentRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
    String deploymentId1 = addComponentDeploymentResponse.getDeploymentId();
    CountDownLatch waitFordeploymentId1 = waitForDeploymentToBeSuccessful(deploymentId1, kernel);
    assertTrue(waitForComponent1ToRun.await(SERVICE_STATE_CHECK_TIMEOUT_SECONDS, TimeUnit.SECONDS));
    assertTrue(waitFordeploymentId1.await(LOCAL_DEPLOYMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
    GetComponentDetailsRequest getComponentDetailsRequest = new GetComponentDetailsRequest();
    getComponentDetailsRequest.setComponentName("Component1");
    ComponentDetails componentDetails = clientConnection.getComponentDetails(getComponentDetailsRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS).getComponentDetails();
    assertEquals("NewWorld", componentDetails.getConfiguration().get("Message"));
    Topic posixUser = kernel.getConfig().find(SERVICES_NAMESPACE_TOPIC, "Component1", RUN_WITH_NAMESPACE_TOPIC, "posixUser");
    assertEquals("nobody", posixUser.getOnce());
    assertTrue(Files.exists(kernel.getNucleusPaths().componentStorePath().resolve(ComponentStore.ARTIFACT_DIRECTORY).resolve("Component1").resolve("1.0.0").resolve("run.sh")));
}
Also used : Path(java.nio.file.Path) RunWithInfo(software.amazon.awssdk.aws.greengrass.model.RunWithInfo) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) CreateLocalDeploymentRequest(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest) CreateLocalDeploymentResponse(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse) Topic(com.aws.greengrass.config.Topic) Map(java.util.Map) HashMap(java.util.HashMap) ComponentDetails(software.amazon.awssdk.aws.greengrass.model.ComponentDetails) GetComponentDetailsRequest(software.amazon.awssdk.aws.greengrass.model.GetComponentDetailsRequest) Order(org.junit.jupiter.api.Order) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 4 with RunWithInfo

use of software.amazon.awssdk.aws.greengrass.model.RunWithInfo in project aws-greengrass-cli by aws-greengrass.

the class DeploymentCommand method getComponentToRunWithInfo.

private Map<String, RunWithInfo> getComponentToRunWithInfo(Map<String, String> runWithOptions, Map<String, SystemResourceLimits> systemLimits) {
    if (runWithOptions == null) {
        runWithOptions = new HashMap<>();
    }
    Map<String, RunWithInfo> componentToRunWithInfo = new HashMap<>();
    for (Map.Entry<String, String> entry : runWithOptions.entrySet()) {
        String componentNameAndRunWithOption = entry.getKey();
        String[] parts = componentNameAndRunWithOption.split(":");
        if (parts.length != 2) {
            throw new IllegalArgumentException("--runWith must be in the following format " + "<component>:{posixUser|windowsUser}=<user>[:<group>] ");
        }
        String componentName = parts[0];
        String runWithOption = parts[1];
        RunWithInfo runWithInfo = componentToRunWithInfo.computeIfAbsent(componentName, k -> new RunWithInfo());
        switch(runWithOption) {
            case RUN_WITH_OPTION_POSIX_USER:
                runWithInfo.setPosixUser(entry.getValue());
                break;
            case RUN_WITH_OPTION_WINDOWS_USER:
                runWithInfo.setWindowsUser(entry.getValue());
                break;
            default:
                throw new IllegalArgumentException("Invalid --runWith option: " + runWithOption);
        }
    }
    for (Map.Entry<String, SystemResourceLimits> mapEntry : systemLimits.entrySet()) {
        String componentName = mapEntry.getKey();
        componentToRunWithInfo.compute(componentName, (k, v) -> {
            if (v == null) {
                v = new RunWithInfo();
            }
            v.setSystemResourceLimits(mapEntry.getValue());
            return v;
        });
    }
    return componentToRunWithInfo.isEmpty() ? null : componentToRunWithInfo;
}
Also used : RunWithInfo(software.amazon.awssdk.aws.greengrass.model.RunWithInfo) HashMap(java.util.HashMap) SystemResourceLimits(software.amazon.awssdk.aws.greengrass.model.SystemResourceLimits) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)4 RunWithInfo (software.amazon.awssdk.aws.greengrass.model.RunWithInfo)4 Test (org.junit.jupiter.api.Test)3 SystemResourceLimits (software.amazon.awssdk.aws.greengrass.model.SystemResourceLimits)3 Map (java.util.Map)2 CreateLocalDeploymentRequest (software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest)2 Topic (com.aws.greengrass.config.Topic)1 ConfigurationUpdateOperation (com.aws.greengrass.deployment.model.ConfigurationUpdateOperation)1 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)1 DeploymentPackageConfiguration (com.aws.greengrass.deployment.model.DeploymentPackageConfiguration)1 LocalOverrideRequest (com.aws.greengrass.deployment.model.LocalOverrideRequest)1 MapType (com.fasterxml.jackson.databind.type.MapType)1 Path (java.nio.file.Path)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)1 Order (org.junit.jupiter.api.Order)1 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)1 ComponentDetails (software.amazon.awssdk.aws.greengrass.model.ComponentDetails)1 CreateLocalDeploymentResponse (software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse)1 GetComponentDetailsRequest (software.amazon.awssdk.aws.greengrass.model.GetComponentDetailsRequest)1