From 69ef3fa7ea922d5128c902611239d3bc5d033e70 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 3 Dec 2010 20:08:37 +0100 Subject: [PATCH] first commit --- sandbox/vsphere/README.txt | 2 + sandbox/vsphere/TODO | 1 + sandbox/vsphere/pom.xml | 138 ++++++++ .../java/org/jclouds/vsphere/Datacenter.java | 56 ++++ .../main/java/org/jclouds/vsphere/Image.java | 56 ++++ .../java/org/jclouds/vsphere/ViConstants.java | 33 ++ .../vsphere/compute/ViComputeService.java | 113 +++++++ .../ViComputeServiceContextBuilder.java | 58 ++++ .../compute/ViComputeServiceContextSpec.java | 43 +++ .../vsphere/compute/ViPropertiesBuilder.java | 58 ++++ .../domain/ViComputeServiceContextModule.java | 151 +++++++++ .../functions/DatacenterToLocation.java | 42 +++ .../functions/LibvirtNodeToLocation.java | 42 +++ .../compute/functions/ViImageToImage.java | 62 ++++ .../functions/VirtualMachineToHardware.java | 98 ++++++ .../VirtualMachineToNodeMetadata.java | 134 ++++++++ .../strategy/ViComputeServiceAdapter.java | 316 ++++++++++++++++++ .../ViComputeServiceContextBuilderTest.java | 33 ++ .../vsphere/compute/ViExperimentLiveTest.java | 97 ++++++ sandbox/vsphere/src/test/resources/log4j.xml | 136 ++++++++ 20 files changed, 1669 insertions(+) create mode 100644 sandbox/vsphere/README.txt create mode 100644 sandbox/vsphere/TODO create mode 100644 sandbox/vsphere/pom.xml create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/Datacenter.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/Image.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/ViConstants.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeService.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeServiceContextBuilder.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeServiceContextSpec.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViPropertiesBuilder.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/domain/ViComputeServiceContextModule.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/DatacenterToLocation.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/LibvirtNodeToLocation.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/ViImageToImage.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/VirtualMachineToHardware.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/VirtualMachineToNodeMetadata.java create mode 100644 sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/strategy/ViComputeServiceAdapter.java create mode 100644 sandbox/vsphere/src/test/java/org/jclouds/vsphere/compute/ViComputeServiceContextBuilderTest.java create mode 100644 sandbox/vsphere/src/test/java/org/jclouds/vsphere/compute/ViExperimentLiveTest.java create mode 100644 sandbox/vsphere/src/test/resources/log4j.xml diff --git a/sandbox/vsphere/README.txt b/sandbox/vsphere/README.txt new file mode 100644 index 0000000000..139597f9cb --- /dev/null +++ b/sandbox/vsphere/README.txt @@ -0,0 +1,2 @@ + + diff --git a/sandbox/vsphere/TODO b/sandbox/vsphere/TODO new file mode 100644 index 0000000000..81960b3b76 --- /dev/null +++ b/sandbox/vsphere/TODO @@ -0,0 +1 @@ +runNodesWithTag: when ask for more than 1 node, cloning step fails cause of concurrent access to the originale virtual disk to be cloned. \ No newline at end of file diff --git a/sandbox/vsphere/pom.xml b/sandbox/vsphere/pom.xml new file mode 100644 index 0000000000..b4ff48cc99 --- /dev/null +++ b/sandbox/vsphere/pom.xml @@ -0,0 +1,138 @@ + + + + + 4.0.0 + + org.jclouds + jclouds-project + 1.0-SNAPSHOT + ../../project/pom.xml + + jclouds-vsphere + jclouds example components for a vijava lib for vSphere + + + trmkrun-ccc,test.trmk-924 + test:///default + 1.0 + FIXME + FIXME + + + + org.jvnet.hudson + vijava + 2120100824 + + + com.jamesmurty.utils + java-xmlbuilder + 0.3 + + + ${project.groupId} + jclouds-core + ${project.version} + test-jar + test + + + ${project.groupId} + jclouds-compute + ${project.version} + + + ${project.groupId} + jclouds-compute + ${project.version} + test-jar + test + + + log4j + log4j + 1.2.14 + test + + + ${project.groupId} + jclouds-log4j + ${project.version} + test + + + ${project.groupId} + jclouds-jsch + ${project.version} + test + + + + + + live + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration + integration-test + + test + + + + + test.libvirt.endpoint + ${test.libvirt.endpoint} + + + test.libvirt.apiversion + ${test.libvirt.apiversion} + + + test.libvirt.identity + ${test.libvirt.identity} + + + test.libvirt.credential + ${test.libvirt.credential} + + + jclouds.compute.blacklist.nodes + ${jclouds.compute.blacklist.nodes} + + + + + + + + + + + + diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/Datacenter.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/Datacenter.java new file mode 100644 index 0000000000..4224d9250e --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/Datacenter.java @@ -0,0 +1,56 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere; + +import com.google.common.base.Objects; + +/** + * This would be replaced with the real java object related to the underlying data center + * + * @author Adrian Cole + */ +public class Datacenter { + + public int id; + public String name; + + public Datacenter(int id, String name) { + this.id = id; + this.name = name; + } + + @Override + public int hashCode() { + return Objects.hashCode(id, name); + } + + @Override + public boolean equals(Object that) { + if (that == null) + return false; + return Objects.equal(this.toString(), that.toString()); + } + + @Override + public String toString() { + return Objects.toStringHelper(this).add("id", id).add("name", name).toString(); + } + +} diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/Image.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/Image.java new file mode 100644 index 0000000000..0a3f5fbe2e --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/Image.java @@ -0,0 +1,56 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere; + +import com.google.common.base.Objects; + +/** + * This would be replaced with the real java object related to the underlying image + * + * @author Adrian Cole + */ +public class Image { + + public int id; + public String name; + + public Image(int id, String name) { + this.id = id; + this.name = name; + } + + @Override + public int hashCode() { + return Objects.hashCode(id, name); + } + + @Override + public boolean equals(Object that) { + if (that == null) + return false; + return Objects.equal(this.toString(), that.toString()); + } + + @Override + public String toString() { + return Objects.toStringHelper(this).add("id", id).add("name", name).toString(); + } + +} diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/ViConstants.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/ViConstants.java new file mode 100644 index 0000000000..d015691241 --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/ViConstants.java @@ -0,0 +1,33 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere; + +/** + * Configuration properties and constants used in libvirt local connections. + * + * @author Adrian Cole + */ +public interface ViConstants { + public static final String PROPERTY_LIBVIRT_DOMAIN_DIR = "jclouds.libvirt.domain.dir"; +} + + +//public static final String PROPERTY_AUTH_TAG = "jclouds.aws.auth.tag"; +//public static final String PROPERTY_HEADER_TAG = "jclouds.aws.header.tag"; \ No newline at end of file diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeService.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeService.java new file mode 100644 index 0000000000..a2531da66f --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeService.java @@ -0,0 +1,113 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute; + +import java.io.StringReader; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutorService; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Provider; +import javax.inject.Singleton; + +import org.jclouds.Constants; +import org.jclouds.collect.Memoized; +import org.jclouds.compute.ComputeServiceContext; +import org.jclouds.compute.domain.Hardware; +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.TemplateBuilder; +import org.jclouds.compute.internal.BaseComputeService; +import org.jclouds.compute.options.TemplateOptions; +import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; +import org.jclouds.compute.strategy.DestroyNodeStrategy; +import org.jclouds.compute.strategy.GetNodeMetadataStrategy; +import org.jclouds.compute.strategy.ListNodesStrategy; +import org.jclouds.compute.strategy.RebootNodeStrategy; +import org.jclouds.compute.strategy.ResumeNodeStrategy; +import org.jclouds.compute.strategy.RunNodesAndAddToSetStrategy; +import org.jclouds.compute.strategy.SuspendNodeStrategy; +import org.jclouds.compute.util.ComputeUtils; +import org.jclouds.domain.Credentials; +import org.jclouds.domain.Location; +import org.xml.sax.InputSource; + +import com.google.common.base.Predicate; +import com.google.common.base.Supplier; +import com.google.common.base.Throwables; +import com.jamesmurty.utils.XMLBuilder; +import com.vmware.vim25.mo.ServiceInstance; + +/** + * + * @author andrea.turli + * + */ +@Singleton +public class ViComputeService extends BaseComputeService { + + private final ServiceInstance client; + + @Inject + protected ViComputeService(ComputeServiceContext context, + Map credentialStore, + @Memoized Supplier> images, + @Memoized Supplier> hardwareProfiles, + @Memoized Supplier> locations, + ListNodesStrategy listNodesStrategy, + GetNodeMetadataStrategy getNodeMetadataStrategy, + RunNodesAndAddToSetStrategy runNodesAndAddToSetStrategy, + RebootNodeStrategy rebootNodeStrategy, + DestroyNodeStrategy destroyNodeStrategy, + ResumeNodeStrategy resumeNodeStrategy, + SuspendNodeStrategy suspendNodeStrategy, + Provider templateBuilderProvider, + Provider templateOptionsProvider, + @Named("NODE_RUNNING") Predicate nodeRunning, + @Named("NODE_TERMINATED") Predicate nodeTerminated, + @Named("NODE_SUSPENDED") Predicate nodeSuspended, ComputeUtils utils, + Timeouts timeouts, + @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor, + ServiceInstance client) { + super(context, credentialStore, images, hardwareProfiles, locations, + listNodesStrategy, getNodeMetadataStrategy, + runNodesAndAddToSetStrategy, rebootNodeStrategy, destroyNodeStrategy, + resumeNodeStrategy, suspendNodeStrategy, templateBuilderProvider, + templateOptionsProvider, nodeRunning, nodeTerminated, nodeSuspended, + utils, timeouts, executor); + + this.client = client; + + } + + @Override + public void destroyNode(String id) { + super.destroyNode(id); + } + + protected T propogate(Exception e) { + Throwables.propagate(e); + assert false; + return null; + } + +} diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeServiceContextBuilder.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeServiceContextBuilder.java new file mode 100644 index 0000000000..fe6f3b1678 --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeServiceContextBuilder.java @@ -0,0 +1,58 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute; + +import static org.jclouds.vsphere.ViConstants.PROPERTY_LIBVIRT_DOMAIN_DIR; + +import java.util.List; +import java.util.Properties; + +import org.jclouds.compute.StandaloneComputeServiceContextBuilder; +import org.jclouds.compute.config.StandaloneComputeServiceContextModule; +import org.jclouds.vsphere.Datacenter; +import org.jclouds.vsphere.Image; +import org.jclouds.vsphere.compute.domain.ViComputeServiceContextModule; + +import com.google.inject.Module; +import com.vmware.vim25.mo.VirtualMachine; + +/** + * + * @author Adrian Cole + */ +public class ViComputeServiceContextBuilder extends StandaloneComputeServiceContextBuilder { + + public ViComputeServiceContextBuilder(Properties props) { + super(props); + + if (!properties.containsKey(PROPERTY_LIBVIRT_DOMAIN_DIR)) + properties.setProperty(PROPERTY_LIBVIRT_DOMAIN_DIR, "/etc/libvirt/qemu"); + } + + @Override + protected void addContextModule(List modules) { + modules.add(createContextModule()); + } + + public StandaloneComputeServiceContextModule createContextModule() { + return new ViComputeServiceContextModule(); + } + +} diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeServiceContextSpec.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeServiceContextSpec.java new file mode 100644 index 0000000000..3108857787 --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViComputeServiceContextSpec.java @@ -0,0 +1,43 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute; + +import org.jclouds.PropertiesBuilder; +import org.jclouds.compute.ComputeService; +import org.jclouds.rest.RestContextSpec; + +import com.google.common.collect.ImmutableSet; +import com.google.inject.Module; + +/** + * @author Adrian Cole + */ +public class ViComputeServiceContextSpec extends RestContextSpec { + + @SuppressWarnings("unchecked") + public ViComputeServiceContextSpec(String endpoint, String identity, String credential, Iterable modules) { + super("vsphere", endpoint, "1", identity, credential, ComputeService.class, ComputeService.class, + PropertiesBuilder.class, (Class) ViComputeServiceContextBuilder.class, modules); + } + + public ViComputeServiceContextSpec(String endpoint, String identity, String credential) { + this(endpoint, identity, credential, ImmutableSet.of()); + } +} \ No newline at end of file diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViPropertiesBuilder.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViPropertiesBuilder.java new file mode 100644 index 0000000000..4089ca9b9c --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/ViPropertiesBuilder.java @@ -0,0 +1,58 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute; + +import static org.jclouds.Constants.PROPERTY_API_VERSION; +import static org.jclouds.Constants.PROPERTY_ENDPOINT; +import static org.jclouds.vsphere.ViConstants.PROPERTY_LIBVIRT_DOMAIN_DIR; +import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_NODE_SUSPENDED; + +import java.util.Properties; + +import org.jclouds.PropertiesBuilder; + +/** + * Builds properties used in Libvirt Clients + * + * @author Andrea Turli + */ +public class ViPropertiesBuilder extends PropertiesBuilder { + @Override + protected Properties defaultProperties() { + Properties properties = super.defaultProperties(); + properties.setProperty(PROPERTY_LIBVIRT_DOMAIN_DIR, "/etc/libvirt/qemu"); + + properties.setProperty(PROPERTY_TIMEOUT_NODE_SUSPENDED, 120 * 1000 + ""); + // auth fail sometimes happens in EC2, as the rc.local script that injects the + // authorized key executes after ssh has started + properties.setProperty("jclouds.ssh.max_retries", "7"); + properties.setProperty("jclouds.ssh.retryable_messages", + "Auth fail,invalid data,End of IO Stream Read,Connection reset,socket is not established"); + return properties; + } + + public ViPropertiesBuilder(Properties properties) { + super(properties); + } + + public ViPropertiesBuilder() { + super(); + } +} diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/domain/ViComputeServiceContextModule.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/domain/ViComputeServiceContextModule.java new file mode 100644 index 0000000000..1e811157c6 --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/domain/ViComputeServiceContextModule.java @@ -0,0 +1,151 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute.domain; + +import static org.jclouds.vsphere.ViConstants.PROPERTY_LIBVIRT_DOMAIN_DIR; + +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.net.MalformedURLException; +import java.net.URI; +import java.rmi.RemoteException; +import java.util.Collection; + +import javax.inject.Named; +import javax.inject.Singleton; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.jclouds.Constants; +import org.jclouds.compute.ComputeServiceAdapter; +import org.jclouds.compute.config.StandaloneComputeServiceContextModule; +import org.jclouds.compute.domain.Hardware; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.TemplateBuilder; +import org.jclouds.compute.suppliers.DefaultLocationSupplier; +import org.jclouds.domain.Location; +import org.jclouds.rest.annotations.Provider; +import org.jclouds.vsphere.Datacenter; +import org.jclouds.vsphere.Image; +import org.jclouds.vsphere.compute.functions.DatacenterToLocation; +import org.jclouds.vsphere.compute.functions.ViImageToImage; +import org.jclouds.vsphere.compute.functions.VirtualMachineToHardware; +import org.jclouds.vsphere.compute.functions.VirtualMachineToNodeMetadata; +import org.jclouds.vsphere.compute.strategy.ViComputeServiceAdapter; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import com.google.common.base.Charsets; +import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.collect.Iterables; +import com.google.common.io.Files; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.Provides; +import com.google.inject.TypeLiteral; +import com.google.inject.name.Names; +import com.jamesmurty.utils.XMLBuilder; +import com.vmware.vim25.mo.ServiceInstance; +import com.vmware.vim25.mo.VirtualMachine; + +/** + * + * @author Adrian Cole + */ +public class ViComputeServiceContextModule extends +StandaloneComputeServiceContextModule { + @Override + protected void configure() { + super.configure(); + bind(new TypeLiteral>() { + }).to(ViComputeServiceAdapter.class); + bind(new TypeLiteral>() { + }).to(DefaultLocationSupplier.class); + bind(new TypeLiteral>() { + }).to(VirtualMachineToNodeMetadata.class); + bind(new TypeLiteral>() { + }).to(ViImageToImage.class); + bind(new TypeLiteral>() { + }).to(VirtualMachineToHardware.class); + bind(new TypeLiteral>() { + }).to(DatacenterToLocation.class); + } + + @Provides + @Singleton + protected ServiceInstance createConnection(@Provider URI endpoint, @Named(Constants.PROPERTY_IDENTITY) String identity, + @Named(Constants.PROPERTY_CREDENTIAL) String credential) throws RemoteException, MalformedURLException { + System.out.println(endpoint); + System.out.println(identity); + System.out.println(credential); + return new ServiceInstance(endpoint.toURL(), identity, credential, true); + } + + @Override + protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { + String domainDir = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_LIBVIRT_DOMAIN_DIR))); + String hardwareId = searchForHardwareIdInDomainDir(domainDir); + String image = searchForImageIdInDomainDir(domainDir); + return template.hardwareId(hardwareId).imageId(image) ; + } + + + private String searchForImageIdInDomainDir(String domainDir) { + // TODO + return "1"; + } + + @SuppressWarnings("unchecked") + private String searchForHardwareIdInDomainDir(String domainDir) { + + Collection xmlDomains = FileUtils.listFiles( new File(domainDir), new WildcardFileFilter("*.xml"), null); + String uuid = ""; + try { + String fromXML = Files.toString(Iterables.get(xmlDomains, 0), Charsets.UTF_8); + XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML))); + uuid = builder.xpathFind("/domain/uuid").getElement().getTextContent(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (XPathExpressionException e) { + e.printStackTrace(); + } + return uuid; + } + + /* + * Map regions = newLinkedHashMap(); + for (String region : Splitter.on(',').split(regionString)) { + regions.put( + region, + URI.create(injector.getInstance(Key.get(String.class, + Names.named(Constants.PROPERTY_ENDPOINT + "." + region))))); + } + return regions; + */ + +} \ No newline at end of file diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/DatacenterToLocation.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/DatacenterToLocation.java new file mode 100644 index 0000000000..e949cde848 --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/DatacenterToLocation.java @@ -0,0 +1,42 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute.functions; + +import javax.inject.Singleton; + +import org.jclouds.domain.Location; +import org.jclouds.domain.LocationScope; +import org.jclouds.domain.internal.LocationImpl; +import org.jclouds.vsphere.Datacenter; + +import com.google.common.base.Function; + +/** + * @author Adrian Cole + */ +@Singleton +public class DatacenterToLocation implements Function { + + @Override + public Location apply(Datacenter from) { + return new LocationImpl(LocationScope.ZONE, from.id + "", from.name, null); + } + +} diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/LibvirtNodeToLocation.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/LibvirtNodeToLocation.java new file mode 100644 index 0000000000..04d4a80ae1 --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/LibvirtNodeToLocation.java @@ -0,0 +1,42 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute.functions; + +import javax.inject.Singleton; + +import org.jclouds.domain.Location; +import org.jclouds.domain.LocationScope; +import org.jclouds.domain.internal.LocationImpl; +import org.jclouds.vsphere.Datacenter; + +import com.google.common.base.Function; + +/** + * @author Adrian Cole + */ +@Singleton +public class LibvirtNodeToLocation implements Function { + + @Override + public Location apply(Datacenter from) { + return new LocationImpl(LocationScope.ZONE, from.id + "", from.name, null); + } + +} diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/ViImageToImage.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/ViImageToImage.java new file mode 100644 index 0000000000..0d7345de7d --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/ViImageToImage.java @@ -0,0 +1,62 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute.functions; + +import javax.annotation.Resource; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.ImageBuilder; +import org.jclouds.compute.domain.OperatingSystemBuilder; +import org.jclouds.compute.domain.OsFamily; +import org.jclouds.compute.reference.ComputeServiceConstants; +import org.jclouds.logging.Logger; + +import com.google.common.base.Function; + +/** + * @author Adrian Cole + */ +@Singleton +public class ViImageToImage implements Function { + @Resource + @Named(ComputeServiceConstants.COMPUTE_LOGGER) + protected Logger logger = Logger.NULL; + + @Override + public Image apply(org.jclouds.vsphere.Image from) { + + ImageBuilder builder = new ImageBuilder(); + builder.ids(from.id + ""); + builder.name(from.name); + builder.description(from.name); + + OsFamily family = null; + try { + family = OsFamily.fromValue(from.name); + builder.operatingSystem(new OperatingSystemBuilder().name(from.name).family(family).description(from.name).build()); + } catch (IllegalArgumentException e) { + logger.debug("<< didn't match os(%s)", from); + } + return builder.build(); + } + +} diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/VirtualMachineToHardware.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/VirtualMachineToHardware.java new file mode 100644 index 0000000000..33f75aa756 --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/VirtualMachineToHardware.java @@ -0,0 +1,98 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute.functions; + +import java.io.IOException; +import java.io.StringReader; +import java.util.List; + +import javax.inject.Singleton; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import org.jclouds.compute.domain.Hardware; +import org.jclouds.compute.domain.HardwareBuilder; +import org.jclouds.compute.domain.Processor; +import org.jclouds.compute.domain.Volume; +import org.jclouds.compute.domain.internal.VolumeImpl; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import com.google.common.base.Function; +import com.google.common.base.Throwables; +import com.google.common.collect.Lists; +import com.jamesmurty.utils.XMLBuilder; +import com.vmware.vim25.mo.VirtualMachine; + +/** + * @author Adrian Cole + */ +@Singleton +public class VirtualMachineToHardware implements Function { + + @Override + public Hardware apply(VirtualMachine from) { + HardwareBuilder builder = new HardwareBuilder(); + + builder.id(from.getMOR().get_value() + ""); + builder.providerId(from.getMOR().get_value() + ""); + builder.name(from.getName()); + List processors = Lists.newArrayList(); + for (int i = 0; i < from.getConfig().getHardware().getNumCPU(); i++) { + processors.add(new Processor(i + 1, 1)); + } + builder.processors(processors); + + builder.ram((int) from.getConfig().getHardware().getMemoryMB()); + List volumes = Lists.newArrayList(); + + /* + XMLBuilder xmlBuilder = XMLBuilder.parse(new InputSource(new StringReader(from.getXMLDesc(0)))); + Document doc = xmlBuilder.getDocument(); + XPathExpression expr = XPathFactory.newInstance().newXPath().compile("//devices/disk[@device='disk']/source/@file"); + NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); + String diskFileName = nodes.item(0).getNodeValue(); + for (int i = 0; i < nodes.getLength(); i++) { + StorageVol storageVol = from.getConnect().storageVolLookupByPath(diskFileName); + String id = storageVol.getKey(); + float size = new Long(storageVol.getInfo().capacity).floatValue(); + volumes.add(new VolumeImpl(id, Volume.Type.LOCAL, size, null, true, false)); + } + */ + + // TODO + builder.volumes((List) volumes); + Float size = new Float(21345); + String id = "dglffdbdflmb"; + volumes.add(new VolumeImpl(id, Volume.Type.LOCAL, size, null, true, false)); + return builder.build(); + } + + protected T propagate(Exception e) { + Throwables.propagate(e); + assert false; + return null; + } +} \ No newline at end of file diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/VirtualMachineToNodeMetadata.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/VirtualMachineToNodeMetadata.java new file mode 100644 index 0000000000..8a0964a7fa --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/functions/VirtualMachineToNodeMetadata.java @@ -0,0 +1,134 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute.functions; + +import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.compute.util.ComputeServiceUtils.parseTagFromName; + +import java.util.Map; +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.collect.FindResourceInSet; +import org.jclouds.collect.Memoized; +import org.jclouds.compute.domain.Hardware; +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.NodeMetadataBuilder; +import org.jclouds.compute.domain.NodeState; +import org.jclouds.compute.domain.OperatingSystemBuilder; +import org.jclouds.domain.Credentials; +import org.jclouds.domain.Location; + +import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableMap; +import com.vmware.vim25.VirtualMachinePowerState; +import com.vmware.vim25.mo.VirtualMachine; + +/** + * @author Adrian Cole + */ +@Singleton +public class VirtualMachineToNodeMetadata implements Function { + + public static final Map domainStateToNodeState = + ImmutableMap. builder() + .put(VirtualMachinePowerState.poweredOn, NodeState.RUNNING)// + //.put(VirtualMachinePowerState.suspended, NodeState.PENDING)// + .put(VirtualMachinePowerState.suspended, NodeState.SUSPENDED)// + .put(VirtualMachinePowerState.poweredOff, NodeState.TERMINATED)// + .build(); + + private final Function findHardwareForDomain; + private final FindLocationForDomain findLocationForDomain; + private final FindImageForDomain findImageForDomain; + private final Map credentialStore; + + @Inject + VirtualMachineToNodeMetadata(Map credentialStore, Function findHardwareForDomain, + FindLocationForDomain findLocationForDomain, FindImageForDomain findImageForDomain) { + this.credentialStore = checkNotNull(credentialStore, "credentialStore"); + this.findHardwareForDomain = checkNotNull(findHardwareForDomain, "findHardwareForDomain"); + this.findLocationForDomain = checkNotNull(findLocationForDomain, "findLocationForDomain"); + this.findImageForDomain = checkNotNull(findImageForDomain, "findImageForDomain"); + } + + @Override + public NodeMetadata apply(VirtualMachine from) { + + // convert the result object to a jclouds NodeMetadata + NodeMetadataBuilder builder = new NodeMetadataBuilder(); + try { + builder.id(from.getMOR().get_value() + ""); + builder.providerId(from.getConfig().getLocationId() + ""); + builder.name(from.getName()); + builder.location(findLocationForDomain.apply(from)); + builder.tag(parseTagFromName(from.getName())); + + builder.operatingSystem(new OperatingSystemBuilder().description(from.getConfig().getGuestFullName()).build()); + builder.hardware(findHardwareForDomain.apply(from)); + + builder.state(domainStateToNodeState.get(from.getRuntime().getPowerState())); + // builder.publicAddresses(ImmutableSet. of(from.publicAddress)); + // builder.privateAddresses(ImmutableSet. of(from.privateAddress)); + builder.credentials(credentialStore.get("node#" + from.getMOR().get_value())); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return builder.build(); + } + + @Singleton + public static class FindImageForDomain extends FindResourceInSet { + + @Inject + public FindImageForDomain(@Memoized Supplier> hardware) { + super(hardware); + } + + @Override + public boolean matches(VirtualMachine from, Image input) { + // TODO + // return input.getProviderId().equals(from.imageId + ""); + return true; + } + } + + @Singleton + public static class FindLocationForDomain extends FindResourceInSet { + + @Inject + public FindLocationForDomain(@Memoized Supplier> hardware) { + super(hardware); + } + + @Override + public boolean matches(VirtualMachine from, Location input) { + // TODO + // return input.getId().equals(from.datacenter + ""); + return true; + } + } +} diff --git a/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/strategy/ViComputeServiceAdapter.java b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/strategy/ViComputeServiceAdapter.java new file mode 100644 index 0000000000..82bbdffac3 --- /dev/null +++ b/sandbox/vsphere/src/main/java/org/jclouds/vsphere/compute/strategy/ViComputeServiceAdapter.java @@ -0,0 +1,316 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute.strategy; + +import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.vsphere.ViConstants.PROPERTY_LIBVIRT_DOMAIN_DIR; + +import java.rmi.RemoteException; +import java.util.List; +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.compute.ComputeService; +import org.jclouds.compute.ComputeServiceAdapter; +import org.jclouds.compute.domain.Template; +import org.jclouds.domain.Credentials; +import org.jclouds.vsphere.Datacenter; +import org.jclouds.vsphere.Image; + +import com.google.common.base.Throwables; +import com.google.common.collect.Lists; +import com.google.inject.name.Named; +import com.vmware.vim25.InvalidProperty; +import com.vmware.vim25.RuntimeFault; +import com.vmware.vim25.mo.InventoryNavigator; +import com.vmware.vim25.mo.ManagedEntity; +import com.vmware.vim25.mo.ServiceInstance; +import com.vmware.vim25.mo.VirtualMachine; + +/** + * defines the connection between the {@link Libvirt} implementation and the jclouds + * {@link ComputeService} + * + */ +@Singleton +public class ViComputeServiceAdapter implements ComputeServiceAdapter { + + private final ServiceInstance client; + + @Inject + public ViComputeServiceAdapter(ServiceInstance client) { + this.client = checkNotNull(client, "client"); + } + + @Override + public VirtualMachine runNodeWithTagAndNameAndStoreCredentials(String tag, + String name, Template template, + Map credentialStore) { + // TODO Auto-generated method stub + return null; + } + + /* + @Override + public Domain runNodeWithTagAndNameAndStoreCredentials(String tag, String name, Template template, + Map credentialStore) { + try { + String domainName = tag; + Domain domain = client.domainLookupByName(domainName); + XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(domain.getXMLDesc(0)))); + Document doc = builder.getDocument(); + String xpathString = "//devices/disk[@device='disk']/source/@file"; + XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xpathString); + NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); + String diskFileName = nodes.item(0).getNodeValue(); + StorageVol storageVol = client.storageVolLookupByPath(diskFileName); + + // cloning volume + String poolName = storageVol.storagePoolLookupByVolume().getName(); + StoragePool storagePool = client.storagePoolLookupByName(poolName); + StorageVol clonedVol = null; + boolean cloned = false; + int retry = 0; + while(!cloned && retry<10) { + try { + clonedVol = cloneVolume(storagePool, storageVol); + cloned = true; + } catch (LibvirtException e) { + retry++; + Thread.sleep(1000); + } + } + // define Domain + String xmlFinal = generateClonedDomainXML(domain.getXMLDesc(0), clonedVol); + Domain newDomain = client.domainDefineXML(xmlFinal); + newDomain.create(); + // store the credentials so that later functions can use them + credentialStore.put(domain.getUUIDString() + "", new Credentials("identity", "credential")); + return newDomain; + } catch (LibvirtException e) { + return propogate(e); + } catch (Exception e) { + return propogate(e); + } + } + */ + + @Override + public Iterable listHardwareProfiles() { + return listNodes(); + } + + @Override + public Iterable listImages() { + /* + int i = 1; + try { + String[] domains = client.listDefinedDomains(); + List images = Lists.newArrayList(); + for (String domainName : domains) { + images.add(new Image(i++, domainName)); + } + return images; + } catch (Exception e) { + return propogate(e); + } + */ + return null; + } + + @Override + public Iterable listNodes() { + try { + ManagedEntity[] vmEntities = new InventoryNavigator(client.getRootFolder()).searchManagedEntities("VirtualMachine"); + List vms = Lists.newArrayList(); + for (ManagedEntity entity : vmEntities) { + System.out.println(entity.getName()); + vms.add((VirtualMachine) entity); + } + return vms; + } catch (InvalidProperty e) { + return propogate(e); + } catch (RuntimeFault e) { + return propogate(e); + } catch (RemoteException e) { + return propogate(e); + } + + } + + @Override + public Iterable listLocations() { + ManagedEntity[] datacenterEntities; + try { + datacenterEntities = new InventoryNavigator(client.getRootFolder()).searchManagedEntities("Datacenter"); + List datacenters = Lists.newArrayList(); + for (int i = 0; i< datacenterEntities.length; i++) { + datacenters.add(new Datacenter(i, datacenterEntities[i].getName())); + } + return datacenters; + } catch (InvalidProperty e) { + return propogate(e); + } catch (RuntimeFault e) { + return propogate(e); + } catch (RemoteException e) { + return propogate(e); + } + + } + + @Override + public VirtualMachine getNode(String id) { + VirtualMachine vm = null; + /* + try { + d = client.domainLookupByUUIDString(id); + } catch (LibvirtException e) { + if (e.getMessage().indexOf("Domain not found: no domain with matching uuid") != -1) + return null; + propogate(e); + } + */ + return vm; + } + + @Override + public void destroyNode(String id) { + /* + try { + client.domainLookupByUUIDString(id).destroy(); + + XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader( + client.domainLookupByUUIDString(id).getXMLDesc(0) + ))); + String diskFileName = builder.xpathFind("//devices/disk[@device='disk']/source").getElement().getAttribute("file"); + StorageVol storageVol = client.storageVolLookupByPath(diskFileName); + storageVol.delete(0); + client.domainLookupByUUIDString(id).undefine(); + + } catch (LibvirtException e) { + propogate(e); + } catch (Exception e) { + propogate(e); + } + */ + } + + @Override + public void rebootNode(String id) { + /* + try { + client.domainLookupByUUIDString(id).reboot(0); + } catch (LibvirtException e) { + propogate(e); + } + */ + } + + @Override + public void resumeNode(String id) { + /* + try { + client.domainLookupByUUIDString(id).resume(); + } catch (LibvirtException e) { + propogate(e); + } + */ + } + + @Override + public void suspendNode(String id) { + /* + try { + client.domainLookupByUUIDString(id).suspend(); + } catch (LibvirtException e) { + propogate(e); + } + */ + } + + protected T propogate(Exception e) { + Throwables.propagate(e); + assert false; + return null; + } + + /* + private static StorageVol cloneVolume(StoragePool storagePool, StorageVol from) throws LibvirtException, + XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException { + return storagePool.storageVolCreateXMLFrom(generateClonedVolumeXML(from.getXMLDesc(0)), from, 0); + } + + private static String generateClonedVolumeXML(String fromXML) throws ParserConfigurationException, SAXException, + IOException, XPathExpressionException, TransformerException { + + Properties outputProperties = generateOutputXMLProperties(); + XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML))); + String nodeNamingConvention = "%s-%s"; + String tag = "-clone"; + String suffix = String.format(nodeNamingConvention, tag, Integer.toHexString(new SecureRandom().nextInt(4095))); + builder.xpathFind("//volume/name").t(suffix); + builder.xpathFind("//volume/key").t(suffix); + builder.xpathFind("//volume/target/path").t(suffix); + + return builder.asString(outputProperties); + } + + private static String generateClonedDomainXML(String fromXML, StorageVol clonedVol) throws ParserConfigurationException, SAXException, + IOException, XPathExpressionException, TransformerException, LibvirtException { + + Properties outputProperties = generateOutputXMLProperties(); + + XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML))); + + String nodeNamingConvention = "%s-%s"; + String tag = "-clone"; + String suffix = String.format(nodeNamingConvention, tag, Integer.toHexString(new SecureRandom().nextInt(4095))); + builder.xpathFind("//domain/name").t(suffix); + // change uuid domain + Element oldChild = builder.xpathFind("//domain/uuid").getElement(); + Node newNode = oldChild.cloneNode(true); + newNode.getFirstChild().setNodeValue(UUID.randomUUID().toString()); + builder.getDocument().getDocumentElement().replaceChild(newNode, oldChild); + + //String fromVolPath = builder.xpathFind("//domain/devices/disk/source").getElement().getAttribute("file"); + builder.xpathFind("//domain/devices/disk/source").a("file", clonedVol.getPath()); + // generate valid MAC address + String fromMACaddress = builder.xpathFind("//domain/devices/interface/mac").getElement().getAttribute("address"); + String lastMACoctet = Integer.toHexString(new SecureRandom().nextInt(255)); + builder.xpathFind("//domain/devices/interface/mac").a("address", + fromMACaddress.substring(0, fromMACaddress.lastIndexOf(":")+1) + lastMACoctet + ); + return builder.asString(outputProperties); + } + + private static Properties generateOutputXMLProperties() { + Properties outputProperties = new Properties(); + // Explicitly identify the output as an XML document + outputProperties.put(javax.xml.transform.OutputKeys.METHOD, "xml"); + // Pretty-print the XML output (doesn't work in all cases) + outputProperties.put(javax.xml.transform.OutputKeys.INDENT, "yes"); + // Get 2-space indenting when using the Apache transformer + outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "2"); + return outputProperties; + } + */ +} \ No newline at end of file diff --git a/sandbox/vsphere/src/test/java/org/jclouds/vsphere/compute/ViComputeServiceContextBuilderTest.java b/sandbox/vsphere/src/test/java/org/jclouds/vsphere/compute/ViComputeServiceContextBuilderTest.java new file mode 100644 index 0000000000..5c507a97a9 --- /dev/null +++ b/sandbox/vsphere/src/test/java/org/jclouds/vsphere/compute/ViComputeServiceContextBuilderTest.java @@ -0,0 +1,33 @@ +package org.jclouds.vsphere.compute; + +import static org.testng.Assert.assertNotNull; + +import java.util.Properties; + +import org.jclouds.compute.ComputeServiceContext; +import org.jclouds.compute.ComputeServiceContextFactory; +import org.jclouds.vsphere.compute.ViComputeServiceContextBuilder; +import org.jclouds.vsphere.compute.ViComputeServiceContextSpec; +import org.testng.annotations.Test; + +/** + * + * @author andrea.turli + * + */ +@Test(groups = "unit") +public class ViComputeServiceContextBuilderTest { + + @Test + public void testCreateContextModule() { + assertNotNull(new ViComputeServiceContextBuilder(new Properties()).createContextModule()); + } + + @Test + public void testCanBuildWithComputeService() { + ComputeServiceContext context = new ComputeServiceContextFactory() + .createContext(new ViComputeServiceContextSpec("https://10.38.102.196/sdk", "Administrator", "41.U17Sh")); + context.getComputeService().listNodes(); + context.close(); + } +} diff --git a/sandbox/vsphere/src/test/java/org/jclouds/vsphere/compute/ViExperimentLiveTest.java b/sandbox/vsphere/src/test/java/org/jclouds/vsphere/compute/ViExperimentLiveTest.java new file mode 100644 index 0000000000..3674fe6563 --- /dev/null +++ b/sandbox/vsphere/src/test/java/org/jclouds/vsphere/compute/ViExperimentLiveTest.java @@ -0,0 +1,97 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.vsphere.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Set; + +import org.jclouds.compute.ComputeServiceContext; +import org.jclouds.compute.ComputeServiceContextFactory; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.Template; +import org.jclouds.vsphere.compute.ViComputeServiceContextSpec; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * + * @author Adrian Cole + */ +@Test(groups = "live", testName = "vsphere.ViExperimentLiveTest") +public class ViExperimentLiveTest { + protected String provider = "vsphere"; + protected String identity; + protected String credential; + protected String endpoint; + protected String apiversion; + + @BeforeClass + protected void setupCredentials() { + identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider + ".identity"); + credential = System.getProperty("test." + provider + ".credential"); + endpoint = System.getProperty("test." + provider + ".endpoint"); + apiversion = System.getProperty("test." + provider + ".apiversion"); + } + + @Test + public void testAndExperiment() { + ComputeServiceContext context = null; + try { + context = new ComputeServiceContextFactory().createContext(new ViComputeServiceContextSpec( + endpoint, identity, credential)); + context.getComputeService().listNodes(); + + + /* + * /* System.out.println("images " + context.getComputeService().listImages()); + * System.out.println("nodes " + context.getComputeService().listNodes()); + * System.out.println("hardware profiles " + + * context.getComputeService().listHardwareProfiles()); + */ + + +/* Template defaultTemplate = context.getComputeService().templateBuilder() + .hardwareId("d106ae67-5a1b-8f91-b311-83c93bcb0a1f").imageId("1") //.locationId("") + .build();*/ + + + /* + * We will probably make a default template out of properties at some point You can control + * the default template via overriding a method in standalonecomputeservicexontextmodule + */ + + Set nodeMetadataSet = context.getComputeService().runNodesWithTag("tty", 1); + for (NodeMetadata nodeMetadata : nodeMetadataSet) { + /* + * context.getComputeService().suspendNode(nodeMetadata.getId()); + * context.getComputeService().resumeNode(nodeMetadata.getId()); + */ + context.getComputeService().destroyNode(nodeMetadata.getId()); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (context != null) + context.close(); + } + } + +} \ No newline at end of file diff --git a/sandbox/vsphere/src/test/resources/log4j.xml b/sandbox/vsphere/src/test/resources/log4j.xml new file mode 100644 index 0000000000..a3b1e56934 --- /dev/null +++ b/sandbox/vsphere/src/test/resources/log4j.xml @@ -0,0 +1,136 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file