No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

207 líneas
5.5 KiB

  1. # imports - standard imports
  2. import contextlib
  3. import os
  4. import logging
  5. import sys
  6. # imports - module imports
  7. import bench
  8. from bench.config.nginx import make_nginx_conf
  9. from bench.config.supervisor import (
  10. generate_supervisor_config,
  11. check_supervisord_config,
  12. )
  13. from bench.config.systemd import generate_systemd_config
  14. from bench.bench import Bench
  15. from bench.utils import exec_cmd, which, get_bench_name, get_cmd_output, log
  16. from bench.utils.system import fix_prod_setup_perms
  17. from bench.exceptions import CommandFailedError
  18. logger = logging.getLogger(bench.PROJECT_NAME)
  19. def setup_production_prerequisites():
  20. """Installs ansible, fail2banc, NGINX and supervisor"""
  21. if not which("ansible"):
  22. exec_cmd(f"sudo {sys.executable} -m pip install ansible")
  23. if not which("fail2ban-client"):
  24. exec_cmd("bench setup role fail2ban")
  25. if not which("nginx"):
  26. exec_cmd("bench setup role nginx")
  27. if not which("supervisord"):
  28. exec_cmd("bench setup role supervisor")
  29. def setup_production(user, bench_path=".", yes=False):
  30. print("Setting Up prerequisites...")
  31. setup_production_prerequisites()
  32. conf = Bench(bench_path).conf
  33. if conf.get("restart_supervisor_on_update") and conf.get("restart_systemd_on_update"):
  34. raise Exception(
  35. "You cannot use supervisor and systemd at the same time. Modify your common_site_config accordingly."
  36. )
  37. if conf.get("restart_systemd_on_update"):
  38. print("Setting Up systemd...")
  39. generate_systemd_config(bench_path=bench_path, user=user, yes=yes)
  40. else:
  41. print("Setting Up supervisor...")
  42. check_supervisord_config(user=user)
  43. generate_supervisor_config(bench_path=bench_path, user=user, yes=yes)
  44. print("Setting Up NGINX...")
  45. make_nginx_conf(bench_path=bench_path, yes=yes)
  46. fix_prod_setup_perms(bench_path, xhiveframework_user=user)
  47. remove_default_nginx_configs()
  48. bench_name = get_bench_name(bench_path)
  49. nginx_conf = f"/etc/nginx/conf.d/{bench_name}.conf"
  50. print("Setting Up symlinks and reloading services...")
  51. if conf.get("restart_supervisor_on_update"):
  52. supervisor_conf_extn = "ini" if is_centos7() else "conf"
  53. supervisor_conf = os.path.join(
  54. get_supervisor_confdir(), f"{bench_name}.{supervisor_conf_extn}"
  55. )
  56. # Check if symlink exists, If not then create it.
  57. if not os.path.islink(supervisor_conf):
  58. os.symlink(
  59. os.path.abspath(os.path.join(bench_path, "config", "supervisor.conf")),
  60. supervisor_conf,
  61. )
  62. if not os.path.islink(nginx_conf):
  63. os.symlink(
  64. os.path.abspath(os.path.join(bench_path, "config", "nginx.conf")), nginx_conf
  65. )
  66. if conf.get("restart_supervisor_on_update"):
  67. reload_supervisor()
  68. if os.environ.get("NO_SERVICE_RESTART"):
  69. return
  70. reload_nginx()
  71. def disable_production(bench_path="."):
  72. bench_name = get_bench_name(bench_path)
  73. conf = Bench(bench_path).conf
  74. # supervisorctl
  75. supervisor_conf_extn = "ini" if is_centos7() else "conf"
  76. supervisor_conf = os.path.join(
  77. get_supervisor_confdir(), f"{bench_name}.{supervisor_conf_extn}"
  78. )
  79. if os.path.islink(supervisor_conf):
  80. os.unlink(supervisor_conf)
  81. if conf.get("restart_supervisor_on_update"):
  82. reload_supervisor()
  83. # nginx
  84. nginx_conf = f"/etc/nginx/conf.d/{bench_name}.conf"
  85. if os.path.islink(nginx_conf):
  86. os.unlink(nginx_conf)
  87. reload_nginx()
  88. def service(service_name, service_option):
  89. if os.path.basename(which("systemctl") or "") == "systemctl" and is_running_systemd():
  90. exec_cmd(f"sudo systemctl {service_option} {service_name}")
  91. elif os.path.basename(which("service") or "") == "service":
  92. exec_cmd(f"sudo service {service_name} {service_option}")
  93. else:
  94. # look for 'service_manager' and 'service_manager_command' in environment
  95. service_manager = os.environ.get("BENCH_SERVICE_MANAGER")
  96. if service_manager:
  97. service_manager_command = (
  98. os.environ.get("BENCH_SERVICE_MANAGER_COMMAND")
  99. or f"{service_manager} {service_option} {service}"
  100. )
  101. exec_cmd(service_manager_command)
  102. else:
  103. log(
  104. f"No service manager found: '{service_name} {service_option}' failed to execute",
  105. level=2,
  106. )
  107. def get_supervisor_confdir():
  108. possiblities = (
  109. "/etc/supervisor/conf.d",
  110. "/etc/supervisor.d/",
  111. "/etc/supervisord/conf.d",
  112. "/etc/supervisord.d",
  113. )
  114. for possiblity in possiblities:
  115. if os.path.exists(possiblity):
  116. return possiblity
  117. def remove_default_nginx_configs():
  118. default_nginx_configs = [
  119. "/etc/nginx/conf.d/default.conf",
  120. "/etc/nginx/sites-enabled/default",
  121. ]
  122. for conf_file in default_nginx_configs:
  123. if os.path.exists(conf_file):
  124. os.unlink(conf_file)
  125. def is_centos7():
  126. return (
  127. os.path.exists("/etc/redhat-release")
  128. and get_cmd_output(
  129. r"cat /etc/redhat-release | sed 's/Linux\ //g' | cut -d' ' -f3 | cut -d. -f1"
  130. ).strip()
  131. == "7"
  132. )
  133. def is_running_systemd():
  134. with open("/proc/1/comm") as f:
  135. comm = f.read().strip()
  136. if comm == "init":
  137. return False
  138. elif comm == "systemd":
  139. return True
  140. return False
  141. def reload_supervisor():
  142. supervisorctl = which("supervisorctl")
  143. with contextlib.suppress(CommandFailedError):
  144. # first try reread/update
  145. exec_cmd(f"{supervisorctl} reread")
  146. exec_cmd(f"{supervisorctl} update")
  147. return
  148. with contextlib.suppress(CommandFailedError):
  149. # something is wrong, so try reloading
  150. exec_cmd(f"{supervisorctl} reload")
  151. return
  152. with contextlib.suppress(CommandFailedError):
  153. # then try restart for centos
  154. service("supervisord", "restart")
  155. return
  156. with contextlib.suppress(CommandFailedError):
  157. # else try restart for ubuntu / debian
  158. service("supervisor", "restart")
  159. return
  160. def reload_nginx():
  161. exec_cmd(f"sudo {which('nginx')} -t")
  162. service("nginx", "reload")