launch 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import shlex
  5. import subprocess
  6. import sys
  7. import threading
  8. import yaml
  9. sys.path.insert(0, os.getcwd())
  10. import mkconfig
  11. # The default manifest file
  12. MANIFEST = "manifest.yaml"
  13. # The default pubkeys file
  14. PUBKEYS = "pubkeys.yaml"
  15. # The TEEMS binary
  16. TEEMS = "./teems"
  17. def launch(node, manifest, config):
  18. manifestdata = manifest[node]
  19. cmdline = ''
  20. if 'launchprefix' in manifestdata:
  21. cmdline = manifestdata['launchprefix'] + ' '
  22. cmdline += TEEMS + " %s %s" % (manifestdata['sprvfile'], node)
  23. if 'args' in manifestdata:
  24. cmdline += ' ' + manifestdata['args']
  25. proc = subprocess.Popen(shlex.split(cmdline), stdin=subprocess.PIPE,
  26. stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=0)
  27. proc.stdin.write(config.encode('utf-8'))
  28. while True:
  29. line = proc.stdout.readline()
  30. if not line:
  31. break
  32. print(node + ": " + line.decode('utf-8'), end='')
  33. if __name__ == "__main__":
  34. aparse = argparse.ArgumentParser(
  35. description='Launch TEEMS nodes'
  36. )
  37. aparse.add_argument('-m', default=MANIFEST,
  38. help='manifest.yaml file')
  39. aparse.add_argument('-p', default=PUBKEYS,
  40. help='pubkeys.yaml file')
  41. aparse.add_argument('node', nargs='*', help='nodes to include')
  42. args = aparse.parse_args()
  43. with open(args.m) as mf:
  44. manifest = yaml.safe_load(mf)
  45. config = mkconfig.create_json(args.m, args.p, args.node)
  46. # There must not be any newlines in the config json string
  47. if "\n" in config:
  48. print("Error: config.json must not contain embedded newlines")
  49. sys.exit(1)
  50. # Now add a trailing newline
  51. config += "\n"
  52. nodelist = args.node
  53. if nodelist is None or len(nodelist) == 0:
  54. nodelist = manifest.keys()
  55. threadlist = []
  56. for node in nodelist:
  57. thread = threading.Thread(target=launch,
  58. args=(node, manifest, config))
  59. thread.start()
  60. threadlist.append(thread)
  61. for thread in threadlist:
  62. thread.join()