Search in sources :

Example 86 with OpsException

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

the class HttpProxyHelper method chooseProxy.

private String chooseProxy(OpsTarget target, List<String> proxies) {
    String bestProxy = null;
    TimeSpan bestTime = null;
    for (String proxy : proxies) {
        // {
        // // We choose the fastest proxy that gives us a 200 response
        // String url = proxy + "acng-report.html";
        // CurlRequest request = new CurlRequest(url);
        // request.setTimeout(5);
        // try {
        // CurlResult result = request.executeRequest(target);
        // if (result.getHttpResult() != 200) {
        // log.info("Unexpected response code while testing proxy: " + proxy + ".  Code=" + result.getHttpResult());
        // continue;
        // }
        // TimeSpan timeTotal = result.getTimeTotal();
        // if (bestTime == null || timeTotal.isLessThan(bestTime)) {
        // bestProxy = proxy;
        // bestTime = timeTotal;
        // }
        // } catch (ProcessExecutionException e) {
        // log.info("Error while testing proxy: " + proxy, e);
        // }
        // }
        {
            // We choose the fastest proxy that gives us a 200 response
            String url = "http://ftp.debian.org/debian/dists/stable/Release.gpg";
            CurlRequest request = new CurlRequest(url);
            request.proxy = proxy;
            request.timeout = TimeSpan.FIVE_SECONDS;
            try {
                CurlResult result = request.executeRequest(target);
                if (result.getHttpResult() != 200) {
                    log.info("Unexpected response code while testing proxy: " + proxy + ".  Code=" + result.getHttpResult());
                    continue;
                }
                TimeSpan timeTotal = result.getTimeTotal();
                if (bestTime == null || timeTotal.isLessThan(bestTime)) {
                    bestProxy = proxy;
                    bestTime = timeTotal;
                }
            } catch (OpsException e) {
                log.info("Error while testing proxy: " + proxy, e);
            }
        }
    }
    return bestProxy;
}
Also used : TimeSpan(com.fathomdb.TimeSpan) CurlResult(org.platformlayer.ops.helpers.CurlResult) OpsException(org.platformlayer.ops.OpsException) CurlRequest(org.platformlayer.ops.helpers.CurlRequest)

Example 87 with OpsException

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

the class HttpBackends method buildUri.

public URI buildUri(NetworkPoint src, String scheme, ItemBase model, int port) throws OpsException {
    Machine machine = instances.getMachine(model);
    InetAddressChooser chooser = InetAddressChooser.preferIpv6();
    InetAddress address = machine.getNetworkPoint().getBestAddress(src, chooser);
    String host = InetAddresses.toAddrString(address);
    URI uri;
    try {
        uri = new URI(scheme, null, host, port, null, null, null);
    } catch (URISyntaxException e) {
        throw new OpsException("Error building URI", e);
    }
    return uri;
}
Also used : OpsException(org.platformlayer.ops.OpsException) InetAddressChooser(org.platformlayer.InetAddressChooser) URISyntaxException(java.net.URISyntaxException) InetAddress(java.net.InetAddress) URI(java.net.URI) Machine(org.platformlayer.ops.Machine)

Example 88 with OpsException

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

the class ConfigMap method write.

public static boolean write(OpsTarget target, File path, Properties properties) throws OpsException {
    String contents;
    try {
        StringWriter sw = new StringWriter();
        properties.store(sw, null);
        contents = sw.toString();
    } catch (IOException e) {
        throw new OpsException("Error serializing properties", e);
    }
    // String existing = target.readTextFile(path);
    // if (Objects.equal(existing, contents)) {
    // return false;
    // }
    FileUpload.upload(target, path, contents);
    return true;
}
Also used : OpsException(org.platformlayer.ops.OpsException) StringWriter(java.io.StringWriter) IOException(java.io.IOException)

Example 89 with OpsException

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

the class DirectImageStore method bringToMachine.

@Override
public void bringToMachine(String imageId, OpsTarget destination, File destinationPath) throws OpsException {
    Properties imageProperties = fileStore.readProperties(imageId);
    if (imageProperties == null) {
        throw new OpsException("Image not found: " + imageId);
    }
    File imageFile = getImageFile(imageId, imageProperties);
    if (destination.isSameMachine(target)) {
        Command copyCommand = Command.build("cp {0} {1}", imageFile, destinationPath);
        destination.executeCommand(copyCommand.setTimeout(TimeSpan.FIVE_MINUTES));
    } else {
        PeerToPeerCopy peerToPeerCopy = Injection.getInstance(PeerToPeerCopy.class);
        peerToPeerCopy.copy(target, imageFile, destination, destinationPath);
    // throw new OpsException("SCPing images between machines not yet implemented");
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) Properties(java.util.Properties) File(java.io.File)

Example 90 with OpsException

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

the class PublicEndpoint method addChildren.

@Override
protected void addChildren() throws OpsException {
    final OwnedEndpoint endpoint;
    {
        endpoint = addChild(OwnedEndpoint.class);
        endpoint.publicPort = publicPort;
        endpoint.publicPortCluster = publicPortCluster;
        endpoint.backendPort = backendPort;
        endpoint.parentItem = parentItem;
        endpoint.transport = transport;
    }
    if (tagItem != null) {
        Tagger tagger = injected(Tagger.class);
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() throws OpsException {
                int maxAttempts = 5;
                int attempt = 0;
                PublicEndpointBase item = endpoint.getItem();
                while (true) {
                    attempt++;
                    if (item == null) {
                        if (!OpsContext.isDelete()) {
                            throw new OpsException("Endpoint not created");
                        } else {
                            log.warn("No endpoint => no tagging to be done");
                            return null;
                        }
                    }
                    List<EndpointInfo> endpointInfos = EndpointInfo.findEndpoints(item.getTags(), publicPort);
                    if (!endpointInfos.isEmpty()) {
                        TagChanges tagChanges = new TagChanges();
                        for (EndpointInfo endpointInfo : endpointInfos) {
                            tagChanges.addTags.add(endpointInfo.toTag());
                        }
                        return tagChanges;
                    }
                    if (attempt != maxAttempts) {
                        log.info("Endpoint not yet found; sleeping and retrying");
                        TimeSpan.FIVE_SECONDS.doSafeSleep();
                        item = platformLayerClient.getItem(item.getKey());
                        continue;
                    } else {
                        throw new OpsException("Cannot find endpoint for port: " + publicPort);
                    }
                }
            }
        };
        tagger.platformLayerKey = tagItem;
        tagger.tagChangesProvider = tagChanges;
        addChild(tagger);
    }
    if (defaultBlocked) {
        // Block on machine's firewall
        log.warn("Not adding firewall block; relying on default block");
    // addChild(IptablesFirewallEntry.build(FirewallRecord.buildBlockPort(protocol, backendPort)));
    }
    if (!Strings.isNullOrEmpty(dnsName)) {
        EndpointDnsRecord dns = injected(EndpointDnsRecord.class);
        dns.destinationPort = publicPort;
        dns.endpointProvider = new Provider<PublicEndpointBase>() {

            @Override
            public PublicEndpointBase get() {
                return endpoint.getItem();
            }
        };
        dns.dnsName = dnsName;
        addChild(dns);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Tagger(org.platformlayer.ops.tagger.Tagger) TagChanges(org.platformlayer.core.model.TagChanges) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) EndpointDnsRecord(org.platformlayer.ops.dns.EndpointDnsRecord) PublicEndpointBase(org.platformlayer.core.model.PublicEndpointBase)

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