]> 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
Add license headers
[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 startVm(String type) {
59 List<Object> cmd = new ArrayList<Object>();
60 cmd.add(executable);
61 cmd.add("startvm");
62 cmd.add(vm.getName());
63 cmd.add("--type");
64 cmd.add(type);
65 new SystemCall(cmd).run();
66 }
67
68 public void applyNats() {
69 StringBuffer script = new StringBuffer("");
70 for (VBoxNat vBoxNat : nats) {
71 for (String id : vBoxNat.getMappings().keySet()) {
72 VBoxPortMapping mapping = vBoxNat.getMappings().get(id);
73 new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
74 "Protocol", mapping.getProtocol(), script)).run();
75 script.append('\n');
76 new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
77 "GuestPort", mapping.getGuest(), script)).run();
78 script.append('\n');
79 new SystemCall(createNatCommand(id, vBoxNat.getDevice(),
80 "HostPort", mapping.getHost(), script)).run();
81 script.append('\n');
82 script.append('\n');
83 }
84 script.append('\n');
85 }
86
87 if (log.isDebugEnabled())
88 log.debug("Port setting script:\n" + script);
89 }
90
91 protected List<Object> createNatCommand(String id, String device,
92 String cfgKey, String value, StringBuffer script) {
93 List<Object> cmd = new ArrayList<Object>();
94 cmd.add(executable);
95 cmd.add("setextradata");
96 cmd.add(vm.getName());
97 cmd.add("VBoxInternal/Devices/" + device + "/0/LUN#0/Config/" + id
98 + "/" + cfgKey);
99 cmd.add(value);
100
101 for (Object arg : cmd) {
102 script.append(arg).append(' ');
103 }
104
105 return cmd;
106 }
107
108 public String getExecutable() {
109 return executable;
110 }
111
112 public void setExecutable(String executable) {
113 this.executable = executable;
114 }
115
116 public List<VBoxNat> getNats() {
117 return nats;
118 }
119
120 public void setNats(List<VBoxNat> boxNats) {
121 nats = boxNats;
122 }
123
124 public void setVm(VBoxMachine vm) {
125 this.vm = vm;
126 }
127
128 }