You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

62 lines
2.1 KiB

  1. #!/usr/bin/env python3
  2. from nilmdb.client.httpclient import HTTPClient, ClientError, ServerError
  3. from nilmdb.utils.printf import *
  4. import nilmrun
  5. import argparse
  6. import os
  7. import time
  8. import sys
  9. def main():
  10. """Run a command on the NilmRun server"""
  11. def_url = os.environ.get("NILMRUN_URL", "http://localhost/nilmrun/")
  12. parser = argparse.ArgumentParser(
  13. description = 'Run a command on the NilmRun server',
  14. formatter_class = argparse.ArgumentDefaultsHelpFormatter)
  15. parser.add_argument("-v", "--version", action="version",
  16. version=nilmrun.__version__)
  17. group = parser.add_argument_group("Standard options")
  18. group.add_argument('-u', '--url',
  19. help = 'NilmRun server URL', default = def_url)
  20. group.add_argument('-n', '--noverify', action="store_true",
  21. help = 'Disable SSL certificate verification')
  22. group = parser.add_argument_group("Program")
  23. group.add_argument('-d', '--detach', action="store_true",
  24. help = 'Run process and return immediately without '
  25. 'printing its output')
  26. group.add_argument('cmd', help="Command to run")
  27. group.add_argument('arg', nargs=argparse.REMAINDER,
  28. help="Arguments for command")
  29. args = parser.parse_args()
  30. client = HTTPClient(baseurl=args.url, verify_ssl=not args.noverify,
  31. post_json=True)
  32. # Run command
  33. pid = client.post("run/command", { "argv": [ args.cmd ] + args.arg })
  34. # If we're detaching, just print the PID
  35. if args.detach:
  36. print(pid)
  37. raise SystemExit(0)
  38. # Otherwise, watch the log output, and kill the process when it's done
  39. # or when this script terminates.
  40. try:
  41. while True:
  42. s = client.get("process/status", { "pid": pid, "clear": 1 })
  43. sys.stdout.write(s['log'])
  44. sys.stdout.flush()
  45. if not s['alive']:
  46. break
  47. time.sleep(1)
  48. finally:
  49. s = client.post("process/remove", { "pid": pid })
  50. raise SystemExit(s['exitcode'])
  51. if __name__ == "__main__":
  52. main()