setup.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import subprocess
  2. import sys
  3. import setup_util
  4. import os
  5. def start(args, logfile, errfile):
  6. setup_util.replace_text('dart/postgresql.yaml', 'host: .*', 'host: ' + args.database_host)
  7. try:
  8. #
  9. # install dart dependencies
  10. #
  11. subprocess.check_call('pub upgrade', shell=True, cwd='dart', stderr=errfile, stdout=logfile)
  12. #
  13. # start dart servers
  14. #
  15. for port in range(9001, 9001 + args.max_threads):
  16. subprocess.Popen('dart server.dart -a 127.0.0.1 -p ' + str(port) + ' -d ' + str(args.max_concurrency / args.max_threads), shell=True, cwd='dart', stderr=errfile, stdout=logfile)
  17. #
  18. # create nginx configuration
  19. #
  20. conf = []
  21. conf.append('worker_processes ' + str(args.max_threads) + ';')
  22. conf.append('error_log stderr error;')
  23. conf.append('events {')
  24. conf.append(' worker_connections 1024;')
  25. conf.append('}')
  26. conf.append('http {')
  27. conf.append(' access_log off;')
  28. conf.append(' include /usr/local/nginx/conf/mime.types;')
  29. conf.append(' default_type application/octet-stream;')
  30. conf.append(' sendfile on;')
  31. conf.append(' upstream dart_cluster {')
  32. for port in range(9001, 9001 + args.max_threads):
  33. conf.append(' server 127.0.0.1:' + str(port) + ';')
  34. conf.append(' keepalive ' + str(args.max_concurrency / args.max_threads) + ';')
  35. conf.append(' }')
  36. conf.append(' server {')
  37. conf.append(' listen 8080;')
  38. conf.append(' location / {')
  39. conf.append(' proxy_pass http://dart_cluster;')
  40. conf.append(' proxy_http_version 1.1;')
  41. conf.append(' proxy_set_header Connection "";')
  42. conf.append(' }')
  43. conf.append(' }')
  44. conf.append('}')
  45. #
  46. # write nginx configuration to disk
  47. #
  48. with open('dart/nginx.conf', 'w') as f:
  49. f.write('\n'.join(conf))
  50. #
  51. # start nginx
  52. #
  53. subprocess.Popen('sudo /usr/local/nginx/sbin/nginx -c `pwd`/nginx.conf', shell=True, cwd='dart', stderr=errfile, stdout=logfile);
  54. return 0
  55. except subprocess.CalledProcessError:
  56. return 1
  57. def stop(logfile, errfile):
  58. #
  59. # stop nginx
  60. #
  61. subprocess.check_call('sudo /usr/local/nginx/sbin/nginx -c `pwd`/nginx.conf -s stop', shell=True, cwd='dart', stderr=errfile, stdout=logfile)
  62. os.remove('dart/nginx.conf')
  63. #
  64. # stop dart servers
  65. #
  66. p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  67. out, err = p.communicate()
  68. for line in out.splitlines():
  69. if 'dart' in line and 'run-tests' not in line:
  70. pid = int(line.split(None, 2)[1])
  71. os.kill(pid, 15)
  72. return 0