setup_common.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os, setup_util, signal, subprocess
  2. # Create start and stop functions for the Play project with the given dir
  3. # and install them in the given module's globals.
  4. def make_setup_for_dir(module_globals, subtest_name):
  5. def start(args, logfile, errfile):
  6. kill_running_process(logfile)
  7. subtest_dir = get_subtest_dir()
  8. install_dir = os.environ['IROOT']
  9. is_windows = os.name == "nt"
  10. cmd_suffix = '.bat' if is_windows else ''
  11. sbt_cmd = os.path.join(install_dir, 'sbt', 'bin', 'sbt'+cmd_suffix)
  12. app_cmd = os.path.join(subtest_dir, 'target','universal','stage','bin',subtest_name+cmd_suffix)
  13. setup_util.replace_text(
  14. os.path.join(subtest_dir,'conf','application.conf'),
  15. "jdbc:mysql:\/\/.*:3306", "jdbc:mysql://" + args.database_host + ":3306")
  16. logfile.write('Staging app: '+sbt_cmd+' stage\n')
  17. subprocess.call(
  18. [sbt_cmd, 'stage'],
  19. stdin=subprocess.PIPE, cwd=subtest_dir, stderr=errfile, stdout=logfile)
  20. logfile.write('Starting app: '+app_cmd+'\n')
  21. subprocess.Popen(
  22. [app_cmd],
  23. shell=True, stdin=subprocess.PIPE, cwd=subtest_dir, stderr=errfile, stdout=logfile)
  24. return 0
  25. def stop(logfile, errfile):
  26. kill_running_process(logfile)
  27. return 0
  28. # Install the start and stop functions in the calling module
  29. module_globals['start'] = start
  30. module_globals['stop'] = stop
  31. def get_subtest_dir():
  32. test_dir = os.environ['TROOT']
  33. return os.path.join(test_dir, subtest_name)
  34. # Kill the running process and delete the RUNNING_PID file (if any).
  35. def kill_running_process(logfile):
  36. subtest_dir = get_subtest_dir()
  37. pidfile = os.path.join(subtest_dir,"target","universal","stage","RUNNING_PID")
  38. if not os.path.exists(pidfile):
  39. logfile.write('No PID file: {}\n'.format(pidfile))
  40. return
  41. logfile.write('Reading and deleting PID file: {}\n'.format(pidfile))
  42. with open(pidfile) as f:
  43. pid = int(f.read())
  44. os.remove(pidfile)
  45. logfile.write('Sending SIGTERM to process: {}\n'.format(pid))
  46. os.kill(pid, signal.SIGTERM)