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) exec_cmd('supervisorctl reload') if os.environ.get('NO_SERVICE_RESTART'): return restart_service('nginx')
def reload_nginx(): try: exec_cmd('sudo {0} -t'.format(find_executable('nginx'))) except: raise service('nginx', 'reload')
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'): 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 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 install_app(app, bench_path=".", verbose=False, no_cache=False, postprocess=True, skip_assets=False): logger.info("installing {}".format(app)) pip_path = os.path.join(bench_path, "env", "bin", "pip") quiet_flag = "-q" if not verbose else "" app_path = os.path.join(bench_path, "apps", app) cache_flag = "--no-cache-dir" if no_cache else "" exec_cmd("{pip} install {quiet} -U -e {app} {no_cache}".format( pip=pip_path, quiet=quiet_flag, app=app_path, no_cache=cache_flag)) add_to_appstxt(app, bench_path=bench_path) if postprocess: if not skip_assets: build_assets(bench_path=bench_path, app=app) conf = get_config(bench_path=bench_path) if conf.get('restart_supervisor_on_update'): restart_supervisor_processes(bench_path=bench_path) if conf.get('restart_systemd_on_update'): restart_systemd_processes(bench_path=bench_path)
def run_certbot_and_setup_ssl(site, custom_domain, bench_path): service('nginx', 'stop') get_certbot() try: exec_cmd("{path} --config /etc/letsencrypt/configs/{site}.cfg certonly".format(path=get_certbot_path(), site=custom_domain or site)) except CommandFailedError: service('nginx', 'start') print "There was a problem trying to setup SSL for your site" return ssl_path = "/etc/letsencrypt/live/{site}/".format(site=custom_domain or site) ssl_config = { "ssl_certificate": os.path.join(ssl_path, "fullchain.pem"), "ssl_certificate_key": os.path.join(ssl_path, "privkey.pem") } if custom_domain: remove_domain(site, custom_domain, bench_path) domains = get_domains(site, bench_path) ssl_config['domain'] = custom_domain domains.append(ssl_config) update_site_config(site, { "domains": domains }, bench_path=bench_path) else: update_site_config(site, ssl_config, bench_path=bench_path) make_nginx_conf(bench_path) service('nginx', 'start')
def install_app(app, bench_path=".", verbose=False, no_cache=False, restart_bench=True, skip_assets=False): print('\n{0}Installing {1}{2}'.format(color.yellow, app, color.nc)) logger.log("installing {}".format(app)) pip_path = os.path.join(bench_path, "env", "bin", "pip") quiet_flag = "-q" if not verbose else "" app_path = os.path.join(bench_path, "apps", app) cache_flag = "--no-cache-dir" if no_cache else "" exec_cmd("{pip} install {quiet} -U -e {app} {no_cache}".format( pip=pip_path, quiet=quiet_flag, app=app_path, no_cache=cache_flag)) if os.path.exists(os.path.join(app_path, 'package.json')): exec_cmd("yarn install", cwd=app_path) add_to_appstxt(app, bench_path=bench_path) if not skip_assets: build_assets(bench_path=bench_path, app=app) if restart_bench: conf = get_config(bench_path=bench_path) if conf.get('restart_supervisor_on_update'): restart_supervisor_processes(bench_path=bench_path) if conf.get('restart_systemd_on_update'): restart_systemd_processes(bench_path=bench_path)
def remove_app(app, bench_path='.'): if app not in get_apps(bench_path): print("No app named {0}".format(app)) sys.exit(1) app_path = os.path.join(bench_path, 'apps', app) site_path = os.path.join(bench_path, 'sites') pip = os.path.join(bench_path, 'env', 'bin', 'pip') for site in os.listdir(site_path): req_file = os.path.join(site_path, site, 'site_config.json') if os.path.exists(req_file): out = subprocess.check_output( ["bench", "--site", site, "list-apps"], cwd=bench_path).decode('utf-8') if re.search(r'\b' + app + r'\b', out): print("Cannot remove, app is installed on site: {0}".format( site)) sys.exit(1) exec_cmd("{0} uninstall -y {1}".format(pip, app), cwd=bench_path) remove_from_appstxt(app, bench_path) shutil.rmtree(app_path) run_frappe_cmd("build", bench_path=bench_path) if get_config(bench_path).get('restart_supervisor_on_update'): restart_supervisor_processes(bench_path=bench_path) if get_config(bench_path).get('restart_systemd_on_update'): restart_systemd_processes(bench_path=bench_path)
def run_certbot_and_setup_ssl(site, custom_domain, bench_path): service('nginx', 'stop') get_certbot() try: exec_cmd( "{path} --config /etc/letsencrypt/configs/{site}.cfg certonly". format(path=get_certbot_path(), site=custom_domain or site)) except CommandFailedError: service('nginx', 'start') print "There was a problem trying to setup SSL for your site" return ssl_path = "/etc/letsencrypt/live/{site}/".format( site=custom_domain or site) ssl_config = { "ssl_certificate": os.path.join(ssl_path, "fullchain.pem"), "ssl_certificate_key": os.path.join(ssl_path, "privkey.pem") } if custom_domain: remove_domain(site, custom_domain, bench_path) domains = get_domains(site, bench_path) ssl_config['domain'] = custom_domain domains.append(ssl_config) update_site_config(site, {"domains": domains}, bench_path=bench_path) else: update_site_config(site, ssl_config, bench_path=bench_path) make_nginx_conf(bench_path) service('nginx', 'start')
def get_app(git_url, branch=None, bench_path='.', skip_assets=False, verbose=False, postprocess=True, overwrite=False): if not os.path.exists(git_url): if not check_url(git_url, raise_err=False): orgs = ['frappe', 'erpnext'] for org in orgs: url = 'https://api.github.com/repos/{org}/{app}'.format( org=org, app=git_url) res = requests.get(url) if res.ok: data = res.json() if 'name' in data: if git_url == data['name']: git_url = 'https://github.com/{org}/{app}'.format( org=org, app=git_url) break # Gets repo name from URL repo_name = git_url.rsplit('/', 1)[1].rsplit('.', 1)[0] shallow_clone = '--depth 1' if check_git_for_shallow_clone() else '' branch = '--branch {branch}'.format(branch=branch) if branch else '' else: repo_name = git_url.split(os.sep)[-1] shallow_clone = '' branch = '--branch {branch}'.format(branch=branch) if branch else '' if os.path.isdir(os.path.join(bench_path, 'apps', repo_name)): # application directory already exists # prompt user to overwrite it if overwrite or click.confirm( '''A directory for the application "{0}" already exists. Do you want to continue and overwrite it?'''.format(repo_name)): shutil.rmtree(os.path.join(bench_path, 'apps', repo_name)) elif click.confirm( '''Do you want to reinstall the existing application?''', abort=True): app_name = get_app_name(bench_path, repo_name) install_app(app=app_name, bench_path=bench_path, verbose=verbose, skip_assets=skip_assets) sys.exit() logger.info('Getting app {0}'.format(repo_name)) exec_cmd("git clone {git_url} {branch} {shallow_clone} --origin upstream". format(git_url=git_url, shallow_clone=shallow_clone, branch=branch), cwd=os.path.join(bench_path, 'apps')) app_name = get_app_name(bench_path, repo_name) install_app(app=app_name, bench_path=bench_path, verbose=verbose, skip_assets=skip_assets)
def renew_certs(): click.confirm('Running this will stop the nginx service temporarily causing your sites to go offline\n' 'Do you want to continue?', abort=True) service('nginx', 'stop') exec_cmd("{path} renew".format(path=get_certbot_path())) service('nginx', 'start')
def execute(bench_path): """This patch checks if bench sudoers is set and regenerate supervisor and sudoers files""" user = get_config('.').get("frappe_user") or getpass.getuser() if is_sudoers_set(): if is_production_set(bench_path): exec_cmd("sudo bench setup supervisor --yes --user {user}".format( user=user)) service("supervisord", "restart") exec_cmd("sudo bench setup sudoers {user}".format(user=user))
def setup_wildcard_ssl(domain, email, bench_path, exclude_base_domain): def _get_domains(domain): domain_list = [domain] if not domain.startswith('*.'): # add wildcard caracter to domain if missing domain_list.append('*.{0}'.format(domain)) else: # include base domain based on flag domain_list.append(domain.replace('*.', '')) if exclude_base_domain: domain_list.remove(domain.replace('*.', '')) return domain_list if not get_config(bench_path).get("dns_multitenant"): print("You cannot setup SSL without DNS Multitenancy") return get_certbot() domain_list = _get_domains(domain.strip()) email_param = '' if email: email_param = '--email {0}'.format(email) try: exec_cmd( "{path} certonly --manual --preferred-challenges=dns {email_param} \ --server https://acme-v02.api.letsencrypt.org/directory \ --agree-tos -d {domain}".format(path=get_certbot_path(), domain=' -d '.join(domain_list), email_param=email_param)) except CommandFailedError: print("There was a problem trying to setup SSL") return ssl_path = "/etc/letsencrypt/live/{domain}/".format(domain=domain) ssl_config = { "wildcard": { "domain": domain, "ssl_certificate": os.path.join(ssl_path, "fullchain.pem"), "ssl_certificate_key": os.path.join(ssl_path, "privkey.pem") } } update_common_site_config(ssl_config) setup_crontab() make_nginx_conf(bench_path) print("Restrting Nginx service") service('nginx', 'restart')
def setup_wildcard_ssl(domain, email, bench_path, exclude_base_domain): def _get_domains(domain): domain_list = [domain] if not domain.startswith('*.'): # add wildcard caracter to domain if missing domain_list.append('*.{0}'.format(domain)) else: # include base domain based on flag domain_list.append(domain.replace('*.', '')) if exclude_base_domain: domain_list.remove(domain.replace('*.', '')) return domain_list if not get_config(bench_path).get("dns_multitenant"): print("You cannot setup SSL without DNS Multitenancy") return get_certbot() domain_list = _get_domains(domain.strip()) email_param = '' if email: email_param = '--email {0}'.format(email) try: exec_cmd("{path} certonly --manual --preferred-challenges=dns {email_param} \ --server https://acme-v02.api.letsencrypt.org/directory \ --agree-tos -d {domain}".format(path=get_certbot_path(), domain=' -d '.join(domain_list), email_param=email_param)) except CommandFailedError: print("There was a problem trying to setup SSL") return ssl_path = "/etc/letsencrypt/live/{domain}/".format(domain=domain) ssl_config = { "wildcard": { "domain": domain, "ssl_certificate": os.path.join(ssl_path, "fullchain.pem"), "ssl_certificate_key": os.path.join(ssl_path, "privkey.pem") } } update_common_site_config(ssl_config) setup_crontab() make_nginx_conf(bench_path) print("Restrting Nginx service") service('nginx', 'restart')
def _delete_symlinks(bench_path): bench_dir = os.path.abspath(bench_path) etc_systemd_system = os.path.join('/', 'etc', 'systemd', 'system') config_path = os.path.join(bench_dir, 'config', 'systemd') unit_files = get_unit_files(bench_dir) for unit_file in unit_files: exec_cmd('sudo rm {etc_systemd_system}/{unit_file_init}'.format( config_path=config_path, etc_systemd_system=etc_systemd_system, unit_file_init="".join(unit_file))) exec_cmd('sudo systemctl daemon-reload')
def _delete_symlinks(bench_path): bench_dir = os.path.abspath(bench_path) etc_systemd_system = os.path.join('/', 'etc', 'systemd', 'system') config_path = os.path.join(bench_dir, 'config', 'systemd') unit_files = get_unit_files(bench_dir) for unit_file in unit_files: exec_cmd('sudo rm {etc_systemd_system}/{unit_file_init}'.format( config_path=config_path, etc_systemd_system=etc_systemd_system, unit_file_init="".join(unit_file) )) exec_cmd('sudo systemctl daemon-reload')
def new_app(app, bench_path='.'): # For backwards compatibility app = app.lower().replace(" ", "_").replace("-", "_") logger.log('creating new app {}'.format(app)) apps = os.path.abspath(os.path.join(bench_path, 'apps')) bench.set_frappe_version(bench_path=bench_path) if bench.FRAPPE_VERSION == 4: exec_cmd("{frappe} --make_app {apps} {app}".format( frappe=get_frappe(bench_path=bench_path), apps=apps, app=app)) else: run_frappe_cmd('make-app', apps, app, bench_path=bench_path) install_app(app, bench_path=bench_path)
def pull_all_apps(bench_path='.', reset=False): '''Check all apps if there no local changes, pull''' rebase = '--rebase' if get_config(bench_path).get('rebase_on_pull') else '' # chech for local changes if not reset: for app in get_apps(bench_path=bench_path): excluded_apps = get_excluded_apps() if app in excluded_apps: print("Skipping reset for app {}".format(app)) continue app_dir = get_repo_dir(app, bench_path=bench_path) if os.path.exists(os.path.join(app_dir, '.git')): out = subprocess.check_output(["git", "status"], cwd=app_dir) out = out.decode('utf-8') if not re.search( r'nothing to commit, working (directory|tree) clean', out): print(''' Cannot proceed with update: You have local changes in app "{0}" that are not committed. Here are your choices: 1. Merge the {0} app manually with "git pull" / "git pull --rebase" and fix conflicts. 1. Temporarily remove your changes with "git stash" or discard them completely with "bench update --reset" or for individual repositries "git reset --hard" 2. If your changes are helpful for others, send in a pull request via GitHub and wait for them to be merged in the core.'''.format(app)) sys.exit(1) excluded_apps = get_excluded_apps() for app in get_apps(bench_path=bench_path): if app in excluded_apps: print("Skipping pull for app {}".format(app)) continue app_dir = get_repo_dir(app, bench_path=bench_path) if os.path.exists(os.path.join(app_dir, '.git')): remote = get_remote(app) if not remote: # remote is False, i.e. remote doesn't exist, add the app to excluded_apps.txt add_to_excluded_apps_txt(app, bench_path=bench_path) print( "Skipping pull for app {}, since remote doesn't exist, and adding it to excluded apps" .format(app)) continue logger.info('pulling {0}'.format(app)) if reset: exec_cmd("git fetch --all", cwd=app_dir) exec_cmd("git reset --hard {remote}/{branch}".format( remote=remote, branch=get_current_branch(app, bench_path=bench_path)), cwd=app_dir) else: exec_cmd("git pull {rebase} {remote} {branch}".format( rebase=rebase, remote=remote, branch=get_current_branch(app, bench_path=bench_path)), cwd=app_dir) exec_cmd('find . -name "*.pyc" -delete', cwd=app_dir)
def setup_production(user, yes=False): "setup bench for production" from bench.config.production_setup import setup_production from bench.utils import run_playbook # Install prereqs for production exec_cmd("sudo pip install ansible") exec_cmd("bench setup role fail2ban") exec_cmd("bench setup role nginx") exec_cmd("bench setup role supervisor") setup_production(user=user, yes=yes)
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_production(user, yes=False): from bench.config.production_setup import setup_production # Install prereqs for production from distutils.spawn import find_executable if not find_executable("ansible"): exec_cmd("sudo -H {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") setup_production(user=user, yes=yes)
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 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 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, yes=False): "setup bench for production" from bench.config.production_setup import setup_production from bench.utils import run_playbook # Install prereqs for production from distutils.spawn import find_executable if not find_executable('ansible'): exec_cmd("sudo pip install ansible") 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") setup_production(user=user, yes=yes)
def service(service, option): if os.path.basename(get_program(['systemctl']) or '') == 'systemctl' and is_running_systemd(): exec_cmd("sudo {service_manager} {option} {service}".format(service_manager='systemctl', option=option, service=service)) elif os.path.basename(get_program(['service']) or '') == 'service': exec_cmd("sudo {service_manager} {service} {option} ".format(service_manager='service', service=service, option=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} {option} {service}").format(service_manager=service_manager, service=service, option=option) exec_cmd(service_manager_command) else: raise Exception, 'No service manager found'
def service(service, option): if os.path.basename(get_program(['systemctl']) or '') == 'systemctl' and is_running_systemd(): exec_cmd("sudo {service_manager} {option} {service}".format(service_manager='systemctl', option=option, service=service)) elif os.path.basename(get_program(['service']) or '') == 'service': exec_cmd("sudo {service_manager} {service} {option} ".format(service_manager='service', service=service, option=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} {option} {service}").format(service_manager=service_manager, service=service, option=option) exec_cmd(service_manager_command) else: raise Exception('No service manager found')
def setup_manager(yes=False, port=23624, domain=None): from six.moves import input from bench.utils import get_sites from bench.config.common_site_config import get_config from bench.config.nginx import make_bench_manager_nginx_conf create_new_site = True if "bench-manager.local" in os.listdir("sites"): ans = input( "Site already exists. Overwrite existing site? [Y/n]: ").lower() while ans not in ("y", "n", ""): ans = input( "Please enter 'y' or 'n'. Site already exists. Overwrite existing site? [Y/n]: " ).lower() 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 already exists. Skipping app download.") else: exec_cmd("bench get-app bench_manager") exec_cmd("bench --site bench-manager.local install-app bench_manager") 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) if domain not in get_sites(bench_path): raise Exception("No such site") make_bench_manager_nginx_conf(bench_path, yes=yes, port=port, domain=domain)
def setup_manager(): "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")
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 already exists. Overwrite existing site? [Y/n]: ').lower() while ans not in ('y', 'n', ''): ans = input( 'Please enter "y" or "n". Site already exists. Overwrite existing site? [Y/n]: ' ).lower() 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 already exists. Skipping app download.') 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 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)
from bench.utils import exec_cmd import os branch = "develop" apps_dir = os.path.join('.', 'apps') apps = [ name for name in os.listdir(apps_dir) if os.path.isdir(os.path.join(apps_dir, name)) ] for app in apps: if app in ['erpnext', 'frappe']: continue app_dir = os.path.join(apps_dir, app) if os.path.exists(app_dir): try: print("Switching for " + app) exec_cmd("git checkout {branch}".format(branch=branch), cwd=app_dir) exec_cmd("git pull upstream {branch}".format(branch=branch), cwd=app_dir) except: pass
def execute(bench_path): exec_cmd('npm install yarn', os.path.join(bench_path, 'apps/frappe'))
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 migrate_env(python, no_backup=False): """ Migrate Virtual Environment to desired Python Version. """ try: # This is with the assumption that a bench is set-up within path. path = os.getcwd() # I know, bad name for a flag. Thanks, Ameya! :| - <*****@*****.**> if not no_backup: # Back, the f*ck up. parch = osp.join(path, 'archived_envs') if not osp.exists(parch): os.mkdir(parch) # Simply moving. Thanks, Ameya. # I'm keen to zip. source = osp.join(path, 'env') target = parch log.debug('Backing up Virtual Environment') stamp = datetime.now().strftime('%Y%m%d_%H%M%S') dest = osp.join(path, str(stamp)) # WARNING: This is an archive, you might have to use virtualenv --relocate # That's because virtualenv creates symlinks with shebangs pointing to executables. # ...and shutil.copytree is a f*cking mess. os.rename(source, dest) shutil.move(dest, target) log.debug('Setting up a New Virtual {python} Environment'.format( python=python)) # Path to Python Executable (Basically $PYTHONPTH) python = which(python) virtualenv = which('virtualenv') nvenv = 'env' pvenv = osp.join(path, nvenv) exec_cmd('{virtualenv} --python {python} {pvenv}'.format( virtualenv=virtualenv, python=python, pvenv=pvenv), cwd=path) pip = osp.join(pvenv, 'bin', 'pip') exec_cmd('{pip} install --upgrade pip'.format(pip=pip)) exec_cmd('{pip} install --upgrade setuptools'.format(pip=pip)) # TODO: Options papps = osp.join(path, 'apps') apps = ['frappe' ] + [app for app in os.listdir(papps) if app != 'frappe'] for app in apps: papp = osp.join(papps, app) if osp.isdir(papp) and osp.exists(osp.join(papp, 'setup.py')): exec_cmd('{pip} install -e {app}'.format(pip=pip, app=papp)) log.debug('Migration Successful to {python}'.format(python=python)) except: log.debug('Migration Error') raise
apps_dir = os.path.join('.', 'apps') apps = [ name for name in os.listdir(apps_dir) if os.path.isdir(os.path.join(apps_dir, name)) ] for app in apps: if app in ['erpnext', 'frappe']: continue app_dir = os.path.join(apps_dir, app) if os.path.exists(app_dir): try: print("Switching for " + app) unshallow = "--unshallow" if os.path.exists( os.path.join(app_dir, ".git", "shallow")) else "" exec_cmd("git config --unset-all remote.upstream.fetch", cwd=app_dir) exec_cmd( "git config --add remote.upstream.fetch '+refs/heads/*:refs/remotes/upstream/*'", cwd=app_dir) exec_cmd( "git fetch upstream {unshallow}".format(unshallow=unshallow), cwd=app_dir) exec_cmd("git checkout {branch}".format(branch=branch), cwd=app_dir) exec_cmd("git pull upstream {branch}".format(branch=branch), cwd=app_dir) except: pass
def migrate_env(python, no_backup = False): """ Migrate Virtual Environment to desired Python Version. """ try: # Clear Cache before Bench Dies. config = get_config(bench_path = os.getcwd()) rredis = urlparse(config['redis_cache']) redis = '{redis} -p {port}'.format( redis = which('redis-cli'), port = rredis.port ) log.debug('Clearing Redis Cache...') exec_cmd('{redis} FLUSHALL'.format(redis = redis)) log.debug('Clearing Redis DataBase...') exec_cmd('{redis} FLUSHDB'.format(redis = redis)) except Exception: log.warn('Please ensure Redis Connections are running or Daemonized.') try: # This is with the assumption that a bench is set-up within path. path = os.getcwd() # I know, bad name for a flag. Thanks, Ameya! :| - <*****@*****.**> if not no_backup: # Back, the f*ck up. parch = osp.join(path, 'archived_envs') if not osp.exists(parch): os.mkdir(parch) # Simply moving. Thanks, Ameya. # I'm keen to zip. source = osp.join(path, 'env') target = parch log.debug('Backing up Virtual Environment') stamp = datetime.now().strftime('%Y%m%d_%H%M%S') dest = osp.join(path, str(stamp)) # WARNING: This is an archive, you might have to use virtualenv --relocate # That's because virtualenv creates symlinks with shebangs pointing to executables. # shebangs, shebangs - ricky martin. # ...and shutil.copytree is a f*cking mess. os.rename(source, dest) shutil.move(dest, target) log.debug('Setting up a New Virtual {python} Environment'.format( python = python )) # Path to Python Executable (Basically $PYTHONPTH) python = which(python) virtualenv = which('virtualenv') nvenv = 'env' pvenv = osp.join(path, nvenv) exec_cmd('{virtualenv} --python {python} {pvenv}'.format( virtualenv = virtualenv, python = python, pvenv = pvenv ), cwd = path) pip = osp.join(pvenv, 'bin', 'pip') # pip 10 seems to have a few problems associated with it, temporary freeze pip at 9.0.3 exec_cmd('{pip} install --upgrade pip==9.0.3'.format(pip=pip)) exec_cmd('{pip} install --upgrade setuptools'.format(pip=pip)) # TODO: Options papps = osp.join(path, 'apps') apps = ['frappe', 'erpnext'] + [app for app in os.listdir(papps) if app not in ['frappe', 'erpnext']] for app in apps: papp = osp.join(papps, app) if osp.isdir(papp) and osp.exists(osp.join(papp, 'setup.py')): exec_cmd('{pip} install -e {app}'.format( pip = pip, app = papp )) log.debug('Migration Successful to {python}'.format( python = python )) except: log.debug('Migration Error') raise
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 migrate_env(python, no_backup = False): """ Migrate Virtual Environment to desired Python Version. """ try: # This is with the assumption that a bench is set-up within path. path = os.getcwd() # I know, bad name for a flag. Thanks, Ameya! :| - <*****@*****.**> if not no_backup: # Back, the f*ck up. parch = osp.join(path, 'archived_envs') if not osp.exists(parch): os.mkdir(parch) # Simply moving. Thanks, Ameya. # I'm keen to zip. source = osp.join(path, 'env') target = parch log.debug('Backing up Virtual Environment') stamp = datetime.now().strftime('%Y%m%d_%H%M%S') dest = osp.join(path, str(stamp)) # WARNING: This is an archive, you might have to use virtualenv --relocate # That's because virtualenv creates symlinks with shebangs pointing to executables. # ...and shutil.copytree is a f*cking mess. os.rename(source, dest) shutil.move(dest, target) log.debug('Setting up a New Virtual {python} Environment'.format( python = python )) # Path to Python Executable (Basically $PYTHONPTH) python = which(python) virtualenv = which('virtualenv') nvenv = 'env' pvenv = osp.join(path, nvenv) exec_cmd('{virtualenv} --python {python} {pvenv}'.format( virtualenv = virtualenv, python = python, pvenv = pvenv ), cwd = path) pip = osp.join(pvenv, 'bin', 'pip') exec_cmd('{pip} install --upgrade pip'.format(pip=pip)) exec_cmd('{pip} install --upgrade setuptools'.format(pip=pip)) # TODO: Options papps = osp.join(path, 'apps') apps = ['frappe'] + [app for app in os.listdir(papps) if app != 'frappe'] for app in apps: papp = osp.join(papps, app) if osp.isdir(papp) and osp.exists(osp.join(papp, 'setup.py')): exec_cmd('{pip} install -e {app}'.format( pip = pip, app = papp )) log.debug('Migration Successful to {python}'.format( python = python )) except: log.debug('Migration Error') raise
def migrate_env(python, no_backup=False): """ Migrate Virtual Environment to desired Python Version. """ try: # Clear Cache before Bench Dies. config = get_config(bench_path=os.getcwd()) rredis = urlparse(config['redis_cache']) redis = '{redis} -p {port}'.format(redis=which('redis-cli'), port=rredis.port) log.debug('Clearing Redis Cache...') exec_cmd('{redis} FLUSHALL'.format(redis=redis)) log.debug('Clearing Redis DataBase...') exec_cmd('{redis} FLUSHDB'.format(redis=redis)) except Exception: log.warn('Please ensure Redis Connections are running or Daemonized.') try: # This is with the assumption that a bench is set-up within path. path = os.getcwd() # I know, bad name for a flag. Thanks, Ameya! :| - <*****@*****.**> if not no_backup: # Back, the f*ck up. parch = osp.join(path, 'archived_envs') if not osp.exists(parch): os.mkdir(parch) # Simply moving. Thanks, Ameya. # I'm keen to zip. source = osp.join(path, 'env') target = parch log.debug('Backing up Virtual Environment') stamp = datetime.now().strftime('%Y%m%d_%H%M%S') dest = osp.join(path, str(stamp)) # WARNING: This is an archive, you might have to use virtualenv --relocate # That's because virtualenv creates symlinks with shebangs pointing to executables. # shebangs, shebangs - ricky martin. # ...and shutil.copytree is a f*cking mess. os.rename(source, dest) shutil.move(dest, target) log.debug('Setting up a New Virtual {python} Environment'.format( python=python)) # Path to Python Executable (Basically $PYTHONPTH) python = which(python) virtualenv = which('virtualenv') nvenv = 'env' pvenv = osp.join(path, nvenv) exec_cmd('{virtualenv} --python {python} {pvenv}'.format( virtualenv=virtualenv, python=python, pvenv=pvenv), cwd=path) pip = osp.join(pvenv, 'bin', 'pip') exec_cmd('{pip} install --upgrade pip'.format(pip=pip)) exec_cmd('{pip} install --upgrade setuptools'.format(pip=pip)) # TODO: Options papps = osp.join(path, 'apps') apps = ['frappe', 'erpnext'] + [ app for app in os.listdir(papps) if app not in ['frappe', 'erpnext'] ] for app in apps: papp = osp.join(papps, app) if osp.isdir(papp) and osp.exists(osp.join(papp, 'setup.py')): exec_cmd('{pip} install -e {app}'.format(pip=pip, app=papp)) log.debug('Migration Successful to {python}'.format(python=python)) except: log.debug('Migration Error') raise