librelist archives

« back to archive

uwsgi + configuration outside of app

uwsgi + configuration outside of app

From:
Date:
2013-05-21 @ 15:49
Hi,

I have a Flask app that I will be running on different servers, each with 
a few different configuration parameters (e.g. location of database). I 
have different uwsgi configuration files when I start the app and a 
configuration file within the app. What I'm not sure how to do is start 
the app with uwsgi while specifying app-level options. In other words, I 
want to effectively say to uwsgi "start the app; load configuration 
options for running on server A", or "start the app; load configuration 
options for running on server B". Ideally, the configuration options for 
all possible servers would be in the app hierarchy.

Thanks!

Demitri

Re: [flask] uwsgi + configuration outside of app

From:
Jonas Brunsgaard
Date:
2013-05-21 @ 16:18
On Tue, May 21, 2013 at 5:49 PM, <thatsanicehatyouhave@me.com> wrote:

> Hi,
>
> I have a Flask app that I will be running on different servers, each with
> a few different configuration parameters (e.g. location of database). I
> have different uwsgi configuration files when I start the app and a
> configuration file within the app. What I'm not sure how to do is start the
> app with uwsgi while specifying app-level options. In other words, I want
> to effectively say to uwsgi "start the app; load configuration options for
> running on server A", or "start the app; load configuration options for
> running on server B". Ideally, the configuration options for all possible
> servers would be in the app hierarchy.
>
> Thanks!
>
> Demitri
>

app = Flask(__name__)
app.config.from_object('yourapplication.default_settings')
app.config.from_envvar('YOURAPPLICATION_SETTINGS')

The essential part is the last line, where you use the os env variables to
point to the server specific settings, wouldn't that be a solution to your
problem.

-
Jonas

Re: [flask] uwsgi + configuration outside of app

From:
Date:
2013-05-21 @ 21:16
Hi Jonas,

On May 21, 2013, at 12:18 PM, Jonas Brunsgaard <jonas.brunsgaard@gmail.com> wrote:

> app = Flask(__name__)
> app.config.from_object('yourapplication.default_settings')
> app.config.from_envvar('YOURAPPLICATION_SETTINGS')
> 
> The essential part is the last line, where you use the os env variables 
to point to the server specific settings, wouldn't that be a solution to 
your problem.

Thanks, I can make that work. That hadn't occurred to me. I poked around 
and found this:


http://stackoverflow.com/questions/13517160/pass-parameters-to-python-flask-via-uwsgi-nginx

It wasn't clear though how I could use that to pass a flag to Flask; if 
anyone has used that in this context I'd appreciate an example of how that
might work.

Cheers,
Demitri

Re: [flask] uwsgi + configuration outside of app

From:
Changjiang Chan
Date:
2013-05-21 @ 23:56
Here is another solution.

1. In  the last line of your config.py add this code to import your own
config.

try:
    from local_config import *
except ImportError:
    pass


2. Create the local_config.py and overwrite variables for that machine,
like DEBUG, dburi

3. Exclude the local_config.py from your version control system. If you use
git, add local_config.py to .gitignore to ensure every server has its own
settings.




On Wed, May 22, 2013 at 5:16 AM, <thatsanicehatyouhave@me.com> wrote:

> Hi Jonas,
>
> On May 21, 2013, at 12:18 PM, Jonas Brunsgaard <jonas.brunsgaard@gmail.com>
> wrote:
>
> app = Flask(__name__)
> app.config.from_object('yourapplication.default_settings')
> app.config.from_envvar('YOURAPPLICATION_SETTINGS')
>
> The essential part is the last line, where you use the os env variables to
> point to the server specific settings, wouldn't that be a solution to your
> problem.
>
>
> Thanks, I can make that work. That hadn't occurred to me. I poked around
> and found this:
>
>
> 
http://stackoverflow.com/questions/13517160/pass-parameters-to-python-flask-via-uwsgi-nginx
>
> It wasn't clear though how I could use that to pass a flag to Flask; if
> anyon e has used that in this context I'd appreciate an example of how that
> might work.
>
> Cheers,
> Demitri
>

Re: [flask] uwsgi + configuration outside of app

From:
Anthony Ford
Date:
2013-05-22 @ 01:37
To add to Changjiang Chan's method:

3a. Add a template version of your local config (to remind you of the
values that must be set) to your repo.
3b. Tell your version control system to ignore changes to that file.
      For git you can use 'git update-index --assume-unchanged
local_config.py' to make the system ignore all changes to local_config.py.
A good way to help yourself remember all values that need to be set, and to
keep yourself from accidentally committing a copy of your local_config.py



Anthony Ford,
KF5IBN,
Research Assistant,
Center for Advanced Radio Astronomy,
University of Texas at Brownsville
ford.anthonyj@gmail.com


On Tue, May 21, 2013 at 6:56 PM, Changjiang Chan <zmccj.comm@gmail.com>wrote:

