vboxadapter now deletes mediums on destroyNode() and non-exisitng nodes are handled in getNode(). VirtualBoxExperimentLiveTest passes consistently

This commit is contained in:
David Ribeiro Alves 2012-03-10 19:45:35 +00:00
parent ad6b655ea1
commit 3c776b8768
2 changed files with 129 additions and 4 deletions

View File

@ -41,12 +41,13 @@ import org.jclouds.logging.Logger;
import org.jclouds.virtualbox.domain.Master;
import org.jclouds.virtualbox.domain.NodeSpec;
import org.jclouds.virtualbox.domain.YamlImage;
import org.virtualbox_4_1.CleanupMode;
import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndForceDeleteItsMedia;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.MachineState;
import org.virtualbox_4_1.SessionState;
import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
@ -138,14 +139,21 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
@Override
public IMachine getNode(String vmName) {
return manager.get().getVBox().findMachine(vmName);
try {
return manager.get().getVBox().findMachine(vmName);
} catch (VBoxException e) {
if (e.getMessage().contains("Could not find a registered machine named")){
return null;
}
throw Throwables.propagate(e);
}
}
@Override
public void destroyNode(String vmName) {
IMachine machine = manager.get().getVBox().findMachine(vmName);
powerDownMachine(machine);
machine.unregister(CleanupMode.Full);
new UnregisterMachineIfExistsAndForceDeleteItsMedia().apply(machine);
}
@Override
@ -201,7 +209,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
while (!machine.getSessionState().equals(SessionState.Unlocked)) {
try {
System.out.println("waiting for unlocking session - session state: " + machine.getSessionState());
logger.info("waiting for unlocking session - session state: " + machine.getSessionState());
Thread.sleep(1000);
} catch (InterruptedException e) {
}

View File

@ -0,0 +1,117 @@
/**
* 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.
*/
/*
U * 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.functions.admin;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.transform;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;
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.DeviceType;
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 com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
@Singleton
public class UnregisterMachineIfExistsAndForceDeleteItsMedia implements Function<IMachine, Void> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
@Override
public Void apply(IMachine machine) {
List<IMedium> mediaToBeDeleted = Collections.emptyList();
try {
mediaToBeDeleted = machine.unregister(CleanupMode.Full);
} 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", machine.getName());
break;
default:
throw e;
}
}
List<IMedium> filteredMediaToBeDeleted = Lists.newArrayList(transform(mediaToBeDeleted,
new DeleteChildrenOfMedium()));
if (!filteredMediaToBeDeleted.isEmpty()) {
try {
IProgress deletion = machine.delete(filteredMediaToBeDeleted);
deletion.waitForCompletion(-1);
} catch (Exception e) {
logger.error(e, "Problem in deleting the media attached to %s", machine.getName());
Throwables.propagate(e);
}
}
return null;
}
private class DeleteChildrenOfMedium implements Function<IMedium, IMedium> {
@Override
public IMedium apply(IMedium medium) {
checkNotNull(medium.getChildren());
if (medium.getDeviceType().equals(DeviceType.HardDisk)) {
for (IMedium child : medium.getChildren()) {
IProgress deletion = child.deleteStorage();
deletion.waitForCompletion(-1);
}
}
return medium;
}
};
}