* 'master' of https://github.com/kakugawa/jclouds:
  WIP: rough code to make test advance
  modify code to parse yaml
  read in byon config via yaml
This commit is contained in:
Adrian Cole 2011-01-25 14:38:26 -08:00
commit d15944c276
6 changed files with 120 additions and 24 deletions

View File

@ -74,6 +74,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
<profiles>

View File

@ -19,6 +19,8 @@
package org.jclouds.byon;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.google.common.base.Objects;
@ -29,16 +31,16 @@ import com.google.common.base.Objects;
* @author Adrian Cole
*/
public class Node {
private String id;
private String description;
private String hostname;
private String osArch;
private String osFamily;
private String osName;
private String osVersion;
private Set<String> tags;
private String username;
private String credential;
public String id;
public String description;
public String hostname;
public String osArch;
public String osFamily;
public String osName;
public String osVersion;
public List<String> tags;
public String username;
public String credential;
public String getId() {
return id;
@ -69,7 +71,10 @@ public class Node {
}
public Set<String> getTags() {
return tags;
Set<String> tagSet = new HashSet<String>();
for (String tag : tags)
tagSet.add(tag);
return tagSet;
}
public String getUsername() {

View File

@ -19,9 +19,14 @@
package org.jclouds.byon.config;
import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Map;
import javax.annotation.Resource;
import javax.inject.Singleton;
import org.jclouds.byon.Node;
@ -29,11 +34,18 @@ import org.jclouds.byon.internal.BYONComputeServiceAdapter;
import org.jclouds.compute.config.JCloudsNativeComputeServiceAdapterContextModule;
import org.jclouds.concurrent.SingleThreaded;
import org.jclouds.location.Provider;
import org.jclouds.logging.Logger;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Provides;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.Loader;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;
/**
*
* @author Adrian Cole
@ -42,6 +54,8 @@ import com.google.inject.Provides;
@SingleThreaded
public class BYONComputeServiceContextModule extends
JCloudsNativeComputeServiceAdapterContextModule<Supplier, Supplier> {
@Resource
protected Logger logger = Logger.NULL;
public BYONComputeServiceContextModule() {
super(Supplier.class, Supplier.class, BYONComputeServiceAdapter.class);
@ -49,15 +63,37 @@ public class BYONComputeServiceContextModule extends
@Provides
@Singleton
Supplier<Map<String, Node>> provideNodeList(@Provider URI uri) {
Supplier<Map<String, Node>> provideNodeList(@Provider final URI uri) {
return new Supplier<Map<String, Node>> (){
@Override
public Map<String, Node> get() {
// TODO parse uri into list, using yaml or something
Constructor constructor = new Constructor(Config.class);
TypeDescription nodeDesc = new TypeDescription(Node.class);
nodeDesc.putListPropertyType("tags", String.class);
constructor.addTypeDescription(nodeDesc);
TypeDescription configDesc = new TypeDescription(Config.class);
configDesc.putMapPropertyType("nodes", String.class, Node.class);
constructor.addTypeDescription(configDesc);
Yaml yaml = new Yaml(new Loader(constructor));
Config config;
try {
URL url = uri.toURL();
InputStream input = url.openStream();
config = (Config)yaml.load(input);
} catch(MalformedURLException e) {
logger.error(e, "URI is not a URL: %s", uri);
return ImmutableMap.<String, Node> of();
} catch(IOException e) {
logger.error(e, "URI could not be read: %s", uri);
return ImmutableMap.<String, Node> of();
}
return config.nodes;
}
};
}

View File

@ -0,0 +1,33 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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.byon.config;
import java.util.Map;
import org.jclouds.byon.Node;
/**
* Type-safe config class for YAML
*
* @author Kelvin Kakugawa
*/
public class Config {
public Map<String, Node> nodes;
}

View File

@ -62,8 +62,12 @@ public class NodeToNodeMetadata implements Function<Node, NodeMetadata> {
builder.location(location.get());
builder.tag(Iterables.get(from.getTags(), 0));
builder
.operatingSystem(new OperatingSystemBuilder().arch(from.getOsArch())
.family(OsFamily.fromValue(from.getOsFamily())).name(from.getOsName()).version(from.getOsVersion())
.operatingSystem(new OperatingSystemBuilder()
.arch(from.getOsArch())
.family(OsFamily.fromValue(from.getOsFamily()))
.name(from.getOsName())
.version(from.getOsVersion())
.description(from.getDescription())
.build());
builder.state(NodeState.RUNNING);
builder.publicAddresses(ImmutableSet.<String> of(from.getHostname()));

View File

@ -34,6 +34,11 @@ import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
//TODO: REMOVE
import com.google.common.base.Predicates;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.ssh.jsch.config.JschSshClientModule;
/**
*
* @author Adrian Cole
@ -47,20 +52,23 @@ public class BYONParseTest {
@BeforeClass
protected void setupCredentials() {
endpoint = System.getProperty("test." + provider + ".endpoint", "file://c:/test.txt");
// endpoint = System.getProperty("test." + provider + ".endpoint", "file://c:/test.txt");
endpoint = System.getProperty("test." + provider + ".endpoint", "file:///Users/kelvin/pg/jclouds/sandbox-apis/byon/src/test/resources/config.yaml");
// NOTE you may not care about identity/credential
identity = System.getProperty("test." + provider + ".identity", "FIXME_IDENTITY");
credential = System.getProperty("test." + provider + ".credential", "FIXME_CREDENTIAL");
// identity = System.getProperty("test." + provider + ".identity", "FIXME_IDENTITY");
identity = System.getProperty("test." + provider + ".identity", "kelvin");
// credential = System.getProperty("test." + provider + ".credential", "FIXME_CREDENTIAL");
credential = System.getProperty("test." + provider + ".credential", "~/.ssh/id_rsa");
}
@Test
public void testNodesParse() {
public void testNodesParse() throws Exception {
ComputeServiceContext context = null;
try {
Properties contextProperties = new Properties();
contextProperties.setProperty("byon.endpoint", endpoint);
context = new ComputeServiceContextFactory().createContext("byon", identity, credential,
ImmutableSet.<Module> of(), contextProperties);
ImmutableSet.<Module> of(new JschSshClientModule()), contextProperties);
assertEquals(context.getProviderSpecificContext().getEndpoint(), URI.create(endpoint));
@ -73,6 +81,11 @@ public class BYONParseTest {
// TODO verify that the node list corresponds correctly to the content at endpoint
context.getComputeService().listNodes();
//TODO: REMOVE
System.out.println(
context.getComputeService().runScriptOnNodesMatching(Predicates.<NodeMetadata>alwaysTrue(), "echo hello")
);
} finally {
if (context != null)
context.close();