Search in sources :

Example 1 with IRemoteCommandLauncher

use of org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher in project linuxtools by eclipse.

the class CommandLauncherProxyTest method testRemoteCommandLauncher.

@Test
public void testRemoteCommandLauncher() {
    IRemoteCommandLauncher cl = null;
    Process p = null;
    IPath commandPath, changeToDirectory;
    String[] args, env;
    try {
        cl = proxyManager.getLauncher(syncProject.getProject());
        assertTrue("Should have returned a remote launcher", cl instanceof RDTCommandLauncher);
    } catch (CoreException e) {
        fail("Should have returned a launcher: " + e.getCause());
    }
    commandPath = new Path("uptime");
    args = new String[] { "-s" };
    env = new String[] { "PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin" };
    changeToDirectory = new Path("/tmp");
    try {
        p = cl.execute(commandPath, args, env, changeToDirectory, new NullProgressMonitor());
        assertNotNull(p);
        p.waitFor();
        assertEquals("Process exited with failure", 0, p.exitValue());
    } catch (Exception e) {
        fail("Unable to execute " + commandPath + " on remote machine: " + e.getMessage());
    }
    InputStream actualIS = p.getInputStream();
    int v = -1;
    try {
        v = actualIS.read();
    } catch (IOException e) {
        fail("Failed to read output of command executed remotely: " + e.getMessage());
    }
    // Ensure something can be read
    assertTrue(v != -1);
    /*
		 * Test it opens connection before execute.
		 */
    IRemoteConnection conn = getConnection();
    conn.close();
    assertFalse(conn.isOpen());
    try {
        p = cl.execute(new Path("ls"), new String[] {}, new String[] {}, null, new NullProgressMonitor());
        assertNotNull(p);
        p.waitFor();
        assertEquals("Process exited with failure", 0, p.exitValue());
    } catch (CoreException | InterruptedException e) {
        fail("Failed to open connection to execute a command: " + e.getMessage());
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) IRemoteConnection(org.eclipse.remote.core.IRemoteConnection) InputStream(java.io.InputStream) IRemoteCommandLauncher(org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher) IOException(java.io.IOException) RDTCommandLauncher(org.eclipse.linuxtools.internal.rdt.proxy.RDTCommandLauncher) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) CoreException(org.eclipse.core.runtime.CoreException) AbstractProxyTest(org.eclipse.linuxtools.remote.proxy.tests.AbstractProxyTest) Test(org.junit.Test)

Example 2 with IRemoteCommandLauncher

use of org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher in project linuxtools by eclipse.

the class CommandLauncherProxyTest method testLocalCommandLauncher.

