Environment variable not expanding in openshift

雨燕双飞 提交于 2019-12-01 04:56:56

问题


It seems that if I use a command like:

rhc env set VARIABLE="$OPENSHIFT_DATA_DIR/file"

the referenced directory variable is never expanded and as a result I can not use it for my app. Is there any way to fix this?

EDIT

As noted by @timo.rieber in his answer, this is not going to work because the variable is resolved locally, where it has no value. In fact:

$ rhc env set VARIABLE="$OPENSHIFT_DATA_DIR/file"
Setting environment variable(s) ... done
$ rhc env show VARIABLE
VARIABLE=/file

However, if I use single quotes to avoid immediate expansion:

$ rhc env set VARIABLE='$OPENSHIFT_DATA_DIR/file'
Setting environment variable(s) ... done
$ rhc env show VARIABLE
VARIABLE=$OPENSHIFT_DATA_DIR/file

Interestingly, this does not work as well (i.e. no expansion happens when it is used by the process) even if apparently this time it is correctly set.


回答1:


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'



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/26335225/environment-variable-not-expanding-in-openshift

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