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

The following examples show how to use org.apache.brooklyn.util.os.Os#mergePaths() . 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: AbstractMongoDBSshDriver.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Override
public void customize() {
    Map<?,?> ports = ImmutableMap.of("port", getServerPort());
    Networking.checkPortsValid(ports);
    List<String> commands = new LinkedList<>();
    commands.add(String.format("mkdir -p %s", getDataDirectory()));

    if (MongoDBAuthenticationUtils.usesAuthentication(entity)) {
        String destinationLocation = Os.mergePaths(getRunDir(), "mongodb-keyfile");
        entity.sensors().set(AbstractMongoDBServer.MONGODB_KEYFILE_DESTINATION, destinationLocation);
        String keyfileContents = entity.config().get(AbstractMongoDBServer.MONGODB_KEYFILE_CONTENTS);
        if (Strings.isNullOrEmpty(keyfileContents)) {
            String keyfileUrl = entity.config().get(AbstractMongoDBServer.MONGODB_KEYFILE_URL);
            if (Strings.isNullOrEmpty(keyfileUrl)) {
                throw new IllegalStateException("MongoDBAuthenticationUtils.usesAuthentication returned true, but neither keyfileContents nor keyfileUrl are set");
            }
            copyResource(keyfileUrl, destinationLocation);
        } else {
            commands.add(BashCommands.pipeTextToFile(keyfileContents, destinationLocation));
        }
        commands.add("chmod 600 " + destinationLocation);
    }

    newScript(CUSTOMIZING)
            .updateTaskAndFailOnNonZeroResultCode()
            .body.append(commands).execute();
    String templateUrl = entity.getConfig(MongoDBServer.MONGODB_CONF_TEMPLATE_URL);
    if (!Strings.isNullOrEmpty(templateUrl)) copyTemplate(templateUrl, getConfFile());
    if (MongoDBAuthenticationUtils.usesAuthentication(entity)) {
        launch(getArgsBuilderWithNoAuthentication((AbstractMongoDBServer) getEntity())
                .add("--dbpath", getDataDirectory()));
        newScript("create-user")
                .body.append(String.format("%s --port %s" +
                        " --host localhost admin --eval \"db.createUser({user: '%s',pwd: '%s',roles: [ 'root' ]})\"",
                Os.mergePaths(getExpandedInstallDir(), "bin/mongo"), getServerPort(), getRootUsername(), MongoDBAuthenticationUtils.getRootPassword(entity)))
                .updateTaskAndFailOnNonZeroResultCode()
                .execute();
        stop();
    }
}
 
Example 2
Source File: InitdServiceInstaller.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public Task<?> getServiceInstallTask() {
    ResourceUtils resource = new ResourceUtils(this);
    String pidFile = entity.getAttribute(SoftwareProcess.PID_FILE);
    String template = resource.getResourceAsString(enricher.config().get(SERVICE_TEMPLATE));
    String serviceName = getServiceName();
    SshMachineLocation sshMachine = EffectorTasks.getSshMachine(entity);
    Map<String, Object> params = MutableMap.<String, Object>of(
            "service.launch_script", Os.mergePaths(getRunDir(), getStartScriptName()),
            "service.name", serviceName,
            "service.user", sshMachine.getUser(),
            "service.log_path", getLogLocation());
    if (pidFile != null) {
        params.put("service.pid_file", pidFile);
    }
    String service = TemplateProcessor.processTemplateContents(template, (EntityInternal)entity, params);
    String tmpServicePath = Os.mergePaths(getRunDir(), serviceName);
    String servicePath = "/etc/init.d/" + serviceName;
    SshPutTaskWrapper putServiceTask = SshTasks.newSshPutTaskFactory(sshMachine, tmpServicePath)
            .contents(service)
            .newTask();
    ProcessTaskWrapper<Integer> installServiceTask = SshTasks.newSshExecTaskFactory(sshMachine,
            BashCommands.chain(
                BashCommands.sudo("mv " + tmpServicePath + " " + servicePath),
                BashCommands.sudo("chmod 0755 " + servicePath),
                BashCommands.sudo("chkconfig --add " + serviceName),
                BashCommands.sudo("chkconfig " + serviceName + " on")))
        .requiringExitCodeZero()
        .newTask();

    return Tasks.<Void>builder()
        .displayName("install (init.d)")
        .description("Install init.d service")
        .add(putServiceTask)
        .add(installServiceTask)
        .build();
}
 
