Merge pull request #192 from andreaturli/clonemachine

issue 384: added CloneAndRegisterMachineFromIsoIfNotAlreadyExists + Live...
This commit is contained in:
Adrian Cole 2011-11-26 08:18:13 -08:00
commit 9272136548
7 changed files with 664 additions and 0 deletions

1
.gitignore vendored
View File

@ -11,4 +11,5 @@ bin/
*.eml
*.ipr
*.iws
*.DS_STORE
TAGS

View File

@ -0,0 +1,186 @@
/**
* 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;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.virtualbox.MachineUtils.lockMachineAndApply;
import static org.virtualbox_4_1.LockType.Write;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import javax.inject.Named;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.virtualbox_4_1.CloneMode;
import org.virtualbox_4_1.CloneOptions;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISnapshot;
import org.virtualbox_4_1.IVirtualBox;
import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
/**
*
* CloneAndRegisterMachineFromIMachineIfNotAlreadyExists will take care of the
* followings: - cloning the master - register the clone machine -
* ensureBridgedNetworkingIsAppliedToMachine(cloneName, macAddress,
* hostInterface)
*
* @author Andrea Turli
*/
public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements
Function<IMachine, IMachine> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private VirtualBoxManager manager;
private ComputeServiceContext context;
private String settingsFile;
private String osTypeId;
private String vmId;
private boolean forceOverwrite;
private String cloneName;
private String hostId;
private String snapshotName;
private String snapshotDesc;
private String controllerIDE;
@Inject
public CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(
VirtualBoxManager manager, ComputeServiceContext context,
String settingsFile, String osTypeId, String vmId,
boolean forceOverwrite, String cloneName, String hostId,
String snashotName, String snapshotDesc, String controllerIDE) {
super();
this.manager = manager;
this.context = context;
this.settingsFile = settingsFile;
this.osTypeId = osTypeId;
this.vmId = vmId;
this.forceOverwrite = forceOverwrite;
this.cloneName = cloneName;
this.hostId = hostId;
this.snapshotName = snashotName;
this.snapshotDesc = snapshotDesc;
this.controllerIDE = controllerIDE;
}
@Override
public IMachine apply(@Nullable IMachine master) {
final IVirtualBox vBox = manager.getVBox();
try {
vBox.findMachine(cloneName);
throw new IllegalStateException("Machine " + cloneName
+ " is already registered.");
} catch (VBoxException e) {
if (machineNotFoundException(e))
return cloneMachine(vBox, cloneName, master);
else
throw e;
}
}
private boolean machineNotFoundException(VBoxException e) {
return e.getMessage().indexOf(
"VirtualBox error: Could not find a registered machine named ") != -1;
}
private IMachine cloneMachine(IVirtualBox vBox, String cloneName,
IMachine master) {
IMachine clonedMachine = manager.getVBox().createMachine(settingsFile,
cloneName, osTypeId, vmId, forceOverwrite);
List<CloneOptions> options = new ArrayList<CloneOptions>();
options.add(CloneOptions.Link);
// takeSnapshotIfNotAlreadyExists
ISnapshot currentSnapshot = new TakeSnapshotIfNotAlreadyAttached(manager,
snapshotName, snapshotDesc).apply(master);
// clone
IProgress progress = currentSnapshot.getMachine().cloneTo(clonedMachine,
CloneMode.MachineState, options);
if (progress.getCompleted())
logger.debug("clone done");
// registering
manager.getVBox().registerMachine(clonedMachine);
// Bridged Network
List<String> activeBridgedInterfaces = new RetrieveActiveBridgedInterfaces(
context).apply(hostId);
checkNotNull(activeBridgedInterfaces);
String macAddress = manager.getVBox().getHost().generateMACAddress();
// TODO this behavior can be improved
String bridgedInterface = activeBridgedInterfaces.get(0);
ensureBridgedNetworkingIsAppliedToMachine(cloneName, macAddress,
bridgedInterface);
// detach iso
ensureMachineHasDistroMediumDetached(cloneName, controllerIDE);
return clonedMachine;
}
private void ensureBridgedNetworkingIsAppliedToMachine(String vmName,
String macAddress, String hostInterface) {
lockMachineAndApply(manager, Write, vmName,
new AttachBridgedAdapterToMachine(0l, macAddress, hostInterface));
}
private void ensureMachineHasDistroMediumDetached(String vmName,
String controllerIDE) {
lockMachineAndApply(
manager,
Write,
vmName,
new DetachDistroMediumToMachine(checkNotNull(controllerIDE,
"controllerIDE")));
}
protected ExecResponse runScriptOnNode(String nodeId, String command,
RunScriptOptions options) {
return context.getComputeService().runScriptOnNode(nodeId, command,
options);
}
protected <T> T propagate(Exception e) {
Throwables.propagate(e);
assert false;
return null;
}
}

View File

