jenkins-job-builder/jenkins_jobs/cmd.py
David Caro 4dd267ee0d Added globbed parameters to the job specification
Now you can specify a globbed parameter when updating or deleting a job,
it will parse the yaml files and select those jobs that match to be
updated/deleted

For example:
  jenkins-jobs --config ~/jenkins.ini update myjobs/ \*only_those\*

Will only update the jobs that have 'only_those' on their name.

For the delete subcommand the option '-p' lets you specify the path
where to load the job list from, so you can use globs for the jobs in
that list (it does not allow you to use globs with non-managed jobs)

Change-Id: I5bb1074845fb143c7c3120c138a6b138d3548305
Signed-off-by: David Caro <dcaroest@redhat.com>
2013-10-18 17:05:25 +02:00

124 lines
5.2 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright (C) 2012 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import ConfigParser
import logging
import os
import sys
def confirm(question):
answer = raw_input('%s (Y/N): ' % question).upper().strip()
if not answer == 'Y':
sys.exit('Aborted')
def main():
import jenkins_jobs.builder
import jenkins_jobs.errors
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('names', help='name(s) of job(s)', nargs='*')
parser_update.add_argument('--delete-old', help='Delete obsolete jobs',
action='store_true',
dest='delete_old', default=False,)
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(s) of job(s)', nargs='*')
parser_delete = subparser.add_parser('delete')
parser_delete.add_argument('name', help='name of job', nargs='+')
parser_delete.add_argument('-p', '--path', default=None,
help='Path to YAML file or directory')
subparser.add_parser('delete-all',
help='Delete *ALL* jobs from Jenkins server, '
'including those not managed by Jenkins Job '
'Builder.')
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)s)")
parser.add_argument(
'--ignore-cache', action='store_true',
dest='ignore_cache', default=False,
help='Ignore the cache and update the jobs anyhow (that will only '
'flush the specified jobs cache)')
parser.add_argument(
'--flush-cache', action='store_true', dest='flush_cache',
default=False, help='Flush all the cache entries before updating')
options = parser.parse_args()
options.log_level = getattr(logging, options.log_level.upper(),
logging.INFO)
logging.basicConfig(level=options.log_level)
logger = logging.getLogger()
conf = '/etc/jenkins_jobs/jenkins_jobs.ini'
if options.conf:
conf = options.conf
else:
# Fallback to script directory
localconf = os.path.join(os.path.dirname(__file__),
'jenkins_jobs.ini')
if os.path.isfile(localconf):
conf = localconf
if os.path.isfile(conf):
logger.debug("Reading config from {0}".format(conf))
conffp = open(conf, 'r')
config = ConfigParser.ConfigParser()
config.readfp(conffp)
elif options.command == 'test':
logger.debug("Not reading config for test output generation")
config = {}
else:
raise jenkins_jobs.errors.JenkinsJobsException(
"A valid configuration file is required when not run as a test")
logger.debug("Config: {0}".format(config))
builder = jenkins_jobs.builder.Builder(config.get('jenkins', 'url'),
config.get('jenkins', 'user'),
config.get('jenkins', 'password'),
config,
ignore_cache=options.ignore_cache,
flush_cache=options.flush_cache)
if options.command == 'delete':
for job in options.name:
logger.info("Deleting jobs in [{0}]".format(job))
builder.delete_job(job, options.path)
elif options.command == 'delete-all':
confirm('Sure you want to delete *ALL* jobs from Jenkins server?\n'
'(including those not managed by Jenkins Job Builder)')
logger.info("Deleting all jobs")
builder.delete_all_jobs()
elif options.command == 'update':
logger.info("Updating jobs in {0} ({1})".format(
options.path, options.names))
jobs = builder.update_job(options.path, options.names)
if options.delete_old:
builder.delete_old_managed(keep=[x.name for x in jobs])
elif options.command == 'test':
builder.update_job(options.path, options.name,
output_dir=options.output_dir)
if __name__ == '__main__':
sys.path.insert(0, '.')
main()