setting environment vars in uboot escape sequences - environment-variables

I have an existing environment which looks like:
printenv
preboot=echo;echo Type \"run flash_nfs\" to mount root filesystem over NFS;echo
How can I set exactly this value?
I tried:
setenv preboot 'echo\;echo Type \"run flash_nfs\" to mount root filesystem over NFS\;echo'
printenv
preboot=echo;echo Type "run flash_nfs" to mount root filesystem over NFS;echo
and
setenv preboot 'echo\;echo Type \\"run flash_nfs\\" to mount root filesystem over NFS\;echo'
printenv
preboot=echo;echo Type \\"run flash_nfs\\" to mount root filesystem over NFS;echo
How can I set the sequence \" as part of a var?

Can you provide more details? Currently, and for sandbox you can do:
=> setenv test 'echo;echo \\"foo\\"'
=> run test
"foo"
=> printenv test
test=echo;echo \"foo\"
=>

Related

Environmet variables set to root container user instead of postgres container user by default

Everytime I start a new instance of a container from the PostgreSQL docker hub's official image, the environment variables are defined and exported in the root user of the shell created by the container instead of being defined and export in the postgres user.
For example, when I input the env command through root user:
PWD=/
HOME=/root
LANG=en_US.utf8
GOSU_VERSION=1.14
PG_MAJOR=15
PG_VERSION=15.1-1.pgdg110+1
TERM=xterm
SHLVL=1
POSTGRES_USER=postgres
PGDATA=/var/lib/postgresql/data
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/15/bin
_=/usr/bin/env
Using the same command but through postgres user:
SHELL=/bin/bash
PWD=/var/lib/postgresql/data
LOGNAME=postgres
HOME=/var/lib/postgresql
TERM=xterm
USER=postgres
SHLVL=1
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
MAIL=/var/mail/postgres
OLDPWD=/var/lib/postgresql/data/base
_=/usr/bin/env
Why is this happening? I've read the documentation but I found nothing. Is there a way to set these variables in postgres user instead of root by default?

Symfony 4.4 does not load variables from env?

I'm running a Symfony app inside a Docker container.
I have an env variable declared:
root#2665da58cc0e:/var/www/project# printenv | grep DATABASE
DATABASE_URL=test
And if I run php -a inside the container I can dump $_ENV:
php > var_dump($_ENV['DATABASE_URL']);
string(4) "test"
And my config looks like:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_sqlite
charset: utf8mb4
url: '%env(DATABASE_URL)%'
I have no mention of DATABASE_URL in my .env file, yet I get
PHP message: PHP Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\EnvNotFoundException: Environment variable not found: "DATABASE_URL"
Why does it happen? Symfony should have taken DATABASE_URL from the system env variables, it does not happen.

Symfony 4.3 does not read .env when ran in a docker container

I have my Symfony application run perfectly fine on my development machine, on several servers except for one where it doesn't read the .env file.
The env file contains this variable:
root#719c10cf645d:/app# grep application_version .env
application_version=3.0.3-64-g642ed7f
The shell environment doesn't:
root#719c10cf645d:/app# env | grep application_version
root#719c10cf645d:/app# set | grep application_version
Symfony doesn't read the .env file:
root#719c10cf645d:/app# ./bin/console >/dev/null
[WARNING] Some commands could not be registered:
In EnvVarProcessor.php line 131:
Environment variable not found: "application_version".
[WARNING] Some commands could not be registered:
In EnvVarProcessor.php line 131:
Environment variable not found: "application_version".
On another server I have:
root#4535704c813a:/app# env | grep application_version
root#4535704c813a:/app# set | grep application_version
root#4535704c813a:/app# grep application_version .env
application_version=3.0.3-64-g642ed7f
root#4535704c813a:/app# ./bin/console >/dev/null
root#4535704c813a:/app#
I'm using the same docker version (19.03.5, build 633a0ea838) on both servers, same OS (Debian), and same docker images (different registry, but same SHA1). So everything should be perfectly identical.
What could be wrong here?
The environment on the two servers were different, and my config/bootstrap.php file didn't load .env files on one machine.
The bottom line is that I didn't follow these updates: https://symfony.com/doc/current/configuration/dot-env-changes.html
I don't know how my app survived without these changes for one year.

Docker: Change word in file at container startup

