Skip to content

Instantly share code, notes, and snippets.

@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 / awaitSsh.java
Last active December 22, 2015 19:09
Java method to wait for SSH to become available
private void awaitSsh(String ip) throws TimeoutException {
SocketOpen socketOpen = compute.getContext().utils().injector().getInstance(SocketOpen.class);
Predicate<HostAndPort> socketTester = retry(socketOpen, 300, 5, 5, SECONDS);
socketTester.apply(HostAndPort.fromParts(ip, 22));
}
@zack-shoylev
zack-shoylev / shellscript
Created September 11, 2013 00:44
Make a custom Ubuntu image on devstack
wget http://uec-images.ubuntu.com/precise/current/precise-server-cloudimg-amd64-disk1.img
glance image-create --name ubuntu --disk-format=raw --container-format=bare --file precise-server-cloudimg-amd64-disk1.img
nova boot jclouds-vm --flavor 1 --image <image id> --key-name jclouds --security-groups jclouds-workshop --poll
@zack-shoylev
zack-shoylev / createNodeNova.java
Last active December 22, 2015 19:09
Nova-specific code for creating a node
try {
zone = nova.getApi().getConfiguredZones().iterator().next();
System.out.println("Zone: " + zone);
Map<String,String> keys = SshKeys.generate();
privateKey = keys.get("private");
Files.write(privateKey, new File("jclouds.pem"), UTF_8);
KeyPairApi kpa = nova.getApi().getKeyPairExtensionForZone(zone).get();
kpa.delete("jclouds");
kpa.createWithPublicKey("jclouds", keys.get("public"));
@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);
@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 / 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 / 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 / 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