Merge pull request #419 from dralves/jclouds-vbox-nat-host-only-working-cluster-and-live-test

added automated addition of the host module to the context
This commit is contained in:
Adrian Cole 2012-03-10 14:44:39 -08:00
commit 1a355c709e
6 changed files with 159 additions and 25 deletions

View File

@ -19,13 +19,20 @@
package org.jclouds.virtualbox; package org.jclouds.virtualbox;
import java.net.URI;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.jclouds.byon.Node;
import org.jclouds.byon.config.CacheNodeStoreModule;
import org.jclouds.compute.StandaloneComputeServiceContextBuilder; import org.jclouds.compute.StandaloneComputeServiceContextBuilder;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.virtualbox.config.VirtualBoxComputeServiceContextModule; import org.jclouds.virtualbox.config.VirtualBoxComputeServiceContextModule;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.inject.Module; import com.google.inject.Module;
/** /**
@ -37,12 +44,31 @@ import com.google.inject.Module;
public class VirtualBoxContextBuilder extends StandaloneComputeServiceContextBuilder<Supplier> { public class VirtualBoxContextBuilder extends StandaloneComputeServiceContextBuilder<Supplier> {
public VirtualBoxContextBuilder(Properties properties) { public VirtualBoxContextBuilder(Properties properties) {
super(Supplier.class, properties); super(Supplier.class, new VirtualBoxPropertiesBuilder(properties).defaultProperties());
} }
@Override @Override
protected void addContextModule(List<Module> modules) { protected void addContextModule(List<Module> modules) {
modules.add(new VirtualBoxComputeServiceContextModule()); modules.add(new VirtualBoxComputeServiceContextModule());
addHostModuleIfNotPresent(modules);
}
protected void addHostModuleIfNotPresent(List<Module> modules) {
if (!Iterables.any(modules, new Predicate<Module>() {
public boolean apply(Module input) {
return input instanceof CacheNodeStoreModule;
}
})) {
CacheNodeStoreModule hostModule = new CacheNodeStoreModule(ImmutableMap.of(
"host",
Node.builder().id("host").name("host installing virtualbox").hostname("localhost")
.osFamily(OsFamily.LINUX.toString()).osDescription(System.getProperty("os.name"))
.osVersion(System.getProperty("os.version")).group("ssh")
.username(System.getProperty("user.name"))
.credentialUrl(URI.create("file://" + System.getProperty("user.home") + "/.ssh/id_rsa"))
.build()));
modules.add(hostModule);
}
} }
} }

View File

@ -41,12 +41,13 @@ import org.jclouds.logging.Logger;
import org.jclouds.virtualbox.domain.Master; import org.jclouds.virtualbox.domain.Master;
import org.jclouds.virtualbox.domain.NodeSpec; import org.jclouds.virtualbox.domain.NodeSpec;
import org.jclouds.virtualbox.domain.YamlImage; 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.IMachine;
import org.virtualbox_4_1.IProgress; import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession; import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.MachineState; import org.virtualbox_4_1.MachineState;
import org.virtualbox_4_1.SessionState; import org.virtualbox_4_1.SessionState;
import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager; import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -138,14 +139,21 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
@Override @Override
public IMachine getNode(String vmName) { 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 @Override
public void destroyNode(String vmName) { public void destroyNode(String vmName) {
IMachine machine = manager.get().getVBox().findMachine(vmName); IMachine machine = manager.get().getVBox().findMachine(vmName);
powerDownMachine(machine); powerDownMachine(machine);
machine.unregister(CleanupMode.Full); new UnregisterMachineIfExistsAndForceDeleteItsMedia().apply(machine);
} }
@Override @Override
@ -201,7 +209,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
while (!machine.getSessionState().equals(SessionState.Unlocked)) { while (!machine.getSessionState().equals(SessionState.Unlocked)) {
try { 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); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }

View File

@ -176,16 +176,8 @@ public class VirtualBoxComputeServiceContextModule extends
String provider = "byon"; String provider = "byon";
String identity = ""; String identity = "";
String credential = ""; String credential = "";
CacheNodeStoreModule hostModule = new CacheNodeStoreModule(ImmutableMap.of(
"host",
Node.builder().id("host").name("host installing virtualbox").hostname("localhost")
.osFamily(OsFamily.LINUX.toString()).osDescription(System.getProperty("os.name"))
.osVersion(System.getProperty("os.version")).group("ssh")
.username(System.getProperty("user.name"))
.credentialUrl(URI.create("file://" + System.getProperty("user.home") + "/.ssh/id_rsa"))
.build()));
return new ComputeServiceContextFactory().createContext(provider, identity, credential, return new ComputeServiceContextFactory().createContext(provider, identity, credential,
ImmutableSet.<Module> of(new SLF4JLoggingModule(), new SshjSshClientModule(), hostModule)); ImmutableSet.<Module> of(new SLF4JLoggingModule(), new SshjSshClientModule()));
} }
@Provides @Provides

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;
}
};
}

View File

@ -135,17 +135,8 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest {
setupCredentials(); setupCredentials();
Properties overrides = new VirtualBoxPropertiesBuilder(setupProperties()).build(); Properties overrides = new VirtualBoxPropertiesBuilder(setupProperties()).build();
CacheNodeStoreModule hostModule = new CacheNodeStoreModule(ImmutableMap.of(
"host",
Node.builder().id("host").name("host installing virtualbox").hostname("localhost")
.osFamily(OsFamily.LINUX.toString()).osDescription(System.getProperty("os.name"))
.osVersion(System.getProperty("os.version")).group("ssh")
.username(System.getProperty("user.name"))
.credentialUrl(URI.create("file://" + System.getProperty("user.home") + "/.ssh/id_rsa"))
.build()));
context = new ComputeServiceContextFactory().createContext(provider, identity, credential, ImmutableSet context = new ComputeServiceContextFactory().createContext(provider, identity, credential, ImmutableSet
.<Module> of(new SLF4JLoggingModule(), new SshjSshClientModule(), hostModule, new ExecutorServiceModule( .<Module> of(new SLF4JLoggingModule(), new SshjSshClientModule(), new ExecutorServiceModule(
singleThreadExec, singleThreadExec)), overrides); singleThreadExec, singleThreadExec)), overrides);
context.utils().injector().injectMembers(this); context.utils().injector().injectMembers(this);

View File

@ -61,7 +61,7 @@ public class SetIpAddressTest {
.addNetworkAdapter( .addNetworkAdapter(
NetworkAdapter.builder().staticIp("localhost").networkAttachmentType(NetworkAttachmentType.NAT) NetworkAdapter.builder().staticIp("localhost").networkAttachmentType(NetworkAttachmentType.NAT)
.build()).build(); .build()).build();
SetIpAddress setIpAddressStmtm = new SetIpAddress(networkInterfaceCard); new SetIpAddress(networkInterfaceCard);
} }
} }