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
|
2014-03-21 17:02:18 +08:00
|
|
|
def build_time(_job,_threshold):
|
2014-03-13 14:12:29 +08:00
|
|
|
#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)
|
2014-03-21 17:02:18 +08:00
|
|
|
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():
|
2014-03-17 16:10:26 +08:00
|
|
|
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():
|
2014-04-02 16:26:20 +08:00
|
|
|
threshold = 0
|
2014-03-21 17:36:27 +08:00
|
|
|
if(os.environ.has_key(key+'-threshold')):
|
|
|
|
threshold = int(os.environ[key+'-threshold'])
|
2014-03-21 17:02:18 +08:00
|
|
|
else:
|
|
|
|
threshold = int(os.environ['jenkins-job-watchdog-threshold'])
|
2014-04-02 16:26:20 +08:00
|
|
|
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
|
|
|
|