@Test
public void testLocalCommandLauncher() {
    IRemoteCommandLauncher cl = null;
    Process p = null;
    InputStream actualIS = null, expectedIS = null;
    IPath commandPath, changeToDirectory;
    String[] args, env;
    try {
        cl = proxyManager.getLauncher(localProject.getProject());
        assertTrue("Should have returned a local launcher", cl instanceof LocalLauncher);
    } catch (CoreException e) {
        fail("Should have returned a launcher: " + e.getCause());
    }
    /*
		 * Prepare arguments for the test
		 */
    commandPath = new Path("/bin/uptime");
    args = new String[] { "-s" };
    StringBuilder fullCmd = new StringBuilder();
    fullCmd.append(commandPath.toOSString());
    for (String s : args) {
        fullCmd.append(" " + s);
    }
    // Use local env variables
    ArrayList<String> envList = new ArrayList<>();
    for (Entry<String, String> entry : System.getenv().entrySet()) {
        envList.add(entry.getKey() + "=" + entry.getValue());
    }
    env = envList.toArray(new String[] {});
    changeToDirectory = new Path("/tmp");
    /*
		 * Run and get results using the proxy
		 */
    try {
        p = cl.execute(commandPath, args, env, changeToDirectory, new NullProgressMonitor());
        assertNotNull(p);
        while (p.isAlive()) {
        }
        // Call to waitFor() will drive to empty result
        // p.waitFor();
        assertEquals("Process exited with failure", 0, p.exitValue());
    } catch (Exception e) {
        fail("Unable to execute " + fullCmd.toString() + " on local machine: " + e.getMessage());
    }
    actualIS = p.getInputStream();
    assertNotNull(actualIS);
    /*
		 * Run and get results using java Runtime
		 */
    try {
        Process expectedProcess = Runtime.getRuntime().exec(fullCmd.toString());
        expectedProcess.waitFor();
        expectedIS = expectedProcess.getInputStream();
        assertNotNull(expectedIS);
    } catch (Exception e) {
        fail("Unable to execute " + fullCmd.toString() + " on  local using Runtime.exec: " + e.getMessage());
    }
    /*
		 * Finally compare results obtained
		 */
    int va = 0, ve = 0;
    do {
        try {
            va = actualIS.read();
            ve = expectedIS.read();
        } catch (IOException e) {
            fail("Unable to read from Input Stream: " + e.getMessage());
        }
        assertEquals("Local proxy command output differs from Runtime.exec", ve, va);
    } while (va != -1);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LocalLauncher(org.eclipse.linuxtools.internal.profiling.launch.LocalLauncher) IPath(org.eclipse.core.runtime.IPath) InputStream(java.io.InputStream) IRemoteCommandLauncher(org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) CoreException(org.eclipse.core.runtime.CoreException) AbstractProxyTest(org.eclipse.linuxtools.remote.proxy.tests.AbstractProxyTest) Test(org.junit.Test)

Example 3 with IRemoteCommandLauncher

use of org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher in project linuxtools by eclipse.

the class RemoteProxyManagerTest method testGetLauncher.

@Test
public void testGetLauncher() {
    IRemoteCommandLauncher cl;
    try {
        /*
			 * Test launcher got for local URIs and project
			 */
        cl = proxyManager.getLauncher(localProject.getLocationURI());
        assertTrue("Should have returned a local launcher", cl instanceof LocalLauncher);
        cl = proxyManager.getLauncher(localProject.getProject());
        assertTrue("Should have returned a local launcher", cl instanceof LocalLauncher);
        /*
			 * Test launcher got for remote project and URI
			 */
        cl = proxyManager.getLauncher(URI.create("ssh://" + CONNECTION_NAME + "/path/to/file"));
        assertTrue("Should have returned a remote file proxy", cl instanceof RDTCommandLauncher);
        cl = proxyManager.getLauncher(syncProject.getProject());
        assertTrue("Should have returned a remote launcher", cl instanceof RDTCommandLauncher);
        /*
			 * Test launcher got for jsch scheme
			 */
        cl = proxyManager.getLauncher(URI.create("jsch://" + USERNAME + "@" + HOST + ":22/path/to/file"));
        assertTrue("Should have returned a remote file proxy", cl instanceof SSHCommandLauncher);
    } catch (CoreException e) {
        fail("Should have returned a launcher: " + e.getCause());
    }
    /*
		 * Test the proxy for unsupported URIs
		 */
    try {
        // As of org.eclipse.remote 2.0, remotetools scheme is no longer
        // support
        cl = proxyManager.getLauncher(URI.create("remotetools://MyConnection/path/to/file"));
        fail("remotetools scheme should not be recognized");
    } catch (CoreException e) {
        assertTrue(e.getMessage(), true);
    }
}
Also used : LocalLauncher(org.eclipse.linuxtools.internal.profiling.launch.LocalLauncher) CoreException(org.eclipse.core.runtime.CoreException) SSHCommandLauncher(org.eclipse.linuxtools.internal.ssh.proxy.SSHCommandLauncher) IRemoteCommandLauncher(org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher) RDTCommandLauncher(org.eclipse.linuxtools.internal.rdt.proxy.RDTCommandLauncher) AbstractProxyTest(org.eclipse.linuxtools.remote.proxy.tests.AbstractProxyTest) Test(org.junit.Test)

Example 4 with IRemoteCommandLauncher

use of org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher in project linuxtools by eclipse.

the class RuntimeProcessFactory method exec.

/**
 * Execute one command using the path selected in 'Linux Tools Path' preference page
 * in the informed project.
 * @param cmdarray An array with the command to be executed and its params.
 * @param envp An array with extra enviroment variables to be used when running
 * the command
 * @param dir The directory used as current directory to run the command.
 * @param project The current project. If null, only system path will be
 * used to look for the command.
 * @param pty PTY for use with Eclipse Console.
 * @return The process started by exec.
 * @throws IOException If problem executing the command occured.
 *
 * @since 3.0
 */
public Process exec(String[] cmdarray, String[] envp, IFileStore dir, IProject project, PTY pty) throws IOException {
    Process p = null;
    try {
        cmdarray = fillPathCommand(cmdarray, project);
        String command = cmdarray[0];
        URI uri = URI.create(command);
        IPath changeToDir = null;
        IPath path;
        IRemoteCommandLauncher launcher;
        envp = updateEnvironment(envp, project);
        if (project != null) {
            IRemoteFileProxy proxy = RemoteProxyManager.getInstance().getFileProxy(project);
            path = new Path(proxy.toPath(uri));
            launcher = RemoteProxyManager.getInstance().getLauncher(project);
            if (dir != null) {
                changeToDir = new Path(proxy.toPath(dir.toURI()));
            }
        } else {
            path = new Path(uri.getPath());
            launcher = RemoteProxyManager.getInstance().getLauncher(uri);
            if (dir != null) {
                changeToDir = new Path(dir.toURI().getPath());
            }
        }
        List<String> cmdlist = new ArrayList<>(Arrays.asList(cmdarray));
        cmdlist.remove(0);
        cmdlist.toArray(cmdarray);
        cmdarray = cmdlist.toArray(new String[0]);
        if (pty == null) {
            p = launcher.execute(path, cmdarray, envp, changeToDir, new NullProgressMonitor());
        } else {
            p = launcher.execute(path, cmdarray, envp, changeToDir, new NullProgressMonitor(), pty);
        }
    } catch (CoreException e) {
        e.printStackTrace();
    }
    return p;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) IRemoteFileProxy(org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy) IRemoteCommandLauncher(org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher) ArrayList(java.util.ArrayList) URI(java.net.URI)

Example 5 with IRemoteCommandLauncher

use of org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher in project linuxtools by eclipse.

the class RPMBuild method remoteBuild.

private IStatus remoteBuild(IResource specFile, OutputStream outStream, List<String> command) throws CoreException, URISyntaxException {
    command.remove(0);
    IRemoteProxyManager rmtProxyMgr = RemoteProxyManager.getInstance();
    IRemoteCommandLauncher rmtCmdLauncher = null;
    rmtCmdLauncher = rmtProxyMgr.getLauncher(new URI(mainFolder));
    command.add(specFile.getLocationURI().getPath());
    String[] empty = new String[0];
    Process pProxy = rmtCmdLauncher.execute(Path.fromOSString(rpmBuildCmd), command.toArray(new String[command.size()]), empty, null, new NullProgressMonitor());
    if (pProxy != null) {
        return Utils.watchProcess(outStream, pProxy);
    }
    return Status.OK_STATUS;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IRemoteProxyManager(org.eclipse.linuxtools.profiling.launch.IRemoteProxyManager) IRemoteCommandLauncher(org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher) URI(java.net.URI)

Aggregations

IRemoteCommandLauncher (org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher)9 IPath (org.eclipse.core.runtime.IPath)7 Path (org.eclipse.core.runtime.Path)7 CoreException (org.eclipse.core.runtime.CoreException)6 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)6 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 IRemoteFileProxy (org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy)3 AbstractProxyTest (org.eclipse.linuxtools.remote.proxy.tests.AbstractProxyTest)3 Test (org.junit.Test)3 File (java.io.File)2 InputStream (java.io.InputStream)2 ILaunchManager (org.eclipse.debug.core.ILaunchManager)2 LocalLauncher (org.eclipse.linuxtools.internal.profiling.launch.LocalLauncher)2 RDTCommandLauncher (org.eclipse.linuxtools.internal.rdt.proxy.RDTCommandLauncher)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 SSHCommandLauncher (org.eclipse.linuxtools.internal.ssh.proxy.SSHCommandLauncher)1 IRemoteProxyManager (org.eclipse.linuxtools.profiling.launch.IRemoteProxyManager)1