setup_nginx.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import subprocess
  2. import sys
  3. import setup_util
  4. import os
  5. root = os.getcwd() + "/servicestack"
  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. #
  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:
  36. pid = int(line.split(None, 2)[1])
  37. os.kill(pid, 9)
  38. return 0