]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.support/src/org/argeo/slc/lib/vbox/VBoxManager.java
Adapt after JCR APIs to node.api bundle move
[gpl/argeo-slc.git] / org.argeo.slc.support / src / org / argeo / slc / lib / vbox / VBoxManager.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 package org.argeo.slc.lib.vbox;
17
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.slc.SlcException;
25 import org.argeo.slc.core.execution.tasks.SystemCall;
26 import org.springframework.core.io.Resource;
27
28 public class VBoxManager {
29 private final static Log log = LogFactory.getLog(VBoxManager.class);
30
31 private VBoxMachine vm;
32 private String executable = "VBoxManage";
33
34 private List<VBoxNat> nats = new ArrayList<VBoxNat>();
35
36 public void importOvf(Resource ovfDefinition) {
37 try {
38 List<Object> cmd = new ArrayList<Object>();
39 cmd.add(executable);
40 cmd.add("import");
41 cmd.add(ovfDefinition.getFile().getCanonicalPath());
42 cmd.add("--vsys 0 --vmname <name>");
43 cmd.add("0");
44 cmd.add("--vmname");
45 cmd.add(vm.getName());
46 new SystemCall(cmd).run();
47 } catch (IOException e) {
48 throw new SlcException("Cannot import OVF appliance "
49 + ovfDefinition, e);
50 }
51 }
52
53 public void startVm() {
54 startVm("gui");
55 }
56
57 public void startVmHeadless() {
58 startVm("vrdp");
59 }
60
61 public void startVm(String type) {
62 List<Object> cmd = new ArrayList<Object>();
63 cmd.add(executable);
64 cmd.add("startvm");
65 cmd.add(vm.getName());
66 cmd.add("--type");
67 cmd.add(type);
68 new SystemCall(cmd).run();
69 }
70
71 public void applyNats() {
72 StringBuffer script = new StringBuffer("");
73 for (VBoxNat vBoxNat : nats) {
74 for (String id : vBoxNat.getMappings().keySet()) {
75 VBoxPortMapping mapping = vBoxNat.getMappings().get(id);
76
77 // Try to delete rule first
78 try {
79 StringBuffer delCmd = new StringBuffer(
80 "VBoxManage modifyvm");
81 delCmd.append(" \"").append(vm.getName()).append("\"");
82 delCmd.append(" --natpf").append(vBoxNat.getDevice())
83 .append(" ");
84 delCmd.append(" delete ");
85 delCmd.append("\"").append(id).append("\"");
86 new SystemCall(delCmd.toString()).run();
87 script.append(delCmd).append("\n");
88 } catch (Exception e) {
89 // silent
90 }
91
92 StringBuffer cmd = new StringBuffer("VBoxManage modifyvm");
93 cmd.append(" \"").append(vm.getName()).append("\"");
94 cmd.append(" --natpf").append(vBoxNat.getDevice()).append(" ");
95 cmd.append("\"");
96 cmd.append(id).append(",");
97 cmd.append(mapping.getProtocol()).append(",");
98 cmd.append(",");
99 cmd.append(mapping.getHostPort()).append(",");
100 cmd.append(vBoxNat.getGuestIp()).append(",");
101 cmd.append(mapping.getGuestPort());
102 cmd.append("\"");
103
104 new SystemCall(cmd.toString()).run();
105 script.append(cmd).append("\n");
106
107 // Older VirtualBox
108 // new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
109 // "Protocol", mapping.getProtocol(), script)).run();
110 // script.append('\n');
111 // new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
112 // "GuestPort", mapping.getGuest(), script)).run();
113 // script.append('\n');
114 // new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
115 // "HostPort", mapping.getHost(), script)).run();
116 // script.append('\n');
117 // script.append('\n');
118 }
119 script.append('\n');
120 }
121
122 if (log.isDebugEnabled())
123 log.debug("Port setting script:\n" + script);
124 }
125
126 protected List<Object> createNatCommand(String id, String device,
127 String cfgKey, String value, StringBuffer script) {
128 List<Object> cmd = new ArrayList<Object>();
129 cmd.add(executable);
130 cmd.add("setextradata");
131 cmd.add(vm.getName());
132 cmd.add("VBoxInternal/Devices/" + device + "/0/LUN#0/Config/" + id
133 + "/" + cfgKey);
134 cmd.add(value);
135
136 for (Object arg : cmd) {
137 script.append(arg).append(' ');
138 }
139
140 return cmd;
141 }
142
143 public String getExecutable() {
144 return executable;
145 }
146
147 public void setExecutable(String executable) {
148 this.executable = executable;
149 }
150
151 public List<VBoxNat> getNats() {
152 return nats;
153 }
154
155 public void setNats(List<VBoxNat> boxNats) {
156 nats = boxNats;
157 }
158
159 public void setVm(VBoxMachine vm) {
160 this.vm = vm;
161 }
162
163 }