Example 3
Source File: MiscClassesRebindTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected Entity loadEntityMemento(String label, String entityId) throws Exception {
    String mementoResourceName = JavaClassNames.cleanSimpleClassName(this) + "-" + label + "-entity-" + entityId+".memento";
    String memento = Streams.readFullyString(getClass().getResourceAsStream(mementoResourceName));
    
    File persistedEntityFile = new File(mementoDir, Os.mergePaths("entities", entityId));
    Files.write(memento.getBytes(), persistedEntityFile);
    
    return newApp = rebind();
}
 
Example 4
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 5
Source File: RebindPolicyPrivateConstructorTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected File getPersistanceFile(BrooklynObjectType type, String id) {
    String dir;
    switch (type) {
        case POLICY: dir = "policies"; break;
        default: throw new UnsupportedOperationException("type="+type);
    }
    return new File(mementoDir, Os.mergePaths(dir, id));
}
 
Example 6
Source File: AbstractBrooklynLauncherRebindTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private File getPersistanceFile(BrooklynObjectType type, String id) {
    String dir;
    switch (type) {
        case ENTITY: dir = "entities"; break;
        case LOCATION: dir = "locations"; break;
        case POLICY: dir = "policies"; break;
        case ENRICHER: dir = "enrichers"; break;
        case FEED: dir = "feeds"; break;
        case CATALOG_ITEM: dir = "catalog"; break;
        case MANAGED_BUNDLE: dir = "bundles"; break;
        default: throw new UnsupportedOperationException("type="+type);
    }
    return new File(persistenceDir, Os.mergePaths(dir, id));
}
 
Example 7
Source File: RebindConfigInheritanceTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected void doReadConfigInheritance(String label, String entityId) throws Exception {
    String mementoFilename = "config-inheritance-"+label+"-"+entityId;
    origMemento = Streams.readFullyString(getClass().getResourceAsStream(mementoFilename));
    
    File persistedEntityFile = new File(mementoDir, Os.mergePaths("entities", entityId));
    Files.write(origMemento.getBytes(), persistedEntityFile);
    
    // we'll make assertions on what we've loaded
    rebind();
    rebindedApp = (Application) newManagementContext.lookup(entityId);
    
    // we'll also make assertions on the contents written out
    RebindTestUtils.waitForPersisted(mgmt());
    newMemento = Joiner.on("\n").join(Files.readLines(persistedEntityFile, Charsets.UTF_8));
}
 
Example 8
Source File: BrooklynLauncherTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(groups="Integration")
public void testWebServerTempDirRespectsDataDirConfig() throws Exception {
    String dataDirName = ".brooklyn-foo"+Strings.makeRandomId(4);
    String dataDir = "~/"+dataDirName;

    launcher = newLauncherForTests(true)
            .brooklynProperties(BrooklynServerConfig.MGMT_BASE_DIR, dataDir)
            .start();
    
    ManagementContext managementContext = launcher.getServerDetails().getManagementContext();
    String expectedTempDir = Os.mergePaths(Os.home(), dataDirName, "planes", managementContext.getManagementNodeId(), "jetty");
    
    File webappTempDir = launcher.getServerDetails().getWebServer().getWebappTempDir();
    assertEquals(webappTempDir.getAbsolutePath(), expectedTempDir);
}
 
Example 9
Source File: BrooklynServerPaths.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public static File getBrooklynWebTmpDir(ManagementContext mgmt) {
    String brooklynMgmtBaseDir = getMgmtBaseDir(mgmt);
    File webappTempDir = new File(Os.mergePaths(brooklynMgmtBaseDir, "planes", mgmt.getManagementNodeId(), "jetty"));
    try {
        FileUtils.forceMkdir(webappTempDir);
        Os.deleteOnExitRecursivelyAndEmptyParentsUpTo(webappTempDir, new File(brooklynMgmtBaseDir)); 
        return webappTempDir;
    } catch (Exception e) {
        Exceptions.propagateIfFatal(e);
        IllegalStateException e2 = new IllegalStateException("Cannot create working directory "+webappTempDir+" for embedded jetty server: "+e, e);
        log.warn(e2.getMessage()+" (rethrowing)");
        throw e2;
    }
}
 
