Java Code Examples for org.apache.brooklyn.util.text.Strings#makeRandomId()

The following examples show how to use org.apache.brooklyn.util.text.Strings#makeRandomId() . 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: MariaDbIntegrationTest.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Test(groups = "Integration")
public void test_localhost() throws Exception {
    String dataDir = "/tmp/mariadb-data-" + Strings.makeRandomId(8);
    MariaDbNode mariadb = app.createAndManageChild(EntitySpec.create(MariaDbNode.class)
            .configure(MariaDbNode.MARIADB_SERVER_CONF, MutableMap.<String, Object>of("skip-name-resolve",""))
            .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, CREATION_SCRIPT)
            .configure(MariaDbNode.DATA_DIR, dataDir));
    LocalhostMachineProvisioningLocation location = new LocalhostMachineProvisioningLocation();

    app.start(ImmutableList.of(location));
    log.info("MariaDB started");

    new VogellaExampleAccess("com.mysql.jdbc.Driver", mariadb.getAttribute(DatastoreCommon.DATASTORE_URL)).readModifyAndRevertDataBase();

    log.info("Ran vogella MySQL example -- SUCCESS");

    // Ensure the data directory was successfully overridden.
    File dataDirFile = new File(dataDir);
    File mariadbSubdirFile = new File(dataDirFile, "mysql");
    Assert.assertTrue(mariadbSubdirFile.exists());

    // Clean up.
    dataDirFile.delete();
}
 
Example 2
Source File: SoftwareProcessEntityTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testInstallDirAndRunDirUsingTilde() throws Exception {
    String dataDirName = ".brooklyn-foo"+Strings.makeRandomId(4);
    String dataDir = "~/"+dataDirName;
    String resolvedDataDir = Os.mergePaths(Os.home(), dataDirName);
    
    MyService entity = app.createAndManageChild(EntitySpec.create(MyService.class)
        .configure(BrooklynConfigKeys.ONBOX_BASE_DIR, dataDir));

    entity.start(ImmutableList.of(loc));

    Assert.assertEquals(Os.nativePath(entity.getAttribute(SoftwareProcess.INSTALL_DIR)),
                        Os.nativePath(Os.mergePaths(resolvedDataDir, "installs/MyService")));
    Assert.assertEquals(Os.nativePath(entity.getAttribute(SoftwareProcess.RUN_DIR)),
                        Os.nativePath(Os.mergePaths(resolvedDataDir, "apps/"+entity.getApplicationId()+"/entities/MyService_"+entity.getId())));
}
 
Example 3
Source File: AbstractManagementContext.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public AbstractManagementContext(BrooklynProperties brooklynProperties) {
    this.managementNodeId = Strings.makeRandomId(8);

    this.configMap = new DeferredBrooklynProperties(brooklynProperties, this);
    this.scratchpad = new BasicScratchpad();
    this.entityDriverManager = new BasicEntityDriverManager();
    this.downloadsManager = BasicDownloadsManager.newDefault(configMap);
    
    this.catalog = new BasicBrooklynCatalog(this);
    this.typeRegistry = new BasicBrooklynTypeRegistry(this);
    
    this.storage = new BrooklynStorageImpl();
    this.rebindManager = new RebindManagerImpl(this); // TODO leaking "this" reference; yuck
    this.managementNodeStateListenerManager = new ManagementNodeStateListenerManager(this); // TODO leaking "this" reference; yuck
    this.highAvailabilityManager = new HighAvailabilityManagerImpl(this, managementNodeStateListenerManager); // TODO leaking "this" reference; yuck
    
    this.entitlementManager = Entitlements.newManager(this, brooklynProperties);
    this.configSupplierRegistry = new BasicExternalConfigSupplierRegistry(this); // TODO leaking "this" reference; yuck
}
 
Example 4
Source File: BindDnsServerImpl.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
protected void appendTemplate(String template, String destination, SshMachineLocation machine) {
    String content = ((BindDnsServerSshDriver) getDriver()).processTemplate(template);
    String temp = "/tmp/template-" + Strings.makeRandomId(6);
    machine.copyTo(new ByteArrayInputStream(content.getBytes()), temp);
    machine.execScript("updating file", ImmutableList.of(
            BashCommands.sudo(String.format("tee -a %s < %s", destination, temp)),
            String.format("rm -f %s", temp)));
}
 
Example 5
Source File: JavaWebAppSshDriver.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
/**
 * Deploys a URL as a webapp at the appserver.
 *
 * Returns a token which can be used as an argument to undeploy,
 * typically the web context with leading slash where the app can be reached (just "/" for ROOT)
 *
 * @see JavaWebAppSoftwareProcess#deploy(String, String) for details of how input filenames are handled
 */
