Java Code Examples for org.apache.brooklyn.util.os.Os#deleteRecursively()

The following examples show how to use org.apache.brooklyn.util.os.Os#deleteRecursively() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CleanOrphanedLocationsIntegrationTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@AfterMethod(alwaysRun = true)
@Override
public void tearDown() throws Exception {
    super.tearDown();
    
    // contents of method copied from BrooklynMgmtUnitTestSupport
    for (ManagementContext mgmt : mgmts) {
        try {
            if (mgmt != null) Entities.destroyAll(mgmt);
        } catch (Throwable t) {
            LOG.error("Caught exception in tearDown method", t);
            // we should fail here, except almost always that masks a primary failure in the test itself,
            // so it would be extremely unhelpful to do so. if we could check if test has not already failed,
            // that would be ideal, but i'm not sure if that's possible with TestNG. ?
        }
    }
    if (destinationDir != null) Os.deleteRecursively(destinationDir);
    mgmts.clear();
}
 
Example 2
Source File: BrooklynLauncherRebindCatalogOsgiTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected Framework initReusableOsgiFramework() {
    if (!reuseOsgi) throw new IllegalStateException("Must first set reuseOsgi");
    
    if (OsgiManager.tryPeekFrameworkForReuse().isAbsent()) {
        BrooklynLauncher launcher = newLauncherForTests(CATALOG_EMPTY_INITIAL);
        launcher.start();
        launcher.terminate();
        Os.deleteRecursively(persistenceDir);
    }
    return OsgiManager.tryPeekFrameworkForReuse().get();
}
 
Example 3
Source File: SoftwareProcessDriverCopyResourcesTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@AfterMethod(alwaysRun = true)
public void tearDown() throws Exception {
    try {
        super.tearDown();
    } finally {
        Os.deleteRecursively(sourceFileDir);
        Os.deleteRecursively(sourceTemplateDir);
        Os.deleteRecursively(installDir);
        Os.deleteRecursively(runDir);
    }
}
 
Example 4
Source File: BrooklynNodeIntegrationTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@AfterMethod(alwaysRun=true)
@Override
public void tearDown() throws Exception {
    try {
        super.tearDown();
    } finally {
        if (pseudoBrooklynPropertiesFile != null) pseudoBrooklynPropertiesFile.delete();
        if (pseudoBrooklynCatalogFile != null) pseudoBrooklynCatalogFile.delete();
        if (brooklynCatalogSourceFile != null) brooklynCatalogSourceFile.delete();
        if (persistenceDir != null) Os.deleteRecursively(persistenceDir);
    }
}
 
Example 5
Source File: ServerResource.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected Response exportPersistenceData(MementoCopyMode preferredOrigin) {
    if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SEE_ALL_SERVER_INFO, null))
        throw WebResourceUtils.forbidden("User '%s' is not authorized to perform this operation", Entitlements.getEntitlementContext().user());

    File dir = null;
    try {
        String label = mgmt().getManagementNodeId()+"-"+Time.makeDateSimpleStampString();
        PersistenceObjectStore targetStore = BrooklynPersistenceUtils.newPersistenceObjectStore(mgmt(), null, 
            "tmp/web-persistence-"+label+"-"+Identifiers.makeRandomId(4));
        dir = ((FileBasedObjectStore)targetStore).getBaseDir();
        // only register the parent dir because that will prevent leaks for the random ID
        Os.deleteOnExitEmptyParentsUpTo(dir.getParentFile(), dir.getParentFile());
        BrooklynPersistenceUtils.writeMemento(mgmt(), targetStore, preferredOrigin);
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ArchiveBuilder.zip().addDirContentsAt( ((FileBasedObjectStore)targetStore).getBaseDir(), ((FileBasedObjectStore)targetStore).getBaseDir().getName() ).stream(baos);
        Os.deleteRecursively(dir);
        String filename = "brooklyn-state-"+label+".zip";
        return Response.ok(baos.toByteArray(), MediaType.APPLICATION_OCTET_STREAM_TYPE)
            .header("Content-Disposition","attachment; filename = "+filename)
            .build();
    } catch (Exception e) {
        log.warn("Unable to serve persistence data (rethrowing): "+e, e);
        if (dir!=null) {
            try {
                Os.deleteRecursively(dir);
            } catch (Exception e2) {
                log.warn("Ignoring error deleting '"+dir+"' after another error, throwing original error ("+e+"); ignored error deleting is: "+e2);
            }
        }
        throw Exceptions.propagate(e);
    }
}
 
