[Esbox-commits] r2184 - trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox
ilpyykko at garage.maemo.org
ilpyykko at garage.maemo.org
Tue Sep 15 17:49:43 EEST 2009
Author: ilpyykko
Date: 2009-09-15 17:49:30 +0300 (Tue, 15 Sep 2009)
New Revision: 2184
Modified:
trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxMachineController.java
trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java
trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java
Log:
VirtualBox port forwarding (#4490)
Modified: trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxMachineController.java
===================================================================
--- trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxMachineController.java 2009-09-15 09:09:45 UTC (rev 2183)
+++ trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxMachineController.java 2009-09-15 14:49:30 UTC (rev 2184)
@@ -11,6 +11,9 @@
package org.maemo.esbox.internal.vm.virtualbox;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -18,13 +21,23 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
import org.maemo.esbox.internal.api.vm.core.BaseLaunchableVirtualMachineController;
import org.maemo.esbox.vm.core.IVirtualMachineConfiguration;
import org.maemo.esbox.vm.virtualbox.IVirtualBoxConfiguration;
import org.maemo.mica.common.core.MicaException;
+import org.maemo.mica.common.core.machine.IMachine;
import org.maemo.mica.common.core.machine.MachineException;
+import org.maemo.mica.common.core.machine.MachineManager;
+import org.maemo.mica.common.core.machine.MachineRegistry;
import org.maemo.mica.common.core.process.ProcessLauncherParameters;
import org.maemo.mica.common.core.process.ProcessLauncherUtils.LaunchResults;
+import org.maemo.mica.common.ui.dialogs.DialogUtils;
+import org.maemo.mica.internal.api.common.core.ILookHereProvider;
import com.nokia.cpp.internal.api.utils.core.TextUtils;
@@ -49,6 +62,90 @@
this.machineName = ((IVirtualBoxConfiguration) configuration).getMachineName();
}
+ /*
+ * (non-Javadoc)
+ * @see org.maemo.esbox.internal.api.vm.core.BaseVirtualMachineController#createLookHereProviders()
+ */
+ @Override
+ public ILookHereProvider[] createLookHereProviders() {
+ if (VirtualBoxUtils.hasPortForwarding((IVirtualBoxConfiguration) machineConfiguration,
+ machineConfiguration.getHostPort(), machineConfiguration.getTargetPort())) {
+ return super.createLookHereProviders();
+ }
+
+ ArrayList<ILookHereProvider> providers = new ArrayList<ILookHereProvider>();
+
+
+ providers.add(new ILookHereProvider() {
+ public String getId() {
+ return "vbnat";
+ }
+
+ public String getLabel() {
+ return "Enable port forwarding";
+ }
+
+ public void handleSelected(final Shell shell) {
+ final int hostPort = machineConfiguration.getHostPort();
+ final int targetPort = machineConfiguration.getTargetPort();
+ final IVirtualBoxConfiguration configuration = (IVirtualBoxConfiguration) machineConfiguration;
+
+ if (targetPort > 0 && hostPort > 0 &&
+ !VirtualBoxUtils.hasPortForwarding(configuration, hostPort, targetPort)) {
+ final String question = "The VirtualBox machine (" + configuration.getMachineName() +
+ ") is using NAT.\n\nDo you want to set SSH port forwarding from " +
+ + targetPort + " to " + hostPort + "?";
+
+ Display.getDefault().asyncExec(new Runnable() {
+ public void run() {
+ String title = "Missing port forwarding";
+ if (MessageDialog.openQuestion(null, title, question)) {
+ VirtualBoxUtils.setPortForwarding(configuration, hostPort, targetPort);
+
+
+ if (isMachineRunning() && DialogUtils.showQuestionDialog(null, "Restart VirtualBox",
+ "VirtualBox needs to be restarted to apply changes.",
+ "Restart", "Leave running")) {
+ ProgressMonitorDialog dialog = new ProgressMonitorDialog(null);
+ try {
+ dialog.run(true, true, new IRunnableWithProgress() {
+
+ public void run(IProgressMonitor monitor)
+ throws InvocationTargetException,
+ InterruptedException {
+ IMachine[] currentMachines = MachineRegistry.getInstance().getCurrentBuildMachines();
+
+ monitor.beginTask("", currentMachines.length);
+ for (IMachine machine : currentMachines) {
+ monitor.worked(1);
+ if (machine == null)
+ continue;
+ MachineManager.getInstance().releaseMachine(machine, new SubProgressMonitor(monitor, 1));
+ if (monitor.isCanceled())
+ break;
+ }
+ monitor.done();
+ }
+
+ });
+ MachineManager.getInstance().scheduleAcquireMachine(getMachine());
+ } catch (Exception e) {
+ Activator.getErrorLogger().logError("Unexpected error shutting down machines", e);
+ }
+ }
+
+ }
+ }
+ });
+
+ }
+
+ }
+
+ });
+ providers.addAll(Arrays.asList(super.createLookHereProviders()));
+ return providers.toArray(new ILookHereProvider[]{});
+ }
/**
* Create command line invoking VBoxManage
* @param command
Modified: trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java
===================================================================
--- trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java 2009-09-15 09:09:45 UTC (rev 2183)
+++ trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java 2009-09-15 14:49:30 UTC (rev 2184)
@@ -19,7 +19,10 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.DialogSettings;
+import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
import org.maemo.esbox.internal.api.vm.ui.preferences.AddressProvider;
import org.maemo.esbox.internal.api.vm.ui.preferences.BaseVirtualMachinePreferencePage;
import org.maemo.esbox.internal.api.vm.ui.preferences.HostInterfaceAddressProvider;
@@ -152,9 +155,27 @@
reporter.log(Activator.createErrorStatus("Did not find all the expected NAT info; guessing from defaults", null));
}
+ if (hostPort.equals(targetPort)) {
+ reporter.log(Activator.createErrorStatus("Host and target ports are equal. Reset to default.", null));
+ targetPort = "2222";
+ hostPort = "22";
+ }
+
reporter.logInfo("Using NAT configuration");
+
reporter.logInfo("Setting Target Address to " + target);
+
+ try {
+ if (checkPortForwarding(getShell(), configuration, Integer.parseInt(hostPort), Integer.parseInt(targetPort))) {
+ reporter.logInfo("Setting SSH port mapping from " + target + ":" + targetPort + " to " + host +
+ ":" + hostPort);
+ }
+ } catch (NumberFormatException e) {
+ reporter.log(Activator.createErrorStatus("Failed to parse port numbers", e));
+ }
+
detected = true;
+
break;
}
@@ -236,10 +257,42 @@
}
}
-
+ /**
+ * Check port forwarding and show dialog if not
+ *
+ * @param shell the parent shell
+ * @param configuration the virtual box configuration
+ * @param hostPort the host port
+ * @param targetPort the target port
+ * @return true if port forwarding was applied
+ */
+ private boolean checkPortForwarding(final Shell shell, IVirtualBoxConfiguration configuration,
+ int hostPort, int targetPort) {
+ final boolean[] result = { false };
+ if (targetPort > 0 && hostPort > 0 &&
+ !VirtualBoxUtils.hasPortForwarding(configuration, hostPort, targetPort)) {
+ final String question = "The VirtualBox machine (" + configuration.getMachineName() +
+ ") is using NAT.\n\nDo you want to set SSH port forwarding from " +
+ + targetPort + " to " + hostPort + "?";
+
+ Display.getDefault().syncExec(new Runnable() {
+ public void run() {
+ String title = "Port forwarding";
+ result[0] = MessageDialog.openQuestion(shell, title, question);
+ }
+ });
+ if (result[0]) {
+ VirtualBoxUtils.setPortForwarding(configuration,hostPort, targetPort);
+ }
+ }
+ return result[0];
+
+ }
+
};
}
+
/* (non-Javadoc)
* @see org.maemo.esbox.internal.api.vm.ui.preferences.BaseVirtualMachinePreferencePage#createVirtualMachinePreferencePage(org.eclipse.jface.preference.IPreferenceStore)
*/
@@ -248,5 +301,4 @@
IPreferenceStore preferenceStore) {
return new VirtualBoxSettingsPreferencePage(preferenceStore);
}
-
}
Modified: trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java
===================================================================
--- trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java 2009-09-15 09:09:45 UTC (rev 2183)
+++ trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java 2009-09-15 14:49:30 UTC (rev 2184)
@@ -40,6 +40,9 @@
*
*/
public class VirtualBoxUtils {
+ private static final String GUESTSSH_HOST_PORT = "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort";
+ private static final String GUESTSSH_GUEST_PORT = "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort";
+ private static final String GUESTSSH_PROTOCOL = "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol";
static final Pattern namePattern = Pattern.compile("Name:\\s*(.*)");
static final Pattern nameUUID22Pattern = Pattern.compile("\"([^\"]+)\"\\s*\\{(.*)\\}");
static final Pattern uuidPattern = Pattern.compile("UUID:\\s*([0-9a-fA-F-]{36})");
@@ -301,4 +304,50 @@
return Collections.emptyList();
return nameToUUIDMap.keySet();
}
+
+ /**
+ * Set VirtualBox extra data.
+ *
+ * @param configuration the VirtualBox configuration
+ * @param key the key
+ * @param value the value
+ */
+ public static void setVirtualMachineExtraData(
+ IVirtualBoxConfiguration configuration, String key,
+ String value) {
+ VirtualBoxUtils.launchAndRunVBoxManage(configuration, 10,
+ "setextradata", configuration.getMachineName(), key, value);
+ }
+
+ /**
+ * Check if configuration has port forwarding with given ports.
+ *
+ * @param configuration the virtual box configuration
+ * @param hostPort the host port
+ * @param targetPort the target port
+ * @return true if configuration has port forwarding to given ports.
+ */
+ public static boolean hasPortForwarding(IVirtualBoxConfiguration configuration, int hostPort, int targetPort) {
+ Map<String, String> extraData = getVirtualMachineExtraData(configuration);
+ return "TCP".equals( extraData.get(GUESTSSH_PROTOCOL) ) &&
+ String.valueOf(hostPort).equals(extraData.get(GUESTSSH_GUEST_PORT)) &&
+ String.valueOf(targetPort).equals(extraData.get(GUESTSSH_HOST_PORT));
+ }
+
+ /**
+ * Setup port forwarding with given values.
+ *
+ * @param configuration the virtual box configuration
+ * @param hostPort the host port
+ * @param targetPort the target port
+ */
+ public static void setPortForwarding(IVirtualBoxConfiguration configuration, int hostPort, int targetPort) {
+ VirtualBoxUtils.setVirtualMachineExtraData(configuration,
+ GUESTSSH_PROTOCOL, "TCP");
+ VirtualBoxUtils.setVirtualMachineExtraData(configuration,
+ GUESTSSH_GUEST_PORT, String.valueOf(hostPort));
+ VirtualBoxUtils.setVirtualMachineExtraData(configuration,
+ GUESTSSH_HOST_PORT, String.valueOf(targetPort));
+ }
+
}
More information about the Esbox-commits
mailing list