diff --git a/bench/config/systemd.py b/bench/config/systemd.py new file mode 100644 index 00000000..2d8ce713 --- /dev/null +++ b/bench/config/systemd.py @@ -0,0 +1,44 @@ +import os, getpass, click +import bench + +def generate_supervisor_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) + + config = template.render(**{ + "bench_dir": bench_dir, + "sites_dir": os.path.join(bench_dir, 'sites'), + "user": user, + "frappe_version": get_current_frappe_version(bench_path), + "use_rq": use_rq(bench_path), + "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'), + "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?', + abort=True) + + with open(conf_path, 'w') as f: + f.write(config) + + update_config({'restart_supervisor_on_update': True}, bench_path=bench_path) diff --git a/bench/utils.py b/bench/utils.py index 029e9ec2..c958a3b3 100755 --- a/bench/utils.py +++ b/bench/utils.py @@ -426,6 +426,35 @@ def restart_supervisor_processes(bench_path='.', web_workers=False): exec_cmd('sudo supervisorctl restart {group}'.format(group=group), cwd=bench_path) +def restart_systemcd_processes(bench_path='.', web_workers=False): + from .config.common_site_config import get_config + conf = get_config(bench_path=bench_path) + bench_name = get_bench_name(bench_path) + + cmd = conf.get('systemd_restart_cmd') + if cmd: + exec_cmd(cmd, cwd=bench_path) + + else: + systemd_status = subprocess.check_output(['sudo', 'systemctl', 'status'], cwd=bench_path) + systemd_status = safe_decode(supervisor_status) + + if web_workers and '{bench_name}-web:'.format(bench_name=bench_name) in systemd_status: + group = '{bench_name}-web: '.format(bench_name=bench_name) + + elif '{bench_name}-workers:'.format(bench_name=bench_name) in systemd_status: + group = '{bench_name}-workers: {bench_name}-web:'.format(bench_name=bench_name) + + # backward compatibility + elif '{bench_name}-processes:'.format(bench_name=bench_name) in systemd_status: + group = '{bench_name}-processes:'.format(bench_name=bench_name) + + # backward compatibility + else: + group = 'frappe:' + + exec_cmd('sudo systemctl restart {group}'.format(group=group), cwd=bench_path) + def set_default_site(site, bench_path='.'): if not site in get_sites(bench_path=bench_path): raise Exception("Site not in bench")