Search in sources :

Example 1 with DnsServer

use of org.platformlayer.service.dns.model.DnsServer 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 2 with DnsServer

use of org.platformlayer.service.dns.model.DnsServer in project platformlayer by platformlayer.

the class DnsService method beforeCreateItem.

@Override
public void beforeCreateItem(ItemBase item) throws OpsException {
    if (item instanceof DnsServer) {
        DnsServer model = (DnsServer) item;
        model.dnsName = normalize(model.dnsName);
        if (Strings.isNullOrEmpty(model.dnsName)) {
            throw new IllegalArgumentException("dnsName must be specified");
        }
    }
    if (item instanceof DnsRecord) {
        DnsRecord model = (DnsRecord) item;
        model.dnsName = normalize(model.dnsName);
        if (Strings.isNullOrEmpty(model.dnsName)) {
            throw new IllegalArgumentException("dnsName must be specified");
        }
        if (Strings.isNullOrEmpty(model.recordType)) {
            model.recordType = "A";
        }
        if (model.recordType.equals("A")) {
        // OK
        } else {
            throw new IllegalArgumentException("Record type not supported: " + model.recordType);
        }
    }
    if (item instanceof DnsZone) {
        DnsZone model = (DnsZone) item;
        model.dnsName = normalize(model.dnsName);
        if (Strings.isNullOrEmpty(model.dnsName)) {
            model.dnsName = normalize(model.getId());
        }
    }
    super.beforeCreateItem(item);
}
Also used : DnsRecord(org.platformlayer.dns.model.DnsRecord) DnsServer(org.platformlayer.service.dns.model.DnsServer) DnsZone(org.platformlayer.service.dns.model.DnsZone)

Example 3 with DnsServer

use of org.platformlayer.service.dns.model.DnsServer in project platformlayer by platformlayer.

the class DnsHelpers method uploadToAllDnsServers.

public void uploadToAllDnsServers(ZoneFile dnsFile) throws OpsException {
    // TODO: Likely problems with concurrent creation of servers and domain entries??
    boolean failed = false;
    for (DnsServer dnsServer : platformLayer.listItems(DnsServer.class)) {
        switch(dnsServer.state) {
            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;
        }
        // TODO: We could have a 'cluster key' that is used to tag the
        // machines providing this service
        Machine machine = instances.findMachine(dnsServer);
        if (machine == null) {
            log.warn("Dns server instance not found: " + dnsServer);
            failed = true;
            continue;
        }
        OpsTarget target = machine.getTarget(service.getSshKey());
        try {
            upload(target, dnsFile);
        } catch (OpsException e) {
            log.warn("Error updloading dns to server: " + dnsServer, e);
            failed = true;
            continue;
        }
    }
    if (failed) {
        throw new OpsException("Could not update all DNS servers in cluster").setRetry(TimeSpan.ONE_MINUTE);
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) OpsException(org.platformlayer.ops.OpsException) DnsServer(org.platformlayer.service.dns.model.DnsServer) Machine(org.platformlayer.ops.Machine)

Aggregations

DnsServer (org.platformlayer.service.dns.model.DnsServer)3 DnsRecord (org.platformlayer.dns.model.DnsRecord)2 OpsException (org.platformlayer.ops.OpsException)2 EndpointInfo (org.platformlayer.core.model.EndpointInfo)1 Machine (org.platformlayer.ops.Machine)1 OpsTarget (org.platformlayer.ops.OpsTarget)1 DnsZone (org.platformlayer.service.dns.model.DnsZone)1