Skip to content

Instantly share code, notes, and snippets.

@josuebrunel
Last active August 29, 2015 14:18
Show Gist options
  • Save josuebrunel/35b23d13a0cd387e9b6e to your computer and use it in GitHub Desktop.
Save josuebrunel/35b23d13a0cd387e9b6e to your computer and use it in GitHub Desktop.
#####################################
# Put that script in your $JENKINS_HOME
# Backkup your jobs first : tar czvf $JENKINS_HOME/jobs.tar.gz $JENKINS_HOME/jobs/*/config.xml
# then
# python disable_scheduled_jobs.py
# check the disable_scheduled_jobs.log
# If everything seems alright, go on jenkins and perform a "reload config from disk"
import os, glob, logging
import pdb
from xml.etree import cElementTree as etree
from xml.dom import minidom
def xml_pretty_print(data):
"""Pretty print xml data
"""
raw_string = etree.tostring(data, 'utf-8')
parsed_string = minidom.parseString(raw_string)
return parsed_string.toprettyxml(indent='\t')
def get_content(f):
xml_data = etree.parse(f).getroot()
return xml_data
def write_data(fname, data):
#content = xml_pretty_print(data)
content = etree.tostring(data)
logging.info("{0}".format(fname))
with open(fname, 'w') as f:
f.write(content.encode('utf-8'))
return True
def isScheduled(xml_data):
trigger = xml_data.find('triggers')
if trigger :
return True
return False
def disable_job(xml_data):
disabled = xml_data.find('disabled')
if disabled.text == 'false':
logging.debug("job already disabled")
return False
logging.debug("job not disabled yet : {0}".format(disabled.text))
xml_data.text = 'true'
return xml_data
if __name__ == '__main__':
#logging.basicConfig(filename='disable_trigered_jobs.log',level=logging.DEBUG,format='%(asctime)s %(message)s')
logging.basicConfig(filename='disable_scheduled_jobs.log',level=logging.DEBUG,format='[%(asctime)s %(levelname)s] %(message)s')
jobs = glob.glob('jobs/*/config.xml')
for config in jobs:
logging.debug("Processing {0}".format(config))
xml_data = get_content(config)
if isScheduled(xml_data):
logging.debug("{0} is scheduled ".format(config))
xml_data = disable_job(xml_data)
if xml_data :
logging.info("{0} is disabled : {1}".format(config, xml_data.find('disabled').text))
write_data(config, xml_data)
else:
logging.debug("Job OK")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment