Search in sources :

Example 1 with DeploymentDescriptor

use of org.folio.okapi.bean.DeploymentDescriptor in project okapi by folio-org.

the class DeploymentManagerTest method test2.

@Test
public void test2(TestContext context) {
    async = context.async();
    LaunchDescriptor descriptor = new LaunchDescriptor();
    descriptor.setExec("java -Dport=%p -jar " + "../okapi-test-module/target/unknown.jar");
    DeploymentDescriptor dd = new DeploymentDescriptor("2", "sid", descriptor);
    dm.deploy(dd, res -> {
        context.assertFalse(res.succeeded());
        async.complete();
    });
}
Also used : DeploymentDescriptor(org.folio.okapi.bean.DeploymentDescriptor) LaunchDescriptor(org.folio.okapi.bean.LaunchDescriptor) Test(org.junit.Test)

Example 2 with DeploymentDescriptor

use of org.folio.okapi.bean.DeploymentDescriptor in project okapi by folio-org.

the class ProxyService method doCallSystemInterface.

/**
 * Actually make a request to a system interface, like _tenant. Assumes we are
 * operating as the correct tenant.
 */
private void doCallSystemInterface(String tenantId, String authToken, ModuleInstance inst, String modPerms, String request, ProxyContext pc, Handler<ExtendedAsyncResult<OkapiClient>> fut) {
    String curTenant = pc.getTenant();
    pc.debug("doCallSystemInterface on " + Json.encode(inst) + " for " + tenantId + " as " + curTenant + " with token " + authToken);
    discoveryManager.get(inst.getModuleDescriptor().getId(), gres -> {
        if (gres.failed()) {
            pc.warn("doCallSystemInterface on " + inst.getModuleDescriptor().getId() + " " + inst.getPath() + " failed. Could not find the module in discovery", gres.cause());
            fut.handle(new Failure<>(gres.getType(), gres.cause()));
            return;
        }
        DeploymentDescriptor instance = pickInstance(gres.result());
        if (instance == null) {
            fut.handle(new Failure<>(USER, "No running instances for module " + inst.getModuleDescriptor().getId() + ". Can not invoke " + inst.getPath()));
            return;
        }
        String baseurl = instance.getUrl();
        pc.debug("doCallSystemInterface Url: " + baseurl + " and " + inst.getPath());
        Map<String, String> headers = sysReqHeaders(pc.getCtx(), tenantId, authToken);
        if (modPerms != null) {
            // We are making an auth call
            RoutingEntry re = inst.getRoutingEntry();
            if (re != null) {
                headers.put(XOkapiHeaders.FILTER, re.getPhase());
            }
            if (!modPerms.isEmpty()) {
                headers.put(XOkapiHeaders.MODULE_PERMISSIONS, modPerms);
            }
            // Clear the permissions-required header that we inherited from the
            // original request (e.g. to tenant-enable), as we do not have those
            // perms set in the target tenant
            headers.put(XOkapiHeaders.PERMISSIONS_REQUIRED, "");
            headers.put(XOkapiHeaders.PERMISSIONS_DESIRED, "");
            logger.debug("Auth call, some tricks with permissions");
        }
        pc.debug("doCallSystemInterface: About to create OkapiClient with headers " + Json.encode(headers));
        OkapiClient cli = new OkapiClient(baseurl, vertx, headers);
        String reqId = inst.getPath().replaceFirst("^[/_]*([^/]+).*", "$1");
        // "tenant" or "tenantpermissions"
        cli.newReqId(reqId);
        cli.enableInfoLog();
        HttpMethod meth = HttpMethod.POST;
        if (request.isEmpty()) {
            pc.debug("doCallSystemInterface: No Req, making a HEAD req");
            meth = HttpMethod.HEAD;
        }
        HttpMethod finalMeth = meth;
        cli.request(meth, inst.getPath(), request, cres -> {
            cli.close();
            if (cres.failed()) {
                String msg = finalMeth + " request for " + inst.getModuleDescriptor().getId() + " " + inst.getPath() + " failed with " + cres.cause().getMessage();
                pc.warn(msg);
                fut.handle(new Failure<>(INTERNAL, msg));
                return;
            }
            // Pass response headers - needed for unit test, if nothing else
            String body = cres.result();
            pc.debug("doCallSystemInterface response: " + body);
            pc.debug("doCallSystemInterface ret " + " hdrs: " + Json.encode(cli.getRespHeaders().entries()));
            pc.passOkapiTraceHeaders(cli);
            fut.handle(new Success<>(cli));
        });
    });
}
Also used : RoutingEntry(org.folio.okapi.bean.RoutingEntry) OkapiClient(org.folio.okapi.common.OkapiClient) DeploymentDescriptor(org.folio.okapi.bean.DeploymentDescriptor) HttpMethod(io.vertx.core.http.HttpMethod)

Example 3 with DeploymentDescriptor

use of org.folio.okapi.bean.DeploymentDescriptor in project okapi by folio-org.

the class ProxyService method resolveUrls.

private void resolveUrls(Iterator<ModuleInstance> it, Handler<ExtendedAsyncResult<Void>> fut) {
    if (!it.hasNext()) {
        fut.handle(new Success<>());
    } else {
        ModuleInstance mi = it.next();
        if (mi.getRoutingEntry().getProxyType() == ProxyType.INTERNAL) {
            mi.setUrl("");
            resolveUrls(it, fut);
            return;
        }
        discoveryManager.get(mi.getModuleDescriptor().getId(), res -> {
            if (res.failed()) {
                fut.handle(new Failure<>(res.getType(), res.cause()));
            } else {
                DeploymentDescriptor instance = pickInstance(res.result());
                if (instance == null) {
                    fut.handle(new Failure<>(NOT_FOUND, "No running module instance found for " + mi.getModuleDescriptor().getId()));
                    return;
                }
                mi.setUrl(instance.getUrl());
                resolveUrls(it, fut);
            }
        });
    }
}
Also used : DeploymentDescriptor(org.folio.okapi.bean.DeploymentDescriptor) ModuleInstance(org.folio.okapi.bean.ModuleInstance)

