Skip to content

Instantly share code, notes, and snippets.

@zack-shoylev
zack-shoylev / gist:5579710
Created May 14, 2013 21:28
Sample jclouds maven pom file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rackspace</groupId>
<artifactId>rackspace</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
@zack-shoylev
zack-shoylev / gist:6238185
Created August 15, 2013 04:06
JCloudsNova.java example for devstack
package org.jclouds.examples.rackspace;
import static com.google.common.io.Closeables.closeQuietly;
import java.io.Closeable;
import java.util.Set;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
@zack-shoylev
zack-shoylev / pom.xml
Last active December 22, 2015 18:59
Download jclouds dependencies
<?xml version="1.0"?>
<!--
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
@zack-shoylev
zack-shoylev / logback.xml
Created September 10, 2013 23:32
jclouds logging xml example
<?xml version="1.0"?>
<!--
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
@zack-shoylev
zack-shoylev / detectExtensions.java
Last active December 22, 2015 19:09
Detects provider-specific extensions
try {
Set<String> zones = nova.getApi().getConfiguredZones();
String zone = Iterables.getFirst(zones, null);
String provider = compute.getContext().toString().contains("rackspace") ?
"Rackspace CloudServers" : "Openstack Nova";
// Use jclouds to check if security groups are supported for this provider
Optional<? extends SecurityGroupApi> securityGroupExt =
nova.getApi().getSecurityGroupExtensionForZone(zone);
@zack-shoylev
zack-shoylev / configureAndStartWebserver.java
Last active December 22, 2015 19:09
Configure and start Apache webserver on jclouds
try {
if("demo:demo".equals(user)) {
// Devstack not configured for public IPs
address = node.getPrivateAddresses().iterator().next();
// GOTCHA: The cirros image is missing a lot of features. It is mostly only viable for testing.
// No bash-shopt: jclouds can't run scripts on it
// No yum/apt/etc... can't install
// No toolchain... can't build
// Very fast boots and good for testing!
@zack-shoylev
zack-shoylev / JCloudsWorkshop.java
Last active December 22, 2015 19:09
Full code for JCloudsWorkshop.java
package org.jclouds.workshop;
import static com.google.common.base.Charsets.UTF_8;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.jclouds.compute.predicates.NodePredicates.inGroup;
import static org.jclouds.scriptbuilder.domain.Statements.exec;
import static org.jclouds.util.Predicates2.retry;
import java.io.BufferedReader;
import java.io.Closeable;
@zack-shoylev
zack-shoylev / JCloudsWorkshop.java
Last active December 22, 2015 19:09
Skeleton for JCloudsWorkshop.java
package org.jclouds.workshop;
import static com.google.common.base.Charsets.UTF_8;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.jclouds.compute.predicates.NodePredicates.inGroup;
import static org.jclouds.scriptbuilder.domain.Statements.exec;
import static org.jclouds.util.Predicates2.retry;
import java.io.BufferedReader;
import java.io.Closeable;
@zack-shoylev
zack-shoylev / init.java
Last active December 22, 2015 19:09
Initialize jclouds example
// Get credentials from a file, if it exists. First line should be "user;password"
if( (new File("credentials.txt")).exists() ) {
BufferedReader br = null;
try {
br = (new BufferedReader(new FileReader("credentials.txt")));
String[] credentials = br.readLine().split(";");
user = credentials[0];
password = credentials[1];
}
@zack-shoylev
zack-shoylev / createNode.java
Last active December 22, 2015 19:09
Create a compute node with jclouds
try {
// Use jclouds to generate a keypair
Map<String,String> keys = SshKeys.generate();
// Get the private key from the keypair map
privateKey = keys.get("private");
Files.write(privateKey, new File("jclouds.pem"), UTF_8);
// Uses security groups if available (determines right approach automatically) to enable SSH and HTTP
TemplateOptions options = compute.templateOptions().inboundPorts(22, 80);