Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 68 lines (60 sloc) 2.68 KB
#!/usr/bin/env python
import jenkins_jobs.builder
import argparse
import ConfigParser
import logging
import sys
def main():
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(help='update, test or delete job',
dest='command')
parser_update = subparser.add_parser('update')
parser_update.add_argument('path', help='Path to YAML file or directory')
parser_update.add_argument('name', help='name of job', nargs='?')
parser_test = subparser.add_parser('test')
parser_test.add_argument('path', help='Path to YAML file or directory')
parser_test.add_argument('-o', dest='output_dir', help='Path to output XML')
parser_test.add_argument('name', help='name of job', nargs='?')
parser_delete = subparser.add_parser('delete')
parser_delete.add_argument('name', help='name of job')
parser.add_argument('--conf', dest='conf', help='Configuration file')
parser.add_argument('-l', '--log_level', dest='log_level', default='info',
help="Log level (default '%default')")
options = parser.parse_args()
options.log_level = getattr(logging, options.log_level.upper(),
logging.INFO)
logging.basicConfig(level=options.log_level)
logger = logging.getLogger()
if options.conf:
conf = options.conf
else:
conf = '/etc/jenkins_jobs/jenkins_jobs.ini'
# Kludge alert - making the config object global so that it can
# be accessed from elsewhere.
global config
if not options.command == 'test':
logger.debug("Reading config from {0}".format(conf))
conffp = open(conf, 'r')
config = ConfigParser.ConfigParser()
config.readfp(conffp)
else:
config = {}
logger.debug("Config: {0}".format(config))
builder = jenkins_jobs.builder.Builder(config.get('jenkins','url'),
config.get('jenkins','user'),
config.get('jenkins','password'))
if options.command == 'delete':
logger.debug("Deleting job {0}".format(options.name))
builder.delete_job(options.name)
elif options.command == 'update':
logger.debug("Updating jobs in {0} ({1})".format(options.path,
options.name))
builder.update_job(options.path, options.name)
elif options.command == 'test':
if(not options.output_dir):
logger.error("You must specify an output dir (-o) in test mode")
sys.exit(1)
builder.update_job(options.path, options.name,
output_dir=options.output_dir)
if __name__ == '__main__':
main()