Search in sources :

Example 6 with EndpointInfo

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

the class DnsHelpers method buildDnsFile.

public ZoneFile buildDnsFile(DnsZone dnsZone) throws OpsException {
    ZoneFile dnsFile = new ZoneFile(dnsZone.dnsName);
    Iterable<DnsServer> dnsServers = platformLayer.listItems(DnsServer.class);
    for (DnsServer dnsServer : dnsServers) {
        switch(dnsServer.getState()) {
            case DELETE_REQUESTED:
            case DELETED:
                log.info("Skipping server (deleted/deleting): " + dnsServer);
                continue;
            case ACTIVE:
                // Good
                break;
            default:
                log.warn("Dns server not yet active: " + dnsServer);
                // failed = true;
                continue;
        }
        List<EndpointInfo> dnsServerEndpoints = EndpointInfo.findEndpoints(dnsServer.getTags(), 53);
        if (dnsServerEndpoints.isEmpty()) {
            throw new OpsException("Cannot find endpoint for: " + dnsServer);
        }
        // Use the ID to produce a stable identifier
        // TODO: What if we shutdown nameservers? Should we do something like consistent hashing instead?
        // i.e. always create ns1 ... ns16, and then dynamically repoint them as we add/remove nameservers?
        // Does this really help?
        // String serverId = dnsServer"ns" + dnsServer.getId();
        String dnsName = dnsServer.dnsName;
        if (dnsName == null) {
            throw new OpsException("DnsName not set on " + dnsServer);
        }
        // TODO: This might not be the right address in complex networks
        for (EndpointInfo dnsServerEndpoint : dnsServerEndpoints) {
            String address = dnsServerEndpoint.publicIp;
            dnsFile.addNS(dnsZone.dnsName, address, dnsName);
        }
    }
    Iterable<DnsRecord> dnsRecords = platformLayer.listItems(DnsRecord.class);
    for (DnsRecord record : dnsRecords) {
        switch(record.getState()) {
            case DELETE_REQUESTED:
            case DELETED:
                log.info("Skipping record (deleted/deleting): " + record);
                continue;
            default:
                break;
        }
        if (!isInZone(record, dnsZone)) {
            continue;
        }
        dnsFile.addAddress(record.dnsName, record.address);
    }
    return dnsFile;
}
Also used : EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsException(org.platformlayer.ops.OpsException) DnsRecord(org.platformlayer.dns.model.DnsRecord) DnsServer(org.platformlayer.service.dns.model.DnsServer)

Example 7 with EndpointInfo

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

the class GetEndpoint method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException {
    // Should this be a tag?
    PlatformLayerClient client = getPlatformLayerClient();
    PlatformLayerKey key = path.resolve(getContext());
    UntypedItem untypedItem = client.getItemUntyped(key, Format.XML);
    List<EndpointInfo> endpoints = EndpointInfo.getEndpoints(untypedItem.getTags());
    return endpoints;
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) UntypedItem(org.platformlayer.common.UntypedItem) EndpointInfo(org.platformlayer.core.model.EndpointInfo) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 8 with EndpointInfo

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

the class TagFromChildren method handler.

@Handler
public void handler() throws OpsException {
    if (OpsContext.isConfigure()) {
        for (OwnedItem<?> childServer : parentController.getChildren(OwnedItem.class)) {
            ItemBase server = childServer.getItem();
            if (server == null) {
                // Right now, we have to go through a retry cycle
                throw new OpsException("Child server not ready");
            }
            List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(server.getTags(), port);
            if (endpoints.isEmpty()) {
                // TODO: Cope in future e.g. if we only need one of two in a cluster
                throw new OpsException("Child server not ready");
            }
            for (EndpointInfo endpoint : endpoints) {
                platformLayer.addTag(parentItem.getKey(), endpoint.toTag());
            }
        }
    }
}
Also used : EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) Handler(org.platformlayer.ops.Handler)

Example 9 with EndpointInfo

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

the class DirectPublicPorts method addChildren.

