From 6a5226e071f181ff699308572e303cc413afd3f5 Mon Sep 17 00:00:00 2001 From: Revant Nandgaonkar Date: Wed, 11 Apr 2018 19:32:15 +0530 Subject: [PATCH] generate frappe-bench.target --- bench/commands/setup.py | 11 +++++- bench/config/systemd.py | 77 ++++++++++++++++++++++++++++++++++------- 2 files changed, 74 insertions(+), 14 deletions(-) diff --git a/bench/commands/setup.py b/bench/commands/setup.py index c7a8d12d..90101d7f 100755 --- a/bench/commands/setup.py +++ b/bench/commands/setup.py @@ -233,6 +233,14 @@ def setup_nginx_proxy_jail(**kwargs): from bench.utils import run_playbook run_playbook('roles/fail2ban/tasks/configure_nginx_jail.yml', extra_vars=kwargs) +@click.command('systemd') +@click.option('--user') +@click.option('--yes', help='Yes to regeneration of systemd config files', is_flag=True, default=False) +def setup_systemd(user=None, yes=False): + "generate configs for systemd with an optional user argument" + from bench.config.systemd import generate_systemd_config + generate_systemd_config(bench_path=".", user=user, yes=yes) + setup.add_command(setup_sudoers) setup.add_command(setup_nginx) setup.add_command(reload_nginx) @@ -254,4 +262,5 @@ setup.add_command(sync_domains) setup.add_command(setup_firewall) setup.add_command(set_ssh_port) setup.add_command(setup_roles) -setup.add_command(setup_nginx_proxy_jail) \ No newline at end of file +setup.add_command(setup_nginx_proxy_jail) +setup.add_command(setup_systemd) diff --git a/bench/config/systemd.py b/bench/config/systemd.py index 2d8ce713..b5579f43 100644 --- a/bench/config/systemd.py +++ b/bench/config/systemd.py @@ -1,20 +1,20 @@ import os, getpass, click import bench -def generate_supervisor_config(bench_path, user=None, yes=False): +def generate_systemd_config(bench_path, user=None, yes=False): from bench.app import get_current_frappe_version, use_rq from bench.utils import get_bench_name, find_executable from bench.config.common_site_config import get_config, update_config, get_gunicorn_workers - template = bench.env.get_template('systemd/frappe-bench.target') if not user: user = getpass.getuser() config = get_config(bench_path=bench_path) bench_dir = os.path.abspath(bench_path) + bench_name = get_bench_name(bench_path) - config = template.render(**{ + bench_info = { "bench_dir": bench_dir, "sites_dir": os.path.join(bench_dir, 'sites'), "user": user, @@ -23,22 +23,73 @@ def generate_supervisor_config(bench_path, user=None, yes=False): "http_timeout": config.get("http_timeout", 120), "redis_server": find_executable('redis-server'), "node": find_executable('node') or find_executable('nodejs'), - "redis_cache_config": os.path.join(bench_dir, 'config', 'systemd/frappe-bench-redis-cache.service'), - "redis_socketio_config": os.path.join(bench_dir, 'config', 'systemd/frappe-bench-redis-socketio.service'), - "redis_queue_config": os.path.join(bench_dir, 'config', 'systemd/frappe-bench-redis-queue.service'), + "redis_cache_config": os.path.join(bench_dir, 'config', 'redis_cache.conf'), + "redis_socketio_config": os.path.join(bench_dir, 'config', 'redis_socketio.conf'), + "redis_queue_config": os.path.join(bench_dir, 'config', 'redis_queue.conf'), "webserver_port": config.get('webserver_port', 8000), "gunicorn_workers": config.get('gunicorn_workers', get_gunicorn_workers()["gunicorn_workers"]), "bench_name": get_bench_name(bench_path), "background_workers": config.get('background_workers') or 1, "bench_cmd": find_executable('bench') - }) + } - conf_path = os.path.join(bench_path, 'config', 'supervisor.conf') - if not yes and os.path.exists(conf_path): - click.confirm('supervisor.conf already exists and this will overwrite it. Do you want to continue?', + if not yes: + click.confirm('current systemd configuration will be overwritten. Do you want to continue?', abort=True) - with open(conf_path, 'w') as f: - f.write(config) + setup_systemd_directory(bench_path) + setup_main_config(bench_info, bench_path) + # setup_workers_config(bench_info) + # setup_web_config(bench_info) + # setup_redis_config(bench_info) - update_config({'restart_supervisor_on_update': True}, bench_path=bench_path) + update_config({'restart_systemd_on_update': True}, bench_path=bench_path) + +def setup_systemd_directory(bench_path): + if not os.path.exists(os.path.join(bench_path, 'config', 'systemd')): + os.makedirs(os.path.join(bench_path, 'config', 'systemd')) + +def setup_main_config(bench_info, bench_path): + # Main config + bench_template = bench.env.get_template('systemd/frappe-bench.target') + bench_config = bench_template.render(**bench_info) + bench_config_path = os.path.join(bench_path, 'config', 'systemd' , bench_info.get("bench_name") + '.target') + + with open(bench_config_path, 'w') as f: + f.write(bench_config) + +def setup_workers_config(bench_info, bench_path): + # Worker Group + bench_workers_target_template = bench.env.get_template('systemd/frappe-bench-workers.target') + bench_default_worker_template = bench.env.get_template('systemd/frappe-bench-frappe-default-worker.service') + bench_short_worker_template = bench.env.get_template('systemd/frappe-bench-frappe-short-worker.service') + bench_long_worker_template = bench.env.get_template('systemd/frappe-bench-frappe-long-worker.service') + bench_schedule_worker_template = bench.env.get_template('systemd/frappe-bench-frappe-schedule.service') + + bench_workers_target_config = bench_workers_target_template.render(**bench_info) + bench_default_worker_config = bench_default_worker_template.render(**bench_info) + bench_short_worker_config = bench_short_worker_template.render(**bench_info) + bench_long_worker_config = bench_long_worker_template.render(**bench_info) + bench_schedule_worker_config = bench_schedule_worker_template.render(**bench_info) + +def setup_web_config(bench_info, bench_path): + # Web Group + bench_web_target_template = bench.env.get_template('systemd/frappe-bench-web.target') + bench_web_service_template = bench.env.get_template('systemd/frappe-bench-frappe-web.service') + bench_node_socketio_template = bench.env.get_template('systemd/frappe-bench-node-socketio.service') + + bench_web_target_config = bench_web_target_template.render(**bench_info) + bench_web_service_config = bench_web_service_template.render(**bench_info) + bench_node_socketio_config = bench_node_socketio_template.render(**bench_info) + +def setup_redis_config(bench_info, bench_path): + # Redis Group + bench_redis_target_template = bench.env.get_template('systemd/frappe-bench-redis.target') + bench_redis_cache_template = bench.env.get_template('systemd/frappe-bench-redis-cache.service') + bench_redis_queue_template = bench.env.get_template('systemd/frappe-bench-redis-queue.service') + bench_redis_socketio_template = bench.env.get_template('systemd/frappe-bench-redis-socketio.service') + + bench_redis_target_config = bench_redis_target_template.render(**bench_info) + bench_redis_cache_config = bench_redis_cache_template.render(**bench_info) + bench_redis_queue_config = bench_redis_queue_template.render(**bench_info) + bench_redis_socketio_config = bench_redis_socketio_template.render(**bench_info)