Search in sources :

Example 91 with OpsException

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

the class PackageDependency method doOperation.

@Handler
public void doOperation() throws OpsException {
    if (OpsContext.isDelete()) {
        // Should we delete the packages? Probably not, because others may also need them
        log.debug("Not removing package on delete (in case someone else also uses it)");
    }
    if (OpsContext.isConfigure() || OpsContext.isValidate()) {
        OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
        List<String> installedPackages = apt.getInstalledPackageInfo(target);
        if (!installedPackages.contains(packageName)) {
            log.info("Package not installed: " + packageName);
            if (OpsContext.isValidate()) {
                throw new OpsException("Package not installed: " + packageName);
            }
            if (repositoryKey != null) {
                apt.addRepositoryKeyUrl(target, repositoryKey.getUrl());
            }
            if (repository != null) {
                apt.addRepository(target, repository.getKey(), repository.getSource());
            }
            if (configuration != null) {
                apt.preconfigurePackages(target, configuration);
            }
            // TODO: Only update once per operation?
            // I think we do want to update aggressively though, because we want to be sure we're up to date
            // as that could well be the reason we're running the operation!
            // We definitely want to update if we added a repository etc above
            apt.update(target, false);
            apt.install(target, packageName);
        } else {
            log.debug("Package is installed: " + packageName);
        }
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) OpsException(org.platformlayer.ops.OpsException) Handler(org.platformlayer.ops.Handler)

Example 92 with OpsException

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

the class ImageFactory method getOrCreateImage.

public DiskImage getOrCreateImage(DiskImage template) throws OpsException {
    DiskImage best = null;
    try {
        for (DiskImage candidate : platformLayer.listItems(DiskImage.class)) {
            if (isMatch(candidate, template)) {
                best = candidate;
                break;
            }
        }
        if (best == null) {
            // We should be owned by the recipe
            PlatformLayerKey recipeKey = template.getRecipeId();
            if (recipeKey != null) {
                template.getTags().add(Tag.buildParentTag(recipeKey));
            }
            best = platformLayer.putItem(template);
        }
    } catch (PlatformLayerClientException e) {
        throw new OpsException("Error fetching or building image", e);
    }
    return best;
}
Also used : PlatformLayerClientException(org.platformlayer.PlatformLayerClientException) OpsException(org.platformlayer.ops.OpsException) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) DiskImage(org.platformlayer.images.model.DiskImage)

Example 93 with OpsException

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

the class PlatformLayerHelpers method setUniqueTags.

