I am trying develop a cookbook to make a flask app work with gunicorn and nginx. I have been successful to the point that the app is running very well with a local sqlite database, see my cookbook at https://github.com/harrywang/flasky-cookbook. The flask app uses environment variables for sending emails such as: MAIL_USERNAME = os.environ.get('MAIL_USERNAME'), how I can pass those environment variables to the ubuntu virtual machines using test kitchen during kitchen converge?
How to pass environment variables to test kitchen in .kitchen.yml
Asked Answered
There is no way to pass environment variables using .kitchen.yml configuration file (see test-kitchen/test-kitchen#662 issue).
I recommend you to set the environment variables in the gunicorn.conf.erb template using the --env
argument:
exec gunicorn --env SECRET_KEY=<%= @secret_key %> --env [...] --workers 3 --bind unix:<%= node['flasky-cookbook']['gunicorn_socket'] %> -m 007 --log-file <%= node['flasky-cookbook']['gunicorn_logfile']%> manage:app
I tried
exec gunicorn --env MAIL_SERVER='email-smtp.us-east-1.amazonaws.com',MAIL_USERNAME='',MAIL_PASSWORD='-redacted-',FLASK_ADMIN='-redacted-' --workers 3 --bind unix:<%= node['flasky-cookbook']['gunicorn_socket'] %> -m 007 --log-file <%= node['flasky-cookbook']['gunicorn_logfile']%> manage:app
but did not work - I kitchen login
and printenv
but cannot see those env variables. Any thought? –
Ancier @Gleam I don't think this is entirely incorrect. I know that you can read ENV variables from the .kitchen.yml file, but I was talking about the other part of the problem: passing the variable to the application. I assumed, perhaps wrongly, that he knew how to do the first part. If you still think this answer is wrong, I will delete it. –
Monoploid
@harryw seems like you can also use
env
to set environment variables. These variables will only be defined inside the application run with gunicorn. Not in your kitchen shell. –
Monoploid I was mostly referring to the first part, you can absolutely pass through environment variables. –
Gleam
OK, @coderanger. Thanks. I removed the first part. I didn't know that, sorry. I know, your answer is better :-P –
Monoploid
@Monoploid thanks a lot for your help. how can I check those env variables have been successfully passed in if I cannot use printenv? Is my way of passing multiple env variable by putting them together with comma correct? I am asking because I could not make flasky email function to work :(. –
Ancier
@harryw You are welcome ;-) You can get the pid of the gunicorn process and look at its environment file in
/proc
. For example with: cat /proc/$(pidof gunicorn)/environ
. For the variables, try passing multiple --env
options or use multiple env SECRET_KEY="..."
lines before calling gunicorn. –
Monoploid You can use Erb formatting in your .kitchen.yml
:
provisioner:
name: chef-solo
attributes:
mycookbook:
mail_username: <%= ENV['MAIL_USERNAME'] %>
And then use node['mycookbook']['mail_username']
in your recipe to pass the value to the application.
I am confused: how can I "use node['mycookbook']['mail_username'] in your recipe to pass the value to the application"? Is your solution passing my host (macbook pro) env variables to guest (ubuntu virtual box instance)? I am very new to Chef - sorry if I am asking stupid questions :) –
Ancier
You'll need to fork the
flasky
cookbook and add something to this template file: github.com/harrywang/flasky-cookbook/blob/master/templates/… to set the environment variable. –
Gleam Also check out github.com/poise/application_python/blob/master/test/cookbooks/… for a simpler Flask deployment example. –
Gleam
thanks a lot for your help. adding something to the template is similar to the solution given by zuazo. The application cookbook seems great but do you know any detailed tutorials on that? The README is too brief for beginners like me. –
Ancier
There is no way to pass environment variables using .kitchen.yml configuration file (see test-kitchen/test-kitchen#662 issue).
I recommend you to set the environment variables in the gunicorn.conf.erb template using the --env
argument:
exec gunicorn --env SECRET_KEY=<%= @secret_key %> --env [...] --workers 3 --bind unix:<%= node['flasky-cookbook']['gunicorn_socket'] %> -m 007 --log-file <%= node['flasky-cookbook']['gunicorn_logfile']%> manage:app
I tried
exec gunicorn --env MAIL_SERVER='email-smtp.us-east-1.amazonaws.com',MAIL_USERNAME='',MAIL_PASSWORD='-redacted-',FLASK_ADMIN='-redacted-' --workers 3 --bind unix:<%= node['flasky-cookbook']['gunicorn_socket'] %> -m 007 --log-file <%= node['flasky-cookbook']['gunicorn_logfile']%> manage:app
but did not work - I kitchen login
and printenv
but cannot see those env variables. Any thought? –
Ancier @Gleam I don't think this is entirely incorrect. I know that you can read ENV variables from the .kitchen.yml file, but I was talking about the other part of the problem: passing the variable to the application. I assumed, perhaps wrongly, that he knew how to do the first part. If you still think this answer is wrong, I will delete it. –
Monoploid
@harryw seems like you can also use
env
to set environment variables. These variables will only be defined inside the application run with gunicorn. Not in your kitchen shell. –
Monoploid I was mostly referring to the first part, you can absolutely pass through environment variables. –
Gleam
OK, @coderanger. Thanks. I removed the first part. I didn't know that, sorry. I know, your answer is better :-P –
Monoploid
@Monoploid thanks a lot for your help. how can I check those env variables have been successfully passed in if I cannot use printenv? Is my way of passing multiple env variable by putting them together with comma correct? I am asking because I could not make flasky email function to work :(. –
Ancier
@harryw You are welcome ;-) You can get the pid of the gunicorn process and look at its environment file in
/proc
. For example with: cat /proc/$(pidof gunicorn)/environ
. For the variables, try passing multiple --env
options or use multiple env SECRET_KEY="..."
lines before calling gunicorn. –
Monoploid © 2022 - 2024 — McMap. All rights reserved.
env MAIL_USERNAME="AKIAJ6NFIS3"
double quotes must be used - single quotes do not work, while using --env single quotes work - to be safe: always use double quotes. – Ancier