Monday, March 10, 2014

Upstart can't read environment variables

I was trying to configure a gunicorn server with upstart for easily starting/stopping my service. But it turned out to be problematic since upstart does not read environment variables.

How did I encounter this error? I was trying to parse signed_request from Facebook canvas via the following line:

fb_request = facebook.parse_signed_request(signed_request, APP_SECRET)

where

APP_SECRET = os.environ.get('FB_APP_SECRET', 'jhdklu3sh4o8y4o8fh34')

I realized that it couldn't read the environment variable and was using the default app secret so couldn't decrypt the signed request. My environment variables were in .bashrc of both regular user and root but it did not read. So I added them to the upstart script:

script
     export HOME=/home/user/project
     cd $HOME
     export FB_APP_ID=859827u4fhk4vnf
     export FB_APP_SECRET=4ojfo92ufo2489fyuo482fy2
     . venv/bin/activate
     exec gunicorn myapp:app
end script


No comments: