]>
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
34e8993f84b142af412e2f4c9074c204d178a7a7
2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.lib
.vbox
;
18 import java
.io
.IOException
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
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
;
28 public class VBoxManager
{
29 private final static Log log
= LogFactory
.getLog(VBoxManager
.class);
31 private VBoxMachine vm
;
32 private String executable
= "VBoxManage";
34 private List
<VBoxNat
> nats
= new ArrayList
<VBoxNat
>();
36 public void importOvf(Resource ovfDefinition
) {
38 List
<Object
> cmd
= new ArrayList
<Object
>();
41 cmd
.add(ovfDefinition
.getFile().getCanonicalPath());
42 cmd
.add("--vsys 0 --vmname <name>");
45 cmd
.add(vm
.getName());
46 new SystemCall(cmd
).run();
47 } catch (IOException e
) {
48 throw new SlcException("Cannot import OVF appliance "
53 public void startVm() {
57 public void startVmHeadless() {
61 public void startVm(String type
) {
62 List
<Object
> cmd
= new ArrayList
<Object
>();
65 cmd
.add(vm
.getName());
68 new SystemCall(cmd
).run();
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
);
77 // Try to delete rule first
79 StringBuffer delCmd
= new StringBuffer(
80 "VBoxManage modifyvm");
81 delCmd
.append(" \"").append(vm
.getName()).append("\"");
82 delCmd
.append(" --natpf").append(vBoxNat
.getDevice())
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
) {
92 StringBuffer cmd
= new StringBuffer("VBoxManage modifyvm");
93 cmd
.append(" \"").append(vm
.getName()).append("\"");
94 cmd
.append(" --natpf").append(vBoxNat
.getDevice()).append(" ");
96 cmd
.append(id
).append(",");
97 cmd
.append(mapping
.getProtocol()).append(",");
99 cmd
.append(mapping
.getHostPort()).append(",");
100 cmd
.append(vBoxNat
.getGuestIp()).append(",");
101 cmd
.append(mapping
.getGuestPort());
104 new SystemCall(cmd
.toString()).run();
105 script
.append(cmd
).append("\n");
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');
122 if (log
.isDebugEnabled())
123 log
.debug("Port setting script:\n" + script
);
126 protected List
<Object
> createNatCommand(String id
, String device
,
127 String cfgKey
, String value
, StringBuffer script
) {
128 List
<Object
> cmd
= new ArrayList
<Object
>();
130 cmd
.add("setextradata");
131 cmd
.add(vm
.getName());
132 cmd
.add("VBoxInternal/Devices/" + device
+ "/0/LUN#0/Config/" + id
136 for (Object arg
: cmd
) {
137 script
.append(arg
).append(' ');
143 public String
getExecutable() {
147 public void setExecutable(String executable
) {
148 this.executable
= executable
;
151 public List
<VBoxNat
> getNats() {
155 public void setNats(List
<VBoxNat
> boxNats
) {
159 public void setVm(VBoxMachine vm
) {