mirror of https://github.com/apache/jclouds.git
Merge pull request #256 from andreaturli/unregister
issue 384: improved UnregisterMachine
This commit is contained in:
commit
57c3c81139
|
@ -59,12 +59,6 @@
|
|||
<artifactId>byon</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.jponge</groupId>
|
||||
<artifactId>lzma-java</artifactId>
|
||||
<version>1.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-security</artifactId>
|
||||
|
|
|
@ -19,21 +19,26 @@
|
|||
|
||||
package org.jclouds.virtualbox.functions.admin;
|
||||
|
||||
|
||||
import com.google.common.base.Function;
|
||||
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.VBoxException;
|
||||
import org.virtualbox_4_1.VirtualBoxManager;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Named;
|
||||
|
||||
public class UnregisterMachineIfExists implements Function<String, Void> {
|
||||
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 com.google.common.base.Function;
|
||||
import com.google.common.base.Throwables;
|
||||
|
||||
public class UnregisterMachineIfExistsAndDeleteItsMedia implements Function<String, Void> {
|
||||
|
||||
@Resource
|
||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||
|
@ -42,26 +47,46 @@ public class UnregisterMachineIfExists implements Function<String, Void> {
|
|||
private VirtualBoxManager manager;
|
||||
private CleanupMode mode;
|
||||
|
||||
public UnregisterMachineIfExists(VirtualBoxManager manager, CleanupMode mode) {
|
||||
public UnregisterMachineIfExistsAndDeleteItsMedia(VirtualBoxManager manager, CleanupMode mode) {
|
||||
this.manager = manager;
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(@Nullable String vmName) {
|
||||
List<IMedium> 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) {
|
||||
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
|
||||
*/
|
||||
try {
|
||||
IProgress deletion = machine.delete(mediaToBeDeleted);
|
||||
deletion.waitForCompletion(-1);
|
||||
} catch (Exception e) {
|
||||
logger.error(e, "Problem in deleting the media attached to %s", machine.getName());
|
||||
propagate(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected <T> T propagate(Exception e) {
|
||||
Throwables.propagate(e);
|
||||
assert false;
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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)));
|
||||
}
|
||||
*/
|
||||
}
|
|
@ -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 {
|
||||
|
@ -90,7 +97,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();
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue