From d4ebe9f610e9a96d3e1d756b39d68d09574f83d8 Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Mon, 12 Dec 2011 17:24:04 +0000 Subject: [PATCH 1/7] issue 384: added Statement to virtualbox --- .../jclouds/virtualbox/domain/Statements.java | 40 +++++++++++++++++++ .../main/resources/functions/getIpAddress.cmd | 0 .../main/resources/functions/getIpAddress.sh | 16 ++++++++ .../test/resources/test_get_ip_address.cmd | 0 .../src/test/resources/test_get_ip_address.sh | 29 ++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java create mode 100644 sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.cmd create mode 100644 sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh create mode 100644 sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.cmd create mode 100644 sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java new file mode 100644 index 0000000000..d2e2f76400 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java @@ -0,0 +1,40 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.virtualbox.domain; + +import org.jclouds.scriptbuilder.domain.Call; +import org.jclouds.scriptbuilder.domain.Statement; + +/** + * VboxManage statements used in shell scripts . + * + * @author Andrea Turli + */ +public class Statements { + + /** + * Extract the IP address from the specified vm and stores the IP address into the variable {@code FOUND_IP_ADDRESS} if successful. + * + * @param vmname + * - the vm name + */ + public static Statement getIpAddress(String vmName) { + return new Call("getIpAddress", vmName); + } + } \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.cmd b/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.cmd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh b/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh new file mode 100644 index 0000000000..418b869ac1 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh @@ -0,0 +1,16 @@ +function getIpAddress { + unset FOUND_IP_ADDRESS; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local VMNAME="$0"; shift + local _FOUND=`vboxmanage guestproperty enumerate "$VMNAME" --patterns "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $4 }' | cut -c 1-14` + [ -n "$_FOUND" ] && { + export FOUND_IP_ADDRESS=$_FOUND + echo [$FOUND_IP_ADDRESS] + return 0 + } || { + return 1 + } +} diff --git a/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.cmd b/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.cmd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh b/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh new file mode 100644 index 0000000000..63b9e272de --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh @@ -0,0 +1,29 @@ +#!/bin/bash +set +u +shopt -s xpg_echo +shopt -s expand_aliases +unset PATH JAVA_HOME LD_LIBRARY_PATH +function abort { + echo "aborting: $@" 1>&2 + exit 1 +} +function getIpAddress { + unset FOUND_IP_ADDRESS; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local VMNAME="$0"; shift + local _FOUND=`vboxmanage guestproperty enumerate "$VMNAME" --patterns "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $4 }' | cut -c 1-14` + [ -n "$_FOUND" ] && { + export FOUND_IP_ADDRESS=$_FOUND + echo [$FOUND_IP_ADDRESS] + return 0 + } || { + return 1 + } +} +export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin +getIpAddress $@ || exit 1 +echo $FOUND_IP_ADDRESS +exit 0 From 19851cd21a64432a2dd497e7717e02d7f9bf1f98 Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Mon, 12 Dec 2011 17:27:20 +0000 Subject: [PATCH 2/7] issue 384: added Statement to virtualbox --- .../GetIPAddressFromGuestAdditionsTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java new file mode 100644 index 0000000000..05b8c9184c --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java @@ -0,0 +1,58 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.virtualbox.domain; + +import static org.jclouds.scriptbuilder.domain.Statements.interpret; +import static org.jclouds.virtualbox.domain.Statements.getIpAddress; +import static org.testng.Assert.assertEquals; + +import java.io.IOException; + +import org.jclouds.scriptbuilder.ScriptBuilder; +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.jclouds.scriptbuilder.domain.ShellToken; +import org.testng.annotations.Test; + +import com.google.common.base.Charsets; +import com.google.common.io.CharStreams; +import com.google.common.io.Resources; + +/** + * @author Andrea Turli + */ +@Test(groups = "unit") +public class GetIPAddressFromGuestAdditionsTest { + + ScriptBuilder getIpAddressBuilder = new ScriptBuilder() + .addStatement(getIpAddress("{args}")) + .addStatement(interpret("echo {varl}FOUND_IP_ADDRESS{varr}{lf}")); + + public void testUNIX() throws IOException { + assertEquals(getIpAddressBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources + .getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8))); + } + + // TODO + /* + public void testWINDOWS() throws IOException { + assertEquals(getIpAddressBuilder.render(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(Resources + .getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8))); + } + */ +} \ No newline at end of file From 9e2f7be43413e30fe11ab419221de1d4356ba2ff Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Mon, 12 Dec 2011 17:24:04 +0000 Subject: [PATCH 3/7] issue 384: added Statement to virtualbox --- .../jclouds/virtualbox/domain/Statements.java | 2 +- .../main/resources/functions/getIpAddress.cmd | 0 .../main/resources/functions/getIpAddress.sh | 16 ++++++++++ .../test/resources/test_get_ip_address.cmd | 0 .../src/test/resources/test_get_ip_address.sh | 29 +++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.cmd create mode 100644 sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh create mode 100644 sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.cmd create mode 100644 sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java index 2e8d0da0ee..c2908a9e58 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java @@ -37,4 +37,4 @@ public class Statements { public static Statement exportIpAddressFromVmNamed(String vmName) { return new Call("exportIpAddressFromVmNamed", vmName); } - } \ No newline at end of file + } diff --git a/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.cmd b/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.cmd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh b/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh new file mode 100644 index 0000000000..418b869ac1 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh @@ -0,0 +1,16 @@ +function getIpAddress { + unset FOUND_IP_ADDRESS; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local VMNAME="$0"; shift + local _FOUND=`vboxmanage guestproperty enumerate "$VMNAME" --patterns "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $4 }' | cut -c 1-14` + [ -n "$_FOUND" ] && { + export FOUND_IP_ADDRESS=$_FOUND + echo [$FOUND_IP_ADDRESS] + return 0 + } || { + return 1 + } +} diff --git a/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.cmd b/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.cmd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh b/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh new file mode 100644 index 0000000000..63b9e272de --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh @@ -0,0 +1,29 @@ +#!/bin/bash +set +u +shopt -s xpg_echo +shopt -s expand_aliases +unset PATH JAVA_HOME LD_LIBRARY_PATH +function abort { + echo "aborting: $@" 1>&2 + exit 1 +} +function getIpAddress { + unset FOUND_IP_ADDRESS; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local VMNAME="$0"; shift + local _FOUND=`vboxmanage guestproperty enumerate "$VMNAME" --patterns "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $4 }' | cut -c 1-14` + [ -n "$_FOUND" ] && { + export FOUND_IP_ADDRESS=$_FOUND + echo [$FOUND_IP_ADDRESS] + return 0 + } || { + return 1 + } +} +export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin +getIpAddress $@ || exit 1 +echo $FOUND_IP_ADDRESS +exit 0 From 9dbf6f9689ede38ff597dad0cb70d19b6d9eefee Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Mon, 12 Dec 2011 17:27:20 +0000 Subject: [PATCH 4/7] issue 384: added Statement to virtualbox --- .../GetIPAddressFromGuestAdditionsTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java new file mode 100644 index 0000000000..05b8c9184c --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java @@ -0,0 +1,58 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.virtualbox.domain; + +import static org.jclouds.scriptbuilder.domain.Statements.interpret; +import static org.jclouds.virtualbox.domain.Statements.getIpAddress; +import static org.testng.Assert.assertEquals; + +import java.io.IOException; + +import org.jclouds.scriptbuilder.ScriptBuilder; +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.jclouds.scriptbuilder.domain.ShellToken; +import org.testng.annotations.Test; + +import com.google.common.base.Charsets; +import com.google.common.io.CharStreams; +import com.google.common.io.Resources; + +/** + * @author Andrea Turli + */ +@Test(groups = "unit") +public class GetIPAddressFromGuestAdditionsTest { + + ScriptBuilder getIpAddressBuilder = new ScriptBuilder() + .addStatement(getIpAddress("{args}")) + .addStatement(interpret("echo {varl}FOUND_IP_ADDRESS{varr}{lf}")); + + public void testUNIX() throws IOException { + assertEquals(getIpAddressBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources + .getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8))); + } + + // TODO + /* + public void testWINDOWS() throws IOException { + assertEquals(getIpAddressBuilder.render(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(Resources + .getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8))); + } + */ +} \ No newline at end of file From 4e834c146df1ab8bc3a47b8e017580b88352f9a9 Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Mon, 12 Dec 2011 17:24:04 +0000 Subject: [PATCH 5/7] issue 384: added Statement to virtualbox --- .../main/resources/functions/getIpAddress.cmd | 0 .../main/resources/functions/getIpAddress.sh | 16 ++++++++++ .../test/resources/test_get_ip_address.cmd | 0 .../src/test/resources/test_get_ip_address.sh | 29 +++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.cmd create mode 100644 sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh create mode 100644 sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.cmd create mode 100644 sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh diff --git a/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.cmd b/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.cmd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh b/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh new file mode 100644 index 0000000000..418b869ac1 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/resources/functions/getIpAddress.sh @@ -0,0 +1,16 @@ +function getIpAddress { + unset FOUND_IP_ADDRESS; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local VMNAME="$0"; shift + local _FOUND=`vboxmanage guestproperty enumerate "$VMNAME" --patterns "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $4 }' | cut -c 1-14` + [ -n "$_FOUND" ] && { + export FOUND_IP_ADDRESS=$_FOUND + echo [$FOUND_IP_ADDRESS] + return 0 + } || { + return 1 + } +} diff --git a/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.cmd b/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.cmd new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh b/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh new file mode 100644 index 0000000000..63b9e272de --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/resources/test_get_ip_address.sh @@ -0,0 +1,29 @@ +#!/bin/bash +set +u +shopt -s xpg_echo +shopt -s expand_aliases +unset PATH JAVA_HOME LD_LIBRARY_PATH +function abort { + echo "aborting: $@" 1>&2 + exit 1 +} +function getIpAddress { + unset FOUND_IP_ADDRESS; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local VMNAME="$0"; shift + local _FOUND=`vboxmanage guestproperty enumerate "$VMNAME" --patterns "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $4 }' | cut -c 1-14` + [ -n "$_FOUND" ] && { + export FOUND_IP_ADDRESS=$_FOUND + echo [$FOUND_IP_ADDRESS] + return 0 + } || { + return 1 + } +} +export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin +getIpAddress $@ || exit 1 +echo $FOUND_IP_ADDRESS +exit 0 From f3e8c617ec6f0b1106d771e80b1c29e3772b1431 Mon Sep 17 00:00:00 2001 From: andreaturli Date: Sun, 18 Dec 2011 19:14:12 +0000 Subject: [PATCH 6/7] issue 384: small fixes to UnregisterMachine to destroy all the media attached, removed lzma lib, add memory to CreateAndInstallVmLiveTest --- sandbox-apis/virtualbox/pom.xml | 6 ---- .../admin/UnregisterMachineIfExists.java | 29 +++++++++++++++---- .../functions/CreateAndInstallVmLiveTest.java | 2 +- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/sandbox-apis/virtualbox/pom.xml b/sandbox-apis/virtualbox/pom.xml index cec894f03f..313d503322 100644 --- a/sandbox-apis/virtualbox/pom.xml +++ b/sandbox-apis/virtualbox/pom.xml @@ -59,12 +59,6 @@ byon ${project.version} - - com.github.jponge - lzma-java - 1.2 - test - org.eclipse.jetty jetty-security diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java index 08d2d25668..1a73c71508 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java @@ -20,18 +20,23 @@ package org.jclouds.virtualbox.functions.admin; -import com.google.common.base.Function; +import java.util.List; + +import javax.annotation.Nullable; +import javax.annotation.Resource; +import javax.inject.Named; + import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.logging.Logger; import org.jclouds.virtualbox.domain.ErrorCode; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.IMedium; +import org.virtualbox_4_1.IProgress; import org.virtualbox_4_1.VBoxException; import org.virtualbox_4_1.VirtualBoxManager; -import javax.annotation.Nullable; -import javax.annotation.Resource; -import javax.inject.Named; +import com.google.common.base.Function; public class UnregisterMachineIfExists implements Function { @@ -49,9 +54,11 @@ public class UnregisterMachineIfExists implements Function { @Override public Void apply(@Nullable String vmName) { + List mediaToBeDeleted = null; + IMachine machine = null; try { - IMachine machine = manager.getVBox().findMachine(vmName); - machine.unregister(mode); + machine = manager.getVBox().findMachine(vmName); + mediaToBeDeleted = machine.unregister(mode); } catch (VBoxException e) { ErrorCode errorCode = ErrorCode.valueOf(e); switch (errorCode) { @@ -62,6 +69,16 @@ public class UnregisterMachineIfExists implements Function { throw e; } } + /** + * deletion of all files is currently disabled on Windows/x64 to +prevent a crash + */ + try { + IProgress deletion = machine.delete(mediaToBeDeleted); + deletion.waitForCompletion(-1); + } catch (Exception e) { + e.printStackTrace(); + } return null; } } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java index 6904324e11..4554116f65 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java @@ -90,7 +90,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { .attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso") .attachHardDisk(0, 1, workingDir + "/testadmin.vdi") .attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build(); - VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).osTypeId(osTypeId) + VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId) .controller(ideController) .forceOverwrite(true) .natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build(); From 9a460c57d05821d632c95b583bc73bdafb7840d5 Mon Sep 17 00:00:00 2001 From: andreaturli Date: Sun, 18 Dec 2011 19:24:08 +0000 Subject: [PATCH 7/7] issue 384: small fixes to UnregisterMachine to destroy all the media attached --- ...sterMachineIfExistsAndDeleteItsMedia.java} | 34 +++++++++------ .../functions/CreateAndInstallVmLiveTest.java | 43 +++++++++++-------- ...hineFromIsoIfNotAlreadyExistsLiveTest.java | 14 +++--- ...MachineIfExistsAndDeleteItsMediaTest.java} | 4 +- 4 files changed, 55 insertions(+), 40 deletions(-) rename sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/{UnregisterMachineIfExists.java => UnregisterMachineIfExistsAndDeleteItsMedia.java} (73%) rename sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/{UnregisterMachineIfExistsTest.java => UnregisterMachineIfExistsAndDeleteItsMediaTest.java} (92%) diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMedia.java similarity index 73% rename from sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java rename to sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMedia.java index 1a73c71508..1a90296d15 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMedia.java @@ -19,7 +19,6 @@ package org.jclouds.virtualbox.functions.admin; - import java.util.List; import javax.annotation.Nullable; @@ -37,8 +36,9 @@ import org.virtualbox_4_1.VBoxException; import org.virtualbox_4_1.VirtualBoxManager; import com.google.common.base.Function; +import com.google.common.base.Throwables; -public class UnregisterMachineIfExists implements Function { +public class UnregisterMachineIfExistsAndDeleteItsMedia implements Function { @Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) @@ -47,7 +47,7 @@ public class UnregisterMachineIfExists implements Function { private VirtualBoxManager manager; private CleanupMode mode; - public UnregisterMachineIfExists(VirtualBoxManager manager, CleanupMode mode) { + public UnregisterMachineIfExistsAndDeleteItsMedia(VirtualBoxManager manager, CleanupMode mode) { this.manager = manager; this.mode = mode; } @@ -62,23 +62,31 @@ public class UnregisterMachineIfExists implements Function { } catch (VBoxException e) { ErrorCode errorCode = ErrorCode.valueOf(e); switch (errorCode) { - case VBOX_E_OBJECT_NOT_FOUND: - logger.debug("Machine %s does not exists, cannot unregister", vmName); - break; - default: - throw e; + case VBOX_E_OBJECT_NOT_FOUND: + logger.debug("Machine %s does not exists, cannot unregister", + vmName); + break; + default: + throw e; } } /** - * deletion of all files is currently disabled on Windows/x64 to -prevent a crash + * deletion of all files is currently disabled on Windows/x64 to prevent a + * crash */ try { - IProgress deletion = machine.delete(mediaToBeDeleted); - deletion.waitForCompletion(-1); + IProgress deletion = machine.delete(mediaToBeDeleted); + deletion.waitForCompletion(-1); } catch (Exception e) { - e.printStackTrace(); + logger.error(e, "Problem in deleting the media attached to %s", machine.getName()); + propagate(e); } return null; } + + protected T propagate(Exception e) { + Throwables.propagate(e); + assert false; + return null; + } } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java index 4554116f65..eeab8c27d9 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java @@ -19,9 +19,19 @@ package org.jclouds.virtualbox.functions; -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.inject.Guice; +import static com.google.common.base.Predicates.equalTo; +import static com.google.common.collect.Iterables.any; +import static com.google.common.collect.Iterables.transform; +import static org.jclouds.virtualbox.domain.ExecutionType.HEADLESS; +import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; +import static org.testng.Assert.assertTrue; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import javax.annotation.Nullable; + import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.config.BaseComputeServiceContextModule; import org.jclouds.compute.domain.Image; @@ -34,24 +44,21 @@ import org.jclouds.net.IPSocket; import org.jclouds.predicates.InetSocketAddressConnect; import org.jclouds.predicates.RetryablePredicate; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; -import org.jclouds.virtualbox.domain.*; -import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists; +import org.jclouds.virtualbox.domain.NatAdapter; +import org.jclouds.virtualbox.domain.StorageController; +import org.jclouds.virtualbox.domain.VmSpec; +import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia; import org.jclouds.virtualbox.util.PropertyUtils; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; -import org.virtualbox_4_1.*; +import org.virtualbox_4_1.CleanupMode; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.StorageBus; +import org.virtualbox_4_1.VirtualBoxManager; -import javax.annotation.Nullable; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -import static com.google.common.base.Predicates.equalTo; -import static com.google.common.collect.Iterables.any; -import static com.google.common.collect.Iterables.transform; -import static org.jclouds.virtualbox.domain.ExecutionType.HEADLESS; -import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; -import static org.testng.Assert.assertTrue; +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.inject.Guice; /** * @author Andrea Turli, Mattias Holmqvist @@ -75,7 +82,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { public void setUp() throws Exception { identity = "toor"; credential = "password"; - new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); } public void testCreateImageMachineFromIso() throws Exception { diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java index 6b04272833..3306ff1764 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java @@ -19,18 +19,18 @@ package org.jclouds.virtualbox.functions; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; + import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.domain.ErrorCode; import org.jclouds.virtualbox.domain.VmSpec; -import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists; +import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.VBoxException; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; - /** * @author Mattias Holmqvist */ @@ -39,19 +39,19 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B @Test public void testCreateNewMachine() throws Exception { String vmName = "jclouds-test-create-1-node"; - new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName) .osTypeId("Debian").forceOverwrite(true).build(); IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); IMachine machine = manager.getVBox().findMachine(vmName); assertEquals(debianNode.getName(), machine.getName()); - new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); } @Test public void testCreateNewMachineWithBadOsType() throws Exception { String vmName = "jclouds-test-create-2-node"; - new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName) .osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build(); try { diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java similarity index 92% rename from sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsTest.java rename to sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java index e0c3628c4d..91b120928d 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java @@ -30,7 +30,7 @@ import static org.easymock.classextension.EasyMock.expect; import static org.easymock.classextension.EasyMock.*; @Test(groups = "unit", testName = "UnregisterMachineIfExistsTest") -public class UnregisterMachineIfExistsTest { +public class UnregisterMachineIfExistsAndDeleteItsMediaTest { @Test public void testUnregisterExistingMachine() throws Exception { @@ -50,7 +50,7 @@ public class UnregisterMachineIfExistsTest { replay(manager, vBox, registeredMachine); - new UnregisterMachineIfExists(manager, mode).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, mode).apply(vmName); } }