Search in sources :

Example 1 with CommitableWriter

use of com.aws.greengrass.util.CommitableWriter in project aws-greengrass-nucleus by aws-greengrass.

the class BootstrapManager method persistBootstrapTaskList.

/**
 * Persist the bootstrap task list to file.
 *
 * @param persistedTaskFilePath Path to the persisted file of bootstrap tasks
 * @throws IOException on I/O error
 */
public void persistBootstrapTaskList(Path persistedTaskFilePath) throws IOException {
    Objects.requireNonNull(persistedTaskFilePath);
    logger.atInfo().kv("filePath", persistedTaskFilePath).log("Saving bootstrap task list to file");
    Files.deleteIfExists(persistedTaskFilePath);
    Files.createFile(persistedTaskFilePath);
    try (CommitableWriter out = CommitableWriter.commitOnClose(persistedTaskFilePath)) {
        SerializerFactory.getFailSafeJsonObjectMapper().writeValue(out, bootstrapTaskStatusList);
    }
    logger.atInfo().kv("filePath", persistedTaskFilePath).log("Bootstrap task list is saved to file");
}
Also used : CommitableWriter(com.aws.greengrass.util.CommitableWriter)

Example 2 with CommitableWriter

use of com.aws.greengrass.util.CommitableWriter in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentDirectoryManagerTest method GIVEN_deployment_WHEN_write_to_file_and_read_THEN_restore_deployment.

@Test
void GIVEN_deployment_WHEN_write_to_file_and_read_THEN_restore_deployment(ExtensionContext context) throws Exception {
    ignoreExceptionOfType(context, JsonParseException.class);
    ignoreExceptionOfType(context, JsonEOFException.class);
    Path actual1 = createNewDeploymentDir(mockArn);
    DeploymentDocument document = mock(DeploymentDocument.class);
    doReturn("mockId").when(document).getDeploymentId();
    Deployment expected = new Deployment(document, Deployment.DeploymentType.IOT_JOBS, "mockId", DEFAULT);
    deploymentDirectoryManager.writeDeploymentMetadata(expected);
    Path metadataFile = actual1.resolve(DEPLOYMENT_METADATA_FILE);
    assertThat(metadataFile.toFile(), anExistingFile());
    Deployment actual = deploymentDirectoryManager.readDeploymentMetadata();
    assertEquals(expected, actual);
    try (CommitableWriter writer = CommitableWriter.commitOnClose(metadataFile)) {
        writer.write("{\"corrupted\"");
    }
    assertThat(actual1.resolve(DEPLOYMENT_METADATA_FILE + "~").toFile(), anExistingFile());
    assertThat(actual1.resolve(DEPLOYMENT_METADATA_FILE + "+").toFile(), not(anExistingFile()));
    Deployment backup = deploymentDirectoryManager.readDeploymentMetadata();
    assertEquals(expected, backup);
    assertThat(actual1.resolve(DEPLOYMENT_METADATA_FILE + "~").toFile(), not(anExistingFile()));
    assertThat(actual1.resolve(DEPLOYMENT_METADATA_FILE + "+").toFile(), not(anExistingFile()));
    try (CommitableWriter writer = CommitableWriter.commitOnClose(metadataFile)) {
        writer.write("again failure to write");
    }
    Deployment backupAgain = deploymentDirectoryManager.readDeploymentMetadata();
    assertEquals(expected, backupAgain);
    assertThat(actual1.resolve(DEPLOYMENT_METADATA_FILE + "~").toFile(), not(anExistingFile()));
    assertThat(actual1.resolve(DEPLOYMENT_METADATA_FILE + "+").toFile(), not(anExistingFile()));
}
Also used : Path(java.nio.file.Path) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) Deployment(com.aws.greengrass.deployment.model.Deployment) CommitableWriter(com.aws.greengrass.util.CommitableWriter) Test(org.junit.jupiter.api.Test)

Example 3 with CommitableWriter

use of com.aws.greengrass.util.CommitableWriter in project aws-greengrass-nucleus by aws-greengrass.

the class BootstrapManagerTest method GIVEN_file_path_WHEN_persist_and_load_bootstrap_tasks_THEN_restore_bootstrap_tasks.

@Test
void GIVEN_file_path_WHEN_persist_and_load_bootstrap_tasks_THEN_restore_bootstrap_tasks(ExtensionContext context, @TempDir Path tempDir) throws Exception {
    ignoreExceptionOfType(context, MismatchedInputException.class);
    BootstrapTaskStatus taskA = new BootstrapTaskStatus(componentA);
    BootstrapTaskStatus taskB = new BootstrapTaskStatus(componentB);
    List<BootstrapTaskStatus> pendingTasks = new ArrayList<>(Arrays.asList(taskA, taskB));
    BootstrapManager bootstrapManager = new BootstrapManager(kernel);
    bootstrapManager.setBootstrapTaskStatusList(pendingTasks);
    Path filePath = tempDir.resolve("testFile.json");
    bootstrapManager.persistBootstrapTaskList(filePath);
    bootstrapManager.loadBootstrapTaskList(filePath);
    assertThat(bootstrapManager.getBootstrapTaskStatusList(), contains(taskA, taskB));
    try (CommitableWriter writer = CommitableWriter.commitOnClose(filePath)) {
    }
    bootstrapManager.loadBootstrapTaskList(filePath);
    assertThat(bootstrapManager.getBootstrapTaskStatusList(), contains(taskA, taskB));
    bootstrapManager.loadBootstrapTaskList(filePath);
    assertThat(bootstrapManager.getBootstrapTaskStatusList(), contains(taskA, taskB));
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) CommitableWriter(com.aws.greengrass.util.CommitableWriter) Test(org.junit.jupiter.api.Test)

Example 4 with CommitableWriter

use of com.aws.greengrass.util.CommitableWriter in project aws-greengrass-nucleus by aws-greengrass.

the class KernelAlternatives method writeLaunchParamsToFile.

/**
 * Write the given string to launch parameters file.
 *
 * @param content file content string
 * @throws IOException on I/O error
 */
public void writeLaunchParamsToFile(String content) throws IOException {
    try (CommitableWriter out = CommitableWriter.abandonOnClose(getLaunchParamsPath())) {
        out.write(content);
        out.commit();
    }
}
Also used : CommitableWriter(com.aws.greengrass.util.CommitableWriter)

Example 5 with CommitableWriter

use of com.aws.greengrass.util.CommitableWriter in project aws-greengrass-nucleus by aws-greengrass.

the class Kernel method writeEffectiveConfig.

/**
 * When a config file gets read, it gets woven together from fragments from multiple sources.  This writes a fresh
 * copy of the config file, as it is, after the weaving-together process.
 *
 * @param p Path to write the effective config into
 */
public void writeEffectiveConfig(Path p) {
    try (CommitableWriter out = CommitableWriter.abandonOnClose(p)) {
        writeConfig(out);
        out.commit();
        logger.atInfo().setEventType("effective-config-dump-complete").addKeyValue("file", p).log();
    } catch (IOException t) {
        logger.atInfo().setEventType("effective-config-dump-error").setCause(t).addKeyValue("file", p).log();
    }
}
Also used : CommitableWriter(com.aws.greengrass.util.CommitableWriter) IOException(java.io.IOException)

Aggregations

CommitableWriter (com.aws.greengrass.util.CommitableWriter)5 Path (java.nio.file.Path)2 Test (org.junit.jupiter.api.Test)2 Deployment (com.aws.greengrass.deployment.model.Deployment)1 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1