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();
}
}
}
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??
}
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;
}
};
}
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;
}
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);
}
Aggregations