diff --git a/bench/config.py b/bench/config.py index 1177aa76..905da273 100644 --- a/bench/config.py +++ b/bench/config.py @@ -3,6 +3,7 @@ import getpass import json import subprocess import shutil +import socket from distutils.spawn import find_executable from jinja2 import Environment, PackageLoader from .utils import get_sites, get_config, update_config, get_redis_version @@ -87,22 +88,40 @@ def generate_nginx_config(bench='.'): }) write_config_file(bench, 'nginx.conf', config) -def generate_redis_cache_config(bench='.'): - template = env.get_template('redis_cache.conf') - conf = { - "maxmemory": get_config().get('cache_maxmemory', '50'), - "port": get_config().get('redis_cache_port', '11311'), - "redis_version": get_redis_version() - } - config = template.render(**conf) - write_config_file(bench, 'redis_cache.conf', config) +def generate_redis_celery_broker_config(bench='.'): + """Redis that is used for queueing celery tasks""" + _generate_redis_config( + template_name='redis_celery_broker.conf', + context={ + }, + bench=bench + ) def generate_redis_async_broker_config(bench='.'): - template = env.get_template('redis_async_broker.conf') - conf = { - "port": get_config().get('redis_async_broker_port', '12311'), - "redis_version": get_redis_version() - } - config = template.render(**conf) - write_config_file(bench, 'redis_async_broker.conf', config) + """Redis that is used to do pub/sub""" + _generate_redis_config( + template_name='redis_async_broker.conf', + context={ + "port": get_config().get('redis_async_broker_port', '12311'), + "bench_path": os.path.abspath(bench) + }, + bench=bench + ) + +def generate_redis_cache_config(bench='.'): + """Redis that is used and optimized for caching""" + _generate_redis_config( + template_name='redis_cache.conf', + context={ + "maxmemory": get_config().get('cache_maxmemory', '50'), + "port": get_config().get('redis_cache_port', '11311'), + "redis_version": get_redis_version() + }, + bench=bench + ) + +def _generate_redis_config(template_name, context, bench): + template = env.get_template(template_name) + redis_config = template.render(**context) + write_config_file(bench, template_name, redis_config) diff --git a/bench/utils.py b/bench/utils.py index 6a8eaf77..c5cdf4de 100644 --- a/bench/utils.py +++ b/bench/utils.py @@ -47,7 +47,7 @@ def init(path, apps_path=None, no_procfile=False, no_backups=False, no_auto_update=False, frappe_path=None, frappe_branch=None, wheel_cache_dir=None, verbose=False): from .app import get_app, install_apps_from_path - from .config import generate_redis_cache_config, generate_redis_async_broker_config + from .config import generate_redis_cache_config, generate_redis_async_broker_config, generate_redis_celery_broker_config global FRAPPE_VERSION if os.path.exists(path): @@ -62,28 +62,69 @@ def init(path, apps_path=None, no_procfile=False, no_backups=False, setup_logging() setup_env(bench=path) - put_config(default_config, bench=path) - # if wheel_cache_dir: - # update_config({"wheel_cache_dir":wheel_cache_dir}, bench=path) - # prime_wheel_cache(bench=path) + + bench_config = make_bench_config() + put_config(bench_config, bench=path) if not frappe_path: frappe_path = 'https://github.com/frappe/frappe.git' get_app('frappe', frappe_path, branch=frappe_branch, bench=path, build_asset_files=False, verbose=verbose) + + if apps_path: + install_apps_from_path(apps_path, bench=path) + + FRAPPE_VERSION = get_current_frappe_version(bench=path) + if FRAPPE_VERSION > 5: + setup_socketio(bench=path) + + build_assets(bench=path) + generate_redis_celery_broker_config(bench=path) + generate_redis_cache_config(bench=path) + generate_redis_async_broker_config(bench=path) + if not no_procfile: setup_procfile(bench=path) if not no_backups: setup_backups(bench=path) if not no_auto_update: setup_auto_update(bench=path) - if apps_path: - install_apps_from_path(apps_path, bench=path) - FRAPPE_VERSION = get_current_frappe_version(bench=path) - if FRAPPE_VERSION > 5: - setup_socketio(bench=path) - build_assets(bench=path) - generate_redis_cache_config(bench=path) - generate_redis_async_broker_config(bench=path) + +def make_bench_config(): + bench_config = {} + bench_config.update(default_config) + bench_config.update(make_ports()) + return bench_config + +def make_ports(benches_path="."): + default_ports = { + "webserver_port": 8000, + "socketio_port": 9000, + "redis_celery_broker_port": 11000, + "redis_async_broker_port": 12000, + "redis_cache_port": 13000 + } + + # collect all existing ports + existing_ports = {} + for folder in os.listdir(benches_path): + bench = os.path.join(benches_path, folder) + if os.path.isdir(bench): + bench_config = get_config(bench) + for key in default_ports.keys(): + value = bench_config.get(key) + if value: + existing_ports.setdefault(key, []).append(value) + + # new port value = max of existing port value + 1 + ports = {} + for key, value in default_ports.items(): + existing_value = max(existing_ports.get(key, [])) + if existing_value: + value = existing_value + 1 + + ports[key] = value + + return ports def exec_cmd(cmd, cwd='.'): from .cli import from_command_line