Search in sources :

Example 51 with OpsException

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

the class GoogleComputeClient method listMachineTypes.

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

Example 52 with OpsException

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

the class GoogleComputeClient method deleteFirewallRule.

public void deleteFirewallRule(Firewall rule) throws OpsException {
    try {
        log.debug("Deleting firewall rule: " + rule);
        Operation operation = compute.firewalls().delete(projectId, rule.getName()).execute();
        waitComplete(operation, 5, TimeUnit.MINUTES);
    // TODO: Check success of operation?
    } catch (IOException e) {
        throw new OpsException("Error deleting firewall", e);
    } catch (TimeoutException e) {
        throw new OpsException("Timeout while waiting for firewall deletion", e);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 53 with OpsException

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

the class GoogleComputeMachine method terminate.

@Override
public void terminate() throws OpsException {
    try {
        Operation operation = computeClient.terminateInstance(instance.getName());
        computeClient.waitComplete(operation, 5, TimeUnit.MINUTES);
    } catch (TimeoutException e) {
        throw new OpsException("Timeout waiting for instance termination", e);
    }
    refreshState();
}
Also used : OpsException(org.platformlayer.ops.OpsException) Operation(com.google.api.services.compute.model.Operation) TimeoutException(java.util.concurrent.TimeoutException)

Example 54 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 {
    Tag tag = Tag.build(Tag.ASSIGNED, instance.getKey().getUrl());
    List<DirectHost> hosts = Lists.newArrayList(platformLayer.listItems(DirectHost.class, TagFilter.byTag(tag)));
    if (hosts.size() > 1) {
        // Huh?
        throw new OpsException("Multiple hosts already assigned");
    }
    DirectHost host;
    if (hosts.isEmpty()) {
        if (OpsContext.isDelete()) {
            host = null;
        } else {
            if (createInstance) {
                DirectCloudHost cloudHost = cloudMap.pickHost(instance);
                host = cloudHost.getModel();
                platformLayer.addTag(host.getKey(), tag);
            } else {
                throw new OpsException("Instance not yet assigned");
            }
        }
    } else {
        host = hosts.get(0);
    }
    RecursionState recursion = getRecursionState();
    if (host != null) {
        this.cloud = platformLayer.getItem(host.cloud, DirectCloud.class);
        this.hostTarget = directHelpers.toTarget(host);
        recursion.pushChildScope(cloud);
        recursion.pushChildScope(host);
        recursion.pushChildScope(hostTarget);
    } else {
        if (!OpsContext.isDelete()) {
            throw new IllegalStateException();
        }
        log.info("No host set; won't recurse in");
        recursion.setPreventRecursion(true);
    }
}
Also used : DirectCloud(org.platformlayer.service.cloud.direct.model.DirectCloud) DirectHost(org.platformlayer.service.cloud.direct.model.DirectHost) OpsException(org.platformlayer.ops.OpsException) DirectCloudHost(org.platformlayer.service.cloud.direct.ops.cloud.DirectCloudHost) Tag(org.platformlayer.core.model.Tag) Handler(org.platformlayer.ops.Handler)

Example 55 with OpsException

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

the class NetworkTunDevice method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (OpsContext.isConfigure()) {
        Command findCommand = Command.build("/sbin/ifconfig {0}", interfaceName);
        boolean found = false;
        try {
            target.executeCommand(findCommand);
            found = true;
        } catch (ProcessExecutionException e) {
            ProcessExecution execution = e.getExecution();
            if (execution.getExitCode() == 1 && execution.getStdErr().contains("Device not found")) {
                found = false;
            } else {
                throw new OpsException("Error checking for interface", e);
            }
        }
        if (!found) {
            // This is actually idempotent, but I think it's scary to rely on it being so
            Command command = Command.build("/usr/sbin/tunctl -t {0}", interfaceName);
            target.executeCommand(command);
        }
        {
            // TODO: Safe to re-run?
            Command command = Command.build("ifconfig {0} up", interfaceName);
            target.executeCommand(command);
        }
        if (bridgeName != null) {
            // TODO: Safe to re-run?
            Command command = Command.build("brctl addif {0} {1}", bridgeName.get(), interfaceName);
            try {
                target.executeCommand(command);
            } catch (ProcessExecutionException e) {
                ProcessExecution execution = e.getExecution();
                if (execution.getExitCode() == 1 && execution.getStdErr().contains("already a member of a bridge")) {
                // OK
                // TODO: Check that the bridge is bridgeName
                } else {
                    throw new OpsException("Error attaching interface to bridge", e);
                }
            }
        }
    }
    if (OpsContext.isDelete()) {
        // TODO: Implement
        log.warn("NetworkTunDevice delete not implemented");
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) 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