remotehost.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Host class
  2. # Copyright (c) 2016, Qualcomm Atheros, Inc.
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import logging
  7. import subprocess
  8. import threading
  9. logger = logging.getLogger()
  10. def execute_thread(command, reply):
  11. cmd = ' '.join(command)
  12. logger.debug("thread run: " + cmd)
  13. try:
  14. status = 0;
  15. buf = subprocess.check_output(command, stderr=subprocess.STDOUT)
  16. except subprocess.CalledProcessError as e:
  17. status = e.returncode
  18. buf = e.output
  19. logger.debug("thread cmd: " + cmd)
  20. logger.debug("thread exit status: " + str(status))
  21. logger.debug("thread exit buf: " + str(buf))
  22. reply.append(status)
  23. reply.append(buf)
  24. class Host():
  25. def __init__(self, host=None, ifname=None, port=None, name="", user="root"):
  26. self.host = host
  27. self.name = name
  28. self.user = user
  29. self.monitors = []
  30. self.monitor_thread = None
  31. self.logs = []
  32. self.ifname = ifname
  33. self.port = port
  34. if self.name == "" and host != None:
  35. self.name = host
  36. def local_execute(self, command):
  37. logger.debug("execute: " + str(command))
  38. try:
  39. status = 0;
  40. buf = subprocess.check_output(command, stderr=subprocess.STDOUT)
  41. except subprocess.CalledProcessError as e:
  42. status = e.returncode
  43. buf = e.output
  44. logger.debug("status: " + str(status))
  45. logger.debug("buf: " + str(buf))
  46. return status, buf
  47. def execute(self, command):
  48. if self.host is None:
  49. return self.local_execute(command)
  50. cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
  51. _cmd = self.name + " execute: " + ' '.join(cmd)
  52. logger.debug(_cmd)
  53. try:
  54. status = 0
  55. buf = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  56. except subprocess.CalledProcessError as e:
  57. status = e.returncode
  58. buf = e.output
  59. logger.debug(self.name + " status: " + str(status))
  60. logger.debug(self.name + " buf: " + str(buf))
  61. return status, buf
  62. # async execute
  63. def execute_run(self, command, res):
  64. if self.host is None:
  65. cmd = command
  66. else:
  67. cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
  68. _cmd = self.name + " execute_run: " + ' '.join(cmd)
  69. logger.debug(_cmd)
  70. t = threading.Thread(target = execute_thread, args=(cmd, res))
  71. t.start()
  72. return t
  73. def wait_execute_complete(self, t, wait=None):
  74. if wait == None:
  75. wait_str = "infinite"
  76. else:
  77. wait_str = str(wait) + "s"
  78. logger.debug(self.name + " wait_execute_complete(" + wait_str + "): ")
  79. if t.isAlive():
  80. t.join(wait)
  81. def get_logs(self, local_log_dir=None):
  82. for log in self.logs:
  83. if local_log_dir:
  84. self.local_execute(["scp", self.user + "@[" + self.host + "]:" + log, local_log_dir])
  85. self.execute(["rm", log])
  86. del self.logs[:]