From fe02844c69a849b4c1a826dbd16a4f5dc28127bc Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Mon, 16 Mar 2020 12:04:18 +0530 Subject: [PATCH] fix: run fix_user_permissions only if production or sudoers is set up --- bench/config/supervisor.py | 3 -- bench/patches/v5/fix_user_permissions.py | 39 +++++++++++++++++------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/bench/config/supervisor.py b/bench/config/supervisor.py index 40b8a92f..eadd0a7f 100644 --- a/bench/config/supervisor.py +++ b/bench/config/supervisor.py @@ -69,9 +69,6 @@ def update_supervisord_conf(user): supervisord_conf = get_supervisord_conf() section = "unix_http_server" - if not supervisord_conf: - return - config = configparser.ConfigParser() config.read(supervisord_conf) config[section]["chmod"] = "0760" diff --git a/bench/patches/v5/fix_user_permissions.py b/bench/patches/v5/fix_user_permissions.py index fba8d9f0..9da2ee99 100644 --- a/bench/patches/v5/fix_user_permissions.py +++ b/bench/patches/v5/fix_user_permissions.py @@ -1,21 +1,38 @@ # imports - standard imports +import os import subprocess -import sys # imports - module imports -from bench.utils import get_cmd_output, exec_cmd, which from bench.cli import change_uid_msg +from bench.config.production_setup import get_supervisor_confdir, is_centos7 +from bench.utils import exec_cmd, get_bench_name, get_cmd_output + + +def is_sudoers_set(): + cmd = ["sudo", "-n", "bench"] + return (not subprocess.call(cmd)) or (change_uid_msg in get_cmd_output(cmd)) + + +def is_production_set(bench_path): + production_setup = False + bench_name = get_bench_name(bench_path) + + supervisor_conf_extn = "ini" if is_centos7() else "conf" + supervisor_conf_file_name = '{bench_name}.{extn}'.format(bench_name=bench_name, extn=supervisor_conf_extn) + supervisor_conf = os.path.join(get_supervisor_confdir(), supervisor_conf_file_name) + + if os.path.exists(supervisor_conf): + production_setup = production_setup or True + + nginx_conf = '/etc/nginx/conf.d/{bench_name}.conf'.format(bench_name=bench_name) + + if os.path.exists(nginx_conf): + production_setup = production_setup or True + + return production_setup def execute(bench_path): - cmd = ["sudo", "-n", "bench"] - - is_bench_sudoers_set = (not subprocess.call(cmd)) or (change_uid_msg in get_cmd_output(cmd)) - is_supervisor_installed = which('supervisorctl') - - if not is_supervisor_installed: - exec_cmd("{} -m pip install supervisor".format(sys.executable)) - - if is_bench_sudoers_set: + if is_sudoers_set() or is_production_set(bench_path): exec_cmd("sudo bench setup supervisor --yes") exec_cmd("sudo bench setup sudoers")