Search in sources :

Example 46 with OpsException

use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.

the class PlatformlayerBackedPool method release.

@Override
public void release(PlatformLayerKey owner, T item) throws OpsException {
    for (int i = 0; i < 10; i++) {
        ItemBase resource = platformLayer.getItem(resourceKey);
        List<Assignment> assignments = findAssignments(resource);
        String key = adapter.toKey(item);
        Assignment assigned = Assignment.find(assignments, key, subkey);
        if (assigned == null) {
            throw new OpsException("Resource not assigned");
        }
        if (!assigned.item.equals(owner.getUrl())) {
            throw new OpsException("Resource not held");
        }
        Tag assignmentTag = assigned.asTag();
        TagChanges tagChanges = new TagChanges();
        tagChanges.removeTags.add(assignmentTag);
        if (null != platformLayer.changeTags(resourceKey, tagChanges, resource.getVersion())) {
            return;
        }
        if (!TimeSpan.ONE_SECOND.doSafeSleep()) {
            break;
        }
    }
// List<PlatformLayerKey> assignedTo = Tag.POOL_ASSIGNMENT.build(t) Tag.ASSIGNED_TO.find(item);
// if (!assignedTo.contains(holder)) {
// throw new OpsException("Resource not owned");
// }
//
// platformLayer.deleteItem(item.getKey());
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) Tag(org.platformlayer.core.model.Tag) TagChanges(org.platformlayer.core.model.TagChanges)

Example 47 with OpsException

use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.

the class OpenstackBackupContextFactory method build.

@Override
public OpenstackBackupContext build(ItemBase item) throws OpsException {
    Machine machine = instances.findMachine(item);
    if (machine == null) {
        throw new OpsException("Cannot determine machine for: " + item);
    }
    StorageConfiguration storageConfiguration = cloud.getStorageConfiguration(machine);
    return build(storageConfiguration);
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpenstackStorageConfiguration(org.platformlayer.service.cloud.openstack.ops.OpenstackStorageConfiguration) StorageConfiguration(org.platformlayer.ops.machines.StorageConfiguration) Machine(org.platformlayer.ops.Machine)

Example 48 with OpsException

use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.

the class CloudInstanceMapper method waitOperation.

private void waitOperation(AsyncServerOperation operation) throws OpsException {
    try {
        log.info("Waiting for server operation to complete");
        operation.waitComplete(2, TimeUnit.MINUTES);
    } catch (TimeoutException e) {
        throw new OpsException("Timeout waiting for server operation to complete", e);
    } catch (OpenstackException e) {
        throw new OpsException("Error waiting for server operation to complete", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpenstackException(org.openstack.client.OpenstackException) TimeoutException(java.util.concurrent.TimeoutException)

Example 49 with OpsException

use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.

the class ScoreHostPolicy method choose.

@Override
public DirectCloudHost choose(List<DirectCloudHost> candidates) throws OpsException {
    final String sameGroupId;
    if (Strings.isNullOrEmpty(hostPolicy.groupId)) {
        sameGroupId = DEFAULT_GROUP;
    } else {
        sameGroupId = hostPolicy.groupId;
    }
    final ItemType sameItemType;
    if (hostPolicy.scoreSameItemType != 0) {
        PlatformLayerKey owner = findOwner(newInstance);
        if (owner == null) {
            throw new OpsException();
        }
        sameItemType = owner.getItemType();
    } else {
        sameItemType = null;
    }
    List<HostRecord> records = Lists.newArrayList();
    for (DirectCloudHost candidate : candidates) {
        HostRecord record = new HostRecord();
        record.candidate = candidate;
        record.groups = Maps.newHashMap();
        if (hostPolicy.scoreSameItemType != 0) {
            record.owners = Maps.newHashMap();
        }
        records.add(record);
        for (String assigned : candidate.getModel().getTags().findAll(Tag.ASSIGNED)) {
            PlatformLayerKey instanceKey = PlatformLayerKey.parse(assigned);
            // TODO: Avoid 1+N
            DirectInstance instance = platformLayer.getItem(instanceKey);
            if (instance == null) {
                // TODO: Warn?
                throw new IllegalStateException();
            }
            switch(instance.getState()) {
                case DELETE_REQUESTED:
                case DELETED:
                    continue;
            }
            HostPolicy instanceHostPolicy = instance.hostPolicy;
            String instanceGroupId = instanceHostPolicy.groupId;
            if (Strings.isNullOrEmpty(instanceGroupId)) {
                instanceGroupId = DEFAULT_GROUP;
            }
            record.groups.put(instance, instanceGroupId);
            if (sameItemType != null) {
                PlatformLayerKey owner = findOwner(instance);
                if (owner != null) {
                    record.owners.put(instance, owner);
                }
            }
            record.all.add(instance);
        }
    }
    Function<HostRecord, Float> score = new Function<HostRecord, Float>() {

        @Override
        public Float apply(HostRecord record) {
            float score = 0;
            for (DirectInstance instance : record.all) {
                if (sameGroupId != null) {
                    String instanceGroupId = record.groups.get(instance);
                    if (Objects.equal(instanceGroupId, sameGroupId)) {
                        score += hostPolicy.scoreSameGroup;
                    }
                }
                if (sameItemType != null) {
                    PlatformLayerKey owner = record.owners.get(instance);
                    if (owner != null && owner.getItemType().equals(sameItemType)) {
                        score += hostPolicy.scoreSameItemType;
                    }
                }
            }
            // Break ties using least-loaded
            score -= record.all.size() / 1000.0f;
            return score;
        }
    };
    HostRecord bestRecord = ScoreChooser.chooseMax(score).choose(records);
    return bestRecord.candidate;
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemType(org.platformlayer.ids.ItemType) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Function(com.google.common.base.Function) HostPolicy(org.platformlayer.core.model.HostPolicy)

Example 50 with OpsException

use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.

the class GoogleComputeClient method listImages.

private Iterable<Image> listImages(String projectId) throws OpsException {
    List<Image> ret = Lists.newArrayList();
    ImageList imageList;
    try {
        log.debug("Listing images in project " + projectId);
        imageList = compute.images().list(projectId).execute();
    } catch (IOException e) {
        throw new OpsException("Error listing images", e);
    }
    if (imageList.getItems() != null) {
        ret.addAll(imageList.getItems());
    }
    return ret;
}
Also used : OpsException(org.platformlayer.ops.OpsException) IOException(java.io.IOException) Image(com.google.api.services.compute.model.Image) ImageList(com.google.api.services.compute.model.ImageList)

Aggregations

OpsException (org.platformlayer.ops.OpsException)142 IOException (java.io.IOException)39 File (java.io.File)19 ItemBase (org.platformlayer.core.model.ItemBase)19 RepositoryException (org.platformlayer.RepositoryException)18 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)17 Handler (org.platformlayer.ops.Handler)17 Tag (org.platformlayer.core.model.Tag)16 Command (org.platformlayer.ops.Command)16 Machine (org.platformlayer.ops.Machine)13 TagChanges (org.platformlayer.core.model.TagChanges)11 OpsTarget (org.platformlayer.ops.OpsTarget)11 TimeoutException (java.util.concurrent.TimeoutException)10 OpenstackException (org.openstack.client.OpenstackException)10 OpsContext (org.platformlayer.ops.OpsContext)10 X509Certificate (java.security.cert.X509Certificate)9 InetAddress (java.net.InetAddress)8 ProjectId (org.platformlayer.ids.ProjectId)8 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)8 List (java.util.List)7