Search in sources :

Example 31 with OpsException

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

the class PlatformLayerFirewallEntry method addChildren.

@Override
protected void addChildren() throws OpsException {
    // TODO: Need to register a dependency on destItem?
    MachineResolver dest = MachineResolver.build(destItem);
    addChild(dest);
    List<Transport> transports;
    if (transport == null) {
        String cidr = sourceCidr;
        if (!Strings.isNullOrEmpty(sourceCidr)) {
            IpRange range = IpRange.parse(cidr);
            if (range.isIpv6()) {
                transport = Transport.Ipv6;
            } else {
                transport = Transport.Ipv4;
            }
        }
    }
    if (transport == null) {
        transports = Transport.all();
    } else {
        transports = Collections.singletonList(transport);
    }
    for (final Transport transport : transports) {
        if (!Strings.isNullOrEmpty(sourceCidr)) {
            IptablesFilterEntry entry = dest.addChild(IptablesFilterEntry.class);
            entry.port = port;
            entry.sourceCidr = sourceCidr;
            entry.protocol = protocol;
            entry.transport = transport;
            entry.ruleKey = uniqueId;
        } else if (sourceItemKey != null) {
            LateBound<IptablesFilterEntry> entry = new LateBound<IptablesFilterEntry>() {

                @Override
                public IptablesFilterEntry get() throws OpsException {
                    ItemBase sourceItem = platformLayerHelpers.getItem(sourceItemKey);
                    NetworkPoint targetNetworkPoint = NetworkPoint.forTargetInContext();
                    boolean required = !OpsContext.isDelete();
                    Machine sourceMachine = instanceHelpers.getMachine(sourceItem, required);
                    if (sourceMachine == null) {
                        // TODO: Store by key? Delete by key?
                        log.warn("Source machine not found for firewall rule; assuming already deleted");
                        return null;
                    }
                    String sourceCidr = null;
                    List<InetAddress> addresses = sourceMachine.getNetworkPoint().findAddresses(targetNetworkPoint);
                    if (transport == Transport.Ipv4) {
                        Iterables.removeIf(addresses, InetAddressUtils.IS_IPV6);
                        if (addresses.size() == 1) {
                            sourceCidr = addresses.get(0).getHostAddress() + "/32";
                        } else {
                            if (addresses.isEmpty()) {
                                return null;
                            }
                            throw new IllegalStateException("Not implemented");
                        }
                    } else {
                        Iterables.removeIf(addresses, InetAddressUtils.IS_IPV4);
                        if (addresses.size() == 1) {
                            sourceCidr = addresses.get(0).getHostAddress() + "/128";
                        } else {
                            if (addresses.isEmpty()) {
                                return null;
                            }
                            throw new IllegalStateException("Not implemented");
                        }
                    }
                    IptablesFilterEntry entry = injected(IptablesFilterEntry.class);
                    entry.port = port;
                    entry.sourceCidr = sourceCidr;
                    entry.protocol = protocol;
                    entry.transport = transport;
                    entry.ruleKey = uniqueId;
                    return entry;
                }

                @Override
                public String getDescription() throws Exception {
                    return "Firewall rules";
                }
            };
            dest.addChild(entry);
        } else {
            // Both empty => wildcard
            IptablesFilterEntry entry = dest.addChild(IptablesFilterEntry.class);
            entry.port = port;
            entry.protocol = protocol;
            entry.transport = transport;
            entry.ruleKey = uniqueId;
        }
    }
// TODO: Add source rules??
}
Also used : IpRange(org.platformlayer.ops.networks.IpRange) OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) IptablesFilterEntry(org.platformlayer.ops.firewall.scripts.IptablesFilterEntry) NetworkPoint(org.platformlayer.ops.networks.NetworkPoint) Machine(org.platformlayer.ops.Machine) OpsException(org.platformlayer.ops.OpsException) List(java.util.List) Transport(org.platformlayer.ops.firewall.Transport) LateBound(org.platformlayer.ops.tree.LateBound)

Example 32 with OpsException

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

the class DnsHelpers method buildDnsFile.

