Skip to content

Instantly share code, notes, and snippets.

@vladimir-bukhtoyarov
Last active May 9, 2017 17:27
Show Gist options
  • Save vladimir-bukhtoyarov/baadaea2c734e302730652a6848f337d to your computer and use it in GitHub Desktop.
Save vladimir-bukhtoyarov/baadaea2c734e302730652a6848f337d to your computer and use it in GitHub Desktop.
Nanocloud simple example
<dependency>
<groupId>org.gridkit.lab</groupId>
<artifactId>nanocloud</artifactId>
<version>0.8.8</version>
</dependency>
<dependency>
<groupId>org.gridkit.lab</groupId>
<artifactId>telecontrol-ssh</artifactId>
<version>0.8.8</version>
</dependency>
import org.gridkit.nanocloud.Cloud;
import org.gridkit.nanocloud.CloudFactory;
import org.gridkit.nanocloud.RemoteNode;
import org.gridkit.vicluster.ViNode;
import java.lang.management.ManagementFactory;
import java.util.concurrent.Callable;
public class FirstNanoCloud {
public static void main(String[] args) throws InterruptedException {
Cloud cloud = CloudFactory.createCloud();
try {
ViNode allNodes = cloud.node("master");
allNodes.x(RemoteNode.REMOTE).useSimpleRemoting();
allNodes.x(RemoteNode.REMOTE).setHostsConfigFile("?na");
allNodes.x(RemoteNode.REMOTE).setRemoteAccount("root");
allNodes.x(RemoteNode.REMOTE).setPassword("password");
allNodes.x(RemoteNode.REMOTE).setRemoteHost("myremotehost" + ":" + 22);
allNodes.touch();
saluteToCaesar(allNodes);
reportMemory(allNodes);
Thread.sleep(10_000l);
} finally {
if (cloud != null) {
cloud.shutdown();
}
}
}
private static void saluteToCaesar(ViNode node) {
node.exec(new Callable<Void>() {
@Override
public Void call() throws Exception {
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
System.out.println("'" + jvmName + "' salute you, Caesar!");
return null;
}
});
}
private static void reportMemory(ViNode node) {
node.exec(new Callable<Void>() {
@Override
public Void call() throws Exception {
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
long totalMemory = Runtime.getRuntime().maxMemory();
System.out.println("My name is '" + jvmName + "'. Memory limit is " + (totalMemory >> 20) + "MiB");
return null;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment