Bez popisu
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.

loader.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import shlex
  5. import warnings
  6. ##@brief Preloader for a multisite process
  7. #
  8. #This loader is a kind of fake loader. In fact it only read configurations
  9. #for the multisite instance and then run a UWSGI process that will run
  10. #the run.py file.
  11. #
  12. #If you want to see the "real" multisite loading process see
  13. #@ref lodel/plugins/multisite/run.py file
  14. #
  15. #@par Implementation details
  16. #Here we have to bootstrap a minimal __loader__ context in order
  17. #to be able to load the settings
  18. #
  19. #This file (once bootstraped) start a new process for uWSGI. uWSGI then
  20. #run lodel.plugins.multisite.run.application function
  21. #@note the uwsgi process in started using the execl function when UWSGI
  22. #will exit this process will stop too
  23. #
  24. try:
  25. from lodel.context import LodelContext
  26. except ImportError:
  27. LODEL_BASE_DIR = os.path.dirname(
  28. os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  29. from lodel.context import LodelContext
  30. from lodel import buildconf
  31. LodelContext.init(LodelContext.MULTISITE)
  32. LodelContext.set(None) #Loading context creation
  33. #Multisite instance settings loading
  34. CONFDIR = os.path.join(os.getcwd(), 'conf.d')
  35. if not os.path.isdir(CONFDIR):
  36. warnings.warn('%s do not exists, default settings used' % CONFDIR)
  37. LodelContext.expose_modules(globals(), {
  38. 'lodel.settings.settings': [('Settings', 'settings')],
  39. 'lodel.plugins.multisite.confspecs': 'multisite_confspecs'})
  40. if not settings.started():
  41. settings('./conf.d', multisite_confspecs.LODEL2_CONFSPECS)
  42. LodelContext.expose_modules(globals(), {
  43. 'lodel.settings': ['Settings']})
  44. ##@brief Starts uwsgi in background using settings
  45. def uwsgi_fork():
  46. sockfile = os.path.join(buildconf.LODEL2VARDIR, 'uwsgi_sockets/')
  47. if not os.path.isdir(sockfile):
  48. os.mkdir(sockfile)
  49. sockfile = os.path.join(sockfile, 'uwsgi_lodel2_multisite.sock')
  50. logfile = os.path.join(
  51. buildconf.LODEL2LOGDIR, 'uwsgi_lodel2_multisite.log')
  52. cmd='{uwsgi} --plugin python3 --http-socket {addr}:{port} --module \
  53. lodel.plugins.multisite.run --socket {sockfile} --logto {logfile} -p {uwsgiworkers}'
  54. cmd = cmd.format(
  55. addr = Settings.server.listen_address,
  56. port = Settings.server.listen_port,
  57. uwsgi= Settings.server.uwsgicmd,
  58. sockfile=sockfile,
  59. logfile = logfile,
  60. uwsgiworkers = Settings.server.uwsgi_workers)
  61. if Settings.server.virtualenv is not None:
  62. cmd += " --virtualenv %s" % Settings.webui.virtualenv
  63. try:
  64. args = shlex.split(cmd)
  65. print("Running %s" % cmd)
  66. exit(os.execl(args[0], *args))
  67. except Exception as e:
  68. print("Webui plugin uwsgi execl fails cmd was '%s' error : " % cmd,
  69. e, file=sys.stderr)
  70. exit(1)
  71. if __name__ == '__main__':
  72. uwsgi_fork()