public ZoneFile buildDnsFile(DnsRecord dnsRecord) throws OpsException {
    List<DnsZone> matches = Lists.newArrayList();
    for (DnsZone dnsZone : platformLayer.listItems(DnsZone.class)) {
        if (isInZone(dnsRecord, dnsZone)) {
            matches.add(dnsZone);
        }
    }
    if (matches.size() == 0) {
        throw new OpsException("Cannot find zone for record: " + dnsRecord.dnsName);
    }
    if (matches.size() != 1) {
        throw new OpsException("Picking between multiple matching zones not yet implemented");
    }
    DnsZone dnsZone = matches.get(0);
    return buildDnsFile(dnsZone);
}
Also used : OpsException(org.platformlayer.ops.OpsException) DnsZone(org.platformlayer.service.dns.model.DnsZone)

Example 33 with OpsException

use of org.platformlayer.ops.OpsException 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 34 with OpsException

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

the class GerritDatabaseController method addChildren.

@Override
protected void addChildren() throws OpsException {
    GerritDatabaseTemplate template = injected(GerritDatabaseTemplate.class);
    DatabaseConnection dbConnection;
    {
        dbConnection = addChild(DatabaseConnection.build(template.getDatabaseServerKey()));
        dbConnection.databaseName = template.getDatabaseName();
    }
    {
        CreateDatabase db = dbConnection.addChild(CreateDatabase.class);
        db.databaseName = template.getDatabaseName();
    }
    {
        CreateUser db = dbConnection.addChild(CreateUser.class);
        db.grantDatabaseName = template.getDatabaseName();
        db.databaseUser = template.getDatabaseUsername();
        db.databasePassword = template.getDatabasePassword();
    }
    {
        RunScript script = dbConnection.addChild(RunScript.class);
        try {
            script.sql = ResourceUtils.get(getClass(), "schema.sql");
        } catch (IOException e) {
            throw new OpsException("Error loading SQL script resource", e);
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) CreateUser(org.platformlayer.ops.postgres.CreateUser) DatabaseConnection(org.platformlayer.ops.postgres.DatabaseConnection) CreateDatabase(org.platformlayer.ops.postgres.CreateDatabase) RunScript(org.platformlayer.ops.postgres.RunScript) IOException(java.io.IOException)

Example 35 with OpsException

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

the class GitServerAssignment method handler.

@Handler
public void handler(GitRepository model) throws Exception {
    PlatformLayerKey assignedTo = Tag.ASSIGNED_TO.findUnique(model.getTags());
    if (OpsContext.isConfigure()) {
        if (assignedTo == null) {
            List<GitService> gitServices = platformLayer.listItems(GitService.class);
            if (gitServices.size() == 0) {
                throw new OpsException("No git service found");
            }
            GitService gitService = RandomChooser.chooseRandom(gitServices);
            if (gitService == null) {
                throw new IllegalStateException();
            }
            assignedTo = gitService.getKey();
            platformLayer.addTag(model.getKey(), Tag.ASSIGNED_TO.build(assignedTo));
        }
    }
    GitService gitService = null;
    if (assignedTo != null) {
        gitService = platformLayer.getItem(assignedTo, GitService.class);
    }
    if (OpsContext.isDelete()) {
        if (gitService == null) {
            log.info("Deleting, but not assigned to a server; nothing to do");
            getRecursionState().setPreventRecursion(true);
            return;
        }
    }
    if (gitService == null) {
        throw new OpsException("No git servers found");
    }
    if (gitService.getState() != ManagedItemState.ACTIVE) {
        throw new OpsException("Server not yet active: " + gitService);
    }
    Machine machine = instances.findMachine(gitService);
    if (machine == null) {
        throw new OpsException("Server machine not found:" + gitService);
    }
    SshKey sshKey = service.getSshKey();
    OpsTarget target = machine.getTarget(sshKey);
    getRecursionState().pushChildScope(OpsTarget.class, target);
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) OpsException(org.platformlayer.ops.OpsException) OpsTarget(org.platformlayer.ops.OpsTarget) GitService(org.platformlayer.service.git.model.GitService) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Machine(org.platformlayer.ops.Machine) 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