Search in sources :

Example 6 with Machine

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

the class MachineResolver method doRecurseOperation.

@Override
public void doRecurseOperation() throws OpsException {
    ItemBase dest = platformLayerHelpers.getItem(key);
    boolean required = !OpsContext.isDelete();
    for (Machine machine : instanceHelpers.getMachines(dest, required)) {
        OpsTarget target = instanceHelpers.getTarget(dest, machine);
        BindingScope scope = BindingScope.push(machine, target);
        try {
            OpsContext opsContext = OpsContext.get();
            OperationRecursor.doRecurseChildren(opsContext, this);
        } finally {
            scope.pop();
        }
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) ItemBase(org.platformlayer.core.model.ItemBase) OpsContext(org.platformlayer.ops.OpsContext) Machine(org.platformlayer.ops.Machine) BindingScope(org.platformlayer.ops.BindingScope)

Example 7 with Machine

use of org.platformlayer.ops.Machine 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 8 with Machine

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

the class PrivateNetworkHelpers method findTunnelAddress.

public ThrowingProvider<String> findTunnelAddress(final PlatformLayerKey machineKey) {
    final NetworkPoint src = NetworkPoint.forPublicInternet();
    return new ThrowingProvider<String>() {

        @Override
        public String build() throws OpsException {
            ItemBase dest = platformLayer.getItem(machineKey);
            Machine machine = instanceHelpers.getMachine(dest, true);
            String address = machine.getNetworkPoint().getBestAddress(src);
            return address;
        }
    };
}
Also used : ItemBase(org.platformlayer.core.model.ItemBase) ThrowingProvider(org.platformlayer.ops.instances.ThrowingProvider) NetworkPoint(org.platformlayer.ops.networks.NetworkPoint) Machine(org.platformlayer.ops.Machine)

Example 9 with Machine

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

the class DnsResolverServiceController method findAddresses.

@Override
public List<InetAddress> findAddresses(NetworkPoint from) throws OpsException {
    Machine machine = instances.getMachine(model);
    if (machine == null) {
        return Collections.emptyList();
    }
    List<InetAddress> addresses = machine.getNetworkPoint().findAddresses(from);
    return addresses;
}
Also used : InetAddress(java.net.InetAddress) Machine(org.platformlayer.ops.Machine)

Example 10 with Machine

use of org.platformlayer.ops.Machine 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

Machine (org.platformlayer.ops.Machine)27 OpsException (org.platformlayer.ops.OpsException)13 OpsTarget (org.platformlayer.ops.OpsTarget)12 ItemBase (org.platformlayer.core.model.ItemBase)7 NetworkPoint (org.platformlayer.ops.networks.NetworkPoint)7 OpsContext (org.platformlayer.ops.OpsContext)5 SshKey (org.platformlayer.ops.helpers.SshKey)5 InetAddress (java.net.InetAddress)4 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)4 Tag (org.platformlayer.core.model.Tag)4 BindingScope (org.platformlayer.ops.BindingScope)4 Handler (org.platformlayer.ops.Handler)4 OpaqueMachine (org.platformlayer.ops.OpaqueMachine)4 List (java.util.List)3 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 TagChanges (org.platformlayer.core.model.TagChanges)2 Tags (org.platformlayer.core.model.Tags)2 ServiceType (org.platformlayer.ids.ServiceType)2