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