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();
}
}
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);
}
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());
}
Aggregations