From d2a70badd3c61bd50b1fdc5dfd456ed77601f446 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Fri, 13 Mar 2020 18:48:22 +0530 Subject: [PATCH] chore: optimized and removed deprecated api usage and imports --- bench/config/production_setup.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/bench/config/production_setup.py b/bench/config/production_setup.py index e6d09098..42729a22 100755 --- a/bench/config/production_setup.py +++ b/bench/config/production_setup.py @@ -1,12 +1,15 @@ -from bench.utils import get_program, exec_cmd, get_cmd_output, fix_prod_setup_perms, get_bench_name, find_executable, CommandFailedError -from bench.config.supervisor import generate_supervisor_config -from bench.config.systemd import generate_systemd_config -from bench.config.nginx import make_nginx_conf -from bench.config.common_site_config import get_config -import os, subprocess +# imports - standard imports +import os import sys from distutils.spawn import find_executable +# imports - module imports +from bench.config.common_site_config import get_config +from bench.config.nginx import make_nginx_conf +from bench.config.supervisor import generate_supervisor_config +from bench.config.systemd import generate_systemd_config +from bench.utils import CommandFailedError, exec_cmd, fix_prod_setup_perms, get_bench_name, get_cmd_output + def setup_production_prerequisites(): if not find_executable("ansible"): @@ -55,6 +58,7 @@ def setup_production(user, bench_path='.', yes=False): reload_nginx() + def disable_production(bench_path='.'): bench_name = get_bench_name(bench_path) @@ -77,10 +81,11 @@ def disable_production(bench_path='.'): reload_nginx() + def service(service, option): - if os.path.basename(get_program(['systemctl']) or '') == 'systemctl' and is_running_systemd(): + if os.path.basename(find_executable('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': + elif os.path.basename(find_executable('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 @@ -93,12 +98,14 @@ def service(service, option): else: raise Exception('No service manager found') + def get_supervisor_confdir(): possiblities = ('/etc/supervisor/conf.d', '/etc/supervisor.d/', '/etc/supervisord/conf.d', '/etc/supervisord.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-enabled/default'] @@ -110,6 +117,7 @@ def remove_default_nginx_configs(): def is_centos7(): return os.path.exists('/etc/redhat-release') and get_cmd_output("cat /etc/redhat-release | sed 's/Linux\ //g' | cut -d' ' -f3 | cut -d. -f1").strip() == '7' + def is_running_systemd(): with open('/proc/1/comm') as f: comm = f.read().strip() @@ -119,6 +127,7 @@ def is_running_systemd(): return True return False + def reload_supervisor(): supervisorctl = find_executable('supervisorctl') @@ -153,7 +162,7 @@ def reload_supervisor(): def reload_nginx(): try: - subprocess.check_output(['sudo', find_executable('nginx'), '-t']) + exec_cmd('sudo {0} -t'.format(find_executable('nginx'))) except: raise