Example 6
Source File: BrooklynLauncherRebindTestToFiles.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups="Integration")
public void testCopyPersistedState() throws Exception {
    EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class);
    populatePersistenceDir(persistenceDir, appSpec);

    File destinationDir = Files.createTempDir();
    String destination = destinationDir.getAbsolutePath();
    String destinationLocation = null; // i.e. file system, rather than object store
    try {
        // Auto will rebind if the dir exists
        BrooklynLauncher launcher = newLauncherDefault(PersistMode.AUTO)
                .highAvailabilityMode(HighAvailabilityMode.MASTER)
                .restServer(false);
        launcher.copyPersistedState(destination, destinationLocation);
        launcher.terminate();
        
        File entities = new File(Os.mergePaths(destination), "entities");
        assertTrue(entities.isDirectory(), "entities directory should exist");
        assertEquals(entities.listFiles().length, 1, "entities directory should contain one file (contained: "+
                Joiner.on(", ").join(entities.listFiles()) +")");

        File nodes = new File(Os.mergePaths(destination, "nodes"));
        assertTrue(nodes.isDirectory(), "nodes directory should exist");
        assertNotEquals(nodes.listFiles().length, 0, "nodes directory should not be empty");

        // Should now have a usable copy in the destinationDir
        // Auto will rebind if the dir exists
        newLauncherDefault(PersistMode.AUTO)
                .restServer(false)
                .persistenceDir(destinationDir)
                .start();
        assertOnlyApp(lastMgmt(), TestApplication.class);
        
    } finally {
        Os.deleteRecursively(destinationDir);
    }
}
 
Example 7
Source File: BrooklynLauncherRebindToCloudObjectStoreTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups="Live")
public void testCopyPersistedState() throws Exception {
    EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class);
    populatePersistenceDir(persistenceDir, appSpec);
    
    String destinationDir = newTempPersistenceContainerName();
    String destinationLocation = persistenceLocationSpec;
    try {
        // Auto will rebind if the dir exists
        BrooklynLauncher launcher = newLauncherDefault(PersistMode.AUTO)
                .restServer(false)
                .persistenceLocation(persistenceLocationSpec);
        BrooklynMementoRawData memento = launcher.retrieveState();
        launcher.persistState(memento, destinationDir, destinationLocation);
        launcher.terminate();
        
        assertEquals(memento.getEntities().size(), 1, "entityMementos="+memento.getEntities().keySet());
        
        // Should now have a usable copy in the destionationDir
        // Auto will rebind if the dir exists
        newLauncherDefault(PersistMode.AUTO)
                .restServer(false)
                .persistenceDir(destinationDir)
                .persistenceLocation(destinationLocation)
                .start();
        assertOnlyApp(lastMgmt(), TestApplication.class);
        
    } finally {
        Os.deleteRecursively(destinationDir);
    }
}
 
Example 8
Source File: AbstractBrooklynLauncherRebindTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@AfterMethod(alwaysRun=true)
public void tearDown() throws Exception {
    for (File file : tmpFiles) {
        if (file.exists()) file.delete();
    }
    for (BrooklynLauncher launcher : launchers) {
        launcher.terminate();
    }
    launchers.clear();
    if (persistenceDir != null) Os.deleteRecursively(persistenceDir);
}
 
Example 9
Source File: FileUtilTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups="Integration")
public void testCopyDir() throws Exception {
    File destParent = Files.createTempDir();
    try {
        Files.write("abc", new File(dir, "afile"), Charsets.UTF_8);
        File destDir = new File(destParent, "dest");
        
        FileUtil.copyDir(dir, destDir);
        
        assertEquals(Files.readLines(new File(destDir, "afile"), Charsets.UTF_8), ImmutableList.of("abc"));
        assertEquals(Files.readLines(new File(dir, "afile"), Charsets.UTF_8), ImmutableList.of("abc"));
    } finally {
        if (destParent != null) Os.deleteRecursively(destParent);
    }
}
 
Example 10
Source File: FileUtilTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups="Integration")
public void testMoveDir() throws Exception {
    File destParent = Files.createTempDir();
    try {
        Files.write("abc", new File(dir, "afile"), Charsets.UTF_8);
        File destDir = new File(destParent, "dest");
        
        FileUtil.moveDir(dir, destDir);
        
        assertEquals(Files.readLines(new File(destDir, "afile"), Charsets.UTF_8), ImmutableList.of("abc"));
        assertFalse(dir.exists());
    } finally {
        if (destParent != null) Os.deleteRecursively(destParent);
    }
}
 
Example 11
Source File: OsTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteRecursivelySubDirs() throws Exception {
    File dir = Os.newTempDir(OsTest.class);
    File subdir = new File(dir, "mysubdir");
    File subfile = new File(subdir, "mysubfile");
    subdir.mkdirs();
    Files.write("abc".getBytes(), subfile);
    
    DeletionResult result = Os.deleteRecursively(dir);
    assertTrue(result.wasSuccessful());
    assertFalse(dir.exists());
}
 
