setup_nginx.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import subprocess
  2. import sys
  3. import setup_util
  4. import os
  5. root = os.getcwd() + "/aspnet"
  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. subprocess.check_call("sudo chown -R $USER:$USER /usr/local/etc/mono", shell=True, stderr=errfile, stdout=logfile)
  16. # nginx
  17. workers = 'worker_processes ' + str(args.max_threads) + ';'
  18. 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);
  19. subprocess.check_call('sudo /usr/local/nginx/sbin/nginx -c ' + root + '/nginx.conf -g "' + workers + '"', shell=True, stderr=errfile, stdout=logfile)
  20. # fastcgi
  21. for port in range(9001, 9001 + args.max_threads):
  22. 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)
  23. return 0
  24. except subprocess.CalledProcessError:
  25. return 1
  26. def stop(logfile, errfile):
  27. if os.name == 'nt':
  28. return 0
  29. subprocess.check_call("sudo /usr/local/nginx/sbin/nginx -c " + root + "/nginx.conf -s stop", shell=True, stderr=errfile, stdout=logfile)
  30. subprocess.check_call("rm -f " + root + "/nginx.upstream.conf", shell=True, stderr=errfile, stdout=logfile)
  31. #
  32. # stop mono
  33. #
  34. p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  35. out, err = p.communicate()
  36. for line in out.splitlines():
  37. if 'mono-server' in line:
  38. pid = int(line.split(None, 2)[1])
  39. os.kill(pid, 15)
  40. return 0