Environment variable not expanding in openshift

2019-02-18 10:46发布

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.

3条回答
爷的心禁止访问
2楼-- · 2019-02-18 11:28

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'
查看更多
狗以群分
3楼-- · 2019-02-18 11:30

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

查看更多
\"骚年 ilove
4楼-- · 2019-02-18 11:44

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.

查看更多
登录 后发表回答