@ -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.functions;
import javax.annotation.Nullable;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.VBoxException;
import com.google.common.base.Function;
/**
* @author Andrea Turli
*/
public class DetachDistroMediumToMachine implements Function<IMachine, Void> {
private final String controller;
public DetachDistroMediumToMachine(String controller) {
this.controller = controller;
}
@Override
public Void apply(@Nullable IMachine machine) {
try {
int controllerPort = 0;
int device = 0;
machine.detachDevice(controller, controllerPort, device);
machine.saveSettings();
} catch (VBoxException e) {
if (!alreadyDetached(e))
throw e;
}
return null;
}
private boolean alreadyDetached(VBoxException e) {
return e.getMessage().indexOf("is already detached from port") != -1;
}
}

View File

@ -0,0 +1,129 @@
package org.jclouds.virtualbox.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.name.Named;
public class RetrieveActiveBridgedInterfaces implements
Function<String, List<String>> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private ComputeServiceContext context;
@Inject
public RetrieveActiveBridgedInterfaces(ComputeServiceContext context) {
super();
this.context = context;
}
@Override
public List<String> apply(String hostId) {
// Bridged Network
String command = "vboxmanage list bridgedifs";
String bridgedIfBlocks = context
.getComputeService()
.runScriptOnNode(hostId, command,
runAsRoot(false).wrapInInitScript(false)).getOutput();
List<String> bridgedInterfaces = retrieveBridgedInterfaceNames(bridgedIfBlocks);
checkNotNull(bridgedInterfaces);
// union of bridgedNetwork with inet up and !loopback
List<String> activeNetworkInterfaceNames = Lists.newArrayList();
Enumeration<NetworkInterface> nets;
try {
Iterable<String> filterdBridgedInterface = null;
nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface inet : Collections.list(nets)) {
filterdBridgedInterface = Iterables.filter(bridgedInterfaces,
new IsActiveBridgedInterface(inet));
for (String filterInetName : filterdBridgedInterface) {
activeNetworkInterfaceNames.add(filterInetName);
}
}
} catch (SocketException e) {
logger.error(e, "Problem in listing network interfaces.");
propagate(e);
}
return activeNetworkInterfaceNames;
}
protected static List<String> retrieveBridgedInterfaceNames(
String bridgedIfBlocks) {
List<String> bridgedInterfaceNames = Lists.newArrayList();
// separate the different bridge block
for (String bridgedIfBlock : Splitter.on(
Pattern.compile("(?m)^[ \t]*\r?\n")).split(bridgedIfBlocks)) {
Iterable<String> bridgedIfName = Iterables.filter(Splitter.on("\n")
.split(bridgedIfBlock), new Predicate<String>() {
@Override
public boolean apply(String arg0) {
return arg0.startsWith("Name:");
}
});
for (String bridgedInterfaceName : bridgedIfName) {
for (String string : Splitter.on("Name:").split(
bridgedInterfaceName)) {
if (!string.isEmpty())
bridgedInterfaceNames.add(string.trim());
}
}
}
return bridgedInterfaceNames;
}
protected <T> T propagate(Exception e) {
Throwables.propagate(e);
assert false;
return null;
}
private class IsActiveBridgedInterface implements Predicate<String> {
private NetworkInterface networkInterface;
public IsActiveBridgedInterface(NetworkInterface networkInterface) {
super();
this.networkInterface = networkInterface;
}
@Override
public boolean apply(String bridgedInterfaceName) {
try {
return (bridgedInterfaceName.startsWith(networkInterface
.getDisplayName()) && networkInterface.isUp() && !networkInterface
.isLoopback());
} catch (SocketException e) {
logger.error(e, "Problem in listing network interfaces.");
propagate(e);
}
return false;
}
};
}

View File

@ -0,0 +1,98 @@
/**
* 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;
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
import static org.testng.Assert.assertEquals;
import static org.virtualbox_4_1.NetworkAttachmentType.Bridged;
import java.util.concurrent.TimeUnit;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.domain.Credentials;
import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Predicate;
/**
* @author Andrea Turli
*/
@Test(groups = "live", singleThreaded = true, testName = "CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest")
public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
BaseVirtualBoxClientLiveTest {
private String settingsFile = null;
private boolean forceOverwrite = true;
private String vmId = "jclouds-image-iso-1";
private String osTypeId = "";
private String controllerIDE = "IDE Controller";
private String diskFormat = "";
private String adminDisk = "testadmin.vdi";
private String guestId = "guest";
private String hostId = "host";
private String snapshotName = "snap";
private String snapshotDesc = "snapDesc";
private String vmName = "jclouds-image-virtualbox-iso-to-machine-test";
private String cloneName = vmName + "_clone";
private String isoName = "ubuntu-11.04-server-i386.iso";
@Test
public void testCloneMachineFromAnotherMachine() throws Exception {
VirtualBoxManager manager = (VirtualBoxManager) context
.getProviderSpecificContext().getApi();
ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest(
hostId, "localhost", guestId, "localhost", new Credentials("toor",
"password"));
IMachine master = null;
try {
Predicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>(
new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS);
master = new IsoToIMachine(manager, adminDisk, diskFormat,
settingsFile, vmName, osTypeId, vmId, forceOverwrite,
controllerIDE, localHostContext, hostId, guestId, socketTester,
"127.0.0.1", 8080).apply(isoName);
} catch (IllegalStateException e) {
// already created
master = manager.getVBox().findMachine(vmName);
}
if (master.getCurrentSnapshot() != null) {
ISession session = manager.openMachineSession(master);
session.getConsole().deleteSnapshot(
master.getCurrentSnapshot().getId());
session.unlockMachine();
}
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(
manager, localHostContext, settingsFile, osTypeId, vmId,
forceOverwrite, cloneName, hostId, snapshotName, snapshotDesc,
controllerIDE).apply(master);
assertEquals(clone.getNetworkAdapter(0L).getAttachmentType(), Bridged);
}
}