Example 12
Source File: BrooklynServerPaths.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public static File getOsgiCacheDirCleanedIfNeeded(ManagementContext mgmt) {
    File cacheDirF = getOsgiCacheDir(mgmt);
    boolean clean = isOsgiCacheForCleaning(mgmt, cacheDirF);
    log.debug("OSGi cache dir computed as "+cacheDirF.getAbsolutePath()+" ("+
        (cacheDirF.exists() ? "already exists" : "does not exist")+", "+
        (clean ? "cleaning now (and on exit)" : "cleaning not requested"));

    if (clean) {
        Os.deleteRecursively(cacheDirF);
        Os.deleteOnExitRecursively(cacheDirF);
    }
    
    return cacheDirF;
}
 
Example 13
Source File: BrooklynClusterIntegrationTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@AfterMethod(alwaysRun=true)
@Override
public void tearDown() throws Exception {
    try {
        super.tearDown();
    } finally {
        if (pseudoBrooklynPropertiesFile != null) pseudoBrooklynPropertiesFile.delete();
        if (pseudoBrooklynCatalogFile != null) pseudoBrooklynCatalogFile.delete();
        if (persistenceDir != null) Os.deleteRecursively(persistenceDir);
    }
}
 
Example 14
Source File: MySqlClusterIntegrationTest.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
private void cleanData() {
    if (app.getChildren().isEmpty()) return;
    for (Entity member : Iterables.getOnlyElement(app.getChildren()).getChildren()) {
        String runDir = member.getAttribute(MySqlNode.RUN_DIR);
        if (runDir != null) {
            Os.deleteRecursively(runDir);
        }
    }
}
 
Example 15
Source File: ArchiveUtilsTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@AfterMethod(alwaysRun=true)
@Override
public void tearDown() throws Exception {
    super.tearDown();
    if (destDir != null) Os.deleteRecursively(destDir);
}
 
Example 16
Source File: FileUtilTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@AfterMethod(alwaysRun=true)
public void tearDown() throws Exception {
    if (file != null) file.delete();
    if (dir != null) Os.deleteRecursively(dir);
}
 
Example 17
Source File: BrooklynLauncherCleanStateTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Test(groups="Integration")
public void testCleanStateState() throws Exception {
    final AtomicReference<Entity> appToKeep = new AtomicReference<>();
    final AtomicReference<Location> locToKeep = new AtomicReference<>();
    final AtomicReference<Location> locToDelete = new AtomicReference<>();
    populatePersistenceDir(persistenceDir, new Function<ManagementContext, Void>() {
        @Override
        public Void apply(ManagementContext mgmt) {
            TestApplication app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class));
            SshMachineLocation loc = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class));
            SshMachineLocation loc2 = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class));
            app.addLocations(ImmutableList.of(loc));
            appToKeep.set(app);
            locToKeep.set(loc);
            locToDelete.set(loc2);
            return null;
        }});

    File destinationDir = Files.createTempDir();
    String destination = destinationDir.getAbsolutePath();
    String destinationLocation = null; // i.e. file system, rather than object store
    try {
        // Clean the state
        BrooklynLauncher launcher = newLauncherDefault(PersistMode.AUTO)
                .highAvailabilityMode(HighAvailabilityMode.MASTER)
                .restServer(false);
        launcher.cleanOrphanedState(destination, destinationLocation);
        launcher.terminate();

        // Sanity checks (copied from BrooklynLauncherRebindTestToFiles#testCopyPersistedState)
        File entities = new File(Os.mergePaths(destination), "entities");
        assertTrue(entities.isDirectory(), "entities directory should exist");
        assertEquals(entities.listFiles().length, 1, "entities directory should contain one file (contained: "+
                Joiner.on(", ").join(entities.listFiles()) +")");

        File nodes = new File(Os.mergePaths(destination, "nodes"));
        assertTrue(nodes.isDirectory(), "nodes directory should exist");
        assertNotEquals(nodes.listFiles().length, 0, "nodes directory should not be empty");

        // Should now have a usable copy in the destinationDir
        newLauncherDefault(PersistMode.AUTO)
                .restServer(false)
                .persistenceDir(destinationDir)
                .start();
        
        Entity restoredApp = Iterables.getOnlyElement(lastMgmt().getEntityManager().getEntities());
        Location restoredLoc = Iterables.getOnlyElement(lastMgmt().getLocationManager().getLocations());
        assertEquals(restoredApp.getId(), appToKeep.get().getId());
        assertEquals(restoredLoc.getId(), locToKeep.get().getId());
    } finally {
        Os.deleteRecursively(destinationDir);
    }
}
 
