import subprocess
import sys
import setup_util
import os

def start(args):
  setup_util.replace_text("dart/postgresql.yaml", "host: '.*'", "host: '" + args.database_host + "'")
  try:
    subprocess.Popen("pub install", shell=True, cwd="dart")
    subprocess.Popen("dart server.dart", shell=True, cwd="dart")
    return 0
  except subprocess.CalledProcessError:
    return 1

def stop():
  p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  out, err = p.communicate()
  for line in out.splitlines():
    if 'dart' in line:
      pid = int(line.split(None, 2)[1])
      os.kill(pid, 9)
  return 0

@@ -51,16 +51,51 @@
 ort os%0A%0A
+DART_SDK = os.environ('DART_SDK')%0A%0A
 def star
@@ -129,17 +129,17 @@
 ce_text(
-%22
+'
 dart/pos
@@ -154,34 +154,31 @@
 yaml
-%22, %22
+', '
 host: 
-'
 .*'
-%22
 , 
-%22
+'
 host: '
-%22
  + a
@@ -198,14 +198,8 @@
 host
- + %22'%22
 )%0A  
@@ -203,16 +203,60 @@
 %0A  try:%0A
+    #%0A    # install dart dependencies%0A    #%0A
     subp
@@ -268,17 +268,32 @@
 s.Popen(
-%22
+DART_SDK + 'bin/
 pub inst
@@ -295,17 +295,17 @@
  install
-%22
+'
 , shell=
@@ -314,28 +314,120 @@
 ue, cwd=
-%22
+'
 dart
-%22
+')%0A    #%0A    # start dart servers%0A    #%0A    for port in range(9001, 9001 + args.max_threads
 )%0A    
+  
 subproce
@@ -439,26 +439,1411 @@
 pen(
-%22dart server.dart%22
+DART_SDK + 'bin/dart server.dart -a 127.0.0.1 -p ' + str(port) + ' -d ' + str(args.max_concurrency / args.max_threads), shell=True, cwd='dart')%0A    #%0A    # create nginx configuration%0A    #%0A    conf = %5B%5D%0A    conf.append('worker_processes ' + str(args.max_threads) + ';')%0A    conf.append('error_log /dev/null crit;')%0A    conf.append('events %7B')%0A    conf.append('    worker_connections ' + str(4 * args.max_concurrency / args.max_threads) + ';')%0A    conf.append('%7D')%0A    conf.append('http %7B')%0A    conf.append('    access_log off;')%0A    conf.append('    include mime.types;')%0A    conf.append('    default_type application/octet-stream;')%0A    conf.append('    sendfile on;')%0A    conf.append('    upstream dart_cluster %7B')%0A    for port in range(9001, 9001 + args.max_threads)%0A      conf.append('        server 127.0.0.1:' + str(port) + ';')%0A    conf.append('    %7D')%0A    conf.append('    server %7B')%0A    conf.append('        listen 8080;')%0A    conf.append('        location / %7B')%0A    conf.append('            proxy_pass http://dart_cluster;')%0A    conf.append('            proxy_http_version 1.1;')%0A    conf.append('        %7D')%0A    conf.append('    %7D')%0A    conf.append('%7D')%0A    #%0A    # write nginx configuration to disk%0A    #%0A    os.remove('dart/nginx.conf')%0A    with open('dart/nginx.conf', 'w') as f:%0A      f.write('%5Cn'.join(conf))%0A    #%0A    # start nginx%0A    #%0A    subprocess.Popen('nginx -c %60pwd%60/nginx.conf'
 , sh
@@ -1860,15 +1860,16 @@
 cwd=
-%22
+'
 dart
-%22)
+');
 %0A   
@@ -1944,16 +1944,180 @@
 stop():%0A
+  #%0A  # stop nginx%0A  #%0A  subprocess.Popen('nginx -c %60pwd%60/nginx.conf -s stop', shell=True, cwd='dart')%0A  os.remove('dart/nginx.conf')%0A  #%0A  # stop dart servers%0A  #%0A
   p = su

Make dart setup file use nginx as a load balancer.