axmol/tools/jenkins-scripts/watchdog.py

63 lines
1.7 KiB
Python
Raw Normal View History

2014-03-12 15:36:09 +08:00
import jenkinsapi
from jenkinsapi.jenkins import Jenkins
import sys
import time
import os
2014-03-13 11:16:21 +08:00
#check & kill dead buid
def build_time(_job,_threshold):
#get jenkins-job-watchdog-threshold
2014-03-13 11:16:21 +08:00
#Get last build running
build = _job.get_last_build()
2014-03-12 15:36:09 +08:00
running = build.is_running()
2014-03-13 11:16:21 +08:00
print 'build_job:',_job,'running:',running
2014-03-12 15:36:09 +08:00
if not running:
return False
2014-03-13 11:16:21 +08:00
#Get numerical ID of the last build.
buildnu = _job.get_last_buildnumber()
2014-03-12 15:36:09 +08:00
print "buildnumber:#",buildnu
2014-03-12 17:18:16 +08:00
#get nowtime
2014-03-12 15:36:09 +08:00
nowtime = time.strftime('%M',time.localtime(time.time()))
#print 'nowtime:',nowtime
2014-03-13 11:16:21 +08:00
#get build start time
2014-03-12 15:36:09 +08:00
timeb = build.get_timestamp()
#print 'buildtime:',str(timeb)[14:16]
buildtime = int(str(timeb)[14:16])
subtime = 0
if int(nowtime) >= buildtime:
subtime = int(nowtime)-buildtime
else:
subtime = 60-buildtime+int(nowtime)
if subtime > _threshold:
2014-03-12 15:36:09 +08:00
#print 'subtime',subtime
2014-03-13 11:16:21 +08:00
#kill dead buid
2014-03-12 15:36:09 +08:00
build.stop()
2014-03-13 11:16:21 +08:00
def main():
username = os.environ['JENKINS_ADMIN']
password = os.environ['JENKINS_ADMIN_PW']
2014-03-13 11:16:21 +08:00
J = Jenkins('http://115.28.134.83:8000',username,password)
#get all jenkins jobs
for key,job in J.iteritems():
threshold = 0
if(os.environ.has_key(key+'-threshold')):
threshold = int(os.environ[key+'-threshold'])
else:
threshold = int(os.environ['jenkins-job-watchdog-threshold'])
build_time(job,threshold)
2014-03-12 15:36:09 +08:00
return(0)
# -------------- main --------------
if __name__ == '__main__':
sys_ret = 0
try:
sys_ret = main()
except:
traceback.print_exc()
sys_ret = 1
finally:
sys.exit(sys_ret)
2014-03-13 11:16:21 +08:00