use of org.jclouds.ec2.domain.KeyPair in project legacy-jclouds-examples by jclouds.
the class MainApp method main.
public static void main(String[] args) throws TimeoutException {
if (args.length < PARAMETERS)
throw new IllegalArgumentException(INVALID_SYNTAX);
// Args
String accesskeyid = args[0];
String secretkey = args[1];
String command = args[2];
String name = args[3];
// Init
RestContext<EC2Client, EC2AsyncClient> context = ContextBuilder.newBuilder("aws-ec2").credentials(accesskeyid, secretkey).build();
// Get a synchronous client
EC2Client client = context.getApi();
try {
if (command.equals("create")) {
KeyPair pair = createKeyPair(client, name);
RunningInstance instance = createSecurityGroupKeyPairAndInstance(client, name);
System.out.printf("instance %s ready%n", instance.getId());
System.out.printf("ip address: %s%n", instance.getIpAddress());
System.out.printf("dns name: %s%n", instance.getDnsName());
System.out.printf("login identity:%n%s%n", pair.getKeyMaterial());
} else if (command.equals("destroy")) {
destroySecurityGroupKeyPairAndInstance(client, name);
} else {
throw new IllegalArgumentException(INVALID_SYNTAX);
}
} finally {
// Close connecton
context.close();
System.exit(0);
}
}
use of org.jclouds.ec2.domain.KeyPair 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