install_config.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @File : config.py
  4. # @Author: becivells
  5. #@Contact : becivells@gmail.com
  6. # @Date : 2017/12/7
  7. #@Software : PyCharm
  8. # @Desc :
  9. import os
  10. APP_NAME = 'webhooks'
  11. APP_INDEX = 'webhooks.py'
  12. APP_PATH_NAME = 'webhooks'
  13. APP_PORT = 21332
  14. APP_WORK = 1
  15. APP_USER = 'flask'
  16. RUN_USER = 'root'
  17. APP_USER_PATH = '/home/flask'
  18. #定义的flask app=FLAKSK()
  19. APP_DEFINE='app'
  20. APP_PATH = APP_USER_PATH + '/' + APP_PATH_NAME
  21. APP_LOG = APP_PATH + '/' +'log'
  22. #PROJECT DIR
  23. LOCAL_BASE_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  24. #CONF PATH
  25. LOCAL_CONF_PATH = LOCAL_BASE_DIR + os.sep + 'install' + os.sep + 'config'
  26. LOCAL_SUPERVISORD_CONF_PATH = LOCAL_CONF_PATH + os.sep + 'supervisord.conf'
  27. LOCAL_GUNICORN_CONF_PATH = LOCAL_CONF_PATH + os.sep + 'supervisor_%s.conf'%(APP_NAME)
  28. LOCAL_PIP_CONF_PATH = LOCAL_CONF_PATH + os.sep + 'pip.conf'
  29. REMOTE_PIP_REQUIREMENT_PATH = APP_USER_PATH + '/' + APP_PATH_NAME + '/' + 'requirements.txt'
  30. gunicorn_conf = '''[program:{APP_NAME}]
  31. command=gunicorn -w{APP_WORK} -b0.0.0.0:{APP_PORT} --error-logfile {APP_LOG}/error.log --access-logfile {APP_LOG}/access.log {APP_INDEX}:{APP_DEFINE} ;应用入口
  32. user={RUN_USER}
  33. directory={APP_PATH} ;web目录
  34. startsecs=5 ;启动时间
  35. stopwaitsecs=0 ;终止等待时间
  36. startretries = 3
  37. autostart=true ;是否自动启动
  38. autorestart=true ;是否自动重启
  39. redirect_stderr=true ;错误日志输出到标准日志
  40. stdout_logfile=/dev/null ;标准日志不输出
  41. stdout_logfile_maxbytes=10MB ;标准日志大小
  42. environment=PYTHONPATH=$PYTHONPATH:{APP_PATH}
  43. '''
  44. def check():
  45. flask_index_name = '.'.join(APP_INDEX.split('.')[:-1])
  46. with open(LOCAL_GUNICORN_CONF_PATH,'w') as f:
  47. f.write(gunicorn_conf.format(
  48. APP_PATH = APP_PATH,
  49. APP_PORT = APP_PORT,
  50. APP_NAME = APP_NAME,
  51. APP_USER = APP_USER,
  52. APP_WORK = APP_WORK,
  53. APP_LOG = APP_LOG,
  54. RUN_USER = RUN_USER,
  55. APP_DEFINE = APP_DEFINE,
  56. APP_INDEX = flask_index_name))
  57. if not os.path.exists(REMOTE_PIP_REQUIREMENT_PATH):
  58. print(u'please create file requirements.txt in %s dir '%(LOCAL_BASE_DIR))
  59. if not os.path.exists(LOCAL_CONF_PATH):
  60. print('please install dir error ')
  61. if not os.path.exists(LOCAL_PIP_CONF_PATH):
  62. print ('pip.conf file not find')
  63. check()