2
0
mirror of https://github.com/frappe/bench.git synced 2024-11-13 16:56:33 +00:00

Add better nginx config generation, fix #20

This commit is contained in:
Pratik Vyas 2014-07-21 10:15:18 +05:30
parent 14a944467b
commit e6a6973eff
4 changed files with 122 additions and 62 deletions

View File

@ -7,11 +7,12 @@ from .utils import setup_auto_update as _setup_auto_update
from .utils import setup_sudoers as _setup_sudoers
from .utils import start as _start
from .utils import setup_procfile as _setup_procfile
from .utils import set_nginx_port as _set_nginx_port
from .utils import build_assets, patch_sites, exec_cmd, update_bench, get_frappe, setup_logging, get_config, update_config, restart_supervisor_processes
from .app import get_app as _get_app
from .app import new_app as _new_app
from .app import pull_all_apps
from .config import generate_config
from .config import generate_nginx_config, generate_supervisor_config
from .migrate3to4 import main as _migrate_3to4
import os
import sys
@ -108,6 +109,13 @@ def migrate_3to4(path):
"Migrate from ERPNext v3.x"
_migrate_3to4(path)
@click.command('set-nginx-port')
@click.argument('site')
@click.argument('port', type=int)
def set_nginx_port(site, port):
"Set nginx port for site"
_set_nginx_port(site, port)
## Setup
@click.group()
def setup():
@ -122,12 +130,12 @@ def setup_sudoers():
@click.command('nginx')
def setup_nginx():
"generate config for nginx"
generate_config('nginx', 'nginx.conf')
generate_nginx_config()
@click.command('supervisor')
def setup_supervisor():
"generate config for supervisor"
generate_config('supervisor', 'supervisor.conf')
generate_supervisor_config()
update_config({'restart_supervisor_on_update': True})
@click.command('auto-update')
@ -191,9 +199,17 @@ def config_update_bench_on_update(state):
state = True if state == 'on' else False
update_config({'update_bench_on_update': state})
@click.command('dns_multitenant')
@click.argument('state', type=click.Choice(['on', 'off']))
def config_dns_multitenant(state):
"Enable/Disable bench updates on running bench update"
state = True if state == 'on' else False
update_config({'dns_multitenant': state})
config.add_command(config_auto_update)
config.add_command(config_update_bench_on_update)
config.add_command(config_restart_supervisor_on_update)
config.add_command(config_dns_multitenant)
#Bench commands
@ -206,5 +222,6 @@ bench.add_command(update)
bench.add_command(restart)
bench.add_command(config)
bench.add_command(start)
bench.add_command(set_nginx_port)
bench.add_command(migrate_3to4)

View File

@ -1,12 +1,13 @@
import os
import getpass
import json
from jinja2 import Environment, PackageLoader
from .utils import get_sites
from .utils import get_sites, get_config
env = Environment(loader=PackageLoader('bench', 'templates'), trim_blocks=True)
def generate_config(application, template_name, bench='.'):
template = env.get_template(template_name)
def generate_supervisor_config(bench='.'):
template = env.get_template('supervisor.conf')
bench_dir = os.path.abspath(bench)
sites_dir = os.path.join(bench_dir, "sites")
sites = get_sites(bench=bench)
@ -18,8 +19,38 @@ def generate_config(application, template_name, bench='.'):
"bench_dir": bench_dir,
"sites_dir": sites_dir,
"user": user,
})
with open("config/supervisor.conf", 'w') as f:
f.write(config)
def get_site_config(site, bench='.'):
with open(os.path.join(bench, 'sites', site, 'site_config.json')) as f:
return json.load(f)
def get_sites_with_config(bench='.'):
sites = get_sites()
return [{
"name": site,
"port": get_site_config(site, bench=bench).get('nginx_port')
} for site in sites]
def generate_nginx_config(bench='.'):
template = env.get_template('nginx.conf')
bench_dir = os.path.abspath(bench)
sites_dir = os.path.join(bench_dir, "sites")
sites = get_sites_with_config(bench=bench)
user = getpass.getuser()
with open("sites/currentsite.txt") as f:
default_site = f.read().strip()
default_site = {
'name': default_site
}
config = template.render(**{
"sites_dir": sites_dir,
"default_site": default_site,
"dns_multitenant": get_config().get('dns_multitenant'),
"sites": sites
})
with open("config/{}.conf".format(application), 'w') as f:
with open("config/nginx.conf", 'w') as f:
f.write(config)