Example 10
Source File: ArchiveUtils.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Deploys an archive file to a remote machine and extracts the contents.
 * <p>
 * Copies the archive file from the given URL to a file in a temporary directory and extracts
 * the contents in the destination directory. For Java archives of type {@code .jar},
 * {@code .war} or {@code .ear} the file is simply copied.
 * 
 * @return true if the archive is downloaded AND unpacked; false if it is downloaded but not unpacked; 
 * throws if there was an error downloading or, for known archive types, unpacking.
 *
 * @see #deploy(String, SshMachineLocation, String)
 * @see #deploy(Map, String, SshMachineLocation, String, String, String)
 * @see #install(SshMachineLocation, String, String, int)
 */
public static boolean deploy(ResourceUtils resolver, Map<String, ?> props, String archiveUrl, SshMachineLocation machine, String destDir, boolean keepArchiveAfterUnpacking, String optionalTmpDir, String optionalDestFile) {
    String destFile = optionalDestFile;
    if (destFile==null) destFile = Urls.getBasename(Preconditions.checkNotNull(archiveUrl, "archiveUrl"));
    if (Strings.isBlank(destFile)) 
        throw new IllegalStateException("Not given filename and cannot infer archive type from '"+archiveUrl+"'");
    
    String tmpDir = optionalTmpDir;
    if (tmpDir==null) tmpDir=Preconditions.checkNotNull(destDir, "destDir");
    if (props==null) props = MutableMap.of();
    String destPath = Os.mergePaths(tmpDir, destFile);

    // Use the location mutex to prevent package manager locking issues
    machine.acquireMutex("installing", "installing archive");
    try {
        int result = install(resolver, props, machine, archiveUrl, destPath, NUM_RETRIES_FOR_COPYING);
        if (result != 0) {
            throw new IllegalStateException(format("Unable to install archive %s to %s", archiveUrl, machine));
        }

        // extract, now using task if available
        MutableList<String> commands = MutableList.copyOf(installCommands(destFile))
                .appendAll(extractCommands(destFile, tmpDir, destDir, false, keepArchiveAfterUnpacking));
        if (DynamicTasks.getTaskQueuingContext()!=null) {
            result = DynamicTasks.queue(SshTasks.newSshExecTaskFactory(machine, commands.toArray(new String[0])).summary("extracting archive").requiringExitCodeZero()).get();
        } else {
            result = machine.execCommands(props, "extracting content", commands);
        }
        if (result != 0) {
            throw new IllegalStateException(format("Failed to expand archive %s on %s", archiveUrl, machine));
        }
        return ArchiveType.of(destFile)!=ArchiveType.UNKNOWN;
    } finally {
        machine.releaseMutex("installing");
    }
}
 
Example 11
Source File: KarafSshDriver.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
@Override
protected String getLogFileLocation() {
    return Os.mergePaths(getRunDir(), "data", "karaf.out");
}
 
