]>
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
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 startVm(String type
) {
59 List
<Object
> cmd
= new ArrayList
<Object
>();
62 cmd
.add(vm
.getName());
65 new SystemCall(cmd
).run();
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();
76 new SystemCall(createNatCommand(id
, vBoxNat
.getDevice(),
77 "GuestPort", mapping
.getGuest(), script
)).run();
79 new SystemCall(createNatCommand(id
, vBoxNat
.getDevice(),
80 "HostPort", mapping
.getHost(), script
)).run();
87 if (log
.isDebugEnabled())
88 log
.debug("Port setting script:\n" + script
);
91 protected List
<Object
> createNatCommand(String id
, String device
,
92 String cfgKey
, String value
, StringBuffer script
) {
93 List
<Object
> cmd
= new ArrayList
<Object
>();
95 cmd
.add("setextradata");
96 cmd
.add(vm
.getName());
97 cmd
.add("VBoxInternal/Devices/" + device
+ "/0/LUN#0/Config/" + id
101 for (Object arg
: cmd
) {
102 script
.append(arg
).append(' ');
108 public String
getExecutable() {
112 public void setExecutable(String executable
) {
113 this.executable
= executable
;
116 public List
<VBoxNat
> getNats() {
120 public void setNats(List
<VBoxNat
> boxNats
) {
124 public void setVm(VBoxMachine vm
) {