public void setUniqueTags(PlatformLayerKey key, Iterable<Tag> uniqueTags) throws OpsException {
    Tags tags = getItemTags(key);
    TagChanges tagChanges = new TagChanges();
    for (Tag setTag : uniqueTags) {
        String tagKey = setTag.getKey();
        List<String> existing = tags.findAll(tagKey);
        if (existing == null || existing.isEmpty()) {
            tagChanges.addTags.add(setTag);
        } else if (existing.size() == 1) {
            String existingValue = existing.get(0);
            if (!Objects.equal(existingValue, setTag.value)) {
                tagChanges.addTags.add(setTag);
                tagChanges.removeTags.add(Tag.build(tagKey, existingValue));
            }
        } else {
            // We probably should replace existing tags...
            throw new OpsException("Found duplicate tag for: " + setTag.key);
        }
    }
    if (!tagChanges.isEmpty()) {
        changeTags(key, tagChanges);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Tag(org.platformlayer.core.model.Tag) TagChanges(org.platformlayer.core.model.TagChanges) Tags(org.platformlayer.core.model.Tags)

Example 94 with OpsException

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

the class SolrSchemaFile method getContentsBytes.

@Override
protected byte[] getContentsBytes() throws OpsException {
    InputStream is = getClass().getResourceAsStream("schema.xml");
    Document dom;
    try {
        boolean isNamespaceAware = true;
        dom = XmlHelper.parseXmlDocument(is, isNamespaceAware);
    } catch (ParserConfigurationException e) {
        throw new OpsException("Error parsing XML template", e);
    } catch (SAXException e) {
        throw new OpsException("Error parsing XML template", e);
    } catch (IOException e) {
        throw new OpsException("Error parsing XML template", e);
    }
    SolrTemplateData template = OpsContext.get().getInjector().getInstance(SolrTemplateData.class);
    Element fieldsElement;
    {
        NodeList fieldsList = dom.getElementsByTagName("fields");
        if (fieldsList.getLength() != 1) {
            throw new OpsException("Expected exactly one fields element");
        }
        fieldsElement = (Element) fieldsList.item(0);
    }
    // TODO: Turn off default dynamic fields??
    for (SolrSchemaField field : template.getFields()) {
        boolean isDynamic = field.name.contains("*");
        Element el = dom.createElement(isDynamic ? "dynamicField" : "field");
        el.setAttribute("name", field.name);
        el.setAttribute("type", field.type);
        el.setAttribute("indexed", String.valueOf(field.indexed));
        el.setAttribute("stored", String.valueOf(field.stored));
        el.setAttribute("multiValued", String.valueOf(field.multiValued));
        // Compression removed in 1.4.1
        // if (field.compressThreshold >= 0) {
        // el.setAttribute("compressed", "true");
        // el.setAttribute("compressThreshold", String.valueOf(field.compressThreshold));
        // }
        fieldsElement.appendChild(el);
    }
    return Utf8.getBytes(DomUtils.toXml(dom));
}
Also used : OpsException(org.platformlayer.ops.OpsException) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SolrSchemaField(org.platformlayer.service.solr.model.SolrSchemaField) SAXException(org.xml.sax.SAXException)

Example 95 with OpsException

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

the class CloudInstanceMapper method doOperation.

@Handler
public void doOperation() throws OpsException, IOException {
    Tags instanceTags = instance.getTags();
    GoogleCloud cloud = findCloud();
    if (cloud == null) {
        throw new OpsException("Could not find cloud");
    }
    GoogleComputeClient computeClient = googleComputeClientFactory.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));
            PublicKey servicePublicKey = service.getSshKey().getKeyPair().getPublic();
            Instance created = computeClient.createInstance(cloud, request, servicePublicKey);
            {
                Tag instanceTag = Tag.build(Tag.ASSIGNED, created.getName());
                platformLayer.addTag(instance.getKey(), instanceTag);
            }
            assignedInstanceIds.add(created.getName());
        }
    }
    if (assignedInstanceIds.isEmpty() && !OpsContext.isDelete()) {
        throw new OpsException("Instance not yet assigned");
    }
    GoogleComputeMachine 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);
        Instance server = computeClient.findInstanceByName(assignedInstanceId);
        if (server == null) {
            if (OpsContext.isConfigure()) {
                throw new OpsException("Unable to find assigned server: " + assignedInstanceId);
            }
        } else {
            server = computeClient.ensureHasPublicIp(server);
            machine = new GoogleComputeMachine(computeClient, cloud, server);
            SshKey sshKey = service.getSshKey();
            target = machine.getTarget(GoogleComputeClient.USER_NAME, sshKey.getKeyPair());
            // We need to use sudo while we set up root access
            ((SshOpsTarget) target).setEnsureRunningAsRoot(true);
        }
    }
    if (!assignedInstanceIds.isEmpty() && OpsContext.isDelete()) {
        for (String instanceId : assignedInstanceIds) {
            Instance server = computeClient.findInstanceByName(instanceId);
            if (server == null) {
                log.warn("Could not find assigned server: " + instanceId + ", ignoring");
                continue;
            }
            // TODO: Remove associated firewall rules
            log.warn("Deleting firewall rules not yet implemented");
            // SecurityGroup securityGroup = null;
            // if (supportsSecurityGroups) {
            // securityGroup = openstackHelpers.getMachineSecurityGroup(computeClient, server);
            // }
            Operation terminateOperation = computeClient.terminateInstance(instanceId);
            try {
                computeClient.waitComplete(terminateOperation, 5, TimeUnit.MINUTES);
            } catch (TimeoutException e) {
                throw new OpsException("Timeout while waiting for instance termination", e);
            }
        // 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());
        // }
        // }
        }
        if (machine != null) {
            machine.refreshState();
        }
    }
    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) Instance(com.google.api.services.compute.model.Instance) GoogleCloudInstance(org.platformlayer.service.cloud.google.model.GoogleCloudInstance) PublicKey(java.security.PublicKey) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) MachineCreationRequest(org.platformlayer.ops.MachineCreationRequest) Operation(com.google.api.services.compute.model.Operation) SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) GoogleComputeClient(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeClient) GoogleCloud(org.platformlayer.service.cloud.google.model.GoogleCloud) Tag(org.platformlayer.core.model.Tag) Tags(org.platformlayer.core.model.Tags) GoogleComputeMachine(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine) TimeoutException(java.util.concurrent.TimeoutException) Handler(org.platformlayer.ops.Handler)

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