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;
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.options.RunScriptOptions;
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.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Inject;
@ -41,27 +35,11 @@ public class GuestAdditionsInstaller implements Predicate<String> {
@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);
}
vboxVersion = Iterables.get(Splitter.on('r').split(context.getProviderSpecificContext().getBuildVersion()), 0);
ListenableFuture<ExecResponse> execFuture = context.getComputeService().submitScriptOnNode(vmName,
new InstallGuestAdditions(vboxVersion), RunScriptOptions.NONE);
ExecResponse execResponse = Futures.getUnchecked(execFuture);
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;
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.callables.RunScriptOnNode.Factory;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.options.RunScriptOptions;
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.ssh.SshClient;
import org.jclouds.virtualbox.domain.ExecutionType;
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.Predicate;
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.inject.Inject;
@ -48,7 +42,6 @@ public class GuestAdditionsInstaller implements Function<String, IMachine> {
// TODO remove this hardcoded value
private String vboxVersion = "4.1.6";
@Inject
public GuestAdditionsInstaller(ComputeServiceContext context, Supplier<VirtualBoxManager> manager,
CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists,
@ -66,26 +59,16 @@ public class GuestAdditionsInstaller implements Function<String, IMachine> {
IMachine vm = manager.get().getVBox().findMachine(vmName);
ensureMachineIsLaunched(vmName);
InstallGuestAdditions installGuestAdditions = new InstallGuestAdditions(vboxVersion);
StatementList statementList = new StatementList(Statements.exec(RunScriptData.aptInstallLazyUpgrade("curl")),
installGuestAdditions);
NodeMetadata vmMetadata = new IMachineToNodeMetadata().apply(vm);
ListenableFuture<ExecResponse> execFuture = context.getComputeService().submitScriptOnNode(vmMetadata.getId(), statementList,
runAsRoot(true).wrapInInitScript(false));
try {
execFuture.get();
} catch (InterruptedException e) {
Throwables.propagate(e);
} catch (ExecutionException e) {
Throwables.propagate(e);
}
ListenableFuture<ExecResponse> execFuture = context.getComputeService().submitScriptOnNode(vmMetadata.getId(),
new InstallGuestAdditions(vboxVersion), RunScriptOptions.NONE);
Futures.getUnchecked(execFuture);
return vm;
}
private void ensureMachineIsLaunched(String vmName) {
machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType, ""));
}
}