def setup_production(user, bench_path='.'): generate_supervisor_config(bench_path=bench_path, user=user) make_nginx_conf(bench_path=bench_path) fix_prod_setup_perms(bench_path, frappe_user=user) remove_default_nginx_configs() bench_name = get_bench_name(bench_path) nginx_conf = '/etc/nginx/conf.d/{bench_name}.conf'.format(bench_name=bench_name) supervisor_conf_extn = "ini" if is_centos7() else "conf" supervisor_conf = os.path.join(get_supervisor_confdir(), '{bench_name}.{extn}'.format( bench_name=bench_name, extn=supervisor_conf_extn)) # Check if symlink exists, If not then create it. if not os.path.islink(supervisor_conf): os.symlink(os.path.abspath(os.path.join(bench_path, 'config', 'supervisor.conf')), supervisor_conf) if not os.path.islink(nginx_conf): os.symlink(os.path.abspath(os.path.join(bench_path, 'config', 'nginx.conf')), nginx_conf) reload_supervisor() if os.environ.get('NO_SERVICE_RESTART'): return reload_nginx()
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 make_nginx_conf(bench_path, yes=False): from bench import env from bench.config.common_site_config import get_config template = env.get_template('nginx.conf') bench_path = os.path.abspath(bench_path) sites_path = os.path.join(bench_path, "sites") config = get_config(bench_path) sites = prepare_sites(config, bench_path) nginx_conf = template.render(**{ "sites_path": sites_path, "http_timeout": config.get("http_timeout"), "sites": sites, "webserver_port": config.get('webserver_port'), "socketio_port": config.get('socketio_port'), "bench_name": get_bench_name(bench_path), "error_pages": get_error_pages(), # for nginx map variable "random_string": "".join(random.choice(string.ascii_lowercase) for i in xrange(7)) }) conf_path = os.path.join(bench_path, "config", "nginx.conf") if not yes and os.path.exists(conf_path): click.confirm('nginx.conf already exists and this will overwrite it. Do you want to continue?', abort=True) with open(conf_path, "w") as f: f.write(nginx_conf)
def setup_manager(yes=False, port=23624, domain=None): "Setup bench-manager.local site with the bench_manager app installed on it" from six.moves import input create_new_site = True if 'bench-manager.local' in os.listdir('sites'): ans = input('Site aleady exists. Overwrite existing new site? [Y/n]: ') while ans.lower() not in ['y', 'n', '']: ans = input('Please type "y" or "n". Site aleady exists. Overwrite existing new site? [Y/n]: ') if ans=='n': create_new_site = False if create_new_site: exec_cmd("bench new-site --force bench-manager.local") if 'bench_manager' in os.listdir('apps'): print('App aleady exists. Skipping downloading the app') else: exec_cmd("bench get-app bench_manager") exec_cmd("bench --site bench-manager.local install-app bench_manager") from bench.config.common_site_config import get_config bench_path = '.' conf = get_config(bench_path) if conf.get('restart_supervisor_on_update') or conf.get('restart_systemd_on_update'): # implicates a production setup or so I presume if not domain: print("Please specify the site name on which you want to host bench-manager using the 'domain' flag") sys.exit(1) from bench.utils import get_sites, get_bench_name bench_name = get_bench_name(bench_path) if domain not in get_sites(bench_path): raise Exception("No such site") from bench.config.nginx import make_bench_manager_nginx_conf make_bench_manager_nginx_conf(bench_path, yes=yes, port=port, domain=domain)
def setup_production(user, bench_path='.', yes=False): if get_config(bench_path).get('restart_supervisor_on_update') and get_config(bench_path).get('restart_systemd_on_update'): raise Exception("You cannot use supervisor and systemd at the same time. Modify your common_site_config accordingly." ) if get_config(bench_path).get('restart_systemd_on_update'): generate_systemd_config(bench_path=bench_path, user=user, yes=yes) else: generate_supervisor_config(bench_path=bench_path, user=user, yes=yes) make_nginx_conf(bench_path=bench_path, yes=yes) fix_prod_setup_perms(bench_path, frappe_user=user) remove_default_nginx_configs() bench_name = get_bench_name(bench_path) nginx_conf = '/etc/nginx/conf.d/{bench_name}.conf'.format(bench_name=bench_name) if get_config(bench_path).get('restart_supervisor_on_update'): supervisor_conf_extn = "ini" if is_centos7() else "conf" supervisor_conf = os.path.join(get_supervisor_confdir(), '{bench_name}.{extn}'.format( bench_name=bench_name, extn=supervisor_conf_extn)) # Check if symlink exists, If not then create it. if not os.path.islink(supervisor_conf): os.symlink(os.path.abspath(os.path.join(bench_path, 'config', 'supervisor.conf')), supervisor_conf) if not os.path.islink(nginx_conf): os.symlink(os.path.abspath(os.path.join(bench_path, 'config', 'nginx.conf')), nginx_conf) if get_config(bench_path).get('restart_supervisor_on_update'): reload_supervisor() if os.environ.get('NO_SERVICE_RESTART'): return reload_nginx()
def make_nginx_conf(bench_path, force=False): from bench import env from bench.config.common_site_config import get_config template = env.get_template('nginx.conf') bench_path = os.path.abspath(bench_path) sites_path = os.path.join(bench_path, "sites") config = get_config(bench_path) sites = prepare_sites(config, bench_path) nginx_conf = template.render(**{ "sites_path": sites_path, "http_timeout": config.get("http_timeout"), "sites": sites, "webserver_port": config.get('webserver_port'), "socketio_port": config.get('socketio_port'), "bench_name": get_bench_name(bench_path) }) conf_path = os.path.join(bench_path, "config", "nginx.conf") if not force and os.path.exists(conf_path): click.confirm('nginx.conf already exists and this will overwrite it. Do you want to continue?', abort=True) with open(conf_path, "w") as f: f.write(nginx_conf)
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 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 get_unit_files(bench_path): bench_name = get_bench_name(bench_path) unit_files = [ [bench_name, ".target"], [bench_name+"-workers", ".target"], [bench_name+"-web", ".target"], [bench_name+"-redis", ".target"], [bench_name+"-frappe-default-worker@", ".service"], [bench_name+"-frappe-short-worker@", ".service"], [bench_name+"-frappe-long-worker@", ".service"], [bench_name+"-frappe-schedule", ".service"], [bench_name+"-frappe-web", ".service"], [bench_name+"-node-socketio", ".service"], [bench_name+"-redis-cache", ".service"], [bench_name+"-redis-queue", ".service"], [bench_name+"-redis-socketio", ".service"], ] return unit_files
def make_nginx_conf(bench_path, yes=False): from bench import env from bench.config.common_site_config import get_config template = env.get_template('nginx.conf') bench_path = os.path.abspath(bench_path) sites_path = os.path.join(bench_path, "sites") config = get_config(bench_path) sites = prepare_sites(config, bench_path) bench_name = get_bench_name(bench_path) allow_rate_limiting = config.get('allow_rate_limiting', False) template_vars = { "sites_path": sites_path, "http_timeout": config.get("http_timeout"), "sites": sites, "webserver_port": config.get('webserver_port'), "socketio_port": config.get('socketio_port'), "bench_name": bench_name, "error_pages": get_error_pages(), "allow_rate_limiting": allow_rate_limiting, # for nginx map variable "random_string": "".join(random.choice(string.ascii_lowercase) for i in xrange(7)) } if allow_rate_limiting: template_vars.update({ 'bench_name_hash': hashlib.sha256(bench_name).hexdigest()[:16], 'limit_conn_shared_memory': get_limit_conn_shared_memory() }) nginx_conf = template.render(**template_vars) conf_path = os.path.join(bench_path, "config", "nginx.conf") if not yes and os.path.exists(conf_path): click.confirm('nginx.conf already exists and this will overwrite it. Do you want to continue?', abort=True) with open(conf_path, "w") as f: f.write(nginx_conf)
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 disable_production(bench_path='.'): bench_name = get_bench_name(bench_path) # supervisorctl supervisor_conf_extn = "ini" if is_centos7() else "conf" supervisor_conf = os.path.join(get_supervisor_confdir(), '{bench_name}.{extn}'.format( bench_name=bench_name, extn=supervisor_conf_extn)) if os.path.islink(supervisor_conf): os.unlink(supervisor_conf) exec_cmd('sudo supervisorctl reread') exec_cmd('sudo supervisorctl update') # nginx nginx_conf = '/etc/nginx/conf.d/{bench_name}.conf'.format(bench_name=bench_name) if os.path.islink(nginx_conf): os.unlink(nginx_conf) reload_nginx()
def disable_production(bench_path='.'): bench_name = get_bench_name(bench_path) # supervisorctl supervisor_conf_extn = "ini" if is_centos7() else "conf" supervisor_conf = os.path.join(get_supervisor_confdir(), '{bench_name}.{extn}'.format( bench_name=bench_name, extn=supervisor_conf_extn)) if os.path.islink(supervisor_conf): os.unlink(supervisor_conf) if get_config(bench_path).get('restart_supervisor_on_update'): reload_supervisor() # nginx nginx_conf = '/etc/nginx/conf.d/{bench_name}.conf'.format(bench_name=bench_name) if os.path.islink(nginx_conf): os.unlink(nginx_conf) reload_nginx()
def make_nginx_conf(bench_path): from bench import env from bench.config.common_site_config import get_config template = env.get_template('nginx.conf') bench_path = os.path.abspath(bench_path) sites_path = os.path.join(bench_path, "sites") config = get_config(bench_path) sites = prepare_sites(config, bench_path) nginx_conf = template.render(**{ "sites_path": sites_path, "http_timeout": config.get("http_timeout"), "sites": sites, "webserver_port": config.get('webserver_port'), "socketio_port": config.get('socketio_port'), "bench_name": get_bench_name(bench_path) }) with open(os.path.join(bench_path, "config", "nginx.conf"), "w") as f: f.write(nginx_conf)
def setup_production(user, bench='.'): generate_supervisor_config(bench_path=bench, user=user) make_nginx_conf(bench_path=bench) fix_prod_setup_perms(bench, frappe_user=user) remove_default_nginx_configs() bench_name = get_bench_name(bench) nginx_conf = '/etc/nginx/conf.d/{bench_name}.conf'.format(bench_name=bench_name) supervisor_conf_extn = "ini" if is_centos7() else "conf" supervisor_conf = os.path.join(get_supervisor_confdir(), '{bench_name}.{extn}'.format( bench_name=bench_name, extn=supervisor_conf_extn)) os.symlink(os.path.abspath(os.path.join(bench, 'config', 'supervisor.conf')), supervisor_conf) os.symlink(os.path.abspath(os.path.join(bench, 'config', 'nginx.conf')), nginx_conf) exec_cmd('supervisorctl reload') if os.environ.get('NO_SERVICE_RESTART'): return restart_service('nginx')
def make_bench_manager_nginx_conf(bench_path, yes=False, port=23624, domain=None): from bench import env from bench.config.site_config import get_site_config from bench.config.common_site_config import get_config template = env.get_template('bench_manager_nginx.conf') bench_path = os.path.abspath(bench_path) sites_path = os.path.join(bench_path, "sites") config = get_config(bench_path) site_config = get_site_config(domain, bench_path=bench_path) sites = prepare_sites(config, bench_path) bench_name = get_bench_name(bench_path) template_vars = { "port": port, "domain": domain, "bench_manager_site_name": "bench-manager.local", "sites_path": sites_path, "http_timeout": config.get("http_timeout"), "webserver_port": config.get('webserver_port'), "socketio_port": config.get('socketio_port'), "bench_name": bench_name, "error_pages": get_error_pages(), "ssl_certificate": site_config.get('ssl_certificate'), "ssl_certificate_key": site_config.get('ssl_certificate_key') } bench_manager_nginx_conf = template.render(**template_vars) conf_path = os.path.join(bench_path, "config", "nginx.conf") if not yes and os.path.exists(conf_path): click.confirm('nginx.conf already exists and bench-manager configuration will be appended to it. Do you want to continue?', abort=True) with open(conf_path, "a") as myfile: myfile.write(bench_manager_nginx_conf)
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)