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

The following examples show how to use org.apache.brooklyn.util.os.Os#tidyPath() . 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: ProcessTool.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public int execCommands(Map<String,?> props, List<String> commands, Map<String,?> env) {
    if (Boolean.FALSE.equals(props.get("blocks"))) {
        throw new IllegalArgumentException("Cannot exec non-blocking: command="+commands);
    }
    OutputStream out = getOptionalVal(props, PROP_OUT_STREAM);
    OutputStream err = getOptionalVal(props, PROP_ERR_STREAM);
    String separator = getOptionalVal(props, PROP_SEPARATOR);
    String directory = getOptionalVal(props, PROP_DIRECTORY);
    File directoryDir = (directory != null) ? new File(Os.tidyPath(directory)) : null;

    List<String> allcmds = toCommandSequence(commands, null);

    String singlecmd = Joiner.on(separator).join(allcmds);
    if (Boolean.TRUE.equals(getOptionalVal(props, PROP_RUN_AS_ROOT))) {
        LOG.warn("Cannot run as root when executing as command; run as a script instead (will run as normal user): "+singlecmd);
    }
    if (LOG.isTraceEnabled()) LOG.trace("Running shell command (process): {}", singlecmd);
    
    return asInt(execProcesses(allcmds, env, directoryDir, out, err, separator, getOptionalVal(props, PROP_LOGIN_SHELL), this), -1);
}
 
Example 2
Source File: SshAbstractTool.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public B from(Map<String,?> props) {
    host = getMandatoryVal(props, PROP_HOST);
    port = getOptionalVal(props, PROP_PORT);
    user = getOptionalVal(props, PROP_USER);
    
    password = getOptionalVal(props, PROP_PASSWORD);
    
    warnOnDeprecated(props, "privateKey", "privateKeyData");
    privateKeyData = getOptionalVal(props, PROP_PRIVATE_KEY_DATA);
    privateKeyPassphrase = getOptionalVal(props, PROP_PRIVATE_KEY_PASSPHRASE);
    
    // for backwards compatibility accept keyFiles and privateKey
    // but sshj accepts only a single privateKeyFile; leave blank to use defaults (i.e. ~/.ssh/id_rsa and id_dsa)
    warnOnDeprecated(props, "keyFiles", null);
    String privateKeyFile = getOptionalVal(props, PROP_PRIVATE_KEY_FILE);
    if (privateKeyFile != null) privateKeyFiles.add(privateKeyFile);
    
    strictHostKeyChecking = getOptionalVal(props, PROP_STRICT_HOST_KEY_CHECKING);
    allocatePTY = getOptionalVal(props, PROP_ALLOCATE_PTY);
    
    String localTempDirPath = getOptionalVal(props, PROP_LOCAL_TEMP_DIR);
    localTempDir = (localTempDirPath == null) ? null : new File(Os.tidyPath(localTempDirPath));
    
    return self();
}
 
Example 3
Source File: MachineLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves the on-box dir.
 * <p>
 * Initialize and pre-create the right onbox working dir, if an ssh machine location.
 * Logs a warning if not.
 */
