setup_nginx.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import subprocess
  2. import sys
  3. import setup_util
  4. import os
  5. root = os.getcwd() + "/nancy"
  6. app = root + "/src"
  7. def start(args, logfile, errfile):
  8. if os.name == 'nt':
  9. return 1
  10. setup_util.replace_text(app + "/Web.config", "localhost", args.database_host)
  11. try:
  12. # build
  13. subprocess.check_call("rm -rf bin obj", shell=True, cwd=app, stderr=errfile, stdout=logfile)
  14. subprocess.check_call("xbuild /p:Configuration=Release", shell=True, cwd=app, stderr=errfile, stdout=logfile)
  15. # nginx
  16. workers = 'worker_processes ' + str(args.max_threads) + ';'
  17. subprocess.check_call('echo "upstream mono {\n' + ';\n'.join('\tserver 127.0.0.1:' + str(port) for port in range(9001, 9001 + args.max_threads)) + ';\n}" > ' + root + '/nginx.upstream.conf', shell=True, stderr=errfile, stdout=logfile);
  18. subprocess.check_call('sudo /usr/local/nginx/sbin/nginx -c ' + root + '/nginx.conf -g "' + workers + '"', shell=True, stderr=errfile, stdout=logfile)
  19. # fastcgi
  20. for port in range(9001, 9001 + args.max_threads):
  21. subprocess.Popen("MONO_OPTIONS=--gc=sgen fastcgi-mono-server4 /applications=/:. /socket=tcp:127.0.0.1:" + str(port) + " &", shell=True, cwd=app, stderr=errfile, stdout=logfile)
  22. return 0
  23. except subprocess.CalledProcessError:
  24. return 1
  25. def stop(logfile, errfile):
  26. if os.name == 'nt':
  27. return 0
  28. subprocess.check_call("sudo /usr/local/nginx/sbin/nginx -c " + root + "/nginx.conf -s stop", shell=True, stderr=errfile, stdout=logfile)
  29. subprocess.check_call("rm -f " + root + "/nginx.upstream.conf", shell=True, stderr=errfile, stdout=logfile)
  30. #
  31. # stop mono
  32. #
  33. p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  34. out, err = p.communicate()
  35. for line in out.splitlines():
  36. if 'mono-server' in line:
  37. pid = int(line.split(None, 2)[1])
  38. os.kill(pid, 15)
  39. return 0