protected void closeWhispering(Closeable closeable, Object context) { closeWhispering(closeable, this, context); }
@Override public int execCommands(Map<String,?> props, List<String> commands) { return execCommands(props, commands, Collections.<String,Object>emptyMap()); }
@Override public int execScript(Map<String,?> props, List<String> commands) { return execScript(props, commands, Collections.<String,Object>emptyMap()); }
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(); } public B host(String val) {
protected String getSummary() { String summary = getOptionalVal(props, PROP_SUMMARY); return (summary != null) ? summary : scriptPath; }
protected String toScript(Map<String,?> props, List<String> commands, Map<String,?> env) { List<String> allcmds = toCommandSequence(commands, env); StringBuilder result = new StringBuilder(); result.append(getOptionalVal(props, PROP_SCRIPT_HEADER)).append('\n'); for (String cmd : allcmds) { result.append(cmd).append('\n'); } return result.toString(); }
protected File writeTempFile(String contents) { return writeTempFile(contents.getBytes()); }
@Override public B from(Map<String,?> props) { super.from(props); sshTries = getOptionalVal(props, PROP_SSH_TRIES); sshTriesTimeout = getOptionalVal(props, PROP_SSH_TRIES_TIMEOUT); sshRetryDelay = getOptionalVal(props, PROP_SSH_RETRY_DELAY); connectTimeout = getOptionalVal(props, PROP_CONNECT_TIMEOUT); sessionTimeout = getOptionalVal(props, PROP_SESSION_TIMEOUT); return self(); } public B connectTimeout(int val) {
protected File writeTempFile(byte[] contents) { return writeTempFile(new ByteArrayInputStream(contents)); }
@Override public B from(Map<String,?> props) { super.from(props); sshExecutable = getOptionalVal(props, PROP_SSH_EXECUTABLE); sshFlags = getOptionalVal(props, PROP_SSH_FLAGS); scpExecutable = getOptionalVal(props, PROP_SCP_EXECUTABLE); return self(); } public B sshExecutable(String val) {
PutFileAction(Map<String,?> props, String path, Supplier<InputStream> contentsSupplier, long length) { String permissions = getOptionalVal(props, PROP_PERMISSIONS); long lastModificationDateVal = getOptionalVal(props, PROP_LAST_MODIFICATION_DATE); long lastAccessDateVal = getOptionalVal(props, PROP_LAST_ACCESS_DATE); if (lastAccessDateVal <= 0 ^ lastModificationDateVal <= 0) { lastAccessDateVal = Math.max(lastAccessDateVal, lastModificationDateVal); lastModificationDateVal = Math.max(lastAccessDateVal, lastModificationDateVal); } this.permissionsMask = Integer.parseInt(permissions, 8); this.lastAccessDate = lastAccessDateVal; this.lastModificationDate = lastModificationDateVal; this.uid = getOptionalVal(props, PROP_OWNER_UID); this.path = checkNotNull(path, "path"); this.contentsSupplier = checkNotNull(contentsSupplier, "contents"); this.length = Ints.checkedCast(checkNotNull(length, "size")); }
public int execScript(Map<String,?> props, String summaryForLogging, List<String> commands, Map<String,?> env) { // TODO scriptHeader are the extra commands we expect the SshTool/ShellTool to add. // Would be better if could get this from the ssh-tool, rather than assuming it will behave as // we expect. String scriptHeader = ShellAbstractTool.getOptionalVal(props, ShellTool.PROP_SCRIPT_HEADER); return execWithLogging(props, summaryForLogging, commands, env, scriptHeader, new ExecRunner() { @Override public int exec(ShellTool ssh, Map<String, ?> flags, List<String> cmds, Map<String, ?> env) { return ssh.execScript(flags, cmds, env); }}); }
public ToolAbstractExecScript(Map<String,?> props) { this.props = props; this.separator = getOptionalVal(props, PROP_SEPARATOR); this.out = getOptionalVal(props, PROP_OUT_STREAM); this.err = getOptionalVal(props, PROP_ERR_STREAM); this.scriptDir = getOptionalVal(props, PROP_SCRIPT_DIR); this.runAsRoot = Boolean.TRUE.equals(getOptionalVal(props, PROP_RUN_AS_ROOT)); this.authSudo = Boolean.TRUE.equals(getOptionalVal(props, PROP_AUTH_SUDO)); this.noExtraOutput = Boolean.TRUE.equals(getOptionalVal(props, PROP_NO_EXTRA_OUTPUT)); this.noDeleteAfterExec = Boolean.TRUE.equals(getOptionalVal(props, PROP_NO_DELETE_SCRIPT)); this.password = getOptionalVal(props, PROP_PASSWORD); this.execTimeout = getOptionalVal(props, PROP_EXEC_TIMEOUT); String summary = getOptionalVal(props, PROP_SUMMARY); if (summary!=null) { summary = Strings.makeValidFilename(summary); if (summary.length()>30) summary = summary.substring(0,30); } this.scriptNameWithoutExtension = "brooklyn-"+ Time.makeDateStampString()+"-"+Identifiers.makeRandomId(4)+ (Strings.isBlank(summary) ? "" : "-"+summary); this.scriptPath = Os.mergePathsUnix(scriptDir, scriptNameWithoutExtension+".sh"); }
@Override public int run() { try { String directory = getOptionalVal(props, PROP_DIRECTORY); File directoryDir = (directory != null) ? new File(Os.tidyPath(directory)) : null; String scriptContents = toScript(props, commands, env); if (LOG.isTraceEnabled()) LOG.trace("Running shell process (process) as script:\n{}", scriptContents); File to = new File(scriptPath); Files.createParentDirs(to); ByteSource.wrap(scriptContents.getBytes()).copyTo(Files.asByteSink(to)); List<String> cmds = buildRunScriptCommand(); cmds.add(0, "chmod +x "+scriptPath); return asInt(execProcesses(cmds, null, directoryDir, out, err, separator, getOptionalVal(props, PROP_LOGIN_SHELL), this), -1); } catch (IOException e) { throw Throwables.propagate(e); } } }.run();