I'm creating a docker image for our fluentd.
The image contains a file called http_forward.conf
It contains:
<store>
type http
endpoint_url ENDPOINTPLACEHOLDER
http_method post # default: post
serializer json # default: form
rate_limit_msec 100 # default: 0 = no rate limiting
raise_on_error true # default: true
authentication none # default: none
username xxx # default: ''
password xxx # default: '', secret: true
</store>
So this is in our image. But we want to use the image for all our environments. Specified with environment variables.
So we create an environment variable for our environment:
ISSUE_SERVICE_URL = http://xxx.dev.xxx.xx/api/fluentdIssue
This env variable contains dev on our dev environment, uat on uat etc.
Than we want to replace our ENDPOINTPLACEHOLDER with the value of our env variable. In bash we can use:
sed -i -- 's/ENDPOINTPLACEHOLDER/'"$ISSUE_SERVICE_URL"'/g' .../http_forward.conf
But how/when do we have to execute this command if we want to use this in our docker container? (we don't want to mount this file)
We did that via ansible coding.
Put the file http_forward.conf as template, and deploy the change depend on the environment, then mount the folder (include the conf file) to docker container.
ISSUE_SERVICE_URL = http://xxx.{{ environment }}.xxx.xx/api/fluentdIssue
playbook will be something like this, I don't test it.
- template: src=http_forward.conf.j2 dest=/config/http_forward.conf mode=0644
- docker:
name: "fluentd"
image: "xxx/fluentd"
restart_policy: always
volumes:
- /config:/etc/fluent
In your DockerFile you should have a line starting with CMD somewhere. You should add it there.
Or you can do it cleaner: set the CMD line to call a script instead. For example CMD ./startup.sh. The file startup.sh will then contain your sed command followed by the command to start your fluentd (I assume that is currently the CMD).

Environment variables and PHP

I have an ubuntu server with a handful of custom environment variables set in /etc/environment as per the ubuntu community recommendation
When I use php from the command line I can use php's getenv() function to access this variables.
Also, if I run phpinfo() from the command line I see all of my variables in the ENVIRONMENT section.
However, when trying to access the same data inside processes being run by php5-fpm this data is not available. All I can see in the ENVIRONMENT section of phpinfo() is:
USER www-data
HOME /var/www
I know the command line uses this ini:
/etc/php5/cli/php.ini
And fpm uses:
/etc/php5/fpm/php.ini
I've not managed to find any differences between the two that would explain why the ENV variables are not coming through in both.
Also if run:
sudo su www-data
and then echo the environment variables I am expecting they are indeed available to the www-data user.
What do I need to do to get my environment variables into the php processes run by fpm?
It turns out that you have to explicitly set the ENV vars in the php-fpm.conf
Here's an example:
[global]
pid = /var/run/php5-fpm.pid
error_log = /var/log/php5-fpm.log
[www]
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
env[MY_ENV_VAR_1] = 'value1'
env[MY_ENV_VAR_2] = 'value2'
1. Setting environment variables automatically in php-fpm.conf
clear_env = no
2. Setting environment variables manually in php-fpm.conf
env[MY_ENV_VAR_1] = 'value1'
env[MY_ENV_VAR_2] = 'value2'
! Both methods are described in php-fpm.conf:
Clear environment in FPM workers Prevents arbitrary environment
variables from reaching FPM worker processes by clearing the
environment in workers before env vars specified in this pool
configuration are added. Setting to "no" will make all environment
variables available to PHP code via getenv(), $_ENV and $_SERVER.
Default Value: yes
clear_env = no
Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are
taken from the current environment. Default Value: clean env
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
I found solution in this github discussion .
The problem is when you run the php-fpm. The process not load the environment.
You can load it in the startup script.
My php-fpm is install by apt-get.
So modify the
/etc/init.d/php5-fpm
and add (beware the space between the dot and the slash)
. /etc/profile
and modify the /etc/profile to add
. /home/user/env.sh
In the env.sh. You can export the environment whatever you need.
Then modify
php-fpm.conf
add env[MY_ENV_VAR_1] = 'value1' under the [www] section.
Last. restart the php-fpm. You'll get the environment load by the fpm.
Adding on to the answers above, I was running php-fpm7 and nginx in an alpine:3.8 docker container. The problem that I faced was the env variables of USER myuser was not getting copied into the USER root
My entrypoint for docker was
sudo nginx # Runs nginx as daemon
sudo php-fpm7 -F -O # Runs php-fpm7 in foreground
The solution for this was
sudo -E nginx
sudo -E php-fpm7 -F -O
-E option of sudo copies all env variables of current user to the root
Of course, your php-fpm.d/www.conf file should have clear_env=no
And FYI, if you're using a daemon service like supervisord they have their own settings to copy the env. For example, supervisord has setting called copy_env=True

Resources