> Here is another solution.
>
> 1. In  the last line of your config.py add this code to import your own
> config.
>
> try:
>     from local_config import *
> except ImportError:
>     pass
>
>
> 2. Create the local_config.py and overwrite variables for that machine,
> like DEBUG, dburi
>
> 3. Exclude the local_config.py from your version control system. If you
> use git, add local_config.py to .gitignore to ensure every server has its
> own settings.
>
>
>
>
> On Wed, May 22, 2013 at 5:16 AM, <thatsanicehatyouhave@me.com> wrote:
>
>> Hi Jonas,
>>
>> On May 21, 2013, at 12:18 PM, Jonas Brunsgaard <
>> jonas.brunsgaard@gmail.com> wrote:
>>
>> app = Flask(__name__)
>> app.config.from_object('yourapplication.default_settings')
>> app.config.from_envvar('YOURAPPLICATION_SETTINGS')
>>
>> The essential part is the last line, where you use the os env variables
>> to point to the server specific settings, wouldn't that be a solution to
>> your problem.
>>
>>
>> Thanks, I can make that work. That hadn't occurred to me. I poked around
>> and found this:
>>
>>
>> 
http://stackoverflow.com/questions/13517160/pass-parameters-to-python-flask-via-uwsgi-nginx
>>
>> It wasn't clear though how I could use that to pass a flag to Flask; if
>> anyon e has used that in this context I'd appreciate an example of how that
>> might work.
>>
>> Cheers,
>> Demitri
>>
>
>

Re: [flask] uwsgi + configuration outside of app

From:
Changjiang Chan
Date:
2013-05-22 @ 06:53
Thanks to Anthony Ford.
Actually, we can set variable description or default value in your
config.py file. Like
  DBURI = mysql://user:pass@dbhost:port/dbname
Then overwrite it with server specific value in your local_config.py
If you put local_config.py into .gitignore, you will not commit it by
mistake. And I think the .gitignore file should be added to your version
control system.

Z.M

在 2013-5-22 上午9:40,"Anthony Ford" <ford.anthonyj@gmail.com>写道:
>
> To add to Changjiang Chan's method:
>
> 3a. Add a template version of your local config (to remind you of the
values that must be set) to your repo.
> 3b. Tell your version control system to ignore changes to that file.
>       For git you can use 'git update-index --assume-unchanged
local_config.py' to make the system ignore all changes to local_config.py.
A good way to help yourself remember all values that need to be set, and to
keep yourself from accidentally committing a copy of your local_config.py
>
>
>
> Anthony Ford,
> KF5IBN,
> Research Assistant,
> Center for Advanced Radio Astronomy,
> University of Texas at Brownsville
> ford.anthonyj@gmail.com
>
>
> On Tue, May 21, 2013 at 6:56 PM, Changjiang Chan <zmccj.comm@gmail.com>
wrote:
>>
>> Here is another solution.
>>
>> 1. In  the last line of your config.py add this code to import your own
config.
>>
>> try:
>>     from local_config import *
>> except ImportError:
>>     pass
>>
>>
>> 2. Create the local_config.py and overwrite variables for that machine,
like DEBUG, dburi
>>
>> 3. Exclude the local_config.py from your version control system. If you
use git, add local_config.py to .gitignore to ensure every server has its
own settings.
>>
>>
>>
>>
>> On Wed, May 22, 2013 at 5:16 AM, <thatsanicehatyouhave@me.com> wrote:
>>>
>>> Hi Jonas,
>>>
>>> On May 21, 2013, at 12:18 PM, Jonas Brunsgaard <
jonas.brunsgaard@gmail.com> wrote:
>>>
>>>> app = Flask(__name__)
>>>> app.config.from_object('yourapplication.default_settings')
>>>> app.config.from_envvar('YOURAPPLICATION_SETTINGS')
>>>>
>>>> The essential part is the last line, where you use the os env
variables to point to the server specific settings, wouldn't that be a
solution to your problem.
>>>
>>>
>>> Thanks, I can make that work. That hadn't occurred to me. I poked
around and found this:
>>>
>>>

http://stackoverflow.com/questions/13517160/pass-parameters-to-python-flask-via-uwsgi-nginx
>>>
>>> It wasn't clear though how I could use that to pass a flag to Flask; if
anyon e has used that in this context I'd appreciate an example of how that
might work.
>>>
>>> Cheers,
>>> Demitri
>>
>>
>

Re: [flask] uwsgi + configuration outside of app

From:
Date:
2013-05-22 @ 15:17
Thanks for the suggestions! I was hoping to keep all of the configurations
under source control, but I'll play with these ideas.

Demitri

Re: [flask] uwsgi + configuration outside of app

From:
Roberto De Ioris
Date:
2013-05-22 @ 15:21
>
> Thanks for the suggestions! I was hoping to keep all of the configurations
> under source control, but I'll play with these ideas.
>
> Demitri
>
>
Take in account you can access options in the uWSGI configs via uwsgi.opt

[uwsgi]
socket = :8080
foobar = test
myserver = 192.168.0.1
....


uwsgi.opt['myserver'] will return 192.168.0.1


Maybe it could be useful

-- 
Roberto De Ioris
http://unbit.it

Re: [flask] uwsgi + configuration outside of app

From:
Date:
2013-05-22 @ 16:10
On May 22, 2013, at 11:21 AM, Roberto De Ioris <roberto@unbit.it> wrote:

> Take in account you can access options in the uWSGI configs via uwsgi.opt

Perfect! That's exactly what I wanted to do. Thanks very much!

For those (like myself) who hadn't seen that before, one only needs an 
"import uwsgi" before accessing the dictionary. What I've done is put the 
name of the configuration file in the uWSGI configs (which are under 
source control), and the server-specific configuration is then located in 
that file, also under source control.

Cheers,
Demitri