From 2a4665ca3708992559af0fa41d4e4ea15dcc597e Mon Sep 17 00:00:00 2001 From: shreyas Date: Thu, 25 Feb 2016 14:12:44 +0530 Subject: [PATCH] [Fix] * Generate common_site_config.json while initializing a new bench * Clean-up some code --- bench/app.py | 2 +- bench/config.py | 45 ++++++++++++++++++------ bench/production_setup.py | 2 +- bench/templates/redis_async_broker.conf | 1 - bench/templates/redis_cache.conf | 1 - bench/templates/redis_celery_broker.conf | 1 - bench/tests/test_init.py | 3 ++ bench/utils.py | 14 ++++---- 8 files changed, 48 insertions(+), 21 deletions(-) diff --git a/bench/app.py b/bench/app.py index 9032f540..0b0ac760 100644 --- a/bench/app.py +++ b/bench/app.py @@ -54,7 +54,7 @@ def get_app(app, git_url, branch=None, bench='.', build_asset_files=True, verbos install_app(app, bench=bench, verbose=verbose) if build_asset_files: build_assets(bench=bench) - conf = get_config() + conf = get_config(bench=bench) if conf.get('restart_supervisor_on_update'): restart_supervisor_processes(bench=bench) diff --git a/bench/config.py b/bench/config.py index 0bcdd9af..7894eb14 100644 --- a/bench/config.py +++ b/bench/config.py @@ -30,7 +30,7 @@ def generate_supervisor_config(bench='.', user=None): sites = get_sites(bench=bench) if not user: user = getpass.getuser() - config = get_config() + config = get_config(bench=bench) config = template.render(**{ "bench_dir": bench_dir, @@ -52,6 +52,30 @@ def get_site_config(site, bench='.'): with open(os.path.join(bench, 'sites', site, 'site_config.json')) as f: return json.load(f) +def generate_common_site_config(bench='.'): + '''Generates the default common_site_config.json while a new bench is created''' + config = get_config(bench=bench) + + celery_broker_port = config.get('redis_celery_broker_port', '11311') + celery_broker = 'redis://localhost:{0}'.format(celery_broker_port) + + async_redis_server_port = config.get('redis_async_broker_port', '12311') + async_redis_server = 'redis://localhost:{0}'.format(async_redis_server_port) + + cache_redis_server_port = config.get('redis_cache_port', '13311') + cache_redis_server = 'redis://localhost:{0}'.format(cache_redis_server_port) + + default_common_site_config = { + "celery_broker" : celery_broker, + "async_redis_server": async_redis_server, + "cache_redis_server": cache_redis_server + } + + #TODO Optionally we need to add the host or domain name in case dns_multitenant is false + + with open(os.path.join(bench, 'sites', 'common_site_config.json'), 'wb') as f: + json.dump(default_common_site_config, f, indent=1, sort_keys=True) + def get_sites_with_config(bench='.'): sites = get_sites(bench=bench) ret = [] @@ -71,8 +95,9 @@ def generate_nginx_config(bench='.'): sites_dir = os.path.join(bench_dir, "sites") sites = get_sites_with_config(bench=bench) user = getpass.getuser() + config = get_config(bench) - if get_config().get('serve_default_site'): + if config.get('serve_default_site'): try: with open("sites/currentsite.txt") as f: default_site = {'name': f.read().strip()} @@ -83,9 +108,9 @@ def generate_nginx_config(bench='.'): config = template.render(**{ "sites_dir": sites_dir, - "http_timeout": get_config().get("http_timeout", 120), + "http_timeout": config.get("http_timeout", 120), "default_site": default_site, - "dns_multitenant": get_config().get('dns_multitenant'), + "dns_multitenant": config.get('dns_multitenant'), "sites": sites }) write_config_file(bench, 'nginx.conf', config) @@ -95,7 +120,7 @@ def generate_redis_celery_broker_config(bench='.'): _generate_redis_config( template_name='redis_celery_broker.conf', context={ - "port": get_config().get('redis_async_broker_port', '12311'), + "port": get_config(bench).get('redis_celery_broker_port', '11311'), "bench_path": os.path.abspath(bench), }, bench=bench @@ -106,21 +131,21 @@ def generate_redis_async_broker_config(bench='.'): _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) + "port": get_config(bench).get('redis_async_broker_port', '12311'), }, bench=bench ) def generate_redis_cache_config(bench='.'): """Redis that is used and optimized for caching""" + config = get_config(bench=bench) + _generate_redis_config( template_name='redis_cache.conf', context={ - "maxmemory": get_config().get('cache_maxmemory', '50'), - "port": get_config().get('redis_cache_port', '11311'), + "maxmemory": config.get('cache_maxmemory', '50'), + "port": config.get('redis_cache_port', '13311'), "redis_version": get_redis_version(), - "bench_path": os.path.abspath(bench), }, bench=bench ) diff --git a/bench/production_setup.py b/bench/production_setup.py index f5df633f..a2d13d4f 100644 --- a/bench/production_setup.py +++ b/bench/production_setup.py @@ -52,7 +52,7 @@ def copy_default_nginx_config(): def setup_production(user, bench='.'): generate_supervisor_config(bench=bench, user=user) generate_nginx_config(bench=bench) - fix_prod_setup_perms(frappe_user=user) + fix_prod_setup_perms(bench, frappe_user=user) remove_default_nginx_configs() if is_centos7(): diff --git a/bench/templates/redis_async_broker.conf b/bench/templates/redis_async_broker.conf index 208d37a0..38237ef2 100644 --- a/bench/templates/redis_async_broker.conf +++ b/bench/templates/redis_async_broker.conf @@ -2,4 +2,3 @@ dbfilename redis_async_broker.rdb dir {{process_files_path}} pidfile {{process_files_path}}/redis_async_broker.pid port {{port}} -bench_path {{bench_path}} diff --git a/bench/templates/redis_cache.conf b/bench/templates/redis_cache.conf index 5f7d996e..d9a80dcc 100644 --- a/bench/templates/redis_cache.conf +++ b/bench/templates/redis_cache.conf @@ -6,4 +6,3 @@ maxmemory {{maxmemory}}mb maxmemory-policy allkeys-lru save "" appendonly no -bench_path {{bench_path}} diff --git a/bench/templates/redis_celery_broker.conf b/bench/templates/redis_celery_broker.conf index 454bd5e7..6535eaa3 100644 --- a/bench/templates/redis_celery_broker.conf +++ b/bench/templates/redis_celery_broker.conf @@ -2,4 +2,3 @@ dbfilename redis_celery_broker.rdb dir {{process_files_path}} pidfile {{process_files_path}}/redis_celery_broker.pid port {{port}} -bench_path {{bench_path}} diff --git a/bench/tests/test_init.py b/bench/tests/test_init.py index 8a45cfe5..64b0a2b6 100644 --- a/bench/tests/test_init.py +++ b/bench/tests/test_init.py @@ -103,5 +103,8 @@ class TestBenchInit(unittest.TestCase): for key, port in ports: self.assertEqual(config_json.get(key), port) + def assert_site_config(self, bench_name): + pass + def assert_exists(self, *args): self.assertTrue(os.path.exists(os.path.join(*args))) diff --git a/bench/utils.py b/bench/utils.py index bb5590ce..0635c8a1 100644 --- a/bench/utils.py +++ b/bench/utils.py @@ -49,7 +49,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, generate_redis_celery_broker_config + from .config import generate_redis_cache_config, generate_redis_async_broker_config, generate_redis_celery_broker_config, generate_common_site_config global FRAPPE_VERSION if os.path.exists(path): @@ -68,6 +68,8 @@ def init(path, apps_path=None, no_procfile=False, no_backups=False, bench_config = make_bench_config() put_config(bench_config, bench=path) + generate_common_site_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) @@ -100,8 +102,8 @@ def make_bench_config(): return bench_config def get_max_worker_count(): - '''This function will return the maximum workers that can be started depending upon''' - '''number of cpu's present on the machine''' + '''This function will return the maximum workers that can be started depending upon + number of cpu's present on the machine''' n_cpus = multiprocessing.cpu_count() return dict(max_workers=2 * n_cpus) @@ -291,7 +293,7 @@ def setup_logging(bench='.'): logger.addHandler(hdlr) logger.setLevel(logging.DEBUG) -def get_config(bench='.'): +def get_config(bench): config_path = os.path.join(bench, 'config.json') if not os.path.exists(config_path): return {} @@ -473,7 +475,7 @@ def drop_privileges(uid_name='nobody', gid_name='nogroup'): # Ensure a very conservative umask os.umask(022) -def fix_prod_setup_perms(frappe_user=None): +def fix_prod_setup_perms(bench='.', frappe_user=None): files = [ "logs/web.error.log", "logs/web.log", @@ -486,7 +488,7 @@ def fix_prod_setup_perms(frappe_user=None): ] if not frappe_user: - frappe_user = get_config().get('frappe_user') + frappe_user = get_config(bench).get('frappe_user') if not frappe_user: print "frappe user not set"