2016-03-08 14:28:03 +01:00
|
|
|
# Host class
|
|
|
|
# Copyright (c) 2016, Qualcomm Atheros, Inc.
|
|
|
|
#
|
|
|
|
# This software may be distributed under the terms of the BSD license.
|
|
|
|
# See README for more details.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
import threading
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
2016-06-23 19:16:35 +02:00
|
|
|
def remote_compatible(func):
|
|
|
|
func.remote_compatible = True
|
|
|
|
return func
|
|
|
|
|
2016-03-08 14:28:03 +01:00
|
|
|
def execute_thread(command, reply):
|
2016-04-29 07:07:32 +02:00
|
|
|
cmd = ' '.join(command)
|
|
|
|
logger.debug("thread run: " + cmd)
|
2016-03-08 14:28:03 +01:00
|
|
|
try:
|
2016-07-03 18:37:50 +02:00
|
|
|
status = 0
|
2016-03-08 14:28:03 +01:00
|
|
|
buf = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
status = e.returncode
|
|
|
|
buf = e.output
|
|
|
|
|
|
|
|
logger.debug("thread cmd: " + cmd)
|
|
|
|
logger.debug("thread exit status: " + str(status))
|
|
|
|
logger.debug("thread exit buf: " + str(buf))
|
|
|
|
reply.append(status)
|
|
|
|
reply.append(buf)
|
|
|
|
|
|
|
|
class Host():
|
|
|
|
def __init__(self, host=None, ifname=None, port=None, name="", user="root"):
|
|
|
|
self.host = host
|
|
|
|
self.name = name
|
|
|
|
self.user = user
|
2016-04-29 07:07:34 +02:00
|
|
|
self.monitors = []
|
|
|
|
self.monitor_thread = None
|
|
|
|
self.logs = []
|
2016-03-08 14:28:03 +01:00
|
|
|
self.ifname = ifname
|
|
|
|
self.port = port
|
2016-04-29 07:07:35 +02:00
|
|
|
self.dev = None
|
2016-03-08 14:28:03 +01:00
|
|
|
if self.name == "" and host != None:
|
|
|
|
self.name = host
|
|
|
|
|
|
|
|
def local_execute(self, command):
|
2016-04-24 11:28:18 +02:00
|
|
|
logger.debug("execute: " + str(command))
|
2016-03-08 14:28:03 +01:00
|
|
|
try:
|
2016-07-03 18:37:50 +02:00
|
|
|
status = 0
|
2016-04-24 11:28:18 +02:00
|
|
|
buf = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
2016-03-08 14:28:03 +01:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
status = e.returncode
|
|
|
|
buf = e.output
|
|
|
|
|
|
|
|
logger.debug("status: " + str(status))
|
|
|
|
logger.debug("buf: " + str(buf))
|
2019-02-02 11:48:30 +01:00
|
|
|
return status, buf.decode()
|
2016-03-08 14:28:03 +01:00
|
|
|
|
|
|
|
def execute(self, command):
|
|
|
|
if self.host is None:
|
|
|
|
return self.local_execute(command)
|
|
|
|
|
2016-04-24 11:28:18 +02:00
|
|
|
cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
|
2016-04-29 07:07:32 +02:00
|
|
|
_cmd = self.name + " execute: " + ' '.join(cmd)
|
2016-03-08 14:28:03 +01:00
|
|
|
logger.debug(_cmd)
|
|
|
|
try:
|
|
|
|
status = 0
|
|
|
|
buf = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
status = e.returncode
|
|
|
|
buf = e.output
|
|
|
|
|
|
|
|
logger.debug(self.name + " status: " + str(status))
|
|
|
|
logger.debug(self.name + " buf: " + str(buf))
|
2019-02-02 11:48:30 +01:00
|
|
|
return status, buf.decode()
|
2016-03-08 14:28:03 +01:00
|
|
|
|
|
|
|
# async execute
|
|
|
|
def execute_run(self, command, res):
|
|
|
|
if self.host is None:
|
2016-04-24 11:28:18 +02:00
|
|
|
cmd = command
|
2016-03-08 14:28:03 +01:00
|
|
|
else:
|
2016-04-24 11:28:18 +02:00
|
|
|
cmd = ["ssh", self.user + "@" + self.host, ' '.join(command)]
|
2016-04-29 07:07:32 +02:00
|
|
|
_cmd = self.name + " execute_run: " + ' '.join(cmd)
|
2016-03-08 14:28:03 +01:00
|
|
|
logger.debug(_cmd)
|
|
|
|
t = threading.Thread(target = execute_thread, args=(cmd, res))
|
|
|
|
t.start()
|
|
|
|
return t
|
|
|
|
|
|
|
|
def wait_execute_complete(self, t, wait=None):
|
|
|
|
if wait == None:
|
|
|
|
wait_str = "infinite"
|
|
|
|
else:
|
|
|
|
wait_str = str(wait) + "s"
|
|
|
|
|
|
|
|
logger.debug(self.name + " wait_execute_complete(" + wait_str + "): ")
|
|
|
|
if t.isAlive():
|
|
|
|
t.join(wait)
|
2016-04-29 07:07:34 +02:00
|
|
|
|
2016-05-19 15:06:44 +02:00
|
|
|
def add_log(self, log_file):
|
|
|
|
self.logs.append(log_file)
|
|
|
|
|
2016-04-29 07:07:34 +02:00
|
|
|
def get_logs(self, local_log_dir=None):
|
|
|
|
for log in self.logs:
|
|
|
|
if local_log_dir:
|
|
|
|
self.local_execute(["scp", self.user + "@[" + self.host + "]:" + log, local_log_dir])
|
|
|
|
self.execute(["rm", log])
|
|
|
|
del self.logs[:]
|