@SuppressWarnings("deprecation")
public static String resolveOnBoxDir(EntityInternal entity, MachineLocation machine) {
    String base = entity.getConfig(BrooklynConfigKeys.ONBOX_BASE_DIR);
    if (base==null) base = machine.getConfig(BrooklynConfigKeys.ONBOX_BASE_DIR);
    if (base!=null && Boolean.TRUE.equals(entity.getConfig(ON_BOX_BASE_DIR_RESOLVED))) return base;
    if (base==null) base = entity.getManagementContext().getConfig().getConfig(BrooklynConfigKeys.ONBOX_BASE_DIR);
    if (base==null) base = entity.getConfig(BrooklynConfigKeys.BROOKLYN_DATA_DIR);
    if (base==null) base = machine.getConfig(BrooklynConfigKeys.BROOKLYN_DATA_DIR);
    if (base==null) base = entity.getManagementContext().getConfig().getConfig(BrooklynConfigKeys.BROOKLYN_DATA_DIR);
    if (base==null) base = "~/brooklyn-managed-processes";
    
    if (base.equals("~")) base=".";
    if (base.startsWith("~/")) base = "."+base.substring(1);

    String resolvedBase = null;
    if (entity.getConfig(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION) || machine.getConfig(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION)) {
        if (log.isDebugEnabled()) log.debug("Skipping on-box base dir resolution for "+entity+" at "+machine);
        if (!Os.isAbsolutish(base)) base = "~/"+base;
        resolvedBase = Os.tidyPath(base);
    } else if (machine instanceof CanResolveOnBoxDir) {
        resolvedBase = ((CanResolveOnBoxDir)machine).resolveOnBoxDirFor(entity, base);
    }
    if (resolvedBase==null) {
        if (!Os.isAbsolutish(base)) base = "~/"+base;
        resolvedBase = Os.tidyPath(base);
        log.warn("Could not resolve on-box directory for "+entity+" at "+machine+"; using "+resolvedBase+", though this may not be accurate at the target (and may fail shortly)");
    }
    entity.config().set(BrooklynConfigKeys.ONBOX_BASE_DIR, resolvedBase);
    entity.config().set(ON_BOX_BASE_DIR_RESOLVED, true);
    return resolvedBase;
}
 
Example 4
Source File: ArchiveBuilder.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Set the location of the generated archive file.
 */
public ArchiveBuilder named(String name) {
    checkNotNull(name);
    String ext = Files.getFileExtension(name);
    if (ext.isEmpty()) {
        name = name + "." + type.toString();
    } else if (type != ArchiveType.of(name)) {
        throw new IllegalArgumentException(String.format("Extension for '%s' did not match archive type of %s", ext, type));
    }
    this.archive = new File(Os.tidyPath(name));
    return this;
}
 
Example 5
Source File: BrooklynServerPaths.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** @see BrooklynServerPaths#getMgmtBaseDir(ManagementContext) */
@SuppressWarnings("deprecation")
public static String getMgmtBaseDir(StringConfigMap brooklynProperties) {
    String base = (String) brooklynProperties.getConfigLocalRaw(BrooklynServerConfig.MGMT_BASE_DIR).orNull();
    if (base==null) {
        base = brooklynProperties.getConfig(BrooklynServerConfig.BROOKLYN_DATA_DIR);
        if (base!=null)
            log.warn("Using deprecated "+BrooklynServerConfig.BROOKLYN_DATA_DIR.getName()+": use "+BrooklynServerConfig.MGMT_BASE_DIR.getName()+" instead; value: "+base);
    }
    if (base==null) base = brooklynProperties.getConfig(BrooklynServerConfig.MGMT_BASE_DIR);
    return Os.tidyPath(base)+File.separator;
}
 
Example 6
Source File: BrooklynServerPaths.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** @see BrooklynServerPaths#getMgmtBaseDir(ManagementContext) */
@SuppressWarnings("deprecation")
public static String getMgmtBaseDir(Map<String,?> brooklynProperties) {
    String base = (String) brooklynProperties.get(BrooklynServerConfig.MGMT_BASE_DIR.getName());
    if (base==null) base = (String) brooklynProperties.get(BrooklynServerConfig.BROOKLYN_DATA_DIR.getName());
    if (base==null) base = BrooklynServerConfig.MGMT_BASE_DIR.getDefaultValue();
    return Os.tidyPath(base)+File.separator;
}
 
Example 7
Source File: BrooklynProperties.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private static void addPropertiesFromFile(BrooklynProperties p, String file) {
    if (file==null) return;
    
    String fileTidied = Os.tidyPath(file);
    File f = new File(fileTidied);

    if (f.exists()) {
        p.addFrom(f);
    }
}
 
Example 8
Source File: BrooklynPropertiesImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private static void addPropertiesFromFile(BrooklynPropertiesImpl p, String file) {
    if (file==null) return;

    String fileTidied = Os.tidyPath(file);
    File f = new File(fileTidied);

    if (f.exists()) {
        p.addFrom(f);
    }
}
 
