You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fix_user_permissions.py 1.8 KiB

пре 1 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # imports - standard imports
  2. import getpass
  3. import os
  4. import subprocess
  5. # imports - module imports
  6. from bench.cli import change_uid_msg
  7. from bench.config.production_setup import get_supervisor_confdir, is_centos7, service
  8. from bench.config.common_site_config import get_config
  9. from bench.utils import exec_cmd, get_bench_name, get_cmd_output
  10. def is_sudoers_set():
  11. """Check if bench sudoers is set"""
  12. cmd = ["sudo", "-n", "bench"]
  13. bench_warn = False
  14. with open(os.devnull, "wb") as f:
  15. return_code_check = not subprocess.call(cmd, stdout=f)
  16. if return_code_check:
  17. try:
  18. bench_warn = change_uid_msg in get_cmd_output(cmd, _raise=False)
  19. except subprocess.CalledProcessError:
  20. bench_warn = False
  21. finally:
  22. return_code_check = return_code_check and bench_warn
  23. return return_code_check
  24. def is_production_set(bench_path):
  25. """Check if production is set for current bench"""
  26. production_setup = False
  27. bench_name = get_bench_name(bench_path)
  28. supervisor_conf_extn = "ini" if is_centos7() else "conf"
  29. supervisor_conf_file_name = f"{bench_name}.{supervisor_conf_extn}"
  30. supervisor_conf = os.path.join(get_supervisor_confdir(), supervisor_conf_file_name)
  31. if os.path.exists(supervisor_conf):
  32. production_setup = production_setup or True
  33. nginx_conf = f"/etc/nginx/conf.d/{bench_name}.conf"
  34. if os.path.exists(nginx_conf):
  35. production_setup = production_setup or True
  36. return production_setup
  37. def execute(bench_path):
  38. """This patch checks if bench sudoers is set and regenerate supervisor and sudoers files"""
  39. user = get_config(".").get("xhiveframework_user") or getpass.getuser()
  40. if is_sudoers_set():
  41. if is_production_set(bench_path):
  42. exec_cmd(f"sudo bench setup supervisor --yes --user {user}")
  43. service("supervisord", "restart")
  44. exec_cmd(f"sudo bench setup sudoers {user}")