use of org.apache.metron.maas.config.Endpoint in project metron by apache.
the class ServiceDiscoveryIntegrationTest method registerService.
private void registerService(String name, String version, AtomicInteger containerId) throws Exception {
ModelEndpoint ep = new ModelEndpoint();
ep.setName(name);
ep.setVersion(version);
ep.setContainerId(containerId.incrementAndGet() + "");
ep.setEndpoint(new Endpoint() {
{
setUrl("http://localhost:9080/ep1");
}
});
registerService(ep);
}
use of org.apache.metron.maas.config.Endpoint in project metron by apache.
the class Runner method readEndpoint.
private static Endpoint readEndpoint(File cwd) throws Exception {
String content = "";
File f = new File(cwd, Constants.ENDPOINT_DAT);
for (int i = 0; i < NUM_ATTEMPTS; i++) {
if (f.exists()) {
try {
content = Files.toString(f, Charsets.US_ASCII);
} catch (IOException ioe) {
}
if (content != null && content.length() > 0) {
try {
Endpoint ep = ConfigUtil.INSTANCE.read(content.getBytes(StandardCharsets.UTF_8), Endpoint.class);
return ep;
} catch (Exception ex) {
LOG.error("Unable to parse " + content + ": " + ex.getMessage(), ex);
}
}
}
Thread.sleep(SLEEP_AMT);
}
throw new IllegalStateException("Unable to start process within the allotted time (10 minutes)");
}
use of org.apache.metron.maas.config.Endpoint in project metron by apache.
the class Runner method main.
public static void main(String... argv) throws Exception {
CommandLine cli = RunnerOptions.parse(new PosixParser(), argv);
String zkQuorum = RunnerOptions.ZK_QUORUM.get(cli);
String zkRoot = RunnerOptions.ZK_ROOT.get(cli);
String script = RunnerOptions.SCRIPT.get(cli);
String name = RunnerOptions.NAME.get(cli);
String version = RunnerOptions.VERSION.get(cli);
String containerId = RunnerOptions.CONTAINER_ID.get(cli);
String hostname = RunnerOptions.HOSTNAME.get(cli);
CuratorFramework client = null;
LOG.info("Running script " + script);
LOG.info("Local Directory Contents");
for (File f : new File(".").listFiles()) {
LOG.info(" " + f.getName());
}
try {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
client = CuratorFrameworkFactory.newClient(zkQuorum, retryPolicy);
client.start();
MaaSConfig config = ConfigUtil.INSTANCE.read(client, zkRoot, new MaaSConfig(), MaaSConfig.class);
JsonInstanceSerializer<ModelEndpoint> serializer = new JsonInstanceSerializer<>(ModelEndpoint.class);
try {
serviceDiscovery = ServiceDiscoveryBuilder.builder(ModelEndpoint.class).client(client).basePath(config.getServiceRoot()).serializer(serializer).build();
} finally {
}
LOG.info("Created service @ " + config.getServiceRoot());
serviceDiscovery.start();
File cwd = new File(script).getParentFile();
File scriptFile = new File(cwd, script);
if (scriptFile.exists() && !scriptFile.canExecute()) {
scriptFile.setExecutable(true);
}
final String cmd = scriptFile.getAbsolutePath();
try {
p = new ProcessBuilder(cmd).directory(cwd).start();
} catch (Exception e) {
LOG.info("Unable to execute " + cmd + " from " + new File(".").getAbsolutePath());
LOG.error(e.getMessage(), e);
throw new IllegalStateException(e.getMessage(), e);
}
try {
LOG.info("Started " + cmd);
Endpoint ep = readEndpoint(cwd);
URL endpointUrl = correctLocalUrl(hostname, ep.getUrl());
ep.setUrl(endpointUrl.toString());
LOG.info("Read endpoint " + ep);
ModelEndpoint endpoint = new ModelEndpoint();
{
endpoint.setName(name);
endpoint.setContainerId(containerId);
endpoint.setEndpoint(ep);
endpoint.setVersion(version);
}
;
ServiceInstanceBuilder<ModelEndpoint> builder = ServiceInstance.<ModelEndpoint>builder().address(endpointUrl.getHost()).id(containerId).name(name).port(endpointUrl.getPort()).registrationTimeUTC(System.currentTimeMillis()).serviceType(ServiceType.STATIC).payload(endpoint);
final ServiceInstance<ModelEndpoint> instance = builder.build();
try {
LOG.info("Installing service instance: " + instance + " at " + serviceDiscovery);
serviceDiscovery.registerService(instance);
LOG.info("Installed instance " + name + ":" + version + "@" + endpointUrl);
} catch (Throwable t) {
LOG.error("Unable to install instance " + name + ":" + version + "@" + endpointUrl, t);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
LOG.info("KILLING CONTAINER PROCESS...");
if (p != null) {
LOG.info("Process destroyed forcibly");
p.destroyForcibly();
}
}
});
} finally {
if (p.waitFor() != 0) {
String stderr = Joiner.on("\n").join(IOUtils.readLines(p.getErrorStream()));
String stdout = Joiner.on("\n").join(IOUtils.readLines(p.getInputStream()));
throw new IllegalStateException("Unable to execute " + script + ". Stderr is: " + stderr + "\nStdout is: " + stdout);
}
}
} finally {
if (serviceDiscovery != null) {
CloseableUtils.closeQuietly(serviceDiscovery);
}
if (client != null) {
CloseableUtils.closeQuietly(client);
}
}
}
use of org.apache.metron.maas.config.Endpoint in project metron by apache.
the class StellarMaaSIntegrationTest method setup.
@BeforeAll
public static void setup() throws Exception {
UnitTestHelper.setJavaLoggingLevel(WebApplicationImpl.class, Level.WARNING);
MockDGAModel.start(8282);
testZkServer = new TestingServer(true);
zookeeperUrl = testZkServer.getConnectString();
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
client = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy);
client.start();
context = new Context.Builder().with(Context.Capabilities.ZOOKEEPER_CLIENT, () -> client).build();
MaaSConfig config = ConfigUtil.INSTANCE.read(client, "/metron/maas/config", new MaaSConfig(), MaaSConfig.class);
discoverer = new ServiceDiscoverer(client, config.getServiceRoot());
discoverer.start();
endpointUrl = new URL("http://localhost:8282");
ModelEndpoint endpoint = new ModelEndpoint();
{
endpoint.setName("dga");
endpoint.setContainerId("0");
Endpoint ep = new Endpoint();
ep.setUrl(endpointUrl.toString());
endpoint.setEndpoint(ep);
endpoint.setVersion("1.0");
}
;
ServiceInstanceBuilder<ModelEndpoint> builder = ServiceInstance.<ModelEndpoint>builder().address(endpointUrl.getHost()).id("0").name("dga").port(endpointUrl.getPort()).registrationTimeUTC(System.currentTimeMillis()).serviceType(ServiceType.STATIC).payload(endpoint);
final ServiceInstance<ModelEndpoint> instance = builder.build();
discoverer.getServiceDiscovery().registerService(instance);
// wait til the endpoint is installed...
for (int i = 0; i < 10; ++i) {
try {
Object o = discoverer.getEndpoint("dga");
if (o != null) {
break;
}
} catch (Exception e) {
}
Thread.sleep(1000);
}
}
Aggregations