Example 12
Source File: VanillaSoftwareProcessTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Test(groups = "Integration")
public void testVanillaSoftwareProcessCanUseResourcesInBundles() throws Exception {
    try {
        new ResourceUtils(this).getResourceAsString("classpath://org/apache/brooklyn/test/osgi/resources/message.txt");
        fail("classpath://org/apache/brooklyn/test/osgi/resources/message.txt should not be on classpath");
    } catch (Exception e) {/* expected */}
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-entities.jar");

    final String catalogItemUrl = "classpath://vanilla-software-process-with-resource.yaml";
    final String catalogYaml = ResourceUtils.create(this)
            .getResourceAsString(catalogItemUrl);

    final URI webConsoleUri = URI.create(getBaseUriRest(server));

    // Test setup
    final EntitySpec<BrooklynNode> spec = EntitySpec.create(BrooklynNode.class);
    final ManagementContext mgmt = getManagementContextFromJettyServerAttributes(server);
    final BrooklynNode node = mgmt.getEntityManager().createEntity(spec);
    node.sensors().set(BrooklynNode.WEB_CONSOLE_URI, webConsoleUri);

    // Add catalogue item.
    HttpToolResponse response = node.http().post(
            "/catalog",
            ImmutableMap.<String, String>of("Content-type", "application/yaml"),
            catalogYaml.getBytes());
    HttpAsserts.assertHealthyStatusCode(response.getResponseCode());

    // Deploy it.
    final String blueprint = "location: localhost\n" +
            "services:\n" +
            "- type: vanilla-software-resource-test:1.0";
    response = node.http().post(
            "/applications",
            ImmutableMap.of("Content-Type", "text/yaml"),
            blueprint.getBytes());
    HttpAsserts.assertHealthyStatusCode(response.getResponseCode());

    // Assert application is eventually running and not on fire.
    final Entity vanilla = mgmt.getApplications().iterator().next().getChildren().iterator().next();
    assertTrue(vanilla instanceof VanillaSoftwareProcess,
            "expected " + VanillaSoftwareProcess.class.getName() + ", found: " + vanilla);
    EntityAsserts.assertAttributeEqualsEventually(vanilla, SoftwareProcess.SERVICE_UP, true);

    // And check that the message was copied to rundir.
    SshMachineLocation machine = Machines.findUniqueMachineLocation(vanilla.getLocations(), SshMachineLocation.class).get();
    String file = Os.mergePaths(vanilla.sensors().get(SoftwareProcess.RUN_DIR), "message.txt");
    String message = Entities.submit(vanilla, SshTasks.newSshFetchTaskFactory(machine, file).newTask()).get();
    assertNotNull(message);
    assertTrue(message.startsWith("Licensed to the Apache Software Foundation"),
            "expected ASF license header, found: " + message);

}
 
Example 13
Source File: ArchiveUtilsTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private void assertSubFilesEqual(File parentDir, Map<String, String> files) throws Exception {
    for (Map.Entry<String, String> entry : archiveContents.entrySet()) {
        File subFile = new File(Os.mergePaths(parentDir.getAbsolutePath(), entry.getKey()));
        assertEquals(Joiner.on("\n").join(Files.readLines(subFile, Charsets.UTF_8)), entry.getValue());
    }
}
 
Example 14
Source File: AbstractfKafkaSshDriver.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
@Override
protected String getLogFileLocation() { return Os.mergePaths(getRunDir(), "console.out"); }
 
Example 15
Source File: RubyRepSshDriver.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
protected String getLogFileLocation() {
    return Os.mergePaths(getRunDir(), "log", "rubyrep.log");
}
 
Example 16
Source File: BindDnsServerSshDriver.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
public String getRfc1912ZonesFile() {
    return Os.mergePaths(getOsSupport().getConfigDirectory(), "rfc1912.zone");
}
 
Example 17
Source File: BindDnsServerSshDriver.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
public String getDynamicDirectory() {
    return Os.mergePaths(getOsSupport().getWorkingDirectory(), "dynamic");
}
 
Example 18
Source File: BindDnsServerSshDriver.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
public String getDataDirectory() {
    return Os.mergePaths(getOsSupport().getWorkingDirectory(), "data");
}
 
Example 19
Source File: BrooklynProperties.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public Builder resetGlobalFiles() {
    defaultLocationMetadataUrl = "classpath://brooklyn/location-metadata.properties";
    globalLocationMetadataFile = Os.mergePaths(Os.home(), ".brooklyn", "location-metadata.properties");
    globalPropertiesFile = Os.mergePaths(Os.home(), ".brooklyn", "brooklyn.properties");
    return this;
}
 
Example 20
Source File: SolrServerSshDriver.java    From brooklyn-library with Apache License 2.0 votes vote down vote up
public String getPidFile() { return Os.mergePaths(getRunDir(), "solr.pid"); }