issue 384: GuestAdditionsInstaller - fix scripts

This commit is contained in:
andreaturli 2012-02-12 00:55:12 +00:00
parent af553b6294
commit 20ec953fb6
8 changed files with 243 additions and 9 deletions

View File

@ -24,6 +24,8 @@ import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
import com.google.common.cache.LoadingCache;
import com.google.inject.Inject;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.ssh.SshClient;
@ -32,6 +34,7 @@ import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.MasterSpec;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.predicates.GuestAdditionsInstaller;
import org.jclouds.virtualbox.util.MachineUtils;
import org.virtualbox_4_1.*;
@ -50,7 +53,8 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final ComputeServiceContext context;
private final Supplier<VirtualBoxManager> manager;
private final CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists;
@ -64,10 +68,11 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
private final MachineUtils machineUtils;
@Inject
public CreateAndInstallVm(Supplier<VirtualBoxManager> manager,
public CreateAndInstallVm(ComputeServiceContext context, Supplier<VirtualBoxManager> manager,
CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists,
Predicate<SshClient> sshResponds, Function<IMachine, SshClient> sshClientForIMachine,
ExecutionType executionType, MachineUtils machineUtils, @Preconfiguration LoadingCache<IsoSpec, URI> preConfiguration) {
this.context = context;
this.manager = manager;
this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
this.sshResponds = sshResponds;
@ -98,6 +103,9 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
logger.debug(">> awaiting installation to finish node(%s)", vmName);
checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh", vmName);
logger.debug(">> awaiting installation of guest additions on vm: %s", vmName);
checkState(new GuestAdditionsInstaller(context).apply(vmName));
logger.debug("<< installation of image complete. Powering down node(%s)", vmName);
ensureMachineHasPowerDown(vmName);

View File

@ -0,0 +1,67 @@
package org.jclouds.virtualbox.predicates;
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
import java.util.concurrent.ExecutionException;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.RunScriptData;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.scriptbuilder.domain.StatementList;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.virtualbox.statements.InstallGuestAdditions;
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.util.concurrent.ListenableFuture;
import com.google.inject.Inject;
@Singleton
public class GuestAdditionsInstaller implements Predicate<String> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final ComputeServiceContext context;
private String vboxVersion;
@Inject
public GuestAdditionsInstaller(ComputeServiceContext context) {
this.context = context;
}
@Override
public boolean apply(String vmName) {
StatementList statementList = prepareStatementList();
ListenableFuture<ExecResponse> execFuture = context.getComputeService().submitScriptOnNode(vmName, statementList,
runAsRoot(true).wrapInInitScript(false));
ExecResponse execResponse = null;
try {
execResponse = execFuture.get();
} catch (InterruptedException e) {
Throwables.propagate(e);
} catch (ExecutionException e) {
Throwables.propagate(e);
}
return execResponse == null ? false : execResponse.getExitCode() == 0;
}
private StatementList prepareStatementList() {
vboxVersion = Iterables.get(Splitter.on('r').split(context.getProviderSpecificContext().getBuildVersion()), 0);
InstallGuestAdditions installGuestAdditions = new InstallGuestAdditions(vboxVersion);
StatementList statementList = new StatementList(Statements.exec(RunScriptData.aptInstallLazyUpgrade("curl")),
installGuestAdditions);
return statementList;
}
}

View File

@ -0,0 +1,10 @@
function installGuestAdditions {
unset OSNAME;
local OSNAME=`lsb_release -d -s | cut -d ' ' -f 1`; shift
if [ $OSNAME = 'Ubuntu' ]
then
echo "OS is Ubuntu"
apt-get -f -y -qq --force-yes install build-essential module-assistant && m-a prepare -i
fi
return 0
}

View File

@ -4,7 +4,7 @@ function installModuleAssistantIfNeeded {
if [ $OSNAME = 'Ubuntu' ]
then
echo "OS is Ubuntu"
apt-get -f -y -qq --force-yes install build-essential module-assistant && m-a prepare -i
apt-get -f -y -qq --force-yes install build-essential module-assistant;
m-a prepare -i
fi
return 0
}

View File

@ -32,7 +32,7 @@ import com.google.common.io.CharStreams;
import com.google.common.io.Resources;
@Test(groups = "unit")
public class GuestAdditionsInstallationTest {
public class InstallGuestAdditionsTest {
@Test
public void testUnix() throws IOException {
InstallGuestAdditions statement = new InstallGuestAdditions("4.1.6");

View File

@ -23,11 +23,16 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
import static org.testng.Assert.assertEquals;
import com.google.inject.Injector;
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.*;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.MasterSpec;
import org.jclouds.virtualbox.domain.NetworkSpec;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists;
import org.jclouds.virtualbox.functions.CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
@ -38,6 +43,7 @@ import org.virtualbox_4_1.StorageBus;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
/**
* @author Andrea Turli

View File

@ -76,7 +76,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(hardDisk)
.attachISO(1, 1, guestAdditionsIso).build();
vmSpecification = VmSpec.builder().id("jclouds#image#create-and-install-vm-test").name(vmName).memoryMB(512).osTypeId("")
vmSpecification = VmSpec.builder().id("jclouds-image-create-and-install-vm-test").name(vmName).memoryMB(512).osTypeId("")
.controller(ideController)
.forceOverwrite(true)
.cleanUpMode(CleanupMode.Full).build();

View File

@ -0,0 +1,143 @@
/**
* 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.predicates;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
import static org.testng.Assert.assertTrue;
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.IsoSpec;
import org.jclouds.virtualbox.domain.MasterSpec;
import org.jclouds.virtualbox.domain.NatAdapter;
import org.jclouds.virtualbox.domain.NetworkSpec;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.LockType;
import org.virtualbox_4_1.StorageBus;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
/**
* @author Andrea Turli
*/
@Test(groups = "live", singleThreaded = true, testName = "GuestAdditionsInstallerLiveTest")
public class GuestAdditionsInstallerLiveTest extends
BaseVirtualBoxClientLiveTest {
private MasterSpec sourceMachineSpec;
@Override
@BeforeClass(groups = "live")
public void setupClient() {
super.setupClient();
String sourceName = VIRTUALBOX_IMAGE_PREFIX
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass()
.getSimpleName());
StorageController ideController = StorageController
.builder()
.name("IDE Controller")
.bus(StorageBus.IDE)
.attachISO(0, 0, operatingSystemIso)
.attachHardDisk(
HardDisk.builder().diskpath(adminDisk).controllerPort(0)
.deviceSlot(1).autoDelete(true).build())
.attachISO(1, 1, guestAdditionsIso).build();
VmSpec sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName)
.osTypeId("").memoryMB(512).cleanUpMode(CleanupMode.Full)
.controller(ideController).forceOverwrite(true).build();
Injector injector = context.utils().injector();
Function<String, String> configProperties = injector
.getInstance(ValueOfConfigurationKeyOrNull.class);
IsoSpec isoSpec = IsoSpec
.builder()
.sourcePath(operatingSystemIso)
.installationScript(
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE)
.replace("HOSTNAME", sourceVmSpec.getVmName())).build();
NetworkSpec networkSpec = //NetworkSpec.builder().build();
NetworkSpec.builder()
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build())
.build();
sourceMachineSpec = MasterSpec.builder().iso(isoSpec).vm(sourceVmSpec).network(networkSpec).build();
}
@Test
public void testGuestAdditionsAreInstalled() throws Exception {
try {
IMachine machine = getVmWithGuestAdditionsInstalled();
assertTrue(machineUtils.lockSessionOnMachineAndApply(machine.getName(), LockType.Shared, new Function<ISession, Boolean>() {
@Override
public Boolean apply(ISession session) {
return session.getMachine().getGuestPropertyValue("/VirtualBox/GuestAdd/Version") != null;
}
}));
} finally {
for (VmSpec spec : ImmutableSet.of(
sourceMachineSpec.getVmSpec())) {
ensureMachineHasPowerDown(spec.getVmName());
undoVm(spec);
}
}
}
private IMachine getVmWithGuestAdditionsInstalled() {
try {
Injector injector = context.utils().injector();
return injector.getInstance(
CreateAndInstallVm.class).apply(
sourceMachineSpec);
} catch (IllegalStateException e) {
// already created
return manager.get().getVBox()
.findMachine(sourceMachineSpec.getVmSpec().getVmId());
}
}
private void ensureMachineHasPowerDown(String vmName) {
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() {
@Override
public Void apply(ISession session) {
IProgress powerDownProgress = session.getConsole().powerDown();
powerDownProgress.waitForCompletion(-1);
return null;
}
});
}
}