Example 4 with DeploymentDescriptor

use of org.folio.okapi.bean.DeploymentDescriptor in project okapi by folio-org.

the class DeploymentManager method undeploy.

public void undeploy(String id, Handler<ExtendedAsyncResult<Void>> fut) {
    logger.info("undeploy instId " + id);
    if (!list.containsKey(id)) {
        fut.handle(new Failure<>(NOT_FOUND, "not found: " + id));
    } else {
        Timer.Context tim = DropwizardHelper.getTimerContext("deploy." + id + ".undeploy");
        DeploymentDescriptor md = list.get(id);
        dm.remove(md.getSrvcId(), md.getInstId(), res -> {
            if (res.failed()) {
                tim.close();
                fut.handle(new Failure<>(res.getType(), res.cause()));
            } else {
                ModuleHandle mh = md.getModuleHandle();
                mh.stop(future -> {
                    if (future.failed()) {
                        tim.close();
                        fut.handle(new Failure<>(INTERNAL, future.cause()));
                    } else {
                        fut.handle(new Success<>());
                        tim.close();
                        list.remove(id);
                    }
                });
            }
        });
    }
}
Also used : Timer(com.codahale.metrics.Timer) DeploymentDescriptor(org.folio.okapi.bean.DeploymentDescriptor) ModuleHandle(org.folio.okapi.util.ModuleHandle)

Example 5 with DeploymentDescriptor

use of org.folio.okapi.bean.DeploymentDescriptor in project okapi by folio-org.

the class DeploymentManager method deploy2.

private void deploy2(Handler<ExtendedAsyncResult<DeploymentDescriptor>> fut, Timer.Context tim, int usePort, DeploymentDescriptor md1, String url) {
    LaunchDescriptor descriptor = md1.getDescriptor();
    if (descriptor == null) {
        ports.free(usePort);
        fut.handle(new Failure<>(USER, "No LaunchDescriptor"));
        tim.close();
        return;
    }
    HashMap<String, EnvEntry> entries = new HashMap<>();
    EnvEntry[] env = descriptor.getEnv();
    if (env != null) {
        for (EnvEntry e : env) {
            entries.put(e.getName(), e);
        }
    }
    em.get(eres -> {
        if (eres.failed()) {
            ports.free(usePort);
            fut.handle(new Failure<>(INTERNAL, "get env: " + eres.cause().getMessage()));
            tim.close();
        } else {
            for (EnvEntry er : eres.result()) {
                entries.put(er.getName(), er);
            }
            if (entries.size() > 0) {
                EnvEntry[] nenv = new EnvEntry[entries.size()];
                int i = 0;
                for (Entry<String, EnvEntry> key : entries.entrySet()) {
                    nenv[i++] = key.getValue();
                }
                descriptor.setEnv(nenv);
            }
            ModuleHandle mh = ModuleHandleFactory.create(vertx, descriptor, ports, usePort);
            mh.start(future -> {
                if (future.succeeded()) {
                    DeploymentDescriptor md2 = new DeploymentDescriptor(md1.getInstId(), md1.getSrvcId(), url, md1.getDescriptor(), mh);
                    md2.setNodeId(md1.getNodeId() != null ? md1.getNodeId() : host);
                    list.put(md2.getInstId(), md2);
                    tim.close();
                    dm.add(md2, res -> fut.handle(new Success<>(md2)));
                } else {
                    tim.close();
                    ports.free(usePort);
                    logger.warn("Deploying " + md1.getSrvcId() + " failed");
                    fut.handle(new Failure<>(USER, future.cause()));
                }
            });
        }
    });
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DeploymentDescriptor(org.folio.okapi.bean.DeploymentDescriptor) ModuleHandle(org.folio.okapi.util.ModuleHandle) Success(org.folio.okapi.common.Success) LaunchDescriptor(org.folio.okapi.bean.LaunchDescriptor) EnvEntry(org.folio.okapi.bean.EnvEntry)

Aggregations

DeploymentDescriptor (org.folio.okapi.bean.DeploymentDescriptor)20 DecodeException (io.vertx.core.json.DecodeException)6 Test (org.junit.Test)6 LinkedList (java.util.LinkedList)4 List (java.util.List)4 LaunchDescriptor (org.folio.okapi.bean.LaunchDescriptor)4 CompList (org.folio.okapi.util.CompList)4 ModuleHandle (org.folio.okapi.util.ModuleHandle)3 HashMap (java.util.HashMap)2 Failure (org.folio.okapi.common.Failure)2 OkapiClient (org.folio.okapi.common.OkapiClient)2 Timer (com.codahale.metrics.Timer)1 HttpMethod (io.vertx.core.http.HttpMethod)1 LinkedHashMap (java.util.LinkedHashMap)1 EnvEntry (org.folio.okapi.bean.EnvEntry)1 HealthDescriptor (org.folio.okapi.bean.HealthDescriptor)1 ModuleInstance (org.folio.okapi.bean.ModuleInstance)1 RoutingEntry (org.folio.okapi.bean.RoutingEntry)1 Success (org.folio.okapi.common.Success)1