diff --git a/code/common/service/java/nu/marginalia/service/discovery/ZkServiceRegistry.java b/code/common/service/java/nu/marginalia/service/discovery/ZkServiceRegistry.java index 43fa5a54..446c99a2 100644 --- a/code/common/service/java/nu/marginalia/service/discovery/ZkServiceRegistry.java +++ b/code/common/service/java/nu/marginalia/service/discovery/ZkServiceRegistry.java @@ -140,13 +140,15 @@ public class ZkServiceRegistry implements ServiceRegistryIf { @Override public int requestPort(String externalHost, ServiceKey key) { + if (!Boolean.getBoolean("service.random-port")) { return switch (key) { - case ServiceKey.Rest rest -> 80; - case ServiceKey.Grpc grpc -> 81; + case ServiceKey.Rest rest -> Integer.getInteger("service.http-port", 80); + case ServiceKey.Grpc grpc -> Integer.getInteger("service.grpc-port",81); }; } + int portRangeLow = 12_000; int portRangeHigh = 12_999; diff --git a/code/services-core/single-service-runner/java/nu/marginalia/SingleService.java b/code/services-core/single-service-runner/java/nu/marginalia/SingleService.java index 5a793a0d..e6ba4870 100644 --- a/code/services-core/single-service-runner/java/nu/marginalia/SingleService.java +++ b/code/services-core/single-service-runner/java/nu/marginalia/SingleService.java @@ -1,6 +1,73 @@ package nu.marginalia; +/** Springboard for launching services outside of docker */ public class SingleService { + + public static void main(String... args) { + if (!configure(args)) { + System.out.println("Usage: SingleService bind-address:bind-port-http:bind-port-grpc announce-address [args...]"); + return; + } + + requireEnv("ZOOKEEPER_HOSTS", "Comma-separated list of zookeeper hosts"); + requireEnv("WMSA_HOME", "Path to the install directory of the project"); + + String serviceName = args[0]; + String[] serviceArgs = new String[args.length - 3]; + System.arraycopy(args, 3, serviceArgs, 0, serviceArgs.length); + + for (var service : Service.values()) { + if (service.name.equals(serviceName)) { + service.run(serviceArgs); + } + } + } + + private static void requireEnv(String env, String desc) { + if (System.getenv(env) == null) { + throw new IllegalArgumentException("Missing environment variable: " + env + " - " + desc); + } + else { + System.out.println("Found environment variable: " + env + " = " + System.getenv(env)); + } + } + + /** Set system properties for the address and ports for the service. + * + * @return true if the configuration was successful + * */ + private static boolean configure(String[] args) { + if (args.length < 3) + return false; + + try { + final String bindAddress_http_grpc = args[1]; + final String announceAddress = args[2]; + + final String[] bindParts = bindAddress_http_grpc.split(":"); + if (bindParts.length < 3) + return false; + + String bindAddress = bindParts[0]; + + int httpPort = Integer.parseInt(bindParts[1]); + int grpcPort = Integer.parseInt(bindParts[2]); + + System.out.println("Configuring service with bind address: " + bindAddress + " http port: " + httpPort + " grpc port: " + grpcPort + " announce address: " + announceAddress); + + System.setProperty("service.bind-address", bindAddress); + System.setProperty("service.http-port", Integer.toString(httpPort)); + System.setProperty("service.grpc-port", Integer.toString(grpcPort)); + System.setProperty("service.host", announceAddress); + + return true; + } + catch (NumberFormatException e) { + return false; + } + + } + enum Service { IndexService("index", "nu.marginalia.index.IndexMain"), ControlService("control", "nu.marginalia.control.ControlMain"), @@ -31,19 +98,5 @@ public class SingleService { } } - public static void main(String... args) { - if (args.length == 0) { - System.out.println("Usage: SingleService [args...]"); - } - String serviceName = args[0]; - String[] serviceArgs = new String[args.length - 1]; - System.arraycopy(args, 1, serviceArgs, 0, serviceArgs.length); - - for (var service : Service.values()) { - if (service.name.equals(serviceName)) { - service.run(serviceArgs); - } - } - } }