setup_nginx.py 1.9 KB

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