Search in sources :

Example 36 with Tag

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

the class EndpointDnsRecord method handler.

@Handler
public void handler() throws OpsException {
    PublicEndpointBase endpoint = endpointProvider.get();
    if (OpsContext.isConfigure()) {
        // Create a DNS record
        Tag parentTag = Tag.buildParentTag(endpoint.getKey());
        List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(endpoint.getTags(), destinationPort);
        if (endpoints.isEmpty()) {
            throw new OpsException("Cannot find endpoint for port: " + destinationPort);
        }
        DnsRecord record = new DnsRecord();
        record.setDnsName(dnsName);
        for (EndpointInfo endpointInfo : endpoints) {
            record.getAddress().add(endpointInfo.publicIp);
        }
        record.getTags().add(parentTag);
        record.setKey(PlatformLayerKey.fromId(dnsName));
        try {
            platformLayerClient.putItemByTag((ItemBase) record, parentTag);
        } catch (PlatformLayerClientException e) {
            throw new OpsException("Error registering persistent instance", e);
        }
    }
}
Also used : PlatformLayerClientException(org.platformlayer.PlatformLayerClientException) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsException(org.platformlayer.ops.OpsException) Tag(org.platformlayer.core.model.Tag) PublicEndpointBase(org.platformlayer.core.model.PublicEndpointBase) DnsRecord(org.platformlayer.dns.model.DnsRecord) Handler(org.platformlayer.ops.Handler)

Example 37 with Tag

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

the class OwnedItem method handler.

@Handler
public void handler() throws OpsException {
    T itemTemplate = buildItemTemplate();
    Tag uniqueTag = getUniqueTag(itemTemplate);
    if (OpsContext.isConfigure()) {
        try {
            item = platformLayer.putItemByTag(itemTemplate, uniqueTag);
        } catch (PlatformLayerClientException e) {
            throw new OpsException("Error creating owned item", e);
        }
    }
    if (OpsContext.isDelete()) {
        List<? extends ItemBase> items = platformLayer.listItems(itemTemplate.getClass(), TagFilter.byTag(uniqueTag));
        if (items.size() != 0) {
            if (items.size() != 1) {
                throw new OpsException("Found multiple items with unique tag: " + uniqueTag);
            }
            item = (T) items.get(0);
            platformLayer.ensureDeleted(item);
        }
    }
}
Also used : PlatformLayerClientException(org.platformlayer.PlatformLayerClientException) OpsException(org.platformlayer.ops.OpsException) Tag(org.platformlayer.core.model.Tag) Handler(org.platformlayer.ops.Handler)

Example 38 with Tag

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

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

the class GlanceImageStore method uploadImage.

@Override
public String uploadImage(OpsTarget target, Tags tags, File imageFile, long rawImageFileSize) throws OpsException {
    OpenstackImageClient client = getOpenstackImageClient();
    String diskFormat = null;
    if (tags != null) {
        // This logic looks suspicious...
        assert false;
        for (Tag tag : tags.getTags()) {
            ImageFormat imageFormat = ImageFormat.fromTags(tags);
            diskFormat = mapToGlanceDiskFormat(imageFormat);
        }
    }
    String glanceBaseUrl;
    String tokenId;
    try {
        Access access = client.getSession().getAuthenticationToken();
        tokenId = access.getToken().getId();
        glanceBaseUrl = client.root().getBaseUrl();
    } catch (OpenstackException e) {
        throw new OpsException("Error getting glance url", e);
    }
    // Upload to glance
    String glanceUploadUrl = glanceBaseUrl;
    if (!glanceUploadUrl.endsWith("/")) {
        glanceUploadUrl += "/";
    }
    glanceUploadUrl += "images";
    String imageName = "image-" + System.currentTimeMillis();
    Command command = Command.build("curl");
    command.addLiteral("--fail");
    command.addLiteral("--upload-file").addFile(imageFile);
    command.addLiteral("-X").addLiteral("POST");
    command.addLiteral("-H").addQuoted("X-Auth-Token: " + tokenId);
    command.addLiteral("-H").addQuoted("Content-Type: application/octet-stream");
    command.addLiteral("-H").addQuoted("X-Image-Meta-Name: " + imageName);
    command.addLiteral("-H").addQuoted("X-Image-Meta-Is-Public: True");
    // } else {
    if (diskFormat != null) {
        command.addLiteral("-H").addQuoted("X-Image-Meta-Disk-Format: " + diskFormat);
    }
    command.addLiteral("-H").addQuoted("X-Image-Meta-Container-Format: bare");
    // }
    // command.addLiteral("-H").addQuoted("X-Image-Meta-Min-Disk: 0");
    // command.addLiteral("-H").addQuoted("X-Image-Meta-Min-Ram: 0");
    // command.addLiteral("-H").addQuoted("X-Image-Meta-Image-Size: " + rawImageFileSize);
    command.addLiteral("-H").addQuoted("X-Image-Meta-Size: " + rawImageFileSize);
    // image_meta = {'name': fields.pop('name'),
    // 'is_public': utils.bool_from_string(
    // fields.pop('is_public', False)),
    // 'disk_format': fields.pop('disk_format', 'raw'),
    // 'min_disk': fields.pop('min_disk', 0),
    // 'min_ram': fields.pop('min_ram', 0),
    // 'container_format': fields.pop('container_format', 'ovf')}
    // glance add name=DebianSqueeze is_public=True disk_format=raw container_format=bare
    // system_id="http://org.platformlayer/service/imagefactory/v1.0:bootstrap"
    // image_size="${RAW_SIZE}" < disk.raw.gz
    command.addQuoted(glanceUploadUrl);
    command.setTimeout(TimeSpan.FIFTEEN_MINUTES);
    ProcessExecution execution = target.executeCommand(command);
    String imageId;
    // String imageLocation;
    {
        // "is_public": true, "deleted_at": null, "min_ram": 0, "size": 925761536}}
        try {
            JSONObject json = new JSONObject(execution.getStdOut());
            JSONObject image = json.getJSONObject("image");
            // imageLocation = image.getString("location");
            imageId = image.getString("id");
        } catch (JSONException e) {
            log.warn("Image upload returned: " + execution.getStdOut());
            throw new OpsException("Error parsing return value from image upload", e);
        }
    }
    if (tags != null) {
        updateImageTags(imageId, tags);
    }
    return imageId;
}
Also used : OpsException(org.platformlayer.ops.OpsException) JSONObject(org.json.JSONObject) Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) OpenstackImageClient(org.openstack.client.common.OpenstackImageClient) Access(org.openstack.model.identity.Access) JSONException(org.json.JSONException) Tag(org.platformlayer.core.model.Tag) OpenstackException(org.openstack.client.OpenstackException) ImageFormat(org.platformlayer.ops.images.ImageFormat)

