use of org.jclouds.openstack.nova.v2_0.features.FlavorApi in project gora by apache.
the class RackspaceOrchestration method performRackspaceOrchestration.
private static void performRackspaceOrchestration(Properties properties) throws InstantiationException, IllegalAccessException, IOException {
rsContinent = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_CONTINENT, "rackspace-cloudservers-us");
rsUser = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_USERNAME, "asf-gora");
rsApiKey = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_APIKEY, null);
rsRegion = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_REGION, "DFW");
String rsFlavourId = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_FLAVORID, null);
String rsImageId = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_IMAGEID, null);
int numServers = Integer.parseInt(DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_NUM_SERVERS, "10"));
String serverName = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_SERVERNAME, "goraci_test_server");
novaApi = ContextBuilder.newBuilder(rsContinent).credentials(rsUser, rsApiKey).buildApi(NovaApi.class);
LOG.info("Defining Rackspace cloudserver continent as: {}, and region: {}.", rsContinent, rsRegion);
//Choose operating system
ImageApi imageApi = novaApi.getImageApi(rsRegion);
Image image = imageApi.get(rsImageId);
//Choose hardware configuration
FlavorApi flavorApi = novaApi.getFlavorApi(rsRegion);
Flavor flavor = flavorApi.get(rsFlavourId);
LOG.info("Defining Rackspace cloudserver flavors as: {}, with image id's {}", rsFlavourId, rsImageId);
//Manage keypairs
KeyPairApi keyPairApi = novaApi.getKeyPairApi(rsRegion).get();
File keyPairFile = null;
String publicKey = null;
//Use your own .pub key which should be on CP
if (DataStoreFactory.findBooleanProperty(properties, MemStore.class.newInstance(), RS_PUBKEY, "true")) {
keyPairFile = new File("~/.ssh/id_rsa.pub");
LOG.info("Uploading local public key from ~/.ssh/id_rsa.pub to Rackspace...");
} else {
//obtain existing key from Rackspace
if (keyPairApi.get("goraci-keypair") != null) {
publicKey = keyPairApi.get("goraci-keypair").getPublicKey();
LOG.info("Obtained existing public key 'goraci-keypair' from Rackspace...");
} else {
//have Rackspace generate us a key
LOG.info("Generating local keypair 'goraci-keypair' and uploading to Rackspace...");
KeyPair keyPair = keyPairApi.create("goraci-keypair");
keyPairFile = new File("/target/goraci-keypair.pem");
try {
Files.write(keyPair.getPrivateKey(), keyPairFile, Charsets.UTF_8);
} catch (IOException e) {
throw new IOException(e);
}
try {
publicKey = Files.toString(keyPairFile, Charsets.UTF_8);
} catch (IOException e) {
throw new IOException(e);
}
keyPairApi.createWithPublicKey("goraci-keypair", publicKey);
}
}
ServerApi serverApi = novaApi.getServerApi(rsRegion);
CreateServerOptions options = CreateServerOptions.Builder.keyPairName("goraci-keypair");
ServerCreated serverCreated = null;
for (int i = 0; i < numServers; i++) {
if (serverName != null) {
serverCreated = serverApi.create(serverName + i, rsImageId, rsFlavourId, options);
} else {
serverCreated = serverApi.create("goraci_test_server" + i, image.getId(), flavor.getId(), options);
}
ServerPredicates.awaitActive(serverApi).apply(serverCreated.getId());
LOG.info("Provisioned node {} of {} with name {}", new Object[] { i + 1, numServers, serverName + i });
}
}
Aggregations