Skip to content

Instantly share code, notes, and snippets.

@kenjiheigel
Last active March 25, 2020 03:06
Show Gist options
  • Save kenjiheigel/bb01385adffa1b5277b7049c65a0e1f1 to your computer and use it in GitHub Desktop.
Save kenjiheigel/bb01385adffa1b5277b7049c65a0e1f1 to your computer and use it in GitHub Desktop.
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.jenkins.results.parser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
/**
* @author Kenji Heigel
*/
public class GroovyScriptExecutor {
public static final String COHORT = "test-5";
public static final boolean DRY_RUN = true;
public static void main(String[] args) throws Exception {
Properties properties = JenkinsResultsParserUtil.getBuildProperties();
List<JenkinsMaster> jenkinsMasters =
JenkinsResultsParserUtil.getJenkinsMasters(properties, 16, COHORT);
// Write your own groovy script or use one of the provided methods.
// Examples:
// String groovyScript = getClearJobQueueGroovyScript("test-job-name");
// String groovyScript = getKillAndClearAllJobsGroovyScript();
String groovyScript = "";
executeJenkinsScript(jenkinsMasters, groovyScript);
}
protected static void executeJenkinsScript(
List<JenkinsMaster> jenkinsMasters, String groovyScript)
throws Exception {
System.out.println(groovyScript);
List<Callable<JenkinsMaster>> callables = new ArrayList<>();
for (final JenkinsMaster jenkinsMaster : jenkinsMasters) {
if (DRY_RUN) {
return;
}
Callable<JenkinsMaster> callable = new Callable<JenkinsMaster>() {
@Override
public JenkinsMaster call() {
JenkinsResultsParserUtil.executeJenkinsScript(
jenkinsMaster.getName(), groovyScript);
System.out.println(jenkinsMaster.getURL());
return jenkinsMaster;
}
};
callables.add(callable);
}
ParallelExecutor<JenkinsMaster> parallelExecutor =
new ParallelExecutor<>(callables, _getExecutorService());
parallelExecutor.execute();
}
protected static String getClearJobQueueGroovyScript(String jobName) {
List<String> lines = new ArrayList<>();
lines.add("def queue = Jenkins.instance.queue;");
lines.add("queue.items.each {");
lines.add("\tif (it.task.name == \"" + jobName + "\") {");
lines.add("\t\tout.println it.task.name;");
lines.add("\t\tq.cancel(it.task);");
lines.add("\t}");
lines.add("}");
return _buildScript(lines);
}
protected static String getKillAndClearAllJobsGroovyScript() {
List<String> lines = new ArrayList<>();
lines.add("def queue = Jenkins.instance.queue;");
lines.add("queue.items.each {");
lines.add("\tq.cancel(it.task);");
lines.add("}");
lines.add("Jenkins.instance.getAllItems(Job.class).each {");
lines.add("\tfor (build in it.builds) {");
lines.add("\t\tif (!build.isBuilding()){");
lines.add("\t\t\tcontinue;");
lines.add("\t\t}");
lines.add("\t\tbuild.doStop();");
lines.add("\t}");
lines.add("}");
return _buildScript(lines);
}
protected static String getKillJobGroovyScript(String jobName) {
List<String> lines = new ArrayList<>();
lines.add(
"Jenkins.instance.getItemByFullName(\"" + jobName +
"\").builds.each {");
lines.add("\tit.doStop()");
lines.add("}");
return _buildScript(lines);
}
protected static void invokeJob(JenkinsMaster jenkinsMaster) {
String jobName = "maintenance-daily";
StringBuilder sb = new StringBuilder();
sb.append(jenkinsMaster.getURL());
sb.append("/buildWithParameters?token=");
sb.append(properties.getProperty("jenkins.authentication.token"));
}
protected static Properties properties;
static {
try {
properties = JenkinsResultsParserUtil.getBuildProperties();
}
catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
private static String _buildScript(List<String> lines) {
StringBuilder sb = new StringBuilder();
for (String line : lines) {
sb.append("\n");
sb.append(line);
}
return sb.toString();
}
private static ExecutorService _getExecutorService() {
return _executorService;
}
private static ExecutorService _executorService =
JenkinsResultsParserUtil.getNewThreadPoolExecutor(25, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment