remotehost.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 remote_compatible(func):
  11. func.remote_compatible = True
  12. return func
  13. def execute_thread(command, reply):
  14. cmd = ' '.join(command)
  15. logger.debug("thread run: " + cmd)
  16. try:
  17. status = 0
  18. buf = subprocess.check_output(command, stderr=subprocess.STDOUT)
  19. except subprocess.CalledProcessError as e:
  20. status = e.returncode
  21. buf = e.output
  22. logger.debug("thread cmd: " + cmd)
  23. logger.debug("thread exit status: " + str(status))
  24. logger.debug("thread exit buf: " + str(buf))
  25. reply.append(status)
  26. reply.append(buf)
  27. class Host():
  28. def __init__(self, host=None, ifname=None, port=None, name="", user="root"):
  29. self.host = host
  30. self.name = name
  31. self.user = user
  32. self.monitors = []
  33. self.monitor_thread = None
  34. self.logs = []
  35. self.ifname = ifname
  36. self.port = port
  37. self.dev = None
  38. if self.name == "" and host != None:
  39. self.name = host
  40. def local_execute(self, command):
  41. logger.debug("execute: " + str(command))
  42. try:
  43. status = 0
  44. buf = subprocess.check_output(command, stderr=subprocess.STDOUT)
  45. except subprocess.CalledProcessError as e:
  46. status = e.returncode
  47. buf = e.output
  48. logger.debug("status: " + str(status))
  49. logger.debug("buf: " + str(buf))
  50. return status, buf
  51. def execute(self, command):
  52. if self.host is None:
  53. return self.local_execute(command)
  54. cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
  55. _cmd = self.name + " execute: " + ' '.join(cmd)
  56. logger.debug(_cmd)
  57. try:
  58. status = 0
  59. buf = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  60. except subprocess.CalledProcessError as e:
  61. status = e.returncode
  62. buf = e.output
  63. logger.debug(self.name + " status: " + str(status))
  64. logger.debug(self.name + " buf: " + str(buf))
  65. return status, buf
  66. # async execute
  67. def execute_run(self, command, res):
  68. if self.host is None:
  69. cmd = command
  70. else:
  71. cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
  72. _cmd = self.name + " execute_run: " + ' '.join(cmd)
  73. logger.debug(_cmd)
  74. t = threading.Thread(target = execute_thread, args=(cmd, res))
  75. t.start()
  76. return t
  77. def wait_execute_complete(self, t, wait=None):
  78. if wait == None:
  79. wait_str = "infinite"
  80. else:
  81. wait_str = str(wait) + "s"
  82. logger.debug(self.name + " wait_execute_complete(" + wait_str + "): ")
  83. if t.isAlive():
  84. t.join(wait)
  85. def add_log(self, log_file):
  86. self.logs.append(log_file)
  87. def get_logs(self, local_log_dir=None):
  88. for log in self.logs:
  89. if local_log_dir:
  90. self.local_execute(["scp", self.user + "@[" + self.host + "]:" + log, local_log_dir])
  91. self.execute(["rm", log])
  92. del self.logs[:]