Enable or disable Jenkins jobs matching regex 82/12382/8
authorAnil Belur <abelur@linuxfoundation.org>
Fri, 24 Aug 2018 04:22:11 +0000 (09:52 +0530)
committerAnil Belur <abelur@linuxfoundation.org>
Mon, 27 Aug 2018 15:47:15 +0000 (21:17 +0530)
ex:
To enable jobs

To disable jobs

JIRA: RELENG-1117
Change-Id: Iff800472cecfe056285f07e1c2461775457cb0ab
Signed-off-by: Anil Belur <abelur@linuxfoundation.org>
lftools/cli/jenkins/__init__.py
lftools/cli/jenkins/jobs.py [new file with mode: 0644]
releasenotes/notes/jenkins-c247796de6390391.yaml [new file with mode: 0644]

index 6da1e45..f865a7f 100644 (file)
@@ -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 (file)
index 0000000..c4107a4
--- /dev/null
@@ -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 (file)
index 0000000..bc86c7c
--- /dev/null
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    Add a **jobs** sub-command to **jenkins** command to enable or disable Jenkins
+    Jobs that match a regular expression.