refactored virtualbox classes

This commit is contained in:
Adrian Cole 2012-02-14 23:21:02 +01:00
parent 2da5d297a5
commit e88a32a75d
2 changed files with 12 additions and 51 deletions

View File

@ -1,26 +1,20 @@
package org.jclouds.virtualbox.predicates; 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.annotation.Resource;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.RunScriptData;
import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.scriptbuilder.domain.StatementList;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.virtualbox.statements.InstallGuestAdditions; import org.jclouds.virtualbox.statements.InstallGuestAdditions;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -41,27 +35,11 @@ public class GuestAdditionsInstaller implements Predicate<String> {
@Override @Override
public boolean apply(String vmName) { public boolean apply(String vmName) {
StatementList statementList = prepareStatementList(); vboxVersion = Iterables.get(Splitter.on('r').split(context.getProviderSpecificContext().getBuildVersion()), 0);
ListenableFuture<ExecResponse> execFuture = context.getComputeService().submitScriptOnNode(vmName,
ListenableFuture<ExecResponse> execFuture = context.getComputeService().submitScriptOnNode(vmName, statementList, new InstallGuestAdditions(vboxVersion), RunScriptOptions.NONE);
runAsRoot(true).wrapInInitScript(false)); ExecResponse execResponse = Futures.getUnchecked(execFuture);
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; 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

@ -1,22 +1,16 @@
package org.jclouds.virtualbox.statements; package org.jclouds.virtualbox.statements;
import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
import java.util.concurrent.ExecutionException;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.RunScriptData;
import org.jclouds.compute.callables.RunScriptOnNode.Factory; import org.jclouds.compute.callables.RunScriptOnNode.Factory;
import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.scriptbuilder.domain.StatementList;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
import org.jclouds.virtualbox.domain.ExecutionType; import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.functions.CreateAndRegisterMachineFromIsoIfNotAlreadyExists; import org.jclouds.virtualbox.functions.CreateAndRegisterMachineFromIsoIfNotAlreadyExists;
@ -29,7 +23,7 @@ import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Throwables; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -48,7 +42,6 @@ public class GuestAdditionsInstaller implements Function<String, IMachine> {
// TODO remove this hardcoded value // TODO remove this hardcoded value
private String vboxVersion = "4.1.6"; private String vboxVersion = "4.1.6";
@Inject @Inject
public GuestAdditionsInstaller(ComputeServiceContext context, Supplier<VirtualBoxManager> manager, public GuestAdditionsInstaller(ComputeServiceContext context, Supplier<VirtualBoxManager> manager,
CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists, CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists,
@ -66,26 +59,16 @@ public class GuestAdditionsInstaller implements Function<String, IMachine> {
IMachine vm = manager.get().getVBox().findMachine(vmName); IMachine vm = manager.get().getVBox().findMachine(vmName);
ensureMachineIsLaunched(vmName); ensureMachineIsLaunched(vmName);
InstallGuestAdditions installGuestAdditions = new InstallGuestAdditions(vboxVersion);
StatementList statementList = new StatementList(Statements.exec(RunScriptData.aptInstallLazyUpgrade("curl")),
installGuestAdditions);
NodeMetadata vmMetadata = new IMachineToNodeMetadata().apply(vm); NodeMetadata vmMetadata = new IMachineToNodeMetadata().apply(vm);
ListenableFuture<ExecResponse> execFuture = context.getComputeService().submitScriptOnNode(vmMetadata.getId(), statementList, ListenableFuture<ExecResponse> execFuture = context.getComputeService().submitScriptOnNode(vmMetadata.getId(),
runAsRoot(true).wrapInInitScript(false)); new InstallGuestAdditions(vboxVersion), RunScriptOptions.NONE);
try { Futures.getUnchecked(execFuture);
execFuture.get();
} catch (InterruptedException e) {
Throwables.propagate(e);
} catch (ExecutionException e) {
Throwables.propagate(e);
}
return vm; return vm;
} }
private void ensureMachineIsLaunched(String vmName) { private void ensureMachineIsLaunched(String vmName) {
machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType, "")); machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType, ""));
} }
} }