diff --git a/bench/cli.py b/bench/cli.py index 548c2af8..db5f586a 100644 --- a/bench/cli.py +++ b/bench/cli.py @@ -16,6 +16,7 @@ from .app import get_app as _get_app from .app import new_app as _new_app from .app import pull_all_apps from .config import generate_nginx_config, generate_supervisor_config +from .production_setup import setup_production as _setup_production import os import sys import logging @@ -225,7 +226,11 @@ def setup_nginx(): def setup_supervisor(): "generate config for supervisor" generate_supervisor_config() - update_config({'restart_supervisor_on_update': True}) + +@click.command('production') +def setup_production(): + "setup bench for production" + _setup_production @click.command('auto-update') def setup_auto_update(): diff --git a/bench/config.py b/bench/config.py index 7d89076a..abb0bf96 100644 --- a/bench/config.py +++ b/bench/config.py @@ -2,7 +2,7 @@ import os import getpass import json from jinja2 import Environment, PackageLoader -from .utils import get_sites, get_config +from .utils import get_sites, get_config, update_config env = Environment(loader=PackageLoader('bench', 'templates'), trim_blocks=True) @@ -22,6 +22,7 @@ def generate_supervisor_config(bench='.'): }) with open("config/supervisor.conf", 'w') as f: f.write(config) + update_config({'restart_supervisor_on_update': True}) def get_site_config(site, bench='.'): with open(os.path.join(bench, 'sites', site, 'site_config.json')) as f: diff --git a/bench/production_setup.py b/bench/production_setup.py new file mode 100644 index 00000000..6aaf4e6d --- /dev/null +++ b/bench/production_setup.py @@ -0,0 +1,40 @@ +from .utils import get_program, exec_cmd, get_cmd_output +from .config import generate_nginx_config, generate_supervisor_config +import os + +def restart_service(service): + program = get_program(['systemctl', 'service']) + if not program: + raise Exception, 'No service manager found' + elif program == 'systemctl': + exec_cmd("{prog} restart {service}".format(prog=program, service=service)) + elif program == 'service': + exec_cmd("{prog} {service} restart ".format(prog=program, service=service)) + +def get_supervisor_confdir(): + possiblities = ('/etc/supervisor/conf.d', '/etc/supervisor.d/', '/etc/supervisord/conf.d') + for possiblity in possiblities: + if os.path.exists(possiblity): + return possiblity + +def remove_default_nginx_configs(): + default_nginx_configs = ['/etc/nginx/conf.d/default.conf', '/etc/nginx/sites-available/default.conf'] + + for conf_file in default_nginx_configs: + if os.path.exists(conf_file): + os.unlink(conf_file) + +def setup_production(bench='.'): + generate_supervisor_config(bench=bench) + generate_nginx_config(bench=bench) + remove_default_nginx_configs() + + if os.path.exists('/etc/redhat-release') and get_cmd_output("cat /etc/redhat-release | sed 's/Linux\ //g' | cut -d" " -f3 | cut -d. -f1") == '7': + supervisor_conf_filename = 'frappe.ini' + else: + supervisor_conf_filename = 'frappe.conf' + + os.symlink(os.path.abspath(os.path.join(bench, 'config', 'supervisor.conf')), os.path.join(get_supervisor_confdir(), supervisor_conf_filename)) + os.symlink(os.path.abspath(os.path.join(bench, 'config', 'supervisor.conf')), '/etc/nginx/conf.d/frappe.conf') + exec_cmd('supervisorctl reload') + restart_service('nginx') diff --git a/bench/utils.py b/bench/utils.py index 43c8f09d..8508243f 100644 --- a/bench/utils.py +++ b/bench/utils.py @@ -136,10 +136,12 @@ def update_bench(): exec_cmd("git pull", cwd=cwd) def setup_sudoers(user): - with open('/etc/sudoers.d/frappe', 'w') as f: + sudoers_file = '/etc/sudoers.d/frappe' + with open(sudoers_file, 'w') as f: f.write("{user} ALL=(ALL) NOPASSWD: {supervisorctl} restart frappe\:\n".format( user=user, supervisorctl=subprocess.check_output('which supervisorctl', shell=True).strip())) + os.chmod(sudoers_file, 0440) def setup_logging(bench='.'): if os.path.exists(os.path.join(bench, 'logs')): @@ -167,8 +169,7 @@ def update_config(new_config, bench='.'): config.update(new_config) put_config(config, bench=bench) -def get_process_manager(): - programs = ['foreman', 'forego', 'honcho'] +def get_program(programs): program = None for p in programs: program = find_executable(p) @@ -176,6 +177,9 @@ def get_process_manager(): break return program +def get_process_manager(): + return get_program(['foreman', 'forego', 'honcho']) + def start(): program = get_process_manager() if not program: @@ -269,3 +273,6 @@ def prime_wheel_cache(bench='.'): wheelhouse=wheel_cache_dir, requirements=requirements) exec_cmd(cmd) + +def is_root(): + pass