def set_common_config(configs): import ast from bench.config.common_site_config import update_config common_site_config = {} for key, value in configs: if value in ("False", "True"): value = ast.literal_eval(value) elif "." in value: try: value = float(value) except ValueError: pass elif "{" in value or "[" in value: try: value = json.loads(value) except ValueError: pass else: try: value = int(value) except ValueError: pass common_site_config[key] = value update_config(common_site_config, 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 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 set_common_config(configs): common_site_config = {} for key, value in configs: if value in ('true', 'false'): value = value.title() try: value = ast.literal_eval(value) except ValueError: pass common_site_config[key] = value update_config(common_site_config, 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 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 config_dns_multitenant(state): update_config({'dns_multitenant': state == 'on'})
def config_restart_systemd_on_update(state): update_config({'restart_systemd_on_update': state == 'on'})
def _update(pull=False, patch=False, build=False, update_bench=False, auto=False, restart_supervisor=False, restart_systemd=False, requirements=False, no_backup=False, bench_path='.', force=False, reset=False): conf = get_config(bench_path=bench_path) version_upgrade = is_version_upgrade(bench_path=bench_path) if version_upgrade[0] or (not version_upgrade[0] and force): validate_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path) before_update(bench_path=bench_path, requirements=requirements) conf.update({"maintenance_mode": 1, "pause_scheduler": 1}) update_config(conf, bench_path=bench_path) if not no_backup: print('Backing up sites...') backup_all_sites(bench_path=bench_path) if pull: pull_all_apps(bench_path=bench_path, reset=reset) if requirements: update_requirements(bench_path=bench_path) update_node_packages(bench_path=bench_path) if version_upgrade[0] or (not version_upgrade[0] and force): pre_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path) import bench.utils, bench.app print('Reloading bench...') if sys.version_info >= (3, 4): import importlib importlib.reload(bench.utils) importlib.reload(bench.app) else: reload(bench.utils) reload(bench.app) if patch: print('Patching sites...') patch_sites(bench_path=bench_path) if build: build_assets(bench_path=bench_path) if version_upgrade[0] or (not version_upgrade[0] and force): post_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path) if restart_supervisor or conf.get('restart_supervisor_on_update'): restart_supervisor_processes(bench_path=bench_path) if restart_systemd or conf.get('restart_systemd_on_update'): restart_systemd_processes(bench_path=bench_path) conf.update({"maintenance_mode": 0, "pause_scheduler": 0}) update_config(conf, bench_path=bench_path) print("_" * 80) print( "Bench: Deployment tool for Frappe and ERPNext (https://erpnext.org).") print( "Open source depends on your contributions, so please contribute bug reports, patches, fixes or cash and be a part of the community" ) print()
def config_serve_default_site(state): "Configure nginx to serve the default site on port 80" state = True if state == 'on' else False update_config({'serve_default_site': state})
def config_update_bench_on_update(state): "Enable/Disable bench updates on running bench update" state = True if state == 'on' else False update_config({'update_bench_on_update': state})
def config_auto_update(state): "Enable/Disable auto update for bench" state = True if state == 'on' else False update_config({'auto_update': state})
def config_http_timeout(seconds): update_config({'http_timeout': seconds})
def config_dns_multitenant(state): "Enable/Disable bench updates on running bench update" state = True if state == 'on' else False update_config({'dns_multitenant': state})
def config_restart_supervisor_on_update(state): "Enable/Disable auto restart of supervisor processes" state = True if state == 'on' else False update_config({'restart_supervisor_on_update': state})
def update(pull=False, patch=False, build=False, bench=False, restart_supervisor=False, restart_systemd=False, requirements=False, backup=True, force=False, reset=False): """command: bench update""" if not is_bench_directory(): """Update only bench CLI if bench update called from outside a bench""" update_bench(bench_repo=True, requirements=True) sys.exit(0) from bench import patches from bench.app import is_version_upgrade, pull_all_apps, validate_branch from bench.config.common_site_config import get_config, update_config bench_path = os.path.abspath(".") patches.run(bench_path=bench_path) conf = get_config(bench_path) if conf.get('release_bench'): print('Release bench detected, cannot update!') sys.exit(1) if not (pull or patch or build or bench or requirements): pull, patch, build, bench, requirements = True, True, True, True, True if bench and conf.get('update_bench_on_update'): update_bench(bench_repo=True, requirements=False) restart_update({ 'pull': pull, 'patch': patch, 'build': build, 'requirements': requirements, 'no-backup': backup, 'restart-supervisor': restart_supervisor, 'reset': reset }) validate_branch() version_upgrade = is_version_upgrade() if version_upgrade[0]: if force: print( "Force flag has been used for a major version change in Frappe and it's apps. \nThis will take significant time to migrate and might break custom apps." ) else: print( "This update will cause a major version change in Frappe/ERPNext from {0} to {1}. \nThis would take significant time to migrate and might break custom apps." .format(*version_upgrade[1:])) click.confirm('Do you want to continue?', abort=True) if version_upgrade[0] or (not version_upgrade[0] and force): validate_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path) before_update(bench_path=bench_path, requirements=requirements) conf.update({"maintenance_mode": 1, "pause_scheduler": 1}) update_config(conf, bench_path=bench_path) if backup: print('Backing up sites...') backup_all_sites(bench_path=bench_path) if pull: pull_all_apps(bench_path=bench_path, reset=reset) if requirements: update_requirements(bench_path=bench_path) update_node_packages(bench_path=bench_path) if patch: print('Patching sites...') patch_sites(bench_path=bench_path) if build: build_assets(bench_path=bench_path) if version_upgrade[0] or (not version_upgrade[0] and force): post_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path) if restart_supervisor or conf.get('restart_supervisor_on_update'): restart_supervisor_processes(bench_path=bench_path) if restart_systemd or conf.get('restart_systemd_on_update'): restart_systemd_processes(bench_path=bench_path) conf.update({"maintenance_mode": 0, "pause_scheduler": 0}) update_config(conf, bench_path=bench_path) print( "_" * 80 + "\nBench: Deployment tool for Frappe and Frappe Applications (https://frappe.io/bench).\nOpen source depends on your contributions, so please contribute bug reports, patches, fixes or cash and be a part of the community" )
def config_restart_systemd_on_update(state): "Enable/Disable auto restart of systemd units" state = True if state == 'on' else False update_config({'restart_systemd_on_update': state})
def config_serve_default_site(state): update_config({'serve_default_site': state == 'on'})
def config_rebase_on_pull(state): update_config({'rebase_on_pull': state == 'on'})
def config_rebase_on_pull(state): "Rebase repositories on pulling" state = True if state == 'on' else False update_config({'rebase_on_pull': state})
def config_http_timeout(seconds): "set http timeout" update_config({'http_timeout': seconds})
def update(pull=False, apps=None, patch=False, build=False, requirements=False, backup=True, force=False, reset=False, restart_supervisor=False, restart_systemd=False): """command: bench update""" from bench import patches from bench.app import is_version_upgrade, pull_apps, validate_branch from bench.config.common_site_config import get_config, update_config bench_path = os.path.abspath(".") patches.run(bench_path=bench_path) conf = get_config(bench_path) if apps and not pull: apps = [] clear_command_cache(bench_path='.') if conf.get('release_bench'): print('Release bench detected, cannot update!') sys.exit(1) if not (pull or patch or build or requirements): pull, patch, build, requirements = True, True, True, True validate_branch() version_upgrade = is_version_upgrade() if version_upgrade[0]: if force: print( "Force flag has been used for a major version change in Frappe and it's apps. \nThis will take significant time to migrate and might break custom apps." ) else: print( "This update will cause a major version change in Frappe/ERPNext from {0} to {1}. \nThis would take significant time to migrate and might break custom apps." .format(*version_upgrade[1:])) click.confirm('Do you want to continue?', abort=True) if version_upgrade[0] or (not version_upgrade[0] and force): validate_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path) conf.update({"maintenance_mode": 1, "pause_scheduler": 1}) update_config(conf, bench_path=bench_path) if backup: print('Backing up sites...') backup_all_sites(bench_path=bench_path) if apps: apps = [app.strip() for app in re.split(",| ", apps) if app] if pull: pull_apps(apps=apps, bench_path=bench_path, reset=reset) if requirements: update_requirements(bench_path=bench_path) update_node_packages(bench_path=bench_path) if patch: print('Patching sites...') patch_sites(bench_path=bench_path) if build: build_assets(bench_path=bench_path) if version_upgrade[0] or (not version_upgrade[0] and force): post_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path) if restart_supervisor or conf.get('restart_supervisor_on_update'): restart_supervisor_processes(bench_path=bench_path) if restart_systemd or conf.get('restart_systemd_on_update'): restart_systemd_processes(bench_path=bench_path) conf.update({"maintenance_mode": 0, "pause_scheduler": 0}) update_config(conf, bench_path=bench_path) print( "_" * 80 + "\nBench: Deployment tool for Frappe and Frappe Applications (https://frappe.io/bench).\nOpen source depends on your contributions, so do give back by submitting bug reports, patches and fixes and be a part of the community :)" )
def config_restart_supervisor_on_update(state): update_config({'restart_supervisor_on_update': state == 'on'})
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)