setup.py 2.7 KB

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