我有一个python脚本,它启动一个java工具来运行回归测试。如果我直接在命令行上运行这些命令,它们工作正常,如果我从Jenkins运行这个脚本,java工具将处于挂起状态,直到我关闭jenkins作业。关闭Jenkins工作后,我看到该工具正在运行。
以下是从python脚本生成的命令:
export DISPLAY=:0;/home/tools/executor/bin/executor -J-Xms128m -J-Xmx1024m -a/home/ExecutorSuites/FWVerification/Platform/update_firmware_polaris.e.xml -r744 <uname> <passwrd>
从Jenkins运行python脚本有什么问题吗?我真的很感激任何帮助。我是一名FW工程师,对java和Jenkins不太了解。
这是python脚本:
import paramiko
import sys
import os
class Unbuffered:
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
sys.stdout=Unbuffered(sys.stdout)
hostname = os.environ['J_TESTMACHINE_IP']
username = os.environ['J_TESTMACHINE_UNAME']
password = os.environ['J_TESTMACHINE_PASSWD']
vapor_username = os.environ['J_VAPOR_UNAME']
vapor_password = os.environ['J_VAPOR_PASSWD']
executor_path = os.environ['J_EXECUTOR_PATH']
vapor_revid = os.environ['J_VAPOR_REVID']
test_results_path = os.environ['J_VAPOR_TESTRESULTS_FILEPATH']
es_home_dir = os.environ['J_ES_HOME_DIR']
testsuites_string = os.environ['J_TEST_SUITE']
testsuites = [x.strip() for x in testsuites_string.split(',')]
localfile = "C:\Jenkins\localfile.txt"
def create_client(hostname, username, password):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname,username=username, password=password)
except BadHostKeyException:
print "The server's host key could not be verified"
except AuthenticationException:
print "if authentication failed"
except SSHException:
print "There were other errors connecting or establishing an SSH session"
except socket.error:
print "a socket error occurred while connecting"
return client
def destroy_client(client):
client.close()
def form_executor_cmd(testsuite):
executor_cmd = "export DISPLAY=:0;";
executor_cmd += executor_path;
executor_cmd += " -J-Xms128m -J-Xmx1024m";
executor_cmd += " -a" + es_home_dir + testsuite;
executor_cmd += " -r" + vapor_revid;
executor_cmd += " -u" + vapor_username + " -p" + vapor_password;
return executor_cmd
def run_remote_cmd(client, remote_cmd):
try:
stdin, stdout, stderr = client.exec_command(remote_cmd)
except SSHException:
print "When executing command "+remote_cmd+", there were other errors connecting or establishing an SSH session"
channel = stdout.channel
status = channel.recv_exit_status()
print "\n Executing remote command "+remote_cmd+" ...\n";
print "Exit Status " + str(status)
return status, stdin, stdout, stderr
def clear_testresults(client, filepath):
print "clearing test results at " + filepath
status, stdin, stdout, stderr = run_remote_cmd(client, "rm "+filepath)
def did_it_pass(client):
ftp = client.open_sftp()
ftp.get(test_results_path, localfile)
f = open(localfile,"a+")
lines = f.readlines()
print lines
f.close()
ftp.close()
os.remove(localfile)
if "Failed" in lines:
return 1
else:
return 0
os.remove('localfile.txt')
###########################################################
# hostname = 'sclab-sfmfv-avatar'
# username = 'root'
# password = 'pmcsfm'
# ssh connection created
client = create_client(hostname, username, password)
#executor_cmd = "export DISPLAY=:0;/home/tools/executor/bin/executor -a/home/ExecutorSuites/FWVerification/Platform/update_firmware_avatar.e.xml -r342 -uSfmFwTest1 -pSfmFWbot01"
failed = 0
for testsuite in testsuites:
print testsuite
clear_testresults(client, test_results_path)
print "\n Executing suite "+testsuite+" ...\n";
executor_cmd = form_executor_cmd(testsuite)
status, stdin, stdout, stderr = run_remote_cmd(client, executor_cmd)
if did_it_pass(client):
print "Testsuite "+ testsuite +"failed"
data = stderr.read();
print data
failed = 1
else:
print "Testsuite "+ testsuite +"succeeded"
data = stdout.read();
print data
if failed == 1 :
sys.exit (1)
# ssh connection destroyed
destroy_client(client)