@Override
protected void addChildren() throws OpsException {
    final CloudInstanceMapper cloudHost;
    {
        cloudHost = injected(CloudInstanceMapper.class);
        cloudHost.createInstance = false;
        cloudHost.instance = backendItem;
        addChild(cloudHost);
    }
    final SocketAddressPoolAssignment assignPublicAddress;
    {
        assignPublicAddress = cloudHost.addChild(SocketAddressPoolAssignment.class);
        assignPublicAddress.holder = backendItem.getKey();
        if (Objects.equal(transport, Transport.Ipv6)) {
            assignPublicAddress.poolProvider = new OpsProvider<ResourcePool<InetSocketAddress>>() {

                @Override
                public ResourcePool<InetSocketAddress> get() throws OpsException {
                    final ResourcePool<AddressModel> pool = directCloudHelpers.getAddressPool6().get();
                    return new AssignPortToAddressPool(pool, publicPort);
                }
            };
        } else {
            List<Integer> publicPortCluster = this.publicPortCluster;
            if (publicPortCluster == null) {
                publicPortCluster = Lists.newArrayList();
                publicPortCluster.add(publicPort);
            }
            if (!publicPortCluster.contains(publicPort)) {
                throw new OpsException("Port set specified, but public port not in the set");
            }
            assignPublicAddress.poolProvider = directCloudHelpers.getPublicAddressPool4(publicPort, publicPortCluster);
        }
    }
    if (Objects.equal(transport, Transport.Ipv6)) {
        // TODO: Do we need separate frontend / backend ports really?
        if (this.publicPort != this.backendPort) {
            throw new UnsupportedOperationException();
        }
    } else {
        for (Protocol protocol : Protocol.TcpAndUdp()) {
            IptablesForwardPort forward = injected(IptablesForwardPort.class);
            forward.publicAddress = assignPublicAddress;
            forward.ruleKey = protocol.name() + "-" + uuid;
            forward.protocol = protocol;
            forward.privateAddress = new OpsProvider<String>() {

                @Override
                public String get() throws OpsException {
                    // Refresh item to pick up new tags
                    backendItem = platformLayerClient.getItem(backendItem.getKey(), DirectInstance.class);
                    PlatformLayerCloudMachine instanceMachine = (PlatformLayerCloudMachine) instanceHelpers.getMachine(backendItem);
                    DirectInstance instance = (DirectInstance) instanceMachine.getInstance();
                    List<InetAddress> addresses = Tag.NETWORK_ADDRESS.find(instance);
                    InetAddress address = InetAddressChooser.preferIpv4().choose(addresses);
                    if (address == null) {
                        throw new IllegalStateException();
                    }
                    if (InetAddressUtils.isIpv6(address)) {
                        // We can't NAT IPV4 -> IPV6 (I think)
                        throw new IllegalStateException();
                    }
                    return address.getHostAddress();
                }
            };
            forward.privatePort = backendPort;
            cloudHost.addChild(forward);
        }
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                TagChanges tagChanges = new TagChanges();
                InetSocketAddress socketAddress = assignPublicAddress.get();
                if (socketAddress == null) {
                    return null;
                }
                if (socketAddress.getPort() != publicPort) {
                    throw new IllegalStateException();
                }
                EndpointInfo endpoint = new EndpointInfo(socketAddress);
                tagChanges.addTags.add(endpoint.toTag());
                return tagChanges;
            }
        };
        for (ItemBase tagItem : tagItems) {
            Tagger tagger = addChild(Tagger.class);
            tagger.platformLayerKey = tagItem.getKey();
            tagger.tagChangesProvider = tagChanges;
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Tagger(org.platformlayer.ops.tagger.Tagger) SocketAddressPoolAssignment(org.platformlayer.ops.pool.SocketAddressPoolAssignment) ItemBase(org.platformlayer.core.model.ItemBase) InetSocketAddress(java.net.InetSocketAddress) PlatformLayerCloudMachine(org.platformlayer.ops.machines.PlatformLayerCloudMachine) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) TagChanges(org.platformlayer.core.model.TagChanges) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) AddressModel(org.platformlayer.core.model.AddressModel) List(java.util.List) Protocol(org.platformlayer.ops.firewall.Protocol) IptablesForwardPort(org.platformlayer.ops.firewall.scripts.IptablesForwardPort) InetAddress(java.net.InetAddress)

Example 10 with EndpointInfo

use of org.platformlayer.core.model.EndpointInfo 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)

Aggregations

EndpointInfo (org.platformlayer.core.model.EndpointInfo)12 OpsException (org.platformlayer.ops.OpsException)7 TagChanges (org.platformlayer.core.model.TagChanges)4 OpsProvider (org.platformlayer.ops.OpsProvider)4 Tagger (org.platformlayer.ops.tagger.Tagger)4 InetSocketAddress (java.net.InetSocketAddress)3 Handler (org.platformlayer.ops.Handler)3 PlatformLayerClient (org.platformlayer.PlatformLayerClient)2 UntypedItem (org.platformlayer.common.UntypedItem)2 ItemBase (org.platformlayer.core.model.ItemBase)2 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)2 PublicEndpointBase (org.platformlayer.core.model.PublicEndpointBase)2 DnsRecord (org.platformlayer.dns.model.DnsRecord)2 ClientAction (com.fathomdb.cli.output.ClientAction)1 InetAddress (java.net.InetAddress)1 List (java.util.List)1 Map (java.util.Map)1 PlatformLayerClientException (org.platformlayer.PlatformLayerClientException)1 AddressModel (org.platformlayer.core.model.AddressModel)1 Tag (org.platformlayer.core.model.Tag)1