Search in sources :

Example 6 with Keypair

use of org.openstack4j.model.compute.Keypair in project airavata by apache.

the class OpenstackIntfImpl method createKeyPair.

@Override
public Keypair createKeyPair(String keyPairName, String publicKey) {
    try {
        Keypair keyp = os.compute().keypairs().create(keyPairName, publicKey);
        logger.info("Keypair created successfully: " + keyp.getName());
        return keyp;
    } catch (Exception ex) {
        ex.printStackTrace();
        // TODO: Check with the team on how to handle exceptions.
        logger.error("Failed to create keypair: " + keyPairName);
        return null;
    }
}
Also used : Keypair(org.openstack4j.model.compute.Keypair) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with Keypair

use of org.openstack4j.model.compute.Keypair in project airavata by apache.

the class CloudIntfTest method jetstreamCreateDeleteServerTest.

/**
 * Test that will create keypair, create server with keypair, delete server, delete keypair.
 */
@Test
@Ignore
public void jetstreamCreateDeleteServerTest() {
    try {
        CloudInterface cloudIntf = new OpenstackIntfImpl("jetstream_openrc.properties");
        // Sample data. This can be determined by the inputs from Airavata.
        String imageId = properties.getProperty("jetstream_imageId");
        String flavorId = properties.getProperty("jetstream_flavorId");
        // Delay in milliseconds used for waiting for server create and delete.
        Integer delay = 30000;
        /* Create Keypair */
        String publicKeyFile = properties.getProperty("publicKeyFile");
        String keyPairName = "testKey";
        Scanner fileScan = new Scanner(new FileInputStream(publicKeyFile));
        String publicKey = fileScan.nextLine();
        Keypair kp = (Keypair) cloudIntf.getKeyPair(keyPairName);
        if (kp == null) {
            kp = (Keypair) cloudIntf.createKeyPair(keyPairName, publicKey);
        }
        logger.info("Keypair created/ retrieved: " + kp.getFingerprint());
        /* Create Server */
        Server newServer = (Server) cloudIntf.createServer("AiravataTest", imageId, flavorId, kp.getName());
        logger.info("Server Created: " + newServer.getId());
        /* Wait 30 seconds until server is active */
        logger.info("Waiting for instance to go ACTIVE...");
        Thread.sleep(delay);
        /* Associate floating ip */
        cloudIntf.addFloatingIP(newServer.getId());
        /* Delete Server */
        cloudIntf.deleteServer(newServer.getId());
        logger.info("Server deleted: " + newServer.getId());
        /* Wait 30 seconds until server is terminated */
        logger.info("Waiting for instance to terminate...");
        Thread.sleep(delay);
        /* Delete Keypair */
        cloudIntf.deleteKeyPair(kp.getName());
        logger.info("Keypair deleted: " + kp.getName());
        Server deleted = (Server) cloudIntf.getServer(newServer.getId());
        assertTrue(newServer != null && deleted == null);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail();
    }
}
Also used : Scanner(java.util.Scanner) CloudInterface(org.apache.airavata.cloud.intf.CloudInterface) Keypair(org.openstack4j.model.compute.Keypair) Server(org.openstack4j.model.compute.Server) OpenstackIntfImpl(org.apache.airavata.cloud.intf.impl.OpenstackIntfImpl) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with Keypair

use of org.openstack4j.model.compute.Keypair in project cloudbreak by hortonworks.

the class OpenStackResourceConnector method createKeyPair.

private void createKeyPair(AuthenticatedContext authenticatedContext, CloudStack stack, OSClient<?> client) {
    KeystoneCredentialView keystoneCredential = openStackClient.createKeystoneCredential(authenticatedContext);
    String keyPairName = keystoneCredential.getKeyPairName();
    if (client.compute().keypairs().get(keyPairName) == null) {
        try {
            Keypair keyPair = client.compute().keypairs().create(keyPairName, stack.getInstanceAuthentication().getPublicKey());
            LOGGER.info("Keypair has been created: {}", keyPair);
        } catch (Exception e) {
            LOGGER.error("Failed to create keypair", e);
            throw new CloudConnectorException(e.getMessage(), e);
        }
    } else {
        LOGGER.info("Keypair already exists: {}", keyPairName);
    }
}
Also used : Keypair(org.openstack4j.model.compute.Keypair) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)

Example 9 with Keypair

use of org.openstack4j.model.compute.Keypair in project cloudbreak by hortonworks.

the class OpenStackPlatformResources method sshKeys.

@Override
public CloudSshKeys sshKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    OSClient<?> osClient = openStackClient.createOSClient(cloudCredential);
    KeystoneCredentialView osCredential = openStackClient.createKeystoneCredential(cloudCredential);
    Set<CloudSshKey> cloudSshKeys = new HashSet<>();
    for (Keypair keypair : osClient.compute().keypairs().list()) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("fingerprint", keypair.getFingerprint());
        properties.put("id", keypair.getId());
        properties.put("publicKey", keypair.getPublicKey());
        properties.put("createdAt", keypair.getCreatedAt());
        CloudSshKey cloudSshKey = new CloudSshKey();
        cloudSshKey.setName(keypair.getName());
        cloudSshKey.setProperties(properties);
        cloudSshKeys.add(cloudSshKey);
    }
    Map<String, Set<CloudSshKey>> result = new HashMap<>();
    result.put(region.value() == null ? osCredential.getTenantName() : region.value(), cloudSshKeys);
    LOGGER.info("openstack cloud ssh keys result: {}", result);
    return new CloudSshKeys(result);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) CloudSshKeys(com.sequenceiq.cloudbreak.cloud.model.CloudSshKeys) CloudSshKey(com.sequenceiq.cloudbreak.cloud.model.CloudSshKey) Keypair(org.openstack4j.model.compute.Keypair) KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView) HashSet(java.util.HashSet)

Aggregations

Keypair (org.openstack4j.model.compute.Keypair)9 Test (org.junit.Test)4 Message (org.apache.camel.Message)3 NovaKeypair (org.openstack4j.openstack.compute.domain.NovaKeypair)3 KeystoneCredentialView (com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView)2 FileNotFoundException (java.io.FileNotFoundException)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 CloudSshKey (com.sequenceiq.cloudbreak.cloud.model.CloudSshKey)1 CloudSshKeys (com.sequenceiq.cloudbreak.cloud.model.CloudSshKeys)1 ActionWentFailException (com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)1 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Scanner (java.util.Scanner)1 Set (java.util.Set)1 CloudInterface (org.apache.airavata.cloud.intf.CloudInterface)1 OpenstackIntfImpl (org.apache.airavata.cloud.intf.impl.OpenstackIntfImpl)1 CamelContext (org.apache.camel.CamelContext)1 Exchange (org.apache.camel.Exchange)1 Producer (org.apache.camel.Producer)1