Repository: jclouds-examples Updated Branches: refs/heads/master 72c84b1ce -> abc789d58
Adds some jclouds Docker examples that work with getcarina.com Project: http://git-wip-us.apache.org/repos/asf/jclouds-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds-examples/commit/abc789d5 Tree: http://git-wip-us.apache.org/repos/asf/jclouds-examples/tree/abc789d5 Diff: http://git-wip-us.apache.org/repos/asf/jclouds-examples/diff/abc789d5 Branch: refs/heads/master Commit: abc789d58b466b6132dab7e8109a722579c5e5ba Parents: 72c84b1 Author: Zack Shoylev <[email protected]> Authored: Fri Mar 18 16:00:16 2016 -0500 Committer: Zack Shoylev <[email protected]> Committed: Mon Mar 21 15:06:32 2016 -0500 ---------------------------------------------------------------------- rackspace/pom.xml | 6 ++ .../carina/CreateComputeContainer.java | 67 +++++++++++++++ .../rackspace/carina/CreateContainer.java | 87 ++++++++++++++++++++ .../rackspace/carina/ListContainer.java | 50 +++++++++++ .../examples/rackspace/carina/Utils.java | 81 ++++++++++++++++++ 5 files changed, 291 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/abc789d5/rackspace/pom.xml ---------------------------------------------------------------------- diff --git a/rackspace/pom.xml b/rackspace/pom.xml index 71ff927..1446e64 100644 --- a/rackspace/pom.xml +++ b/rackspace/pom.xml @@ -41,6 +41,12 @@ <artifactId>jclouds-sshj</artifactId> <version>${jclouds.version}</version> </dependency> + <dependency> + <groupId>org.apache.jclouds.labs</groupId> + <artifactId>docker</artifactId> + <!-- TODO: Update version to variable jclouds.version on next release --> + <version>2.0.0-SNAPSHOT</version> + </dependency> <!-- Rackspace US dependencies --> <dependency> <groupId>org.apache.jclouds.provider</groupId> http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/abc789d5/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/CreateComputeContainer.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/CreateComputeContainer.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/CreateComputeContainer.java new file mode 100644 index 0000000..3476b9e --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/CreateComputeContainer.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.examples.rackspace.carina; + +import static org.jclouds.compute.predicates.NodePredicates.runningInGroup; + +import java.io.IOException; +import java.util.Set; + +import org.jclouds.compute.ComputeServiceContext; +import org.jclouds.compute.RunNodesException; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.Template; +import org.jclouds.docker.compute.options.DockerTemplateOptions; + +/** + * This example creates a container in Carina using the jclouds compute abstraction + */ + +public class CreateComputeContainer { + + public static void main(String[] args) throws IOException, RunNodesException { + // Get a context with docker that offers the portable ComputeService api + ComputeServiceContext client = Utils.getComputeApiFromCarinaDirectory(args[0]); + + // Carina does not allow privileged mode containers + DockerTemplateOptions dto = new DockerTemplateOptions(); + dto.privileged(false); + + // Use a known sshd image for demonstration purposes: sickp/apline-sshd + Template template = client.getComputeService() + .templateBuilder() + .options(dto) + .imageNameMatches("sickp/alpine-sshd") + .build(); + + // Run a couple nodes accessible via group container + Set<? extends NodeMetadata> nodes = client.getComputeService().createNodesInGroup("jcloudscontainertest", 2, template); + + // Show the nodes + for(NodeMetadata node : nodes) { + System.out.println("Node: " + node.getName()); + } + + // Cleanup + client.getComputeService().destroyNodesMatching(runningInGroup("jcloudscontainertest")); + + // Release resources + client.close(); + } +} http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/abc789d5/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/CreateContainer.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/CreateContainer.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/CreateContainer.java new file mode 100644 index 0000000..e1e6958 --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/CreateContainer.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.examples.rackspace.carina; + +import static org.jclouds.examples.rackspace.carina.Utils.getDockerApiFromCarinaDirectory; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.UUID; + +import org.jclouds.docker.DockerApi; +import org.jclouds.docker.domain.Config; +import org.jclouds.docker.domain.Container; +import org.jclouds.docker.domain.HostConfig; + +import com.google.common.collect.ImmutableList; + +/** + * This example creates a Carina container + * + * To use, create/login at getcarina.com; then create a cluster and download the "get access" zip file. + * Then extract the zip archive to a directory and pass the directory path as a parameter to main. + */ +public class CreateContainer { + + public static void main(String[] args) throws IOException { + + DockerApi dockerApi = getDockerApiFromCarinaDirectory(args[0]); + + /** + * Specifying .publishAllPorts(true) in the HostConfig when *creating* the container is the simplest and + * arguably best way to publish the ports this container will be using. Using .portBindings to specify particular + * ports is somewhat more involved. + * + * However, because of https://github.com/docker/docker/issues/4635, TCP and UDP will be exposed on different + * ports. + */ + Container container = dockerApi.getContainerApi().createContainer("mumble", + Config.builder() + .image("extra/mumble") + .hostConfig( + HostConfig.builder() + .publishAllPorts(true) + .build()) + .env( + ImmutableList.of( + "MAX_USERS=50", + "SERVER_TEXT=Welcome to My Mumble Server", + "SUPW=" + UUID.randomUUID() + )) + .build()); + + String id = container.id(); + + dockerApi.getContainerApi().startContainer(id); + + for(Entry<String, List<Map<String, String>>> portList : dockerApi.getContainerApi().inspectContainer(id).networkSettings().ports().entrySet()) { + for(Map<String, String> port: portList.getValue()) { + System.out.println("Port: " + portList.getKey() + " -> " + port.get("HostIp") + ":" + port.get("HostPort")); + } + } + + // Cleanup + dockerApi.getContainerApi().stopContainer(id); + dockerApi.getContainerApi().removeContainer(id); + + dockerApi.close(); + } +} http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/abc789d5/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/ListContainer.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/ListContainer.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/ListContainer.java new file mode 100644 index 0000000..7500034 --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/ListContainer.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.examples.rackspace.carina; + +import static org.jclouds.examples.rackspace.carina.Utils.getDockerApiFromCarinaDirectory; + +import java.io.IOException; + +import org.jclouds.docker.DockerApi; +import org.jclouds.docker.domain.ContainerSummary; + +/** + * This example lists Carina containers + * + * To use, create/login at getcarina.com; then create a cluster and download the "get access" zip file. + * Then extract the zip archive to a directory and pass the directory path as a parameter to main. + */ +public class ListContainer { + + public static void main(String[] args) throws IOException { + + DockerApi dockerApi = getDockerApiFromCarinaDirectory(args[0]); + + for( ContainerSummary c : dockerApi.getContainerApi().listContainers()) { + System.out.println(c); + } + + dockerApi.close(); + } + + + + +} http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/abc789d5/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/Utils.java ---------------------------------------------------------------------- diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/Utils.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/Utils.java new file mode 100644 index 0000000..cf6f04d --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/carina/Utils.java @@ -0,0 +1,81 @@ +package org.jclouds.examples.rackspace.carina;/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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. + */ + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.Properties; + +import org.jclouds.ContextBuilder; +import org.jclouds.compute.ComputeServiceContext; +import org.jclouds.docker.DockerApi; +import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; +import org.jclouds.sshj.config.SshjSshClientModule; + +import com.google.common.collect.ImmutableSet; +import com.google.common.io.Files; +import com.google.inject.Module; + +public class Utils { + public static DockerApi getDockerApiFromCarinaDirectory(String path) throws IOException { + // docker.ps1 contains the endpoint + String endpoint = "https://" + + Files.readFirstLine(new File(joinPath(path, "docker.ps1")), + Charset.forName("UTF-8")).split("=")[1].replace("\"", "").substring(6); + + // enable logging + Iterable<Module> modules = ImmutableSet.<Module> of(new SLF4JLoggingModule()); + Properties overrides = new Properties(); + + // disable certificate checking for Carina + overrides.setProperty("jclouds.trust-all-certs", "true"); + + return ContextBuilder.newBuilder("docker") + // Use the unencrypted credentials + .credentials(joinPath(path, "cert.pem"), joinPath(path, "key.pem")) + .overrides(overrides) + .endpoint(endpoint) + .modules(modules) + .buildApi(DockerApi.class); + } + + public static ComputeServiceContext getComputeApiFromCarinaDirectory(String path) throws IOException { + // docker.ps1 contains the endpoint + String endpoint = "https://" + + Files.readFirstLine(new File(joinPath(path, "docker.ps1")), + Charset.forName("UTF-8")).split("=")[1].replace("\"", "").substring(6); + + // enable logging and sshj + Iterable<Module> modules = ImmutableSet.<Module> of(new SLF4JLoggingModule(), new SshjSshClientModule()); + Properties overrides = new Properties(); + + // disable certificate checking for Carina + overrides.setProperty("jclouds.trust-all-certs", "true"); + + return ContextBuilder.newBuilder("docker") + .credentials(joinPath(path, "cert.pem"), joinPath(path, "key.pem")) + .modules(modules) + .overrides(overrides) + .endpoint(endpoint) + .buildView(ComputeServiceContext.class); + } + + // Concatenate two different paths + public static String joinPath(String path1, String path2) { + return new File(path1, path2).toString(); + } +}
