Skip to content
Permalink
Browse files
Adds some jclouds Docker examples that work with getcarina.com
  • Loading branch information
zack-shoylev committed Mar 21, 2016
1 parent 72c84b1 commit abc789d58b466b6132dab7e8109a722579c5e5ba
Showing 5 changed files with 291 additions and 0 deletions.
@@ -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>
@@ -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();
}
}
@@ -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();
}
}
@@ -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();
}




}
@@ -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();
}
}

0 comments on commit abc789d

Please sign in to comment.