+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.argeo.slc.lib.vbox;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.SlcException;
import org.argeo.slc.core.execution.tasks.SystemCall;
+import org.springframework.core.io.Resource;
public class VBoxManager {
private final static Log log = LogFactory.getLog(VBoxManager.class);
- private String machineName;
+ private VBoxMachine vm;
private String executable = "VBoxManage";
private List<VBoxNat> nats = new ArrayList<VBoxNat>();
+ public void importOvf(Resource ovfDefinition) {
+ try {
+ List<Object> cmd = new ArrayList<Object>();
+ cmd.add(executable);
+ cmd.add("import");
+ cmd.add(ovfDefinition.getFile().getCanonicalPath());
+ cmd.add("--vsys 0 --vmname <name>");
+ cmd.add("0");
+ cmd.add("--vmname");
+ cmd.add(vm.getName());
+ new SystemCall(cmd).run();
+ } catch (IOException e) {
+ throw new SlcException("Cannot import OVF appliance "
+ + ovfDefinition, e);
+ }
+ }
+
+ public void startVm() {
+ startVm("gui");
+ }
+
+ public void startVmHeadless() {
+ startVm("vrdp");
+ }
+
+ public void startVm(String type) {
+ List<Object> cmd = new ArrayList<Object>();
+ cmd.add(executable);
+ cmd.add("startvm");
+ cmd.add(vm.getName());
+ cmd.add("--type");
+ cmd.add(type);
+ new SystemCall(cmd).run();
+ }
+
public void applyNats() {
StringBuffer script = new StringBuffer("");
for (VBoxNat vBoxNat : nats) {
for (String id : vBoxNat.getMappings().keySet()) {
VBoxPortMapping mapping = vBoxNat.getMappings().get(id);
- new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
- "Protocol", mapping.getProtocol(), script)).run();
- script.append('\n');
- new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
- "GuestPort", mapping.getGuest(), script)).run();
- script.append('\n');
- new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
- "HostPort", mapping.getHost(), script)).run();
- script.append('\n');
- script.append('\n');
+
+ // Try to delete rule first
+ try {
+ StringBuffer delCmd = new StringBuffer(
+ "VBoxManage modifyvm");
+ delCmd.append(" \"").append(vm.getName()).append("\"");
+ delCmd.append(" --natpf").append(vBoxNat.getDevice())
+ .append(" ");
+ delCmd.append(" delete ");
+ delCmd.append("\"").append(id).append("\"");
+ new SystemCall(delCmd.toString()).run();
+ script.append(delCmd).append("\n");
+ } catch (Exception e) {
+ // silent
+ }
+
+ StringBuffer cmd = new StringBuffer("VBoxManage modifyvm");
+ cmd.append(" \"").append(vm.getName()).append("\"");
+ cmd.append(" --natpf").append(vBoxNat.getDevice()).append(" ");
+ cmd.append("\"");
+ cmd.append(id).append(",");
+ cmd.append(mapping.getProtocol()).append(",");
+ cmd.append(",");
+ cmd.append(mapping.getHostPort()).append(",");
+ cmd.append(vBoxNat.getGuestIp()).append(",");
+ cmd.append(mapping.getGuestPort());
+ cmd.append("\"");
+
+ new SystemCall(cmd.toString()).run();
+ script.append(cmd).append("\n");
+
+ // Older VirtualBox
+ // new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
+ // "Protocol", mapping.getProtocol(), script)).run();
+ // script.append('\n');
+ // new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
+ // "GuestPort", mapping.getGuest(), script)).run();
+ // script.append('\n');
+ // new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
+ // "HostPort", mapping.getHost(), script)).run();
+ // script.append('\n');
+ // script.append('\n');
}
script.append('\n');
}
List<Object> cmd = new ArrayList<Object>();
cmd.add(executable);
cmd.add("setextradata");
- cmd.add(machineName);
+ cmd.add(vm.getName());
cmd.add("VBoxInternal/Devices/" + device + "/0/LUN#0/Config/" + id
+ "/" + cfgKey);
cmd.add(value);
return cmd;
}
- public String getMachineName() {
- return machineName;
- }
-
- public void setMachineName(String machineName) {
- this.machineName = machineName;
- }
-
public String getExecutable() {
return executable;
}
nats = boxNats;
}
+ public void setVm(VBoxMachine vm) {
+ this.vm = vm;
+ }
+
}