Environment variable not expanding in openshift

烂漫一生 提交于 2019-12-01 06:59:38

Problem explanation:

The value of $OPENSHIFT_DATA_DIR gets resolved on your local computer. Most likely it will be empty as this variable is not set on your machine.

It is not possible to read values of builtin server-side environment variables like $OPENSHIFT_DATA_DIR. Only externally set variables will be exposed. You can try this by the following example:

user@machine:~$ rhc env list

user@machine:~$ rhc env set VARIABLE="file"
Setting environment variable(s) ... done

user@machine:~$ rhc env list
VARIABLE=file

user@machine:~$ rhc env unset VARIABLE
Removing environment variables is a destructive operation that may result in loss of data.
VARIABLE

Are you sure you wish to remove the environment variable(s) above from application 'yourapp'?(yes|no):yes

Removing environment variable(s) ... done

user@machine:~$ rhc env list

Solution:

Set two environment variables and use them within your program code to build your path:

1. Define a first environment variable to define the builtin environment variable to read

user@machine:~$ rhc env set MY_ENV_VAR_FOR_BASE_DIR="OPENSHIFT_DATA_DIR"
Setting environment variable(s) ... done

2. Define a second environment variable to specify your folder within the given directory

user@machine:~$ rhc env set MY_TARGET_FOLDER="file"
Setting environment variable(s) ... done

3. Within your code (python example) build your path

(InteractiveConsole)
>>> import os

>>> os.environ.get("MY_ENV_VAR_FOR_BASE_DIR")
'OPENSHIFT_DATA_DIR'

>>> os.environ.get("OPENSHIFT_DATA_DIR")
'/var/lib/openshift/your_user_dir/app-root/data/'

>>> os.environ.get("MY_TARGET_FOLDER")
'file'

>>> os.path.join(os.environ.get(os.environ.get("MY_ENV_VAR_FOR_BASE_DIR")), os.environ.get("MY_TARGET_FOLDER"))
'/var/lib/openshift/your_user_dir/app-root/data/file'

I recall running "rhc env set ...", but my node express server was still seeing 'undefined' until I ran "export FOO". So try that.

Although this question is pretty old, I figured I would post what is probably the solution currently. The environment variables expand in OpenShift pods somewhat like they would in the shell using $(VAR) syntax, so you you could set a variable DATABASE_URL like;

oc set env dc service -e 'DATABASE_URL=mysql://user:pass@$(MARIADB_SERVICE_HOST):$(MARIADB_SERVICE_PORT)/dbname'

then those variables will get expanded at deployment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!