View File

@ -3,67 +3,57 @@ upstream frappe {
server 127.0.0.1:8000 fail_timeout=0;
}
server {
listen 80;
client_max_body_size 4G;
server_name
{% for sitename in sites %}
{{ sitename }}
{% endfor %}
;
keepalive_timeout 5;
sendfile on;
root {{ sites_dir }};
{% macro server_block(site, port=80, default=False, server_name=None, sites=None, dns_multitenant=False) -%}
server {
listen {{ site.port if not default else port }} {% if default %} default {% endif %};
client_max_body_size 4G;
{% if dns_multitenant and sites %}
server_name {% for site in sites %} {{ site.name }} {% endfor %};
{% else %}
server_name {{ site.name if not server_name else server_name }};
{% endif %}
keepalive_timeout 5;
sendfile on;
root {{ sites_dir }};
location /private/ {
internal;
try_files /$uri =424;
}
location /private/ {
internal;
try_files /$uri =424;
}
location /assets {
try_files $uri =404;
}
location /assets {
try_files $uri =404;
}
location / {
try_files /$host/public/$uri @magic;
}
location / {
try_files /{{ "$host" if dns_multitenant else site.name }}/public/$uri @magic;
}
location @magic {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Use-X-Accel-Redirect True;
proxy_read_timeout 120;
proxy_redirect off;
proxy_pass http://frappe;
}
}
location @magic {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
{% if not dns_multitenant %}
proxy_set_header Host {{ site.name }};
{% endif %}
proxy_set_header X-Use-X-Accel-Redirect True;
proxy_read_timeout 120;
proxy_redirect off;
proxy_pass http://frappe;
}
}
{%- endmacro %}
server {
listen 80 default;
client_max_body_size 4G;
server_name localhost;
keepalive_timeout 5;
sendfile on;
root {{ sites_dir }};
{% for site in sites %}
location /private/ {
internal;
try_files /$uri =424;
}
{% if site.port %}
{{ server_block(site) }}
{% endif %}
location /assets {
try_files $uri =404;
}
{% endfor %}
location / {
try_files /{{ default_site }}/public/$uri @magic;
}
{% if default_site %}
{{ server_block(default_site, default=True, server_name="frappe_default_site") }}
{% endif %}
location @magic {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Use-X-Accel-Redirect True;
proxy_set_header Host {{ default_site }};
proxy_read_timeout 120;
proxy_redirect off;
proxy_pass http://frappe;
}
}
{% if dns_multitenant and sites %}
{{ server_block(None,sites=sites, dns_multitenant=True) }}
{% endif %}

View File

@ -184,3 +184,25 @@ def get_cmd_output(cmd, cwd='.'):
def restart_supervisor_processes():
exec_cmd("sudo supervisorctl restart frappe:")
def get_site_config(site, bench='.'):
config_path = os.path.join(bench, 'sites', site, 'site_config.json')
if not os.path.exists(config_path):
return {}
with open(config_path) as f:
return json.load(f)
def put_site_config(site, config, bench='.'):
config_path = os.path.join(bench, 'sites', site, 'site_config.json')
with open(config_path, 'w') as f:
return json.dump(config, f, indent=1)
def update_site_config(site, new_config, bench='.'):
config = get_site_config(site, bench=bench)
config.update(new_config)
put_site_config(site, config, bench=bench)
def set_nginx_port(site, port, bench='.'):
if site not in get_sites(bench=bench):
raise Exception("No such site")
update_site_config(site, {"nginx_port": port}, bench=bench)