Example 40 with Tag

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

the class GlanceImageStore method findImages.

@Override
public List<CloudImage> findImages(List<Tag> tags) throws OpsException {
    List<CloudImage> matches = Lists.newArrayList();
    OpenstackImageClient glanceClient = getOpenstackImageClient();
    Iterable<Image> images;
    try {
        images = glanceClient.root().images().list(true);
    } catch (OpenstackException e) {
        throw new OpsException("Error listing images", e);
    }
    for (Image image : images) {
        boolean match = true;
        for (Tag tag : tags) {
            match = false;
            String tagKey = tag.getKey();
            String tagValue = tag.getValue();
            boolean checked = false;
            if (ImageFormat.isImageFormatTag(tag)) {
                ImageFormat format = ImageFormat.fromTag(tag);
                // if ("qcow2".equals(tagValue)) {
                // format = ImageFormat.DiskQcow2;
                // } else {
                // throw new UnsupportedOperationException("Unknown glance disk_format: " + tagValue);
                // }
                // 
                // Tag mappedTag = format.toTag();
                // 
                // match = Objects.equal(mappedTag.getValue().getDiskFormat(), glanceDiskFormat);
                // checked = true;
                String glanceDiskFormat = mapToGlanceDiskFormat(format);
                if (glanceDiskFormat != null) {
                    match = Objects.equal(image.getDiskFormat(), glanceDiskFormat);
                    checked = true;
                }
            }
            if (!checked) {
                for (Entry<String, Object> meta : image.getProperties().asMap().entrySet()) {
                    if (Objects.equal(tagKey, meta.getKey())) {
                        String content = (String) meta.getValue();
                        // OS BUG #885044: Content contains whitespace
                        content = content.trim();
                        if (content.equals(tagValue)) {
                            match = true;
                        }
                    }
                }
            }
            if (!match) {
                break;
            }
        }
        if (match) {
            matches.add(new GlanceImage(image));
        }
    }
    return matches;
}
Also used : OpsException(org.platformlayer.ops.OpsException) CloudImage(org.platformlayer.ops.images.CloudImage) Image(org.openstack.model.image.Image) CloudImage(org.platformlayer.ops.images.CloudImage) OpenstackException(org.openstack.client.OpenstackException) ImageFormat(org.platformlayer.ops.images.ImageFormat) OpenstackImageClient(org.openstack.client.common.OpenstackImageClient) JSONObject(org.json.JSONObject) Tag(org.platformlayer.core.model.Tag)

Aggregations

Tag (org.platformlayer.core.model.Tag)41 OpsException (org.platformlayer.ops.OpsException)16 ItemBase (org.platformlayer.core.model.ItemBase)8 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)8 Tags (org.platformlayer.core.model.Tags)8 Handler (org.platformlayer.ops.Handler)8 TagChanges (org.platformlayer.core.model.TagChanges)6 JSONObject (org.json.JSONObject)4 OpenstackException (org.openstack.client.OpenstackException)4 RepositoryException (org.platformlayer.RepositoryException)4 Machine (org.platformlayer.ops.Machine)4 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)3 SQLException (java.sql.SQLException)3 OpenstackImageClient (org.openstack.client.common.OpenstackImageClient)3 Filter (org.platformlayer.Filter)3 PlatformLayerClient (org.platformlayer.PlatformLayerClient)3 StateFilter (org.platformlayer.StateFilter)3 OpsTarget (org.platformlayer.ops.OpsTarget)3 UniqueTag (org.platformlayer.ops.UniqueTag)3 SshKey (org.platformlayer.ops.helpers.SshKey)3