From: Anil Belur Date: Fri, 24 Aug 2018 04:22:11 +0000 (+0530) Subject: Enable or disable Jenkins jobs matching regex X-Git-Tag: v0.17.0~8^2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F82%2F12382%2F8;p=releng%2Flftools.git Enable or disable Jenkins jobs matching regex ex: To enable jobs To disable jobs JIRA: RELENG-1117 Change-Id: Iff800472cecfe056285f07e1c2461775457cb0ab Signed-off-by: Anil Belur --- diff --git a/lftools/cli/jenkins/__init__.py b/lftools/cli/jenkins/__init__.py index 6da1e458..f865a7fb 100644 --- a/lftools/cli/jenkins/__init__.py +++ b/lftools/cli/jenkins/__init__.py @@ -17,6 +17,7 @@ import jenkins as jenkins_python # Don't confuse this with the function ... from six.moves.urllib.error import HTTPError from lftools.cli.jenkins.builds import builds +from lftools.cli.jenkins.jobs import jobs from lftools.cli.jenkins.nodes import nodes from lftools.cli.jenkins.plugins import plugins_init @@ -166,5 +167,6 @@ jenkins_cli.add_command(nodes) jenkins_cli.add_command(builds) jenkins_cli.add_command(get_credentials, name='get-credentials') jenkins_cli.add_command(groovy) +jenkins_cli.add_command(jobs) jenkins_cli.add_command(quiet_down, name='quiet-down') jenkins_cli.add_command(remove_offline_nodes, name='remove-offline-nodes') diff --git a/lftools/cli/jenkins/jobs.py b/lftools/cli/jenkins/jobs.py new file mode 100644 index 00000000..c4107a4c --- /dev/null +++ b/lftools/cli/jenkins/jobs.py @@ -0,0 +1,68 @@ +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Jenkins Jobs.""" + +__author__ = 'Anil Belur' + +import click + +enable_disable_jobs = """ +import jenkins.* +import jenkins.model.* +import hudson.* +import hudson.model.* + +def jobTypes = [hudson.model.FreeStyleProject.class] + +def filter = {{job-> + if (job.disabled == true) {{ + println("${{job.fullName}}") + }} + job.getDisplayName().contains("{0}") +}} + +def disableClosure = {{job->job.{1}()}} + +jobTypes.each{{ className-> + jenkins.model.Jenkins.instance.getAllItems(className).findAll(filter).each(disableClosure)}} +""" + + +@click.group() +@click.pass_context +def jobs(ctx): + """Command to update Jenkins Jobs.""" + pass + + +@click.command() +@click.argument('regex') +@click.pass_context +def enable(ctx, regex): + """Enable all Jenkins jobs matching REGEX.""" + server = ctx.obj['server'] + + result = server.run_script(enable_disable_jobs.format(regex, "enable")) + print(result) + + +@click.command() +@click.argument('regex') +@click.pass_context +def disable(ctx, regex): + """Disable all Jenkins jobs matching REGEX.""" + server = ctx.obj['server'] + + result = server.run_script(enable_disable_jobs.format(regex, "disable")) + print(result) + + +jobs.add_command(enable) +jobs.add_command(disable) diff --git a/releasenotes/notes/jenkins-c247796de6390391.yaml b/releasenotes/notes/jenkins-c247796de6390391.yaml new file mode 100644 index 00000000..bc86c7cc --- /dev/null +++ b/releasenotes/notes/jenkins-c247796de6390391.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add a **jobs** sub-command to **jenkins** command to enable or disable Jenkins + Jobs that match a regular expression.