Add a command to generate configuration examples

Config files are generated from templates (which remplace previous example files).

- solve the issue of hard-to-explain configuration examples
- ease pkg path seeking (avoid it, actually)
- add working defaults for sqlite and unix socket paths (instead of
  /replace/me/path/example)
- move settings comments from default_settings.py to ihatemoney.cfg.j2, as it is
  the one that will be facing user.
This commit is contained in:
Jocelyn Delalande 2017-10-23 09:49:08 +02:00
parent efd2b66ac6
commit e4074ccadd
11 changed files with 114 additions and 66 deletions

View file

@ -21,6 +21,7 @@ Changed
Added
=====
- ``ihatemoney generate-config`` to give working examples of config files (#275)
- Statistics tab (#257)
- Python3.6 support (#259)
- ALLOW_PUBLIC_PROJECT_CREATION setting (#262)

View file

@ -1,18 +0,0 @@
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName ihatemoney.example.com
# Uncomment the python-home option if you use a virtualenv
WSGIDaemonProcess ihatemoney user=www-data group=www-data threads=5 python-path=/path/to/ihatemoney/ihatemoney # python-home=/path/to/your/venv
WSGIScriptAlias / /path/to/ihatemoney/ihatemoney/wsgi.py
ErrorLog /var/log/apache2/ihatemoney.example.com_error.log
CustomLog /var/log/apache2/ihatemoney.example.com_access.log combined
<Directory /path/to/ihatemoney>
WSGIProcessGroup ihatemoney
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
# Alias value may be some messy path, within python libs.
# You may want to use "find $VIRTUAL_ENV -path */ihatemoney*/static" to find it.
Alias /static/ /path/to/ihatemoney/ihatemoney/static/
</VirtualHost>

View file

@ -1,7 +0,0 @@
backlog = 2048
daemon = False
debug = True
workers = 3
logfile = "/path/to/your/app/ihatemoney.gunicorn.log"
loglevel = "info"
bind = "unix:/path/to/your/app/ihatemoney.gunicorn.sock"

View file

@ -1,6 +0,0 @@
[program:ihatemoney]
command=/path/to/your/app/venv/bin/gunicorn -c /etc/ihatemoney/gunicorn.conf.py ihatemoney.wsgi:application
user=www
autostart=true
autorestart=true
redirect_stderr=True

View file

@ -0,0 +1,19 @@
<VirtualHost *:80>
ServerAdmin admin@example.com # CUSTOMIZE
ServerName ihatemoney.example.com # CUSTOMIZE
WSGIDaemonProcess ihatemoney user=www-data group=www-data threads=5 python-path={{ pkg_path }} {% if venv_path %}python-home={{ venv_path }}{% endif %}
WSGIScriptAlias / {{ pkg_path }}/wsgi.py
ErrorLog /var/log/apache2/ihatemoney.example.com_error.log
CustomLog /var/log/apache2/ihatemoney.example.com_access.log combined
<Directory {{ pkg_path }}>
WSGIProcessGroup ihatemoney
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
Alias /static/ {{ pkg_path }}/static/
</VirtualHost>

View file

@ -0,0 +1,8 @@
backlog = 2048
daemon = False
debug = True
workers = 3
# log to stdout,
logfile = "-" # Is the default setting for gunicorn>=20
loglevel = "info"
bind = "unix:/tmp/ihatemoney.gunicorn.sock"

View file

@ -0,0 +1,36 @@
# You can find more information about what these settings mean in the
# documentation, available online at
# http://ihatemoney.readthedocs.io/en/latest/installation.html#configuration
# Turn this on if you want to have more output on what's happening under the
# hood. DO NOT TURN IT ON IN PRODUCTION.
DEBUG = False
# The database URI, reprensenting the type of database and how to connect to it.
# Enter an absolute path here.
SQLALCHEMY_DATABASE_URI = 'sqlite:///var/lib/ihatemoney/ihatemoney.sqlite'
SQLACHEMY_ECHO = DEBUG
# Will likely become the default value in flask-sqlalchemy >=3 ; could be removed
# then:
SQLALCHEMY_TRACK_MODIFICATIONS = False
# This secret key is random and auto-generated, it protects cookies and user sessions
SECRET_KEY = "{{ secret_key }}"
# A python tuple describing the name and email adress of the sender of the mails.
MAIL_DEFAULT_SENDER = ("Budget manager", "budget@notmyidea.org") # CUSTOMIZE
# If set to True, a demonstration project will be activated.
ACTIVATE_DEMO_PROJECT = True
# If not empty, the specified password must be entered to create new projects.
# DO NOT enter the password in cleartext. Generate a password hash with
# "ihatemoney generate_password_hash" instead.
ADMIN_PASSWORD = ""
# If set to True (default value) anyone can create a new project.
ALLOW_PUBLIC_PROJECT_CREATION = True
# If set to True, an administration dashboard is available.
ACTIVATE_ADMIN_DASHBOARD = False

View file

@ -1,11 +1,9 @@
server {
server_name yourur;
server_name ihatemoney.example.com; # CUSTOMIZE
keepalive_timeout 5;
location /static/ {
# Alias value may be some messy path, within python libs.
# You may want to use "find $VIRTUAL_ENV -path */ihatemoney*/static" to find it.
alias /path/to/app/ihatemoney/static/;
alias {{ pkg_path }}/static/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@ -26,5 +24,5 @@ server {
}
upstream ihatemoney_backend {
server unix:/path/to/app/ihatemoney.gunicorn.sock;
server unix:/tmp/ihatemoney.gunicorn.sock;
}

View file

@ -0,0 +1,6 @@
[program:ihatemoney]
command={{ venv_path }}/bin/gunicorn -c /etc/ihatemoney/gunicorn.conf.py ihatemoney.wsgi:application
user=ihatemoney
autostart=true
autorestart=true
redirect_stderr=true

View file

@ -1,37 +1,10 @@
# You can find more information about what these settings mean in the
# documentation, available online at
# http://ihatemoney.readthedocs.io/en/latest/installation.html#configuration
# Turn this on if you want to have more output on what's happening under the
# hood.
DEBUG = False
# The database URI, reprensenting the type of database and how to connect to it.
# Enter an absolute path here.
# Verbose and documented settings are in conf-templates/ihatemoney.cfg.j2
DEBUG = SQLACHEMY_ECHO = False
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/ihatemoney.db'
SQLACHEMY_ECHO = DEBUG
# Will likely become the default value in flask-sqlalchemy >=3 ; could be removed
# then:
SQLALCHEMY_TRACK_MODIFICATIONS = False
# You need to change this secret key, otherwise bad things might happen to your
# users.
SECRET_KEY = "tralala"
# A python tuple describing the name and email adress of the sender of the mails.
MAIL_DEFAULT_SENDER = ("Budget manager", "budget@notmyidea.org")
# If set to True, a demonstration project will be activated.
ACTIVATE_DEMO_PROJECT = True
# If not empty, the specified password must be entered to create new projects.
# DO NOT enter the password in cleartext. Generate a password hash with
# "ihatemoney generate_password_hash" instead.
ADMIN_PASSWORD = ""
# If set to True (default value) anyone can create a new project.
ALLOW_PUBLIC_PROJECT_CREATION = True
# If set to True, an administration dashboard is available.
ACTIVATE_ADMIN_DASHBOARD = False

View file

@ -1,8 +1,13 @@
#!/usr/bin/env python
import os
import pkgutil
import random
from getpass import getpass
from flask_script import Manager, Command
from flask_script import Manager, Command, Option
from flask_migrate import Migrate, MigrateCommand
from jinja2 import Template
from werkzeug.security import generate_password_hash
from ihatemoney.run import create_app
@ -18,6 +23,38 @@ class GeneratePasswordHash(Command):
print(generate_password_hash(password))
class ConfigTemplate(Command):
def get_options(self):
return [
Option('config_file', choices=[
'ihatemoney.cfg',
'apache-vhost.conf',
'gunicorn.conf.py',
'supervisord.conf',
'nginx.conf',
]),
]
@staticmethod
def gen_secret_key():
return ''.join([
random.SystemRandom().choice(
'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
for i in range(50)])
def run(self, config_file):
template_content = pkgutil.get_data(
'ihatemoney',
os.path.join('conf-templates/', config_file) + '.j2'
).decode('utf-8')
print(Template(template_content).render(
pkg_path=os.path.abspath(os.path.dirname(__file__)),
venv_path=os.environ.get('VIRTUAL_ENV'),
secret_key=self.gen_secret_key(),
))
def main():
app = create_app()
Migrate(app, db)
@ -25,6 +62,7 @@ def main():
manager = Manager(app)
manager.add_command('db', MigrateCommand)
manager.add_command('generate_password_hash', GeneratePasswordHash)
manager.add_command('generate-config', ConfigTemplate)
manager.run()