Search in sources :

Example 1 with OpenstackNotFoundException

use of org.openstack.client.OpenstackNotFoundException in project platformlayer by platformlayer.

the class OpenstackCasStore method findArtifact.

@Override
public CasStoreObject findArtifact(Md5Hash hash) {
    OpenstackStorageClient storageClient = getStorageClient();
    try {
        List<StorageObject> storageObjects = Lists.newArrayList(storageClient.listObjects(containerName, null, null));
        String findHash = hash.toHex();
        for (StorageObject storageObject : storageObjects) {
            String storageObjectHash = storageObject.getHash();
            if (storageObjectHash.equalsIgnoreCase(findHash)) {
                return new OpenstackCasObject(storageObject);
            }
        }
    } catch (OpenstackNotFoundException e) {
        log.debug("Not found (404) returned from Openstack");
        return null;
    }
    return null;
}
Also used : StorageObject(org.openstack.model.storage.StorageObject) OpenstackStorageClient(org.openstack.client.storage.OpenstackStorageClient) OpenstackNotFoundException(org.openstack.client.OpenstackNotFoundException)

Example 2 with OpenstackNotFoundException

use of org.openstack.client.OpenstackNotFoundException in project platformlayer by platformlayer.

the class CloudInstanceMapper method doOperation.

@Handler
public void doOperation() throws OpsException, IOException {
    Tags instanceTags = instance.getTags();
    OpenstackCloud cloud = findCloud();
    if (cloud == null) {
        throw new OpsException("Could not find cloud");
    }
    OpenstackComputeClient computeClient = openstack.getComputeClient(cloud);
    getRecursionState().pushChildScope(cloud);
    List<String> assignedInstanceIds = instanceTags.findAll(Tag.ASSIGNED);
    if (assignedInstanceIds.isEmpty()) {
        if (createInstance && !OpsContext.isDelete()) {
            MachineCreationRequest request = buildMachineCreationRequest();
            PlatformLayerKey instanceKey = instance.getKey();
            request.tags.add(Tag.buildParentTag(instanceKey));
            String serverName = buildServerName();
            Server created = openstack.createInstance(cloud, serverName, request);
            {
                Tag instanceTag = Tag.build(Tag.ASSIGNED, created.getId());
                platformLayer.addTag(instance.getKey(), instanceTag);
            }
            assignedInstanceIds.add(created.getId());
        }
    }
    if (assignedInstanceIds.isEmpty() && !OpsContext.isDelete()) {
        throw new OpsException("Instance not yet assigned");
    }
    Machine machine = null;
    OpsTarget target = null;
    if (!assignedInstanceIds.isEmpty()) {
        if (assignedInstanceIds.size() != 1) {
            log.warn("Multiple instance ids found: " + assignedInstanceIds);
        }
        // We just take the first instance id
        String assignedInstanceId = Iterables.getFirst(assignedInstanceIds, null);
        Server server = openstack.findServerById(cloud, assignedInstanceId);
        if (server == null) {
            if (OpsContext.isConfigure()) {
                throw new OpsException("Unable to find assigned server: " + assignedInstanceId);
            }
        } else {
            server = openstack.ensureHasPublicIp(cloud, server);
            AsyncServerOperation powerOnOperation = openstack.ensurePoweredOn(cloud, server);
            if (powerOnOperation != null) {
                waitOperation(powerOnOperation);
            }
            machine = new OpenstackComputeMachine(openstack, cloud, server);
            SshKey sshKey = service.getSshKey();
            target = machine.getTarget(sshKey);
        }
    }
    if (!assignedInstanceIds.isEmpty() && OpsContext.isDelete()) {
        CloudBehaviours cloudBehaviours = new CloudBehaviours(cloud);
        boolean supportsSecurityGroups = cloudBehaviours.supportsSecurityGroups();
        for (String instanceId : assignedInstanceIds) {
            Server server = openstack.findServerById(cloud, instanceId);
            if (server == null) {
                log.warn("Could not find assigned server: " + instanceId + ", ignoring");
                continue;
            }
            SecurityGroup securityGroup = null;
            if (supportsSecurityGroups) {
                securityGroup = openstackHelpers.getMachineSecurityGroup(computeClient, server);
            }
            AsyncServerOperation terminateOperation = openstack.terminateInstance(cloud, instanceId);
            if (securityGroup != null) {
                // We need to terminate the instance before we delete the security group it uses
                if (terminateOperation != null) {
                    waitOperation(terminateOperation);
                }
                try {
                    log.info("Deleting security group: " + securityGroup.getId());
                    computeClient.root().securityGroups().securityGroup(securityGroup.getId()).delete();
                } catch (OpenstackNotFoundException e) {
                    log.info("Ignoring not-found error while deleting security group: " + securityGroup.getId());
                }
            }
        }
    }
    RecursionState recursion = getRecursionState();
    if (OpsContext.isDelete() && machine == null) {
        recursion.setPreventRecursion(true);
    } else {
        recursion.pushChildScope(machine);
        recursion.pushChildScope(target);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpenstackCloud(org.platformlayer.service.cloud.openstack.model.OpenstackCloud) Server(org.openstack.model.compute.Server) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) MachineCreationRequest(org.platformlayer.ops.MachineCreationRequest) SecurityGroup(org.openstack.model.compute.SecurityGroup) OpenstackComputeMachine(org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine) Machine(org.platformlayer.ops.Machine) SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) OpenstackNotFoundException(org.openstack.client.OpenstackNotFoundException) CloudBehaviours(org.platformlayer.service.cloud.openstack.ops.openstack.CloudBehaviours) Tag(org.platformlayer.core.model.Tag) OpenstackComputeMachine(org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine) Tags(org.platformlayer.core.model.Tags) AsyncServerOperation(org.openstack.client.compute.AsyncServerOperation) Handler(org.platformlayer.ops.Handler)

