Get environment name, cluster name or ip of server


#1

Enonic version: 6.6.2-SNAPSHOT
OS: Mac and Linux

In production I would like to display nice designed error pages, which does not expose any error message to the end user.
However in QA and development I want to continue using the Enonic XP red error page (which contains the error message and stacktrace)

The host in a request can be faked by for instance using /etc/hosts, which I’m doing to have multiple “virtual” hosts on my development laptop.

So instead if want to check use the environment name, cluster name or ip of server instead to determine which error page to show.

I guess I could add some environment name to the site config, but there might be other uses for environment name, cluster name or ip of server in the future. Perhaps when logging stuff.

So is there any way to get environment name, cluster name or ip of server?


#2

One way to do it with JS (maybe not the best one) is to get the system info from the /status/jvm.properties URL. And set the environment variables you need in your startup script.
http://localhost:8080/status/jvm.properties

You can use lib-cache so you only need to make the http request once.

http://xp.readthedocs.io/en/stable/operations/monitoring.html#basic-status-info


#3

Yeah I could use http://localhost:8080/status/cluster which contains both the cluster name and hostname

I’d rather not have to do a request though. An API would be nice :slight_smile:


#4

I choose to use a custom cfg file for environment specific stuff in XP_HOME/config/com.enonic.myapp.cfg that can be changed run-time without restarting the server.


#5

We should have the status info more accessible. Will create a task to make the StatusReporters available in Javascript too.


#6

If this is a general requirement, maybe it could be enabled with a switch?


#7

Example code without caching:

export function handleError(err) {
    let boolProduction = true;
    try {
        const clusterName = JSON.parse(httpRequest({ url: `http://localhost:8080/status/cluster` }).body).name;
        boolProduction = clusterName === 'xp-prod-cluster' || (clusterName !== 'xp-dev-cluster' && clusterName !== 'xp-qa-cluster');
    }
    catch(e) {
        log.error(`catch in handleError ${e.message}`);
    }
    finally {
        if(boolProduction) {
            log.error('handleError(%s)', stringify(err));
            return respondWithErrorPage(err.status);
        }
    }
}

#8

Has this been looked at in the later XP version? Any way one can acquire the current hostname without using an API or a custom config file?


#9

Nothing I am aware of.
But getting hostname is usually not needed (you get only internal hostname, and only if you are “lucky”)

If it is what really you need, do it yourself in java but beware of corner cases https://stackoverflow.com/a/7800008


#10

It has been needed in several situations in my experience. I have some colleagues that really need this aswell. So far a manual config file has been the solution, but XP should “know” this, we configure the vhost after all, and it would be nice if it was exposed to the developer instead of resorting to hacky solutions.


#11

Check out host in request
https://developer.enonic.com/docs/xp/stable/framework/http#http-request


#12

Do you all need this in http context? If so, as @rymsha says, all info is available in the context object. If you need something else, like clusters or internal host(s) it is currently not easily accessible.


#13