summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/SystemtapModel.java
blob: 60d7d76be955887d5af387772e55d438af3a65c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************************
 * Copyright (c) 2010 Intel Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.sdk.remotetools.actions;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.rse.subsystems.terminals.core.ITerminalServiceSubSystem;
import org.eclipse.rse.ui.SystemBasePlugin;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.yocto.remote.utils.ShellSession;
import org.yocto.remote.utils.RemoteHelper;
import org.yocto.remote.utils.CommonHelper;

public class SystemtapModel extends BaseModel {
	protected static final String DEFAULT_INIT_SCRIPT = "oe-init-build-env";
	protected static final String SYSTEMTAP_CONSOLE = "Systemtap Console";

	private static final String TASK_NAME = "systemtap command";

	protected MessageConsole sessionConsole;
	private String metadata_location;
	private String builddir_location;
	private String remote_host;
	private String user_id;
	private String systemtap_script;
	private String systemtap_args;

	Display display;
	
	public SystemtapModel(String metadata_location, String builddir_location, String remote_host, String user_id, String systemtap_script, String systemtap_args, Display display) {
		super(null, TASK_NAME, "", "");
		this.metadata_location = metadata_location;
		this.builddir_location = builddir_location;
		this.remote_host = remote_host;
		this.user_id = user_id;
		this.systemtap_script = systemtap_script;
		this.systemtap_args = systemtap_args;
		this.display = display;
	}
	
	@Override
	public void preProcess(IProgressMonitor monitor) 
			throws InvocationTargetException, InterruptedException {
		final ITerminalServiceSubSystem terminalSubSystem = RemoteHelper.getTerminalSubSystem(host);
		if (!terminalSubSystem.isConnected()) {
			try {
				ProgressMonitorDialog dialog = new ProgressMonitorDialog(null);
				dialog.run(true, true, new IRunnableWithProgress(){
					@Override
					public void run(IProgressMonitor monitor) {
						monitor.beginTask("Connecting to remote target ...", 100);
						try {
							terminalSubSystem.connect(new NullProgressMonitor(), false);
							monitor.done();
						} catch (Exception e) {
							CommonHelper.showErrorDialog("Connection failure", null, e.getMessage());
							monitor.done();
						}
					}
				});
			} catch (OperationCanceledException e) {
				// user canceled, return silently
			} catch (Exception e) {
				SystemBasePlugin.logError(e.getLocalizedMessage(), e);
			}
		}
		}
	
		protected String changeTerm = "export TERM=vt100;";
    
	@Override
	public void process(IProgressMonitor monitor) 
			throws InvocationTargetException, InterruptedException {
		try {
			ShellSession shell = new ShellSession(ShellSession.SHELL_TYPE_BASH, 
												new File(this.metadata_location),new File(this.builddir_location),
												DEFAULT_INIT_SCRIPT, sessionConsole.newOutputStream());
			boolean acceptedKey = shell.ensureKnownHostKey(user_id, remote_host);
			if (acceptedKey) {
				String crosstapCmd = "crosstap " + user_id + "@" + remote_host + " " + systemtap_script;
				if (systemtap_args != null)
					crosstapCmd = crosstapCmd + " " + systemtap_args;
				shell.execute(crosstapCmd);
			}
		} catch (Exception e) {
			throw new InvocationTargetException(e,e.getMessage());
		}
	}
}