OpenShift Migration - ENV Variables In Conf File N

2019-08-14 06:33发布

问题:

I am migrating a webapp from OpenShift v2 to OpenShift Pro. This is a regular Java Tomcat app.

My webapp on OpenShift v2 had an additional docBase specified in server.xml like this:

        <Context docBase="${OPENSHIFT_DATA_DIR}/documents" path="/documents" />

During deployment the environment variable OPENSHIFT_DATA_DIR was replaced with its value: /opt/app-root/data

Just as you would expect.

I have done the equivalent (though quite different) set up in OpenShift Pro but the deployment failed. Upon investigation, I saw this in the log:

Caused by: java.lang.IllegalArgumentException: The main resource set specified [/deployments/${OPENSHIFT_DATA_DIR}/documents] is not valid

To overcome this I merely hard coded /opt/app-root/data in server.xml like this:

<Context docBase="/opt/app-root/data/documents" path="/documents" />

The next deployment worked.

My questions are:

Is this a bug in the deployment process?

Is this the wrong way to get environment variables injected into configuration files?

If so, what is the right way?

Any help would be appreciated, I don't like hard coding things, it invariably comes back to bite you at some future date...

回答1:

None of the special environment variables that were set in V2 are set in V3. In respect of V3, there is also no standard location that a persistent volume is mounted, it is sort of up to you where you mount it. That the variable doesn't show as being expanded may simply be due to whatever processes the file, leaving it as the variable reference if no such environment variable exists. This presumes the substitution still occurs under V3 and wasn't something special about V2.

If you want to keep the environment variables in case you want to override them, or just to avoid changing code that needs them, you can set the environment variables in the deployment config, or you can add a .s2i/environment file in your source code repo and add environment settings in it instead. Those in the .s2i/environment file actually become part of the application image when built. They can later be overridden by environment variables in the deployment config if needed.



回答2:

If you are looking to set any environment variable, please check this and this (s2i script section).

Basically, you should introduce .s2i/bin folder into the codebase, which can have override multiple set of scripts and introduce any custom code. This is similar to action hooks in V2.

Here, probably you wish to introduce setting environment variable before actual run. e.g.

.s2i/bin/run script

#!/bin/bash
echo "Set environment variable before running application"
exec /usr/libexec/s2i/run

Few things to be careful of:

  • By introducing your script, it is the completely overwritten. In case if you wish to call standard run script after your code, you must call that script. e.g. /usr/libexec/s2i/run in the last line.

  • The default run script call should be the last line. Please do not expect the control to come back to execute anything written after that.