Search in sources :

Example 81 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class CreationValidator method validateCreateItem.

public void validateCreateItem(ItemBase item) throws OpsException {
    // Object model;
    // try {
    // model = managed.getModel(); // Throws if not valid XML
    // } catch (Exception e) {
    // throw new OpsException("Invalid model", e);
    // }
    Class<? extends ItemBase> modelClass = item.getClass();
    SingletonService singletonServiceAnnotation = modelClass.getAnnotation(SingletonService.class);
    if (singletonServiceAnnotation != null) {
        // Only one can be created per scope
        Iterable<? extends ItemBase> items = platformLayer.listItems(modelClass);
        List<PlatformLayerKey> matches = Lists.newArrayList();
        for (ItemBase peer : items) {
            switch(peer.getState()) {
                case ACTIVE:
                case CREATION_REQUESTED:
                case BUILD:
                case BUILD_ERROR:
                    matches.add(peer.getKey());
                    break;
                case DELETE_REQUESTED:
                case DELETED:
                    break;
                default:
                    throw new IllegalStateException();
            }
        }
        if (!matches.isEmpty()) {
            throw new OpsException("Cannot create multiple instances of: " + modelClass.getName() + ".  Others=" + Joiner.on(",").join(matches));
        }
    }
}
Also used : SingletonService(org.platformlayer.xaas.SingletonService) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 82 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class FederatedPlatformLayerClient method mapToChildForPut.

private MappedPlatformLayerKey mapToChildForPut(PlatformLayerKey plk) {
    FederationMapping childKey = federationMap.getClientForCreate(plk);
    ManagedItemId childItemId = plk.getItemId();
    ChildClient childClient = getClient(childKey);
    MappedPlatformLayerKey mapped = new MappedPlatformLayerKey();
    mapped.child = childClient;
    mapped.key = new PlatformLayerKey(childKey.host, childKey.project, plk.getServiceType(), plk.getItemType(), childItemId);
    return mapped;
}
Also used : PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) ManagedItemId(org.platformlayer.ids.ManagedItemId)

Example 83 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class ManagedItemResource method checkItemKey.

private void checkItemKey(ItemBase item) throws OpsException {
    PlatformLayerKey key = item.getKey();
    ManagedItemId itemId = getItemId();
    ServiceType serviceType = getServiceType();
    ItemType itemType = getItemType();
    ProjectId project = getProject();
    if (key != null) {
        if (key.getItemId() != null && !equal(key.getItemId(), itemId)) {
            throw new OpsException("Item id mismatch");
        }
        if (key.getServiceType() != null && !equal(key.getServiceType(), serviceType)) {
            throw new OpsException("Service type mismatch");
        }
        if (key.getItemType() != null && !key.getItemType().isEmpty() && !equal(key.getItemType(), itemType)) {
            throw new OpsException("Item type mismatch");
        }
        if (key.getProject() != null && !equal(key.getProject(), project)) {
            throw new OpsException("Project mismatch");
        }
    }
    key = new PlatformLayerKey(null, project, serviceType, itemType, itemId);
    item.setKey(key);
}
Also used : OpsException(org.platformlayer.ops.OpsException) ServiceType(org.platformlayer.ids.ServiceType) ItemType(org.platformlayer.ids.ItemType) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) ManagedItemId(org.platformlayer.ids.ManagedItemId)

Example 84 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey 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 85 with PlatformLayerKey

use of org.platformlayer.core.model.PlatformLayerKey in project platformlayer by platformlayer.

the class SpreadChooser method choose.

@Override
public DirectCloudHost choose(List<DirectCloudHost> candidates) throws OpsException {
    List<HostRecord> records = Lists.newArrayList();
    for (DirectCloudHost candidate : candidates) {
        HostRecord record = new HostRecord();
        record.candidate = candidate;
        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 hostPolicy = instance.hostPolicy;
            if (Objects.equal(groupId, hostPolicy.groupId)) {
                record.matchingPolicy.add(instance);
            }
            record.all.add(instance);
        }
    }
    Function<HostRecord, Float> score = new Function<HostRecord, Float>() {

        @Override
        public Float apply(HostRecord input) {
            // Lower score is better
            float score = input.matchingPolicy.size();
            // For breaking ties
            score += input.all.size() / 100000.0;
            return score;
        }
    };
    HostRecord bestRecord = ScoreChooser.chooseMin(score).choose(records);
    return bestRecord.candidate;
}
Also used : Function(com.google.common.base.Function) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) HostPolicy(org.platformlayer.core.model.HostPolicy)

Aggregations

PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)86 PlatformLayerClient (org.platformlayer.PlatformLayerClient)21 OpsException (org.platformlayer.ops.OpsException)16 ItemBase (org.platformlayer.core.model.ItemBase)14 ManagedItemId (org.platformlayer.ids.ManagedItemId)13 UntypedItem (org.platformlayer.common.UntypedItem)10 ProjectId (org.platformlayer.ids.ProjectId)10 Tag (org.platformlayer.core.model.Tag)8 RepositoryException (org.platformlayer.RepositoryException)7 ServiceType (org.platformlayer.ids.ServiceType)7 JobData (org.platformlayer.jobs.model.JobData)7 InstanceBase (org.platformlayer.core.model.InstanceBase)6 ItemType (org.platformlayer.ids.ItemType)6 OpsTarget (org.platformlayer.ops.OpsTarget)6 JaxbHelper (org.platformlayer.xml.JaxbHelper)6 Handler (org.platformlayer.ops.Handler)5 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)4 UntypedItemXml (org.platformlayer.UntypedItemXml)4 FederationKey (org.platformlayer.ids.FederationKey)4 Machine (org.platformlayer.ops.Machine)4