From d0fd7849e5a976ea3611f2c22cd26b1a2450f1dc Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Sat, 15 Oct 2011 17:01:41 +0200 Subject: [PATCH] Started on StartVboxIfNotAlreadyRunning --- .../StartVBoxIfNotAlreadyRunning.java | 88 +++++++++++++++++++ .../StartVBoxIfNotAlreadyRunningLiveTest.java | 50 +++++++++++ 2 files changed, 138 insertions(+) create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/StartVBoxIfNotAlreadyRunning.java create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/StartVBoxIfNotAlreadyRunningLiveTest.java diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/StartVBoxIfNotAlreadyRunning.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/StartVBoxIfNotAlreadyRunning.java new file mode 100644 index 0000000000..f0bea271b3 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/StartVBoxIfNotAlreadyRunning.java @@ -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 com.google.common.base.Function; +import com.google.common.cache.Cache; +import org.jclouds.compute.ComputeServiceContext; +import org.jclouds.compute.reference.ComputeServiceConstants; +import org.jclouds.domain.Credentials; +import org.jclouds.logging.Logger; +import org.jclouds.net.IPSocket; +import org.jclouds.predicates.InetSocketAddressConnect; +import org.virtualbox_4_1.VirtualBoxManager; + +import javax.annotation.Nullable; +import javax.annotation.Resource; +import javax.inject.Named; + +import java.net.URI; +import java.util.concurrent.ExecutionException; + +import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot; + +public class StartVBoxIfNotAlreadyRunning implements Function { + + @Resource + @Named(ComputeServiceConstants.COMPUTE_LOGGER) + protected Logger logger = Logger.NULL; + private ComputeServiceContext context; + private String hostId; + private Credentials credentials; + + public StartVBoxIfNotAlreadyRunning(ComputeServiceContext context, String hostId, Credentials credentials) { + this.context = context; + this.hostId = hostId; + this.credentials = credentials; + } + + @Override + public VirtualBoxManager apply(@Nullable URI endpoint) { + + // TODO Really create new object here? Should we cache these instead? + VirtualBoxManager manager = VirtualBoxManager.createInstance(hostId); + + if (new InetSocketAddressConnect().apply(new IPSocket(endpoint.getHost(), endpoint.getPort()))) { + manager.connect(endpoint.toASCIIString(), credentials.identity, credentials.credential); + return manager; + } + + logger.debug("disabling password access"); + context.getComputeService().runScriptOnNode(hostId, "VBoxManage setproperty websrvauthlibrary null", runAsRoot(false).wrapInInitScript(false)); + logger.debug("starting vboxwebsrv"); + String vboxwebsrv = "vboxwebsrv -t 10000 -v -b"; + if (isOSX(hostId)) + vboxwebsrv = "cd /Applications/VirtualBox.app/Contents/MacOS/ && " + vboxwebsrv; + + context.getComputeService().runScriptOnNode(hostId, vboxwebsrv, runAsRoot(false).wrapInInitScript(false).blockOnComplete(false).nameTask("vboxwebsrv")); + + manager.connect(endpoint.toASCIIString(), credentials.identity, credentials.credential); + return manager; + } + + private boolean isOSX(String hostId) { + return context.getComputeService().getNodeMetadata(hostId).getOperatingSystem().getDescription().equals( + "Mac OS X"); + } + +} diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/StartVBoxIfNotAlreadyRunningLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/StartVBoxIfNotAlreadyRunningLiveTest.java new file mode 100644 index 0000000000..8b167a408a --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/StartVBoxIfNotAlreadyRunningLiveTest.java @@ -0,0 +1,50 @@ +/* + * * + * * 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 org.jclouds.compute.ComputeServiceContext; +import org.jclouds.domain.Credentials; +import org.testng.annotations.Test; +import org.virtualbox_4_1.SessionState; +import org.virtualbox_4_1.VirtualBoxManager; + +import java.net.URI; + +import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; +import static org.testng.Assert.assertEquals; + +@Test(groups = "live", singleThreaded = true, testName = "startVBoxIfNotAlreadyRunningLiveTest") +public class StartVBoxIfNotAlreadyRunningLiveTest { + + @Test + public void testStartVbox() throws Exception { + Credentials localhostCredentials = new Credentials("toor", "password"); + ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest("hostId", "localhost", "guestId", "localhost", localhostCredentials); + + URI endpointUri = URI.create("http://localhost:18083/"); + + VirtualBoxManager manager = new StartVBoxIfNotAlreadyRunning(localHostContext, "hostId", localhostCredentials).apply(endpointUri); + + assertEquals(manager.getSessionObject().getState(), SessionState.Unlocked); + } + +}