Search in sources :

Example 1 with LocalLauncher

use of hudson.Launcher.LocalLauncher in project hudson-2.x by hudson.

the class WindowsSlaveInstaller method runElevated.

/**
     * Invokes slave.exe with a SCM management command.
     *
     * <p>
     * If it fails in a way that indicates the presence of UAC, retry in an UAC compatible manner.
     */
static int runElevated(File slaveExe, String command, TaskListener out, File pwd) throws IOException, InterruptedException {
    try {
        return new LocalLauncher(out).launch().cmds(slaveExe, command).stdout(out).pwd(pwd).join();
    } catch (IOException e) {
        if (e.getMessage().contains("CreateProcess") && e.getMessage().contains("=740")) {
        // fall through
        } else {
            throw e;
        }
    }
    // error code 740 is ERROR_ELEVATION_REQUIRED, indicating that
    // we run in UAC-enabled Windows and we need to run this in an elevated privilege
    SHELLEXECUTEINFO sei = new SHELLEXECUTEINFO();
    sei.fMask = SEE_MASK_NOCLOSEPROCESS;
    sei.lpVerb = "runas";
    sei.lpFile = slaveExe.getAbsolutePath();
    sei.lpParameters = "/redirect redirect.log " + command;
    sei.lpDirectory = pwd.getAbsolutePath();
    sei.nShow = SW_HIDE;
    if (!Shell32.INSTANCE.ShellExecuteEx(sei))
        throw new IOException("Failed to shellExecute: " + Native.getLastError());
    try {
        return Kernel32Utils.waitForExitProcess(sei.hProcess);
    } finally {
        FileInputStream fin = new FileInputStream(new File(pwd, "redirect.log"));
        IOUtils.copy(fin, out.getLogger());
        fin.close();
    }
}
Also used : LocalLauncher(hudson.Launcher.LocalLauncher) SHELLEXECUTEINFO(hudson.util.jna.SHELLEXECUTEINFO) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with LocalLauncher

use of hudson.Launcher.LocalLauncher in project hudson-2.x by hudson.

the class Channels method newJVM.

/**
     * Launches a new JVM with the given classpath, establish a communication channel,
     * and return a {@link Channel} to it.
     *
     * @param displayName
     *      Human readable name of what this JVM represents. For example "Selenium grid" or "Hadoop".
     *      This token is used for messages to {@code listener}.
     * @param listener
     *      The progress of the launcher and the failure information will be sent here. Must not be null.
     * @param workDir
     *      If non-null, the new JVM will have this directory as the working directory. This must be a local path.
     * @param classpath
     *      The classpath of the new JVM. Can be null if you just need {@code slave.jar} (and everything else
     *      can be sent over the channel.) But if you have jars that are known to be necessary by the new JVM,
     *      setting it here will improve the classloading performance (by avoiding remote class file transfer.)
     *      Classes in this classpath will also take precedence over any other classes that's sent via the channel
     *      later, so it's also useful for making sure you get the version of the classes you want.
     * @param vmb
     *      A partially configured {@link JVMBuilder} that allows the caller to fine-tune the launch parameter.
     *
     * @return
     *      never null
     * @since 1.361
     */
public static Channel newJVM(String displayName, TaskListener listener, JVMBuilder vmb, FilePath workDir, ClasspathBuilder classpath) throws IOException {
    ServerSocket serverSocket = new ServerSocket();
    serverSocket.bind(new InetSocketAddress("localhost", 0));
    serverSocket.setSoTimeout(10 * 1000);
    // use -cp + FQCN instead of -jar since remoting.jar can be rebundled (like in the case of the swarm plugin.)
    vmb.classpath().addJarOf(Channel.class);
    vmb.mainClass(Launcher.class);
    if (classpath != null)
        vmb.args().add("-cp").add(classpath);
    vmb.args().add("-connectTo", "localhost:" + serverSocket.getLocalPort());
    listener.getLogger().println("Starting " + displayName);
    Proc p = vmb.launch(new LocalLauncher(listener)).stdout(listener).pwd(workDir).start();
    Socket s = serverSocket.accept();
    serverSocket.close();
    return forProcess("Channel to " + displayName, Computer.threadPoolForRemoting, new BufferedInputStream(new SocketInputStream(s)), new BufferedOutputStream(new SocketOutputStream(s)), null, p);
}
Also used : Proc(hudson.Proc) LocalLauncher(hudson.Launcher.LocalLauncher) SocketOutputStream(hudson.remoting.SocketOutputStream) BufferedInputStream(java.io.BufferedInputStream) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) SocketInputStream(hudson.remoting.SocketInputStream) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 3 with LocalLauncher

use of hudson.Launcher.LocalLauncher in project hudson-2.x by hudson.

the class WindowsServiceLifecycle method restart.

@Override
public void restart() throws IOException, InterruptedException {
    File me = getHudsonWar();
    File home = me.getParentFile();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamTaskListener task = new StreamTaskListener(baos);
    task.getLogger().println("Restarting a service");
    int r = new LocalLauncher(task).launch().cmds(new File(home, "hudson.exe"), "restart").stdout(task).pwd(home).join();
    if (r != 0)
        throw new IOException(baos.toString());
}
Also used : LocalLauncher(hudson.Launcher.LocalLauncher) StreamTaskListener(hudson.util.StreamTaskListener) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Aggregations

LocalLauncher (hudson.Launcher.LocalLauncher)3 File (java.io.File)2 IOException (java.io.IOException)2 Proc (hudson.Proc)1 SocketInputStream (hudson.remoting.SocketInputStream)1 SocketOutputStream (hudson.remoting.SocketOutputStream)1 StreamTaskListener (hudson.util.StreamTaskListener)1 SHELLEXECUTEINFO (hudson.util.jna.SHELLEXECUTEINFO)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1