Search in sources :

Example 1 with ImageApi

use of org.jclouds.openstack.nova.v2_0.features.ImageApi 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 });
    }
}
Also used : ImageApi(org.jclouds.openstack.nova.v2_0.features.ImageApi) FlavorApi(org.jclouds.openstack.nova.v2_0.features.FlavorApi) KeyPair(org.jclouds.openstack.nova.v2_0.domain.KeyPair) KeyPairApi(org.jclouds.openstack.nova.v2_0.extensions.KeyPairApi) ServerCreated(org.jclouds.openstack.nova.v2_0.domain.ServerCreated) IOException(java.io.IOException) Image(org.jclouds.openstack.nova.v2_0.domain.Image) Flavor(org.jclouds.openstack.nova.v2_0.domain.Flavor) CreateServerOptions(org.jclouds.openstack.nova.v2_0.options.CreateServerOptions) MemStore(org.apache.gora.memory.store.MemStore) NovaApi(org.jclouds.openstack.nova.v2_0.NovaApi) ServerApi(org.jclouds.openstack.nova.v2_0.features.ServerApi) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 MemStore (org.apache.gora.memory.store.MemStore)1 NovaApi (org.jclouds.openstack.nova.v2_0.NovaApi)1 Flavor (org.jclouds.openstack.nova.v2_0.domain.Flavor)1 Image (org.jclouds.openstack.nova.v2_0.domain.Image)1 KeyPair (org.jclouds.openstack.nova.v2_0.domain.KeyPair)1 ServerCreated (org.jclouds.openstack.nova.v2_0.domain.ServerCreated)1 KeyPairApi (org.jclouds.openstack.nova.v2_0.extensions.KeyPairApi)1 FlavorApi (org.jclouds.openstack.nova.v2_0.features.FlavorApi)1 ImageApi (org.jclouds.openstack.nova.v2_0.features.ImageApi)1 ServerApi (org.jclouds.openstack.nova.v2_0.features.ServerApi)1 CreateServerOptions (org.jclouds.openstack.nova.v2_0.options.CreateServerOptions)1