@Override
public String deploy(final String url, final String targetName) {
    final String canonicalTargetName = getFilenameContextMapper().convertDeploymentTargetNameToFilename(targetName);
    final String dest = getDeployDir() + "/" + canonicalTargetName;
    //write to a .tmp so autodeploy is not triggered during upload
    final String tmpDest = dest + "." + Strings.makeRandomId(8) + ".tmp";
    final String msg = String.format("deploying %s to %s:%s", new Object[]{url, getHostname(), dest});
    log.info(entity + " " + msg);
    Tasks.setBlockingDetails(msg);
    try {
        final String copyTaskMsg = String.format("copying %s to %s:%s", new Object[]{url, getHostname(), tmpDest});
        DynamicTasks.queue(copyTaskMsg, new Runnable() {
            @Override
            public void run() {
                int result = copyResource(url, tmpDest);
                if (result != 0) {
                    throw new IllegalStateException("Invalid result " + result + " while " + copyTaskMsg);
                }
            }
        });

        // create a backup
        DynamicTasks.queue(SshTasks.newSshExecTaskFactory(getMachine(), String.format("mv -f %s %s.bak", dest, dest))
                .allowingNonZeroExitCode());

        //rename temporary upload file to .war to be picked up for deployment
        DynamicTasks.queue(SshTasks.newSshExecTaskFactory(getMachine(), String.format("mv -f %s %s", tmpDest, dest))
                .requiringExitCodeZero());
        log.debug("{} deployed {} to {}:{}", new Object[]{entity, url, getHostname(), dest});

        DynamicTasks.waitForLast();
    } finally {
        Tasks.resetBlockingDetails();
    }
    return getFilenameContextMapper().convertDeploymentTargetNameToContext(canonicalTargetName);
}
 
Example 6
Source File: Os.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected boolean check(String candidate) {
    try {
        File f = new File(candidate);
        if (!f.exists()) {
            log.debug("TmpDirFinder: Candidate tmp dir '"+candidate+"' does not exist");
            return false;
        }
        if (!f.isDirectory()) {
            log.debug("TmpDirFinder: Candidate tmp dir '"+candidate+"' is not a directory");
            return false;
        }
        File f2 = new File(f, "brooklyn-tmp-check-"+Strings.makeRandomId(4));
        if (!f2.createNewFile()) {
            log.debug("TmpDirFinder: Candidate tmp dir '"+candidate+"' cannot have files created inside it ("+f2+")");
            return false;
        }
        if (!f2.delete()) {
            log.debug("TmpDirFinder: Candidate tmp dir '"+candidate+"' cannot have files deleted inside it ("+f2+")");
            return false;
        }
        
        return true;
    } catch (Exception e) {
        log.debug("TmpDirFinder: Candidate tmp dir '"+candidate+"' is not valid: "+e);
        return false;
    }
}
 
Example 7
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 8
Source File: LocalResourcesDownloader.java    From SeaCloudsPlatform with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a tmp directory based on:</br>
 * TMP_OS_DIRECTORY/brooklyn/ENTITY_ID/RANDOM_STRING(8)
 * @return
 */
public static String findATmpDir(){
    String osTmpDir = new Os.TmpDirFinder().get().get();
    return osTmpDir + File.separator +
            BROOKLYN_DIR + File.separator +
            Strings.makeRandomId(8);
}
 
Example 9
Source File: BindDnsServerSshDriver.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
private void copyAsRoot(String template, String destination) {
    String content = processTemplate(template);
    String temp = "/tmp/template-" + Strings.makeRandomId(6);
    getMachine().copyTo(new ByteArrayInputStream(content.getBytes()), temp);
    getMachine().execScript("copying file", ImmutableList.of(BashCommands.sudo(String.format("mv %s %s", temp, destination))));
}
 
Example 10
Source File: BasicBrooklynCatalog.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private String getIdWithRandomDefault() {
    return idAsSymbolicNameWithoutVersion != null ? idAsSymbolicNameWithoutVersion : Strings.makeRandomId(10);
}
 
Example 11
Source File: LocalManagementContext.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public void generateManagementPlaneId() {
    if (this.managementPlaneId != null) {
        throw new IllegalStateException("Request to generate a management plane ID for node "+getManagementNodeId()+" but one already exists (" + managementPlaneId + ")");
    }
    this.managementPlaneId = Strings.makeRandomId(8);
}