]>
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
4713c9d764f33b45977cf061aeb1cb201f6c12e9
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.lib
.vbox
;
19 import java
.io
.IOException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
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
;
29 public class VBoxManager
{
30 private final static Log log
= LogFactory
.getLog(VBoxManager
.class);
32 private VBoxMachine vm
;
33 private String executable
= "VBoxManage";
35 private List
<VBoxNat
> nats
= new ArrayList
<VBoxNat
>();
37 public void importOvf(Resource ovfDefinition
) {
39 List
<Object
> cmd
= new ArrayList
<Object
>();
42 cmd
.add(ovfDefinition
.getFile().getCanonicalPath());
43 cmd
.add("--vsys 0 --vmname <name>");
46 cmd
.add(vm
.getName());
47 new SystemCall(cmd
).run();
48 } catch (IOException e
) {
49 throw new SlcException("Cannot import OVF appliance "
54 public void startVm() {
58 public void startVmHeadless() {
62 public void startVm(String type
) {
63 List
<Object
> cmd
= new ArrayList
<Object
>();
66 cmd
.add(vm
.getName());
69 new SystemCall(cmd
).run();
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
);
78 // Try to delete rule first
80 StringBuffer delCmd
= new StringBuffer(
81 "VBoxManage modifyvm");
82 delCmd
.append(" \"").append(vm
.getName()).append("\"");
83 delCmd
.append(" --natpf").append(vBoxNat
.getDevice())
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
) {
93 StringBuffer cmd
= new StringBuffer("VBoxManage modifyvm");
94 cmd
.append(" \"").append(vm
.getName()).append("\"");
95 cmd
.append(" --natpf").append(vBoxNat
.getDevice()).append(" ");
97 cmd
.append(id
).append(",");
98 cmd
.append(mapping
.getProtocol()).append(",");
100 cmd
.append(mapping
.getHostPort()).append(",");
101 cmd
.append(vBoxNat
.getGuestIp()).append(",");
102 cmd
.append(mapping
.getGuestPort());
105 new SystemCall(cmd
.toString()).run();
106 script
.append(cmd
).append("\n");
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');
123 if (log
.isDebugEnabled())
124 log
.debug("Port setting script:\n" + script
);
127 protected List
<Object
> createNatCommand(String id
, String device
,
128 String cfgKey
, String value
, StringBuffer script
) {
129 List
<Object
> cmd
= new ArrayList
<Object
>();
131 cmd
.add("setextradata");
132 cmd
.add(vm
.getName());
133 cmd
.add("VBoxInternal/Devices/" + device
+ "/0/LUN#0/Config/" + id
137 for (Object arg
: cmd
) {
138 script
.append(arg
).append(' ');
144 public String
getExecutable() {
148 public void setExecutable(String executable
) {
149 this.executable
= executable
;
152 public List
<VBoxNat
> getNats() {
156 public void setNats(List
<VBoxNat
> boxNats
) {
160 public void setVm(VBoxMachine vm
) {