Search in sources :

Example 11 with OpsException

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

the class AptPackageManager method getInstalledPackageInfo.

public List<String> getInstalledPackageInfo(OpsTarget target) throws OpsException {
    AptInfoCache cached = getCache(target);
    if (cached.installedPackages == null) {
        Command command = Command.build("/usr/bin/dpkg --get-selections");
        ProcessExecution execution = target.executeCommand(command);
        final List<String> packages = Lists.newArrayList();
        for (String line : Splitter.on("\n").split(execution.getStdOut())) {
            line = line.trim();
            if (line.isEmpty()) {
                continue;
            }
            List<String> tokens = Lists.newArrayList(Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings().split(line));
            if (tokens.size() != 2) {
                throw new OpsException("Error parsing line; expected 2 items: " + line);
            }
            String state = tokens.get(1);
            if (state.equals("install")) {
                String packageName = tokens.get(0);
                int colonIndex = packageName.indexOf(':');
                if (colonIndex != -1) {
                    // Architecture sometimes follows package name
                    packageName = packageName.substring(0, colonIndex);
                }
                packages.add(packageName);
            } else if (state.equals("deinstall")) {
            // Not installed (?)
            } else {
                throw new OpsException("Unknown package state in line: " + line);
            }
        }
        cached.installedPackages = packages;
    } else {
        log.debug("Re-using cached package info");
    }
    return cached.installedPackages;
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 12 with OpsException

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

the class FilesystemBackedPool method release.

@Override
public void release(PlatformLayerKey owner, T item) throws OpsException {
    File symlink = new File(assignedDir, toKey(item));
    FilesystemInfo info = target.getFilesystemInfoFile(symlink);
    if (info == null) {
        throw new OpsException("Symlink not found");
    }
    if (!Objects.equal(info.symlinkTarget, toFile(owner).getAbsolutePath())) {
        throw new OpsException("Resource not assigned to owner");
    }
    target.rm(symlink);
}
Also used : FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) OpsException(org.platformlayer.ops.OpsException) File(java.io.File)

Example 13 with OpsException

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

the class IpsecHelpers method getIpsecSecret.

public Secret getIpsecSecret() throws OpsException {
    Secret secret = null;
    for (ProviderOf<HasIpsecPolicy> ipsecPolicyProvider : providerHelper.listItemsProviding(HasIpsecPolicy.class)) {
        ItemBase item = ipsecPolicyProvider.getItem();
        if (item.getState() != ManagedItemState.ACTIVE) {
            continue;
        }
        HasIpsecPolicy ipsec = ipsecPolicyProvider.get();
        if (secret != null) {
            throw new IllegalStateException("Multiple IPSEC policies found");
        }
        secret = ipsec.getIpsecPreSharedKey(item);
        if (secret == null) {
            // Should we allow this?
            throw new IllegalStateException();
        }
    }
    if (secret == null) {
        throw new OpsException("Ipsec policy not found");
    }
    return secret;
}
Also used : Secret(org.platformlayer.core.model.Secret) OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase)

Example 14 with OpsException

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

the class ForEach method doRecursion.

public void doRecursion(Object controller, SshKey sshKey, Class<? extends ItemBase> machineItemClass, Class<? extends ItemBase> dataItemClass) throws OpsException {
    boolean failed = false;
    OpsContext ops = OpsContext.get();
    List<ItemBase> dataItems = Lists.newArrayList();
    ItemBase contextDataItem = ops.getInstance(dataItemClass);
    if (contextDataItem != null) {
        dataItems.add(contextDataItem);
    } else {
        for (ItemBase dataItem : platformLayer.listItems(dataItemClass)) {
            dataItems.add(dataItem);
        }
    }
    Object contextMachine = ops.getInstance(machineItemClass);
    if (contextMachine != null) {
        // We are presumably building the machine item
        PlatformLayerKey targetItemKey = ops.getJobRecord().getTargetItemKey();
        ItemBase machineItem = (ItemBase) contextMachine;
        if (!Objects.equal(targetItemKey, machineItem.getKey())) {
            throw new OpsException("Expected to find same model");
        }
        Machine machine = instances.findMachine(machineItem);
        if (machine == null) {
            log.warn("Server instance not found: " + contextMachine);
            failed = true;
        } else {
            OpsTarget target = machine.getTarget(sshKey);
            failed |= processDataItems(controller, dataItems, machineItem, machine, target);
        }
    } else {
        // We are building a data item
        for (ItemBase machineItem : platformLayer.listItems(machineItemClass)) {
            if (machineItem.getState() != ManagedItemState.ACTIVE) {
                log.warn("Machine not yet active: " + machineItem);
                failed = true;
                continue;
            }
            Machine machine = instances.findMachine(machineItem);
            if (machine == null) {
                log.warn("Server instance not found: " + machineItem);
                failed = true;
                continue;
            }
            OpsTarget target = machine.getTarget(sshKey);
            failed |= processDataItems(controller, dataItems, machineItem, machine, target);
        }
    }
    if (failed) {
        throw new OpsException("Could not update all servers").setRetry(TimeSpan.ONE_MINUTE);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) OpsTarget(org.platformlayer.ops.OpsTarget) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) OpsContext(org.platformlayer.ops.OpsContext) Machine(org.platformlayer.ops.Machine)

Example 15 with OpsException

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

the class ZookeeperStatusChecker method handler.

@Handler
public void handler(OpsTarget target, ZookeeperServer zookeeperServer) throws OpsException {
    if (OpsContext.isConfigure() || OpsContext.isValidate()) {
        int port = ZookeeperConstants.ZK_PUBLIC_PORT;
        List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(zookeeperServer.getTags(), port);
        EndpointInfo endpoint = EndpointChooser.any().choose(endpoints);
        if (endpoint == null) {
            throw new OpsException("Cannot find endpoint for zookeeper");
        }
        InetSocketAddress socketAddress = endpoint.asSocketAddress();
        {
            ZookeeperResponse response;
            try {
                // IPV6 requires ipsec; use the IPV4 loopback instead
                socketAddress = new InetSocketAddress(InetAddresses.forString("127.0.0.1"), socketAddress.getPort());
                response = ZookeeperUtils.sendCommand(target, socketAddress, "ruok");
                Deviations.assertEquals("imok", response.getRaw(), "Zookeeper ruok status");
            } catch (OpsException e) {
                Deviations.fail("Unable to connect to zookeeper", e);
            }
        }
        {
            ZookeeperResponse response;
            try {
                response = ZookeeperUtils.sendCommand(target, socketAddress, "srvr");
                Map<String, String> responseMap = response.asMap();
                String mode = responseMap.get("Mode");
                Deviations.assertIn(Arrays.asList("follower", "leader"), mode, "Zookeeper mode");
            } catch (OpsException e) {
                Deviations.fail("Unable to connect to zookeeper", e);
            }
        }
    }
}
Also used : EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsException(org.platformlayer.ops.OpsException) ZookeeperResponse(org.platformlayer.service.zookeeper.ops.ZookeeperUtils.ZookeeperResponse) InetSocketAddress(java.net.InetSocketAddress) Map(java.util.Map) 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