remotehost.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. self.dev = None
  35. if self.name == "" and host != None:
  36. self.name = host
  37. def local_execute(self, command):
  38. logger.debug("execute: " + str(command))
  39. try:
  40. status = 0;
  41. buf = subprocess.check_output(command, stderr=subprocess.STDOUT)
  42. except subprocess.CalledProcessError as e:
  43. status = e.returncode
  44. buf = e.output
  45. logger.debug("status: " + str(status))
  46. logger.debug("buf: " + str(buf))
  47. return status, buf
  48. def execute(self, command):
  49. if self.host is None:
  50. return self.local_execute(command)
  51. cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
  52. _cmd = self.name + " execute: " + ' '.join(cmd)
  53. logger.debug(_cmd)
  54. try:
  55. status = 0
  56. buf = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  57. except subprocess.CalledProcessError as e:
  58. status = e.returncode
  59. buf = e.output
  60. logger.debug(self.name + " status: " + str(status))
  61. logger.debug(self.name + " buf: " + str(buf))
  62. return status, buf
  63. # async execute
  64. def execute_run(self, command, res):
  65. if self.host is None:
  66. cmd = command
  67. else:
  68. cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
  69. _cmd = self.name + " execute_run: " + ' '.join(cmd)
  70. logger.debug(_cmd)
  71. t = threading.Thread(target = execute_thread, args=(cmd, res))
  72. t.start()
  73. return t
  74. def wait_execute_complete(self, t, wait=None):
  75. if wait == None:
  76. wait_str = "infinite"
  77. else:
  78. wait_str = str(wait) + "s"
  79. logger.debug(self.name + " wait_execute_complete(" + wait_str + "): ")
  80. if t.isAlive():
  81. t.join(wait)
  82. def add_log(self, log_file):
  83. self.logs.append(log_file)
  84. def get_logs(self, local_log_dir=None):
  85. for log in self.logs:
  86. if local_log_dir:
  87. self.local_execute(["scp", self.user + "@[" + self.host + "]:" + log, local_log_dir])
  88. self.execute(["rm", log])
  89. del self.logs[:]