def generate_supervisor_config(bench_path, user=None): from bench.app import get_current_frappe_version 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('supervisor.conf') if not user: user = getpass.getuser() config = get_config(bench=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, "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', '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'), "frappe_version": get_current_frappe_version(), "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) }) with open(os.path.join(bench_path, 'config', 'supervisor.conf'), 'w') as f: f.write(config) update_config({'restart_supervisor_on_update': True}, bench=bench_path)
def generate_supervisor_config(bench_path, user=None, force=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('supervisor.conf') if not user: user = getpass.getuser() config = get_config(bench=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', '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 }) conf_path = os.path.join(bench_path, 'config', 'supervisor.conf') if not force 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=bench_path)
def setup_production_prerequisites(): """Installs ansible, fail2banc, NGINX and supervisor""" if not find_executable("ansible"): exec_cmd("sudo {0} -m pip install ansible".format(sys.executable)) if not find_executable("fail2ban-client"): exec_cmd("bench setup role fail2ban") if not find_executable("nginx"): exec_cmd("bench setup role nginx") if not find_executable("supervisord"): exec_cmd("bench setup role supervisor")
def setup_procfile(bench_path, force=False): procfile_path = os.path.join(bench_path, 'Procfile') if not force and os.path.exists(procfile_path): click.confirm('A Procfile already exists and this will overwrite it. Do you want to continue?', abort=True) procfile = bench.env.get_template('Procfile').render(node=find_executable("node") \ or find_executable("nodejs")) with open(procfile_path, 'w') as f: f.write(procfile)
def setup_procfile(bench_path, force=False): procfile_path = os.path.join(bench_path, 'Procfile') if not force and os.path.exists(procfile_path): click.confirm( 'A Procfile already exists and this will overwrite it. Do you want to continue?', abort=True) procfile = bench.env.get_template('Procfile').render(node=find_executable("node") \ or find_executable("nodejs")) with open(procfile_path, 'w') as f: f.write(procfile)
def setup_procfile(bench_path, yes=False): config = get_config(bench_path=bench_path) procfile_path = os.path.join(bench_path, 'Procfile') if not yes and os.path.exists(procfile_path): click.confirm('A Procfile already exists and this will overwrite it. Do you want to continue?', abort=True) procfile = bench.env.get_template('Procfile').render( node=find_executable("node") or find_executable("nodejs"), use_rq=use_rq(bench_path), webserver_port=config.get('webserver_port')) with open(procfile_path, 'w') as f: f.write(procfile)
def generate_supervisor_config(bench_path, user=None): from bench.app import get_current_frappe_version 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('supervisor.conf') if not user: user = getpass.getuser() config = get_config(bench=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, "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', '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'), "frappe_version": get_current_frappe_version(), "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) }) with open(os.path.join(bench_path, 'config', 'supervisor.conf'), 'w') as f: f.write(config) update_config({'restart_supervisor_on_update': True}, bench=bench_path)
def reload_nginx(): try: exec_cmd('sudo {0} -t'.format(find_executable('nginx'))) except: raise service('nginx', 'reload')
def reload_supervisor(): supervisorctl = find_executable('supervisorctl') try: # first try reread/update exec_cmd('sudo {0} reread'.format(supervisorctl)) exec_cmd('sudo {0} update'.format(supervisorctl)) return except CommandFailedError: pass try: # something is wrong, so try reloading exec_cmd('sudo {0} reload'.format(supervisorctl)) return except CommandFailedError: pass try: # then try restart for centos service('supervisord', 'restart') return except CommandFailedError: pass try: # else try restart for ubuntu / debian service('supervisor', 'restart') return except CommandFailedError: pass
def reload_nginx(): try: subprocess.check_output(['sudo', find_executable('nginx'), '-t']) except: raise service('nginx', 'reload')
def service(service_name, service_option): if os.path.basename(find_executable('systemctl') or '') == 'systemctl' and is_running_systemd(): systemctl_cmd = "sudo {service_manager} {service_option} {service_name}" exec_cmd(systemctl_cmd.format(service_manager='systemctl', service_option=service_option, service_name=service_name)) elif os.path.basename(find_executable('service') or '') == 'service': service_cmd = "sudo {service_manager} {service_name} {service_option}" exec_cmd(service_cmd.format(service_manager='service', service_name=service_name, service_option=service_option)) else: # look for 'service_manager' and 'service_manager_command' in environment service_manager = os.environ.get("BENCH_SERVICE_MANAGER") if service_manager: service_manager_command = (os.environ.get("BENCH_SERVICE_MANAGER_COMMAND") or "{service_manager} {service_option} {service}").format(service_manager=service_manager, service=service, service_option=service_option) exec_cmd(service_manager_command) else: log("No service manager found: '{0} {1}' failed to execute".format(service_name, service_option), level=2)
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('supervisor.conf') 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', '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?', abort=True) with open(conf_path, 'w') as f: f.write(config) update_config({'restart_supervisor_on_update': True}, bench_path=bench_path) update_config({'restart_systemd_on_update': False}, bench_path=bench_path)
def generate_systemd_config(bench_path, user=None, yes=False, stop=False, create_symlinks=False, delete_symlinks=False): 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) if stop: exec_cmd('sudo systemctl stop -- $(systemctl show -p Requires {bench_name}.target | cut -d= -f2)'.format(bench_name=bench_name)) return if create_symlinks: _create_symlinks(bench_path) return if delete_symlinks: _delete_symlinks(bench_path) return number_of_workers = config.get('background_workers') or 1 background_workers = [] for i in range(number_of_workers): background_workers.append(get_bench_name(bench_path) + "-frappe-default-worker@" + str(i+1) + ".service") for i in range(number_of_workers): background_workers.append(get_bench_name(bench_path) + "-frappe-short-worker@" + str(i+1) + ".service") for i in range(number_of_workers): background_workers.append(get_bench_name(bench_path) + "-frappe-long-worker@" + str(i+1) + ".service") bench_info = { "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', '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), "worker_target_wants": " ".join(background_workers), "bench_cmd": find_executable('bench') } if not yes: click.confirm('current systemd configuration will be overwritten. Do you want to continue?', abort=True) setup_systemd_directory(bench_path) setup_main_config(bench_info, bench_path) setup_workers_config(bench_info, bench_path) setup_web_config(bench_info, bench_path) setup_redis_config(bench_info, bench_path) update_config({'restart_systemd_on_update': True}, bench_path=bench_path) update_config({'restart_supervisor_on_update': False}, bench_path=bench_path)
def generate_systemd_config(bench_path, user=None, yes=False, stop=False, create_symlinks=False, delete_symlinks=False): 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) if stop: exec_cmd( 'sudo systemctl stop -- $(systemctl show -p Requires {bench_name}.target | cut -d= -f2)' .format(bench_name=bench_name)) return if create_symlinks: _create_symlinks(bench_path) return if delete_symlinks: _delete_symlinks(bench_path) return number_of_workers = config.get('background_workers') or 1 background_workers = [] for i in range(number_of_workers): background_workers.append( get_bench_name(bench_path) + "-dataent-default-worker@" + str(i + 1) + ".service") for i in range(number_of_workers): background_workers.append( get_bench_name(bench_path) + "-dataent-short-worker@" + str(i + 1) + ".service") for i in range(number_of_workers): background_workers.append( get_bench_name(bench_path) + "-dataent-long-worker@" + str(i + 1) + ".service") bench_info = { "bench_dir": bench_dir, "sites_dir": os.path.join(bench_dir, 'sites'), "user": user, "dataent_version": get_current_dataent_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', '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), "worker_target_wants": " ".join(background_workers), "bench_cmd": find_executable('bench') } if not yes: click.confirm( 'current systemd configuration will be overwritten. Do you want to continue?', abort=True) setup_systemd_directory(bench_path) setup_main_config(bench_info, bench_path) setup_workers_config(bench_info, bench_path) setup_web_config(bench_info, bench_path) setup_redis_config(bench_info, bench_path) update_config({'restart_systemd_on_update': True}, bench_path=bench_path) update_config({'restart_supervisor_on_update': False}, bench_path=bench_path)
def setup_procfile(bench_path): procfile = bench.env.get_template('Procfile').render(node=find_executable("node") \ or find_executable("nodejs")) with open(os.path.join(bench_path, 'Procfile'), 'w') as f: f.write(procfile)
def reload_nginx(): subprocess.check_output(['sudo', find_executable('nginx'), '-t']) service('nginx', 'reload')