]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/vbox/VBoxManager.java
Improve SSH UI user info
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / lib / vbox / VBoxManager.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.lib.vbox;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.slc.SlcException;
26 import org.argeo.slc.core.execution.tasks.SystemCall;
27 import org.springframework.core.io.Resource;
28
29 public class VBoxManager {
30 private final static Log log = LogFactory.getLog(VBoxManager.class);
31
32 private VBoxMachine vm;
33 private String executable = "VBoxManage";
34
35 private List<VBoxNat> nats = new ArrayList<VBoxNat>();
36
37 public void importOvf(Resource ovfDefinition) {
38 try {
39 List<Object> cmd = new ArrayList<Object>();
40 cmd.add(executable);
41 cmd.add("import");
42 cmd.add(ovfDefinition.getFile().getCanonicalPath());
43 cmd.add("--vsys 0 --vmname <name>");
44 cmd.add("0");
45 cmd.add("--vmname");
46 cmd.add(vm.getName());
47 new SystemCall(cmd).run();
48 } catch (IOException e) {
49 throw new SlcException("Cannot import OVF appliance "
50 + ovfDefinition, e);
51 }
52 }
53
54 public void startVm() {
55 startVm("gui");
56 }
57
58 public void startVmHeadless() {
59 startVm("vrdp");
60 }
61
62 public void startVm(String type) {
63 List<Object> cmd = new ArrayList<Object>();
64 cmd.add(executable);
65 cmd.add("startvm");
66 cmd.add(vm.getName());
67 cmd.add("--type");
68 cmd.add(type);
69 new SystemCall(cmd).run();
70 }
71
72 public void applyNats() {
73 StringBuffer script = new StringBuffer("");
74 for (VBoxNat vBoxNat : nats) {
75 for (String id : vBoxNat.getMappings().keySet()) {
76 VBoxPortMapping mapping = vBoxNat.getMappings().get(id);
77
78 // Try to delete rule first
79 try {
80 StringBuffer delCmd = new StringBuffer(
81 "VBoxManage modifyvm");
82 delCmd.append(" \"").append(vm.getName()).append("\"");
83 delCmd.append(" --natpf").append(vBoxNat.getDevice())
84 .append(" ");
85 delCmd.append(" delete ");
86 delCmd.append("\"").append(id).append("\"");
87 new SystemCall(delCmd.toString()).run();
88 script.append(delCmd).append("\n");
89 } catch (Exception e) {
90 // silent
91 }
92
93 StringBuffer cmd = new StringBuffer("VBoxManage modifyvm");
94 cmd.append(" \"").append(vm.getName()).append("\"");
95 cmd.append(" --natpf").append(vBoxNat.getDevice()).append(" ");
96 cmd.append("\"");
97 cmd.append(id).append(",");
98 cmd.append(mapping.getProtocol()).append(",");
99 cmd.append(",");
100 cmd.append(mapping.getHostPort()).append(",");
101 cmd.append(vBoxNat.getGuestIp()).append(",");
102 cmd.append(mapping.getGuestPort());
103 cmd.append("\"");
104
105 new SystemCall(cmd.toString()).run();
106 script.append(cmd).append("\n");
107
108 // Older VirtualBox
109 // new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
110 // "Protocol", mapping.getProtocol(), script)).run();
111 // script.append('\n');
112 // new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
113 // "GuestPort", mapping.getGuest(), script)).run();
114 // script.append('\n');
115 // new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
116 // "HostPort", mapping.getHost(), script)).run();
117 // script.append('\n');
118 // script.append('\n');
119 }
120 script.append('\n');
121 }
122
123 if (log.isDebugEnabled())
124 log.debug("Port setting script:\n" + script);
125 }
126
127 protected List<Object> createNatCommand(String id, String device,
128 String cfgKey, String value, StringBuffer script) {
129 List<Object> cmd = new ArrayList<Object>();
130 cmd.add(executable);
131 cmd.add("setextradata");
132 cmd.add(vm.getName());
133 cmd.add("VBoxInternal/Devices/" + device + "/0/LUN#0/Config/" + id
134 + "/" + cfgKey);
135 cmd.add(value);
136
137 for (Object arg : cmd) {
138 script.append(arg).append(' ');
139 }
140
141 return cmd;
142 }
143
144 public String getExecutable() {
145 return executable;
146 }
147
148 public void setExecutable(String executable) {
149 this.executable = executable;
150 }
151
152 public List<VBoxNat> getNats() {
153 return nats;
154 }
155
156 public void setNats(List<VBoxNat> boxNats) {
157 nats = boxNats;
158 }
159
160 public void setVm(VBoxMachine vm) {
161 this.vm = vm;
162 }
163
164 }