View File

@ -0,0 +1,104 @@
/**
* 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;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.createNiceMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.VBoxException;
/**
* @author Andrea Turli
*/
@Test(groups = "unit", testName = "DetachDistroMediumToMachineTest")
public class DetachDistroMediumToMachineTest {
@Test
public void testDetachDistroMedium() throws Exception {
String controller = "IDE Controller";
IMachine machine = createMock(IMachine.class);
machine.saveSettings();
machine.detachDevice(controller, 0, 0);
replay(machine);
new DetachDistroMediumToMachine(controller).apply(machine);
verify(machine);
}
@Test
public void testAcceptAlreadyDetachedDistroMedium() throws Exception {
String controller = "IDE Controller";
IMachine machine = createNiceMock(IMachine.class);
final StringBuilder errorBuilder = new StringBuilder();
errorBuilder.append("VirtualBox error: ");
errorBuilder.append("Medium '/Users/johndoe/jclouds-virtualbox-test/ubuntu-11.04-server-i386.iso' ");
errorBuilder.append("is already detached from port 0, device 0 of controller 'IDE Controller' ");
errorBuilder.append("of this virtual machine (0x80BB000C)");
String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class),
isoAlreadyAttachedException);
machine.detachDevice(controller, 0, 0);
expectLastCall().andThrow(isoAttachedException);
replay(machine);
new DetachDistroMediumToMachine(controller).apply(machine);
verify(machine);
}
@Test(expectedExceptions = VBoxException.class)
public void testFailOnOtherVBoxErrors() throws Exception {
String controller = "IDE Controller";
IMachine machine = createNiceMock(IMachine.class);
final StringBuilder errorBuilder = new StringBuilder();
errorBuilder.append("VirtualBox error: ");
errorBuilder.append("Some other VBox error");
String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class),
isoAlreadyAttachedException);
machine.detachDevice(controller, 0, 0);
expectLastCall().andThrow(isoAttachedException);
replay(machine);
new DetachDistroMediumToMachine(controller).apply(machine);
}
}

View File

@ -0,0 +1,88 @@
/**
* 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;
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
import static org.jclouds.virtualbox.functions.RetrieveActiveBridgedInterfaces.retrieveBridgedInterfaceNames;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import java.util.List;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.domain.Credentials;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
/**
* @author Andrea Turli
*/
@Test(groups = "live", singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesLiveTest")
public class RetrieveActiveBridgedInterfacesLiveTest extends
BaseVirtualBoxClientLiveTest {
public static final String TEST1 = "Name: eth0\n"
+ "GUID: 30687465-0000-4000-8000-00261834d0cb\n"
+ "Dhcp: Disabled\n"
+ "IPAddress: 209.x.x.x\n"
+ "NetworkMask: 255.255.255.0\n"
+ "IPV6Address: fe80:0000:0000:0000:0226:18ff:fe34:d0cb\n"
+ "IPV6NetworkMaskPrefixLength: 64\n"
+ "HardwareAddress: 00:26:18:34:d0:cb\n"
+ "MediumType: Ethernet\n"
+ "Status: Up\n"
+ "VBoxNetworkName: HostInterfaceNetworking-eth0\n"
+ "\n"
+ "Name: vbox0\n"
+ "GUID: 786f6276-0030-4000-8000-5a3ded993fed\n"
+ "Dhcp: Disabled\n"
+ "IPAddress: 192.168.56.1\n"
+ "NetworkMask: 255.255.255.0\n"
+ "IPV6Address: fe80:0000:0000:0000:0226:18ff:fe34:d0cb\n"
+ "IPV6NetworkMaskPrefixLength: 0\n"
+ "HardwareAddress: 5a:3d:ed:99:3f:ed\n"
+ "MediumType: Ethernet\n"
+ "Status: Down\n"
+ "VBoxNetworkName: HostInterfaceNetworking-vbox0\n";
public static final List<String> expectedBridgedInterfaces = ImmutableList.of("eth0", "vbox0");
private String guestId = "guest";
private String hostId = "host";
@Test
public void retrieveBridgedInterfaceNamesTest() {
List<String> activeBridgedInterfaceNames = retrieveBridgedInterfaceNames(TEST1);
assertEquals(activeBridgedInterfaceNames, expectedBridgedInterfaces);
}
@Test
public void retrieveAvailableBridgedInterfaceInfoTest() {
ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest(
hostId, "localhost", guestId, "localhost", new Credentials("toor",
"password"));
List<String> bridgedInterface = new RetrieveActiveBridgedInterfaces(
localHostContext).apply(hostId);
assertFalse(bridgedInterface.isEmpty());
}
}