Example 3 with OpenstackNotFoundException

use of org.openstack.client.OpenstackNotFoundException in project platformlayer by platformlayer.

the class OpenstackCloudContext method terminateInstance.

public AsyncServerOperation terminateInstance(OpenstackCloud cloud, String instanceId) throws OpsException {
    try {
        OpenstackComputeClient computeClient = getComputeClient(cloud);
        log.info("Terminating server: " + instanceId);
        AsyncServerOperation deleteOperation = computeClient.deleteServer(instanceId);
        return deleteOperation;
    } catch (OpenstackNotFoundException e) {
        log.info("Could not find instance to be terminated, assuming already terminated: " + instanceId);
        return null;
    } catch (OpenstackException e) {
        throw new OpsException("Error terminating server", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) OpenstackNotFoundException(org.openstack.client.OpenstackNotFoundException) AsyncServerOperation(org.openstack.client.compute.AsyncServerOperation) OpenstackException(org.openstack.client.OpenstackException)

Example 4 with OpenstackNotFoundException

use of org.openstack.client.OpenstackNotFoundException in project platformlayer by platformlayer.

the class OpenstackCloudContext method findServerById.

// private AmazonEC2Client buildEc2Client() throws OpsException {
// OpsContext opsContext = OpsContext.get();
// 
// AmazonEC2Client amazonEC2Client = opsContext.getUserInfo().buildEc2Client();
// 
// return amazonEC2Client;
// }
// 
// // // sshKeyPair = cloud.generateSshKeyPair(sshKeyName);
// // public KeyPair generateSshKeyPair(String sshKeyName) throws OpsException {
// // KeyPair sshKeyPair;
// //
// // try {
// // // OpenStack EC2 api does not support import
// // // // We create a new keypair each time... we don't want the user
// // // logging in
// // // KeyPair sshKeyPair = generateKeyPair("RSA", null);
// //
// // // ImportKeyPairRequest importKeyPairRequest = new
// // // ImportKeyPairRequest();
// // // importKeyPairRequest.setPublicKeyMaterial(publicKeyMaterial);
// // // ImportKeyPairResult importKeyPairResult =
// // // computeClient.importKeyPair(importKeyPairRequest);
// // // sshKeyName = importKeyPairResult.getKeyName();
// // AmazonEC2Client ec2Api = buildEc2Client();
// //
// // CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest(sshKeyName);
// // CreateKeyPairResult createKeyPairResult = ec2Api.createKeyPair(createKeyPairRequest);
// //
// // sshKeyPair = extractKeyPair(createKeyPairResult);
// //
// // return sshKeyPair;
// // } catch (AmazonClientException e) {
// // throw new OpsException("Error building server", e);
// // }
// // }
// 
public Server findServerById(OpenstackCloud cloud, String serverId) throws OpsException {
    OpenstackComputeClient computeClient = getComputeClient(cloud);
    try {
        log.info("Getting server info for: " + serverId);
        Server server = computeClient.root().servers().server(serverId).show();
        return server;
    } catch (OpenstackNotFoundException e) {
        return null;
    } catch (OpenstackException e) {
        throw new OpsException("Error getting server", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Server(org.openstack.model.compute.Server) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) OpenstackNotFoundException(org.openstack.client.OpenstackNotFoundException) OpenstackException(org.openstack.client.OpenstackException)

Aggregations

OpenstackNotFoundException (org.openstack.client.OpenstackNotFoundException)4 OpenstackComputeClient (org.openstack.client.common.OpenstackComputeClient)3 OpsException (org.platformlayer.ops.OpsException)3 OpenstackException (org.openstack.client.OpenstackException)2 AsyncServerOperation (org.openstack.client.compute.AsyncServerOperation)2 Server (org.openstack.model.compute.Server)2 OpenstackStorageClient (org.openstack.client.storage.OpenstackStorageClient)1 SecurityGroup (org.openstack.model.compute.SecurityGroup)1 StorageObject (org.openstack.model.storage.StorageObject)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 Tag (org.platformlayer.core.model.Tag)1 Tags (org.platformlayer.core.model.Tags)1 Handler (org.platformlayer.ops.Handler)1 Machine (org.platformlayer.ops.Machine)1 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)1 OpsTarget (org.platformlayer.ops.OpsTarget)1 SshKey (org.platformlayer.ops.helpers.SshKey)1 OpenstackCloud (org.platformlayer.service.cloud.openstack.model.OpenstackCloud)1 CloudBehaviours (org.platformlayer.service.cloud.openstack.ops.openstack.CloudBehaviours)1 OpenstackComputeMachine (org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine)1