diff --git a/bench/cli.py b/bench/cli.py index ce21f7af..97fa5122 100644 --- a/bench/cli.py +++ b/bench/cli.py @@ -50,7 +50,7 @@ def cmd_requires_root(): if len(sys.argv) > 2 and sys.argv[2] in ('production', 'sudoers', 'lets-encrypt'): return True #Changed > to >=, unsure if will cause the apolcaypse - if len(sys.argv) >= 2 and sys.argv[1] in ('patch', 'renew-lets-encrypt'): + if len(sys.argv) >= 2 and sys.argv[1] in ('patch', 'renew-lets-encrypt', 'disable-production'): return True def change_dir(): diff --git a/bench/commands/__init__.py b/bench/commands/__init__.py index 9baf4bb7..64526956 100644 --- a/bench/commands/__init__.py +++ b/bench/commands/__init__.py @@ -29,7 +29,8 @@ bench_command.add_command(switch_to_v5) from bench.commands.utils import (start, restart, set_nginx_port, set_ssl_certificate, set_ssl_certificate_key, set_url_root, - set_mariadb_host, set_default_site, download_translations, shell, backup_site, backup_all_sites, release, renew_lets_encrypt) + set_mariadb_host, set_default_site, download_translations, shell, backup_site, backup_all_sites, release, renew_lets_encrypt, + disable_production) bench_command.add_command(start) bench_command.add_command(restart) bench_command.add_command(set_nginx_port) @@ -44,9 +45,11 @@ bench_command.add_command(backup_site) bench_command.add_command(backup_all_sites) bench_command.add_command(release) bench_command.add_command(renew_lets_encrypt) +bench_command.add_command(disable_production) from bench.commands.setup import setup bench_command.add_command(setup) + from bench.commands.config import config bench_command.add_command(config) diff --git a/bench/commands/utils.py b/bench/commands/utils.py index c9517851..a24051e8 100644 --- a/bench/commands/utils.py +++ b/bench/commands/utils.py @@ -128,3 +128,10 @@ def release(app, bump_type, develop, master, owner, repo_name, remote): from bench.release import release release(bench_path='.', app=app, bump_type=bump_type, develop=develop, master=master, remote=remote, owner=owner, repo_name=repo_name) + + +@click.command('disable-production') +def disable_production(): + """Disables production environment for the bench.""" + from bench.config.production_setup import disable_production + disable_production(bench_path='.') diff --git a/bench/config/production_setup.py b/bench/config/production_setup.py index b4ab3bc1..9cbd3c39 100755 --- a/bench/config/production_setup.py +++ b/bench/config/production_setup.py @@ -23,18 +23,39 @@ def setup_production(user, bench_path='.'): 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') + exec_cmd('sudo supervisorctl reload') if os.environ.get('NO_SERVICE_RESTART'): return service('nginx', 'restart') +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) + + service('nginx', 'reload') def service(service, option): if os.path.basename(get_program(['systemctl']) or '') == 'systemctl' and is_running_systemd(): - exec_cmd("{service_manager} {option} {service}".format(service_manager='systemctl', option=option, service=service)) + 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("{service_manager} {service} {option} ".format(service_manager='service', service=service, option=option)) + 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") diff --git a/bench/tests/test_setup_production.py b/bench/tests/test_setup_production.py index afe1a85b..7320b254 100644 --- a/bench/tests/test_setup_production.py +++ b/bench/tests/test_setup_production.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals from bench.tests import test_init -from bench.config.production_setup import setup_production, get_supervisor_confdir +from bench.config.production_setup import setup_production, get_supervisor_confdir, disable_production import bench.utils import os import getpass @@ -47,6 +47,21 @@ class TestSetupProduction(test_init.TestBenchInit): self.assert_supervisor_config(bench_name, use_rq=False) self.assert_supervisor_process(bench_name, use_rq=False) + def test_disable_production(self): + bench_name = 'test-disable-prod' + self.test_init(bench_name, frappe_branch='master') + + user = getpass.getuser() + + bench_path = os.path.join(os.path.abspath(self.benches_path), bench_name) + setup_production(user, bench_path) + + disable_production(bench_path) + + self.assert_nginx_link(bench_name) + self.assert_supervisor_link(bench_name) + self.assert_supervisor_process(bench_name=bench_name, disable_production=True) + def assert_nginx_config(self, bench_name): conf_src = os.path.join(os.path.abspath(self.benches_path), bench_name, 'config', 'nginx.conf') conf_dest = "/etc/nginx/conf.d/{bench_name}.conf".format(bench_name=bench_name) @@ -128,7 +143,7 @@ class TestSetupProduction(test_init.TestBenchInit): for key in tests: self.assertTrue(key.format(bench_name=bench_name) in f) - def assert_supervisor_process(self, bench_name, use_rq=True): + def assert_supervisor_process(self, bench_name, use_rq=True, disable_production=False): out = bench.utils.get_cmd_output("sudo supervisorctl status") if "STARTING" in out: @@ -160,7 +175,20 @@ class TestSetupProduction(test_init.TestBenchInit): ]) for key in tests: - self.assertTrue(re.search(key.format(bench_name=bench_name), out)) + if disable_production: + self.assertFalse(re.search(key.format(bench_name=bench_name), out)) + else: + self.assertTrue(re.search(key.format(bench_name=bench_name), out)) + def assert_nginx_link(self, bench_name): + nginx_conf_name = '{bench_name}.conf'.format(bench_name=bench_name) + nginx_conf_path = os.path.join('/etc/nginx/conf.d', nginx_conf_name) + self.assertFalse(os.path.islink(nginx_conf_path)) + def assert_supervisor_link(self, bench_name): + supervisor_conf_dir = get_supervisor_confdir() + supervisor_conf_name = '{bench_name}.conf'.format(bench_name=bench_name) + supervisor_conf_path = os.path.join(supervisor_conf_dir, supervisor_conf_name) + + self.assertFalse(os.path.islink(supervisor_conf_path))