Example 9
Source File: BrooklynPropertiesFactoryHelper.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public BrooklynProperties.Factory.Builder createPropertiesBuilder() {
    if (brooklynProperties == null) {
        BrooklynProperties.Factory.Builder builder = BrooklynProperties.Factory.builderDefault();

        if (Strings.isNonEmpty(globalBrooklynPropertiesFile)) {
            File globalProperties = new File(Os.tidyPath(globalBrooklynPropertiesFile));
            if (globalProperties.exists()) {
                globalProperties = resolveSymbolicLink(globalProperties);
                checkFileReadable(globalProperties);
                // brooklyn.properties stores passwords (web-console and cloud credentials),
                // so ensure it has sensible permissions
                checkFilePermissionsX00(globalProperties);
                LOG.debug("Using global properties file " + globalProperties);
            } else {
                LOG.debug("Global properties file " + globalProperties + " does not exist, will ignore");
            }
            builder.globalPropertiesFile(globalProperties.getAbsolutePath());
        } else {
            LOG.debug("Global properties file disabled");
            builder.globalPropertiesFile(null);
        }
        
        if (Strings.isNonEmpty(localBrooklynPropertiesFile)) {
            File localProperties = new File(Os.tidyPath(localBrooklynPropertiesFile));
            localProperties = resolveSymbolicLink(localProperties);
            checkFileReadable(localProperties);
            checkFilePermissionsX00(localProperties);
            builder.localPropertiesFile(localProperties.getAbsolutePath());
        }

        if (propertiesSupplier != null) {
            builder.propertiesSupplier(propertiesSupplier);
        }
        return builder;
    } else {
        if (globalBrooklynPropertiesFile != null)
            LOG.warn("Ignoring globalBrooklynPropertiesFile "+globalBrooklynPropertiesFile+" because explicit brooklynProperties supplied");
        if (localBrooklynPropertiesFile != null)
            LOG.warn("Ignoring localBrooklynPropertiesFile "+localBrooklynPropertiesFile+" because explicit brooklynProperties supplied");
        return Builder.fromProperties(brooklynProperties);
    }
}
 
Example 10
Source File: ClassFinder.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/** finds a jar at a url, or for directories, jars under a path */
@Beta
public static List<URL> toJarUrls(String url) throws MalformedURLException {
    if (url==null) throw new NullPointerException("Cannot read from null");
    if (url=="") throw new NullPointerException("Cannot read from empty string");
    
    List<URL> result = Lists.newArrayList();
    
    String protocol = Urls.getProtocol(url);
    if (protocol!=null) {
        // it's a URL - easy
        if ("file".equals(protocol)) {
            url = ResourceUtils.tidyFileUrl(url);
        }
        result.add(new URL(url));
    } else {
        // treat as file
        String tidiedPath = Os.tidyPath(url);
        File tidiedFile = new File(tidiedPath);
        if (tidiedFile.isDirectory()) {
            List<File> toscan = Lists.newLinkedList();
            toscan.add(tidiedFile);
            while (toscan.size() > 0) {
                File file = toscan.remove(0);
                if (file.isFile()) {
                    if (file.getName().toLowerCase().endsWith(".jar")) {
                        result.add(new URL("file://"+file.getAbsolutePath()));
                    }
                } else if (file.isDirectory()) {
                    for (File subfile : file.listFiles()) {
                        toscan.add(subfile);
                    }
                } else {
                    log.info("Cannot read "+file+"; not a file or directory");
                }
            }
        } else {
            result.add(tidiedFile.toURI().toURL());
        }
    }
    
    return result;
}
 
Example 11
Source File: ShellAbstractTool.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public ShellAbstractTool(String localTempDir) {
    this(localTempDir == null ? null : new File(Os.tidyPath(localTempDir)));
}
 
Example 12
Source File: BrooklynServerPaths.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
protected static String resolveAgainstBaseDir(StringConfigMap brooklynProperties, String path) {
    if (!Os.isAbsolutish(path)) path = Os.mergePaths(getMgmtBaseDir(brooklynProperties), path);
    return Os.tidyPath(path);
}