Example 18
Source File: OsgiManager.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public void stop() {
    if (reuseFramework) {
        for (Bundle b: framework.getBundleContext().getBundles()) {
            if (!bundlesAtStartup.contains(b)) {
                try {
                    log.info("Uninstalling "+b+" from OSGi container in "+framework.getBundleContext().getProperty(Constants.FRAMEWORK_STORAGE));
                    b.uninstall();
                } catch (BundleException e) {
                    Exceptions.propagateIfFatal(e);
                    log.warn("Unable to uninstall "+b+"; container in "+framework.getBundleContext().getProperty(Constants.FRAMEWORK_STORAGE)+" will not be reused: "+e, e);
                    reuseFramework = false;
                    break;
                }
            }
        }
    }
    
    if (!reuseFramework || !REUSED_FRAMEWORKS_ARE_KEPT_RUNNING) {
        Osgis.ungetFramework(framework);
    }
    
    if (reuseFramework) {
        synchronized (OSGI_FRAMEWORK_CONTAINERS_FOR_REUSE) {
            OSGI_FRAMEWORK_CONTAINERS_FOR_REUSE.add(framework);
        }
        
    } else if (BrooklynServerPaths.isOsgiCacheForCleaning(mgmt, osgiFrameworkCacheDir)) {
        // See exception reported in https://issues.apache.org/jira/browse/BROOKLYN-72
        // We almost always fail to delete he OSGi temp directory due to a concurrent modification.
        // Therefore keep trying.
        final AtomicReference<DeletionResult> deletionResult = new AtomicReference<DeletionResult>();
        Repeater.create("Delete OSGi cache dir")
                .until(new Callable<Boolean>() {
                    @Override
                    public Boolean call() {
                        deletionResult.set(Os.deleteRecursively(osgiFrameworkCacheDir));
                        return deletionResult.get().wasSuccessful();
                    }})
                .limitTimeTo(Duration.ONE_SECOND)
                .backoffTo(Duration.millis(50))
                .run();
        if (deletionResult.get().getThrowable()!=null) {
            log.debug("Unable to delete "+osgiFrameworkCacheDir+" (possibly being modified concurrently?): "+deletionResult.get().getThrowable());
        }
    }
    osgiFrameworkCacheDir = null;
    framework = null;
    
    Os.deleteRecursively(brooklynBundlesCacheDir);
    brooklynBundlesCacheDir = null;
}
 
Example 19
Source File: SoftwareProcessSshDriverIntegrationTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Test(groups="Integration")
public void testCopyResourceCreatingParentDir() throws Exception {
    /*
     * TODO copyResource will now always create the parent dir, irrespective of the createParentDir value!
     * In SshMachineLocation on 2014-05-29, Alex added: mkdir -p `dirname '$DEST'`
     * 
     * Changing this test to assert that parent dir always created; should we delete boolean createParentDir
     * from the copyResource method?
     * 
     * TODO Have also deleted test that if relative path is given it will write that relative to $RUN_DIR.
     * That is not the case: it is relative to $HOME, which seems fine. For example, if copyResource
     * is used during install phase then $RUN_DIR would be the wrong default. 
     * Is there any code that relies on this behaviour?
     */
    File tempDataDirSub = new File(tempDataDir, "subdir");
    File tempDest = new File(tempDataDirSub, "tempDest.txt");
    String tempLocalContent = "abc";
    File tempLocal = new File(tempDataDir, "tempLocal.txt");
    Files.write(tempLocalContent, tempLocal, Charsets.UTF_8);
    
    localhost.config().set(BrooklynConfigKeys.ONBOX_BASE_DIR, tempDataDir.getAbsolutePath());

    MyService entity = app.createAndManageChild(EntitySpec.create(MyService.class));
    app.start(ImmutableList.of(localhost));

    // First confirm that even if createParentDir==false that it still gets created!
    try {
        entity.getDriver().copyResource(tempLocal.toURI().toString(), tempDest.getAbsolutePath(), false);
        assertEquals(Files.readLines(tempDest, Charsets.UTF_8), ImmutableList.of(tempLocalContent));
    } finally {
        Os.deleteRecursively(tempDataDirSub);
    }

    // Copy to absolute path
    try {
        entity.getDriver().copyResource(tempLocal.toURI().toString(), tempDest.getAbsolutePath(), true);
        assertEquals(Files.readLines(tempDest, Charsets.UTF_8), ImmutableList.of(tempLocalContent));
    } finally {
        Os.deleteRecursively(tempDataDirSub);
    }
}
 
Example 20
Source File: AbstractLoadTest.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
@Override
protected void tearDownPlatform() {
    if (launcher != null) launcher.terminate();
    if (persistenceDir != null) Os.deleteRecursively(persistenceDir);
}