X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjsch%2FRemoteExec.java;h=814e080481219641027d74f96df82648cf8f0eab;hb=1fdb1b4e7b1d2b0cabb6483238301b857a6392fa;hp=d07e6ab6cf4273924f87ab1ecf9e18088c9e648c;hpb=67f896c4f853b336046d8586ec514c96f468c88a;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java index d07e6ab6c..814e08048 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/RemoteExec.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.argeo.slc.jsch; import java.io.BufferedReader; @@ -48,6 +64,8 @@ public class RemoteExec extends AbstractJschTask { private Resource stdOut = null; private ExecutionResources executionResources; + private String user; + /** * If set, stdout is written to it as a list of lines. Cleared before each * run. @@ -56,6 +74,14 @@ public class RemoteExec extends AbstractJschTask { private Boolean logEvenIfStdOutLines = false; private Boolean quiet = false; + public RemoteExec() { + } + + public RemoteExec(SshTarget sshTarget, String cmd) { + setSshTarget(sshTarget); + setCommand(cmd); + } + public void run(Session session) { List commandsToUse = new ArrayList(commands); String commandToUse = command; @@ -96,9 +122,17 @@ public class RemoteExec extends AbstractJschTask { } if (forceShell) { - if (commandToUse.indexOf(';') >= 0 - || commandToUse.indexOf('\n') >= 0) { - StringTokenizer st = new StringTokenizer(commandToUse, ";\n"); + // for the time being do not interpret both \n and ; + // priority to \n + // until we know how to parse ; within "" + if (commandToUse.indexOf('\n') >= 0) { + StringTokenizer st = new StringTokenizer(commandToUse, "\n"); + while (st.hasMoreTokens()) { + String cmd = st.nextToken(); + commandsToUse.add(cmd); + } + } else if (commandToUse.indexOf(';') >= 0) { + StringTokenizer st = new StringTokenizer(commandToUse, ";"); while (st.hasMoreTokens()) { String cmd = st.nextToken(); commandsToUse.add(cmd); @@ -109,6 +143,19 @@ public class RemoteExec extends AbstractJschTask { commandToUse = null; } + // run as user + if (user != null) { + if (commandsToUse.size() > 0) { + commandsToUse.add(0, "su - " + user); + commandsToUse.add("exit"); + } else { + if (command.indexOf('\"') >= 0) + throw new SlcException( + "Don't know how to su a command with \", use shell instead."); + commandToUse = "su - " + user + " -c \"" + command + "\""; + } + } + // execute command(s) if (commandToUse != null) { if (commandsToUse.size() != 0) @@ -400,4 +447,8 @@ public class RemoteExec extends AbstractJschTask { this.executionResources = executionResources; } + public void